-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylintRunner.go
86 lines (73 loc) · 2.33 KB
/
pylintRunner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package tools
import (
"codacy/cli-v2/config"
"codacy/cli-v2/plugins"
"codacy/cli-v2/utils"
"fmt"
"os"
"os/exec"
"path/filepath"
)
func RunPylint(workDirectory string, toolInfo *plugins.ToolInfo, files []string, outputFile string, outputFormat string) error {
// Get Python binary from venv
pythonPath := filepath.Join(toolInfo.InstallDir, "venv", "bin", "python3")
// Construct base command with -m pylint to run pylint module
args := []string{"-m", "pylint"}
// Always use JSON output format since we'll convert to SARIF if needed
args = append(args, "--output-format=json")
// Check if a config file exists in the expected location and use it if present
if configFile, exists := ConfigFileExists(config.Config, "pylint.rc"); exists {
args = append(args, fmt.Sprintf("--rcfile=%s", configFile))
}
// Create a temporary file for JSON output if we need to convert to SARIF
var tempFile string
if outputFormat == "sarif" {
tmp, err := os.CreateTemp("", "pylint-*.json")
if err != nil {
return fmt.Errorf("failed to create temporary file: %w", err)
}
tempFile = tmp.Name()
tmp.Close()
defer os.Remove(tempFile)
args = append(args, fmt.Sprintf("--output=%s", tempFile))
} else if outputFile != "" {
args = append(args, fmt.Sprintf("--output=%s", outputFile))
}
// Add files to analyze - if no files specified, analyze current directory
if len(files) > 0 {
args = append(args, files...)
} else {
args = append(args, ".")
}
// Create and run command
cmd := exec.Command(pythonPath, args...)
cmd.Dir = workDirectory
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run the command
err := cmd.Run()
if err != nil {
// Pylint returns non-zero exit code when it finds issues
// We should not treat this as an error
if _, ok := err.(*exec.ExitError); !ok {
return fmt.Errorf("failed to run Pylint: %w", err)
}
}
// If SARIF output is requested, convert JSON to SARIF
if outputFormat == "sarif" {
jsonOutput, err := os.ReadFile(tempFile)
if err != nil {
return fmt.Errorf("failed to read pylint output: %w", err)
}
sarifOutput := utils.ConvertPylintToSarif(jsonOutput)
if outputFile != "" {
err = os.WriteFile(outputFile, sarifOutput, 0644)
if err != nil {
return fmt.Errorf("failed to write SARIF output: %w", err)
}
} else {
fmt.Println(string(sarifOutput))
}
}
return nil
}