|
| 1 | +package pip |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/snyk/error-catalog-golang-public/opensource/ecosystems" |
| 13 | + "github.com/snyk/error-catalog-golang-public/snyk" |
| 14 | + "github.com/snyk/error-catalog-golang-public/snyk_errors" |
| 15 | +) |
| 16 | + |
| 17 | +// Report represents the minimal JSON output from pip install --report |
| 18 | +// needed to build a dependency graph. |
| 19 | +type Report struct { |
| 20 | + Install []InstallItem `json:"install"` |
| 21 | +} |
| 22 | + |
| 23 | +// InstallItem represents a single package in the pip install report. |
| 24 | +type InstallItem struct { |
| 25 | + Metadata PackageMetadata `json:"metadata"` |
| 26 | + Requested bool `json:"requested"` // True if explicitly requested in requirements |
| 27 | +} |
| 28 | + |
| 29 | +// IsDirectDependency returns true if this package is a direct dependency. |
| 30 | +func (item InstallItem) IsDirectDependency() bool { |
| 31 | + return item.Requested |
| 32 | +} |
| 33 | + |
| 34 | +// PackageMetadata contains the package name, version, and dependencies. |
| 35 | +// |
| 36 | +//nolint:tagliatelle // requires_dist is the field name used by pip's JSON output |
| 37 | +type PackageMetadata struct { |
| 38 | + Name string `json:"name"` |
| 39 | + Version string `json:"version"` |
| 40 | + RequiresDist []string `json:"requires_dist"` // List of dependencies (e.g., "urllib3 (<3,>=1.21.1)") |
| 41 | +} |
| 42 | + |
| 43 | +// GetInstallReport runs pip install with --dry-run and --report flags to get |
| 44 | +// a JSON report of what would be installed from a requirements file. |
| 45 | +// No files are written to disk; the report is captured from stdout. |
| 46 | +// |
| 47 | +// This is a convenience wrapper around GetInstallReportWithExecutor that uses |
| 48 | +// the default executor. For testing, use GetInstallReportWithExecutor directly. |
| 49 | +func GetInstallReport(ctx context.Context, requirementsFile string) (*Report, error) { |
| 50 | + return GetInstallReportWithExecutor(ctx, requirementsFile, &DefaultExecutor{}) |
| 51 | +} |
| 52 | + |
| 53 | +// GetInstallReportWithExecutor is a testable version that accepts a CommandExecutor. |
| 54 | +// It runs pip install with the following flags: |
| 55 | +// - --dry-run: Don't actually install anything |
| 56 | +// - --ignore-installed: Show all packages, not just new ones |
| 57 | +// - --report -: Output JSON report to stdout (dash means stdout) |
| 58 | +// - --quiet: Suppress non-error output (except the report) |
| 59 | +// - -r: Read from requirements file |
| 60 | +func GetInstallReportWithExecutor(ctx context.Context, requirementsFile string, executor CommandExecutor) (*Report, error) { |
| 61 | + if requirementsFile == "" { |
| 62 | + return nil, fmt.Errorf("requirements file path cannot be empty") |
| 63 | + } |
| 64 | + |
| 65 | + // Build pip command arguments |
| 66 | + args := []string{ |
| 67 | + "install", |
| 68 | + "--dry-run", |
| 69 | + "--ignore-installed", |
| 70 | + "--report", "-", |
| 71 | + "--quiet", |
| 72 | + "-r", requirementsFile, |
| 73 | + } |
| 74 | + |
| 75 | + // Execute the command |
| 76 | + output, err := executor.Execute(ctx, "pip", args...) |
| 77 | + if err != nil { |
| 78 | + return nil, classifyPipError(err) |
| 79 | + } |
| 80 | + |
| 81 | + // Parse the JSON report |
| 82 | + var report Report |
| 83 | + if err := json.Unmarshal(output, &report); err != nil { |
| 84 | + return nil, fmt.Errorf("failed to parse pip report: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return &report, nil |
| 88 | +} |
| 89 | + |
| 90 | +// CommandExecutor is an interface for executing commands. |
| 91 | +// This allows for dependency injection and easier testing. |
| 92 | +type CommandExecutor interface { |
| 93 | + Execute(ctx context.Context, name string, args ...string) ([]byte, error) |
| 94 | +} |
| 95 | + |
| 96 | +// DefaultExecutor uses os/exec to run commands. |
| 97 | +type DefaultExecutor struct{} |
| 98 | + |
| 99 | +// Execute runs a command and returns its stdout output. |
| 100 | +func (e *DefaultExecutor) Execute(ctx context.Context, name string, args ...string) ([]byte, error) { |
| 101 | + cmd := exec.CommandContext(ctx, name, args...) |
| 102 | + |
| 103 | + var stdout bytes.Buffer |
| 104 | + var stderr bytes.Buffer |
| 105 | + cmd.Stdout = &stdout |
| 106 | + cmd.Stderr = &stderr |
| 107 | + |
| 108 | + if err := cmd.Run(); err != nil { |
| 109 | + return nil, &pipError{ |
| 110 | + err: err, |
| 111 | + stderr: stderr.String(), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return stdout.Bytes(), nil |
| 116 | +} |
| 117 | + |
| 118 | +// pipError wraps an error with stderr output from pip. |
| 119 | +type pipError struct { |
| 120 | + err error |
| 121 | + stderr string |
| 122 | +} |
| 123 | + |
| 124 | +func (e *pipError) Error() string { |
| 125 | + return fmt.Sprintf("%v (stderr: %s)", e.err, e.stderr) |
| 126 | +} |
| 127 | + |
| 128 | +func (e *pipError) Unwrap() error { |
| 129 | + return e.err |
| 130 | +} |
| 131 | + |
| 132 | +// classifyPipError analyzes pip error output and returns appropriate error catalog error. |
| 133 | +// |
| 134 | +//nolint:gocyclo // Error classification requires checking multiple patterns sequentially |
| 135 | +func classifyPipError(err error) error { |
| 136 | + var pipErr *pipError |
| 137 | + if !errors.As(err, &pipErr) { |
| 138 | + // Not a pip error, return as-is |
| 139 | + return fmt.Errorf("pip install failed: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + stderr := pipErr.stderr |
| 143 | + |
| 144 | + // Check for syntax errors in requirements.txt |
| 145 | + if strings.Contains(stderr, "Invalid requirement") || |
| 146 | + strings.Contains(stderr, "Could not parse") || |
| 147 | + strings.Contains(stderr, "invalid requirement") || |
| 148 | + strings.Contains(stderr, "InvalidVersion") || |
| 149 | + strings.Contains(stderr, "Invalid version") { |
| 150 | + return ecosystems.NewSyntaxIssuesError( |
| 151 | + fmt.Sprintf("Invalid syntax in requirements file: %s", stderr), |
| 152 | + snyk_errors.WithCause(pipErr.err), |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + // Check for package not found errors |
| 157 | + if strings.Contains(stderr, "Could not find a version") || |
| 158 | + strings.Contains(stderr, "No matching distribution") || |
| 159 | + strings.Contains(stderr, "Could not find a version that satisfies") { |
| 160 | + return ecosystems.NewPythonPackageNotFoundError( |
| 161 | + fmt.Sprintf("Package not found: %s", stderr), |
| 162 | + snyk_errors.WithCause(pipErr.err), |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + // Check for Python version mismatch |
| 167 | + if strings.Contains(stderr, "requires Python") || |
| 168 | + strings.Contains(stderr, "Requires-Python") { |
| 169 | + return ecosystems.NewPipUnsupportedPythonVersionError( |
| 170 | + fmt.Sprintf("Python version mismatch: %s", stderr), |
| 171 | + snyk_errors.WithCause(pipErr.err), |
| 172 | + ) |
| 173 | + } |
| 174 | + |
| 175 | + // Check for conflicting requirements |
| 176 | + if strings.Contains(stderr, "Conflict") || |
| 177 | + strings.Contains(stderr, "conflicting") || |
| 178 | + strings.Contains(stderr, "incompatible") { |
| 179 | + return ecosystems.NewPythonVersionConfictError( |
| 180 | + fmt.Sprintf("Conflicting package requirements: %s", stderr), |
| 181 | + snyk_errors.WithCause(pipErr.err), |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + // Check for context cancellation or timeout |
| 186 | + if errors.Is(pipErr.err, context.Canceled) { |
| 187 | + // User-initiated cancellation (e.g., Ctrl+C) - not a catalog error |
| 188 | + return fmt.Errorf("pip install canceled: %w", pipErr.err) |
| 189 | + } |
| 190 | + if errors.Is(pipErr.err, context.DeadlineExceeded) { |
| 191 | + // Timeout - use catalog timeout error |
| 192 | + return snyk.NewTimeoutError( |
| 193 | + fmt.Sprintf("Pip install timed out: %s", stderr), |
| 194 | + snyk_errors.WithCause(pipErr.err), |
| 195 | + ) |
| 196 | + } |
| 197 | + |
| 198 | + // Generic installation failure |
| 199 | + return ecosystems.NewInstallationFailureError( |
| 200 | + fmt.Sprintf("Pip install failed: %s", stderr), |
| 201 | + snyk_errors.WithCause(pipErr.err), |
| 202 | + ) |
| 203 | +} |
0 commit comments