1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package builder2v1 4 5import ( 6 "fmt" 7 "path/filepath" 8 "regexp" 9 "runtime" 10 11 "github.com/spdx/tools-golang/spdx/common" 12 "github.com/spdx/tools-golang/spdx/v2_1" 13 "github.com/spdx/tools-golang/utils" 14) 15 16// BuildPackageSection2_1 creates an SPDX Package (version 2.1), returning 17// that package or error if any is encountered. Arguments: 18// - packageName: name of package / directory 19// - dirRoot: path to directory to be analyzed 20// - pathsIgnore: slice of strings for filepaths to ignore 21func BuildPackageSection2_1(packageName string, dirRoot string, pathsIgnore []string) (*v2_1.Package, error) { 22 // build the file section first, so we'll have it available 23 // for calculating the package verification code 24 filepaths, err := utils.GetAllFilePaths(dirRoot, pathsIgnore) 25 if err != nil { 26 return nil, err 27 } 28 osType := runtime.GOOS 29 30 re, ok := regexp.Compile("/+") 31 if ok != nil { 32 return nil, err 33 } 34 35 dirRootLen := 0 36 if osType == "windows" { 37 dirRootLen = len(dirRoot) 38 } 39 40 files := []*v2_1.File{} 41 fileNumber := 0 42 for _, fp := range filepaths { 43 newFilePatch := "" 44 if osType == "windows" { 45 newFilePatch = filepath.FromSlash("." + fp[dirRootLen:]) 46 } else { 47 newFilePatch = filepath.FromSlash("./" + fp) 48 } 49 newFile, err := BuildFileSection2_1(re.ReplaceAllLiteralString(newFilePatch, string(filepath.Separator)), dirRoot, fileNumber) 50 if err != nil { 51 return nil, err 52 } 53 files = append(files, newFile) 54 fileNumber++ 55 } 56 // get the verification code 57 code, err := utils.GetVerificationCode2_1(files, "") 58 if err != nil { 59 return nil, err 60 } 61 62 // now build the package section 63 pkg := &v2_1.Package{ 64 PackageName: packageName, 65 PackageSPDXIdentifier: common.ElementID(fmt.Sprintf("Package-%s", packageName)), 66 PackageDownloadLocation: "NOASSERTION", 67 FilesAnalyzed: true, 68 IsFilesAnalyzedTagPresent: true, 69 PackageVerificationCode: code, 70 PackageLicenseConcluded: "NOASSERTION", 71 PackageLicenseInfoFromFiles: []string{}, 72 PackageLicenseDeclared: "NOASSERTION", 73 PackageCopyrightText: "NOASSERTION", 74 Files: files, 75 } 76 77 return pkg, nil 78} 79