@@ -2,29 +2,17 @@ package python
22
33import (
44 _ "embed"
5- "encoding/json"
65 "io"
7- "os"
8- "os/exec"
9- "path/filepath"
106 "regexp"
117 "strings"
128
13- "github.com/xmirrorsecurity/opensca-cli/v3/cmd/config"
14- "github.com/xmirrorsecurity/opensca-cli/v3/opensca/logs"
159 "github.com/xmirrorsecurity/opensca-cli/v3/opensca/model"
1610)
1711
1812// ParseSetup 解析setup.py
1913func ParseSetup (file * model.File ) * model.DepGraph {
2014
21- // 尝试调用python解析
22- root := ParseSetupPyWithPython (file )
23- if root != nil && len (root .Children ) > 0 {
24- return root
25- }
26-
27- root = & model.DepGraph {Path : file .Relpath ()}
15+ root := & model.DepGraph {Path : file .Relpath ()}
2816
2917 // 静态解析
3018 file .OpenReader (func (reader io.Reader ) {
@@ -57,73 +45,3 @@ func ParseSetup(file *model.File) *model.DepGraph {
5745
5846 return root
5947}
60-
61- //go:embed oss.py
62- var ossPy []byte
63-
64- // oss.py 脚本输出的依赖结构
65- type setupDep struct {
66- Name string `json:"name"`
67- Version string `json:"version"`
68- License string `json:"license"`
69- Packages []string `json:"packages"`
70- InstallRequires []string `json:"install_requires"`
71- Requires []string `json:"requires"`
72- }
73-
74- func ParseSetupPyWithPython (file * model.File ) * model.DepGraph {
75-
76- if ! config .Conf ().Optional .Dynamic {
77- return nil
78- }
79-
80- if _ , err := exec .LookPath ("python" ); err != nil {
81- return nil
82- }
83-
84- dir := filepath .Dir (file .Abspath ())
85- ossfile := filepath .Join (dir , "oss.py" )
86-
87- // 创建 oss.py
88- if err := os .WriteFile (ossfile , ossPy , 0777 ); err != nil {
89- logs .Warn (err )
90- return nil
91- }
92-
93- // 解析 setup.py
94- cmd := exec .Command ("python" , ossfile , file .Abspath ())
95- out , _ := cmd .CombinedOutput ()
96- startTag , endTag := `opensca_start<<` , `>>opensca_end`
97- startIndex , endIndex := strings .Index (string (out ), startTag ), strings .Index (string (out ), endTag )
98- if startIndex == - 1 || endIndex == - 1 {
99- return nil
100- } else {
101- out = out [startIndex + len (startTag ) : endIndex ]
102- }
103-
104- // 获取解析结果
105- var dep setupDep
106- if err := json .Unmarshal (out , & dep ); err != nil {
107- logs .Warn (err )
108- return nil
109- }
110-
111- root := & model.DepGraph {Name : dep .Name , Version : dep .Version , Path : file .Relpath ()}
112- root .AppendLicense (dep .License )
113-
114- for _ , pkg := range [][]string {dep .Packages , dep .InstallRequires , dep .Requires } {
115- for _ , p := range pkg {
116- index := strings .IndexAny (p , "=<>" )
117- var name , version string
118- if index > - 1 {
119- name = p [:index ]
120- version = p [index :]
121- } else {
122- name = p
123- }
124- root .AppendChild (& model.DepGraph {Name : name , Version : version })
125- }
126- }
127-
128- return root
129- }
0 commit comments