ParsePythonWithEnv 借助pipenv解析python依赖
(ctx context.Context, file *model.File)
| 19 | |
| 20 | // ParsePythonWithEnv 借助pipenv解析python依赖 |
| 21 | func 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 | |
| 58 | func pipenvGraph(ctx context.Context, dir string) *model.DepGraph { |
| 59 |