Skip to content

Commit 5551923

Browse files
fix: basic Python scanning to determine detection environment
1 parent 1d61ea8 commit 5551923

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

cmd/murphy/internal/internalcmd/scanner_scan.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func scannerScanRun(cmd *cobra.Command, args []string) {
6262
IsNoBuild: env.DoNotBuild,
6363
}
6464
ctx = model.WithScanTask(ctx, scantask)
65+
ctx = context.WithValue(ctx, "is_internalcmd", true)
6566
e = inspector.ManagedInspect(ctx)
6667
if e != nil {
6768
logger.Error(e)

module/python/python.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func collectDepsInfo(ctx context.Context, dir string) ([][2]string, error) {
193193
delete(unknownVersionComps, s)
194194
}
195195
}
196-
if len(unknownVersionComps) != 0 {
196+
if is_internalcmd := ctx.Value("is_internalcmd"); is_internalcmd == nil && len(unknownVersionComps) != 0 {
197197
// try to resolve version from pip list
198198
m, e := getEnvPipListMap(ctx)
199199
if e != nil {

module/python/venv.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ func pipdeptree(dir string, logger *zap.SugaredLogger) ([]PipdeptreeStruct, erro
210210
}
211211
func updatePackage(dir string, logger *zap.SugaredLogger, k, v string) {
212212
var out bytes.Buffer
213-
cmd := exec.Command("./pip", "install", k+"=="+v)
213+
version := k
214+
if v != "" {
215+
version = version + "==" + v
216+
}
217+
cmd := exec.Command("./pip", "install", version)
214218
cmd.Stdout = &out
215219
cmd.Dir = dir
216220
if err := cmd.Run(); err != nil {
@@ -246,6 +250,26 @@ func delVenv(dir string, logger *zap.SugaredLogger) {
246250
}
247251
logger.Debug("delete venv success ")
248252
}
253+
func directDependenceSurvival(mod *[]model.DependencyItem, nvMp map[string]string) {
254+
var exist = make(map[string]string)
255+
for _, i := range *mod {
256+
exist[i.CompName] = exist[i.CompVersion]
257+
}
258+
for k, v := range nvMp {
259+
if _, ok := exist[k]; !ok {
260+
*mod = append(*mod, model.DependencyItem{
261+
Component: model.Component{
262+
CompName: k,
263+
CompVersion: v,
264+
EcoRepo: model.EcoRepo{
265+
Ecosystem: "pip",
266+
Repository: "",
267+
},
268+
},
269+
})
270+
}
271+
}
272+
}
249273
func Run(ctx context.Context, dir string, logger *zap.SugaredLogger, nvMp map[string]string) ([]model.DependencyItem, error) {
250274
var mod []model.DependencyItem
251275
var venvDir = filepath.Join(dir, "virtual_venv")
@@ -297,6 +321,8 @@ func Run(ctx context.Context, dir string, logger *zap.SugaredLogger, nvMp map[st
297321
mod = append(mod, buildTree(j, 0))
298322
}
299323
}
324+
// 对于没有pip install成功的依赖 只加入pipreqs中列出的直接依赖
325+
directDependenceSurvival(&mod, newRequirements)
300326
defer delVenv(venvDir, logger)
301327
return mod, err
302328
}

0 commit comments

Comments
 (0)