xref: /aosp_15_r20/external/spdx-tools/tvsaver/saver2v3/save_package.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package saver2v3
4
5import (
6	"fmt"
7	"io"
8	"sort"
9	"strings"
10
11	"github.com/spdx/tools-golang/spdx/common"
12	"github.com/spdx/tools-golang/spdx/v2_3"
13)
14
15func renderPackage2_3(pkg *v2_3.Package, w io.Writer) error {
16	if pkg.PackageName != "" {
17		fmt.Fprintf(w, "PackageName: %s\n", pkg.PackageName)
18	}
19	if pkg.PackageSPDXIdentifier != "" {
20		fmt.Fprintf(w, "SPDXID: %s\n", common.RenderElementID(pkg.PackageSPDXIdentifier))
21	}
22	if pkg.PackageVersion != "" {
23		fmt.Fprintf(w, "PackageVersion: %s\n", pkg.PackageVersion)
24	}
25	if pkg.PackageFileName != "" {
26		fmt.Fprintf(w, "PackageFileName: %s\n", pkg.PackageFileName)
27	}
28	if pkg.PackageSupplier != nil && pkg.PackageSupplier.Supplier != "" {
29		if pkg.PackageSupplier.SupplierType == "" {
30			fmt.Fprintf(w, "PackageSupplier: %s\n", pkg.PackageSupplier.Supplier)
31		} else {
32			fmt.Fprintf(w, "PackageSupplier: %s: %s\n", pkg.PackageSupplier.SupplierType, pkg.PackageSupplier.Supplier)
33		}
34	}
35	if pkg.PackageOriginator != nil && pkg.PackageOriginator.Originator != "" {
36		if pkg.PackageOriginator.OriginatorType == "" {
37			fmt.Fprintf(w, "PackageOriginator: %s\n", pkg.PackageOriginator.Originator)
38		} else {
39			fmt.Fprintf(w, "PackageOriginator: %s: %s\n", pkg.PackageOriginator.OriginatorType, pkg.PackageOriginator.Originator)
40		}
41	}
42	if pkg.PackageDownloadLocation != "" {
43		fmt.Fprintf(w, "PackageDownloadLocation: %s\n", pkg.PackageDownloadLocation)
44	}
45	if pkg.PrimaryPackagePurpose != "" {
46		fmt.Fprintf(w, "PrimaryPackagePurpose: %s\n", pkg.PrimaryPackagePurpose)
47	}
48	if pkg.ReleaseDate != "" {
49		fmt.Fprintf(w, "ReleaseDate: %s\n", pkg.ReleaseDate)
50	}
51	if pkg.BuiltDate != "" {
52		fmt.Fprintf(w, "BuiltDate: %s\n", pkg.BuiltDate)
53	}
54	if pkg.ValidUntilDate != "" {
55		fmt.Fprintf(w, "ValidUntilDate: %s\n", pkg.ValidUntilDate)
56	}
57	if pkg.FilesAnalyzed == true {
58		if pkg.IsFilesAnalyzedTagPresent == true {
59			fmt.Fprintf(w, "FilesAnalyzed: true\n")
60		}
61	} else {
62		fmt.Fprintf(w, "FilesAnalyzed: false\n")
63	}
64	if pkg.PackageVerificationCode != nil && pkg.PackageVerificationCode.Value != "" && pkg.FilesAnalyzed == true {
65		if len(pkg.PackageVerificationCode.ExcludedFiles) == 0 {
66			fmt.Fprintf(w, "PackageVerificationCode: %s\n", pkg.PackageVerificationCode.Value)
67		} else {
68			fmt.Fprintf(w, "PackageVerificationCode: %s (excludes: %s)\n", pkg.PackageVerificationCode.Value, strings.Join(pkg.PackageVerificationCode.ExcludedFiles, ", "))
69		}
70	}
71
72	for _, checksum := range pkg.PackageChecksums {
73		fmt.Fprintf(w, "PackageChecksum: %s: %s\n", checksum.Algorithm, checksum.Value)
74	}
75
76	if pkg.PackageHomePage != "" {
77		fmt.Fprintf(w, "PackageHomePage: %s\n", pkg.PackageHomePage)
78	}
79	if pkg.PackageSourceInfo != "" {
80		fmt.Fprintf(w, "PackageSourceInfo: %s\n", textify(pkg.PackageSourceInfo))
81	}
82	if pkg.PackageLicenseConcluded != "" {
83		fmt.Fprintf(w, "PackageLicenseConcluded: %s\n", pkg.PackageLicenseConcluded)
84	}
85	if pkg.FilesAnalyzed == true {
86		for _, s := range pkg.PackageLicenseInfoFromFiles {
87			fmt.Fprintf(w, "PackageLicenseInfoFromFiles: %s\n", s)
88		}
89	}
90	if pkg.PackageLicenseDeclared != "" {
91		fmt.Fprintf(w, "PackageLicenseDeclared: %s\n", pkg.PackageLicenseDeclared)
92	}
93	if pkg.PackageLicenseComments != "" {
94		fmt.Fprintf(w, "PackageLicenseComments: %s\n", textify(pkg.PackageLicenseComments))
95	}
96	if pkg.PackageCopyrightText != "" {
97		fmt.Fprintf(w, "PackageCopyrightText: %s\n", textify(pkg.PackageCopyrightText))
98	}
99	if pkg.PackageSummary != "" {
100		fmt.Fprintf(w, "PackageSummary: %s\n", textify(pkg.PackageSummary))
101	}
102	if pkg.PackageDescription != "" {
103		fmt.Fprintf(w, "PackageDescription: %s\n", textify(pkg.PackageDescription))
104	}
105	if pkg.PackageComment != "" {
106		fmt.Fprintf(w, "PackageComment: %s\n", textify(pkg.PackageComment))
107	}
108	for _, s := range pkg.PackageExternalReferences {
109		fmt.Fprintf(w, "ExternalRef: %s %s %s\n", s.Category, s.RefType, s.Locator)
110		if s.ExternalRefComment != "" {
111			fmt.Fprintf(w, "ExternalRefComment: %s\n", textify(s.ExternalRefComment))
112		}
113	}
114	for _, s := range pkg.PackageAttributionTexts {
115		fmt.Fprintf(w, "PackageAttributionText: %s\n", textify(s))
116	}
117
118	fmt.Fprintf(w, "\n")
119
120	// also render any files for this package
121	sort.Slice(pkg.Files, func(i, j int) bool {
122		return pkg.Files[i].FileSPDXIdentifier < pkg.Files[j].FileSPDXIdentifier
123	})
124	for _, fi := range pkg.Files {
125		renderFile2_3(fi, w)
126	}
127
128	return nil
129}
130