1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3package builder2v2 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_2" 13 "github.com/spdx/tools-golang/utils" 14) 15 16// BuildPackageSection2_2 creates an SPDX Package (version 2.2), 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_2(packageName string, dirRoot string, pathsIgnore []string) (*v2_2.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 osType := runtime.GOOS 26 27 if err != nil { 28 return nil, err 29 } 30 31 re, ok := regexp.Compile("/+") 32 if ok != nil { 33 return nil, err 34 } 35 dirRootLen := 0 36 if osType == "windows" { 37 dirRootLen = len(dirRoot) 38 } 39 40 files := []*v2_2.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_2(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 57 // get the verification code 58 code, err := utils.GetVerificationCode2_2(files, "") 59 if err != nil { 60 return nil, err 61 } 62 63 // now build the package section 64 pkg := &v2_2.Package{ 65 PackageName: packageName, 66 PackageSPDXIdentifier: common.ElementID(fmt.Sprintf("Package-%s", packageName)), 67 PackageDownloadLocation: "NOASSERTION", 68 FilesAnalyzed: true, 69 IsFilesAnalyzedTagPresent: true, 70 PackageVerificationCode: code, 71 PackageLicenseConcluded: "NOASSERTION", 72 PackageLicenseInfoFromFiles: []string{}, 73 PackageLicenseDeclared: "NOASSERTION", 74 PackageCopyrightText: "NOASSERTION", 75 Files: files, 76 } 77 78 return pkg, nil 79} 80