55 "errors"
66 "fmt"
77 "os/exec"
8+ "path/filepath"
89 "strings"
910 "sync"
1011
@@ -65,7 +66,8 @@ func (p Plugin) BuildDepGraphsFromDir(
6566
6667 for _ , file := range files {
6768 g .Go (func () error {
68- result , err := p .buildDepGraphFromFile (ctx , log , file , pythonVersion , options .Python .NoBuildIsolation )
69+ projectName := GetProjectName (file .RelPath , dir , options )
70+ result , err := p .buildDepGraphFromFile (ctx , log , file , pythonVersion , options .Python .NoBuildIsolation , projectName )
6971 if err != nil {
7072 attrs := []logger.Field {
7173 logger .Attr (logFieldFile , file .RelPath ),
@@ -82,8 +84,7 @@ func (p Plugin) BuildDepGraphsFromDir(
8284 result = ecosystems.SCAResult {
8385 ProjectDescriptor : identity.ProjectDescriptor {
8486 Identity : identity.ProjectIdentity {
85- Type : "pip" ,
86- TargetFile : & file .RelPath ,
87+ Type : "pip" ,
8788 },
8889 },
8990 Error : err ,
@@ -101,9 +102,9 @@ func (p Plugin) BuildDepGraphsFromDir(
101102 return nil , fmt .Errorf ("error building dependency graphs: %w" , err )
102103 }
103104
104- processedFiles := make ([]string , 0 , len (results ))
105- for _ , result := range results {
106- processedFiles = append (processedFiles , result . ProjectDescriptor . GetTargetFile () )
105+ processedFiles := make ([]string , 0 , len (files ))
106+ for _ , file := range files {
107+ processedFiles = append (processedFiles , file . RelPath )
107108 }
108109
109110 return & ecosystems.PluginResult {
@@ -143,17 +144,43 @@ func (p Plugin) discoverRequirementsFiles(ctx context.Context, dir string, optio
143144 return files , nil
144145}
145146
147+ // GetProjectName determines the project name based on the file path and options.
148+ // If --project-name is set, it uses that. Otherwise, it uses the directory name
149+ // containing the file. For example:
150+ // - "project/test/requirements.txt" -> "test"
151+ // - "project/requirements.txt" -> "project"
152+ // - "requirements.txt" (with scanDir="/path/to/myproject") -> "myproject"
153+ func GetProjectName (filePath string , scanDir string , options * ecosystems.SCAPluginOptions ) string {
154+ // If --project-name is explicitly set, use it
155+ if options .Global .ProjectName != nil && * options .Global .ProjectName != "" {
156+ return * options .Global .ProjectName
157+ }
158+
159+ // Extract the directory name from the file path
160+ dir := filepath .Dir (filePath )
161+ projectName := filepath .Base (dir )
162+
163+ // If we're at the root or the directory name is ".", use the scan directory name
164+ if projectName == "." || projectName == "/" || projectName == "" {
165+ return filepath .Base (scanDir )
166+ }
167+
168+ return projectName
169+ }
170+
146171// buildDepGraphFromFile builds a dependency graph from a requirements.txt file.
147172func (p Plugin ) buildDepGraphFromFile (
148173 ctx context.Context ,
149174 log logger.Logger ,
150175 file discovery.FindResult ,
151176 pythonVersion string ,
152177 noBuildIsolation bool ,
178+ projectName string ,
153179) (ecosystems.SCAResult , error ) {
154180 log .Debug (ctx , "Building dependency graph" ,
155181 logger .Attr (logFieldFile , file .RelPath ),
156- logger .Attr ("python_version" , pythonVersion ))
182+ logger .Attr ("python_version" , pythonVersion ),
183+ logger .Attr ("project_name" , projectName ))
157184
158185 // Get pip install report (dry-run, no actual installation)
159186 report , err := GetInstallReport (ctx , log , file .Path , noBuildIsolation )
@@ -162,7 +189,7 @@ func (p Plugin) buildDepGraphFromFile(
162189 }
163190
164191 // Convert report to dependency graph
165- depGraph , err := report .ToDependencyGraph (ctx , log , PkgManagerPip )
192+ depGraph , err := report .ToDependencyGraph (ctx , log , PkgManagerPip , projectName )
166193 if err != nil {
167194 return ecosystems.SCAResult {}, fmt .Errorf ("failed to convert pip report to dependency graph for %s: %w" , file .RelPath , err )
168195 }
@@ -174,8 +201,7 @@ func (p Plugin) buildDepGraphFromFile(
174201 DepGraph : depGraph ,
175202 ProjectDescriptor : identity.ProjectDescriptor {
176203 Identity : identity.ProjectIdentity {
177- Type : "pip" ,
178- TargetFile : & file .RelPath ,
204+ Type : "pip" ,
179205 },
180206 },
181207 }, nil
0 commit comments