MCPcopy Index your code
hub / github.com/XmirrorSecurity/OpenSCA-cli / ParsePythonWithEnv

Function ParsePythonWithEnv

opensca/sca/python/env.go:21–56  ·  view source on GitHub ↗

ParsePythonWithEnv 借助pipenv解析python依赖

(ctx context.Context, file *model.File)

Source from the content-addressed store, hash-verified

19
20// ParsePythonWithEnv 借助pipenv解析python依赖
21func ParsePythonWithEnv(ctx context.Context, file *model.File) *model.DepGraph {
22
23 if _, err := exec.LookPath("python"); err != nil {
24 return nil
25 }
26 if _, err := exec.LookPath("pipenv"); err != nil {
27 return nil
28 }
29
30 // 复制到临时目录
31 tempdir := common.MkdirTemp("pipenv")
32 tempfile := filepath.Join(tempdir, filepath.Base(file.Abspath()))
33 src, _ := os.Open(file.Abspath())
34 dst, _ := os.Create(tempfile)
35 io.Copy(dst, src)
36 src.Close()
37 dst.Close()
38 defer os.RemoveAll(tempdir)
39
40 dir, name := filepath.Split(tempfile)
41
42 if filter.PythonRequirementsTxt(name) {
43 runCmd(ctx, dir, "pipenv", "install", "-r", name, "-i", "https://pypi.tuna.tsinghua.edu.cn/simple")
44 } else if filter.PythonPipfile(name) {
45 runCmd(ctx, dir, "pipenv", "install", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple")
46 } else {
47 return nil
48 }
49
50 defer runCmd(ctx, dir, "pipenv", "--rm")
51 root := pipenvGraph(ctx, dir)
52 if root != nil {
53 root.Path = file.Relpath()
54 }
55 return root
56}
57
58func pipenvGraph(ctx context.Context, dir string) *model.DepGraph {
59

Callers 1

ScaMethod · 0.85

Calls 7

MkdirTempFunction · 0.92
runCmdFunction · 0.85
pipenvGraphFunction · 0.85
AbspathMethod · 0.80
RelpathMethod · 0.80
CopyMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected