|
| 1 | +package adj |
| 2 | + |
| 3 | +import ( |
| 4 | + "os/exec" |
| 5 | + "path" |
| 6 | + "strings" |
| 7 | + |
| 8 | + trace "github.com/rs/zerolog/log" |
| 9 | + |
| 10 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/logger/validator" |
| 11 | +) |
| 12 | + |
| 13 | +func Validate() { |
| 14 | + |
| 15 | + trace.Info().Msg("Starting ADJ-Validator") |
| 16 | + |
| 17 | + // Check for Python interpreter is installed and accessible |
| 18 | + pyCmd := pythonCommand() |
| 19 | + if pyCmd == "" { |
| 20 | + trace.Fatal().Msg("No Python interpreter found in PATH. Please install Python 3 and ensure it's accessible via 'python3', 'python', or 'py' command.") |
| 21 | + } |
| 22 | + |
| 23 | + // Construct the full path to the ADJ validator script |
| 24 | + validatorPath := path.Join(RepoPath, ADJValidatorScript) |
| 25 | + |
| 26 | + trace.Debug().Msgf("Running ADJ Validator using command: %s %s --no-color", pyCmd, validatorPath) |
| 27 | + |
| 28 | + // Execute the ADJ validator script and capture its output |
| 29 | + cmd := exec.Command(pyCmd, validatorPath, "--no-color") |
| 30 | + output, err := cmd.CombinedOutput() |
| 31 | + |
| 32 | + // Log the output of the validator for debugging purposes |
| 33 | + validator.LogADJValidatorOutput(output) |
| 34 | + |
| 35 | + // If the command returns a non-zero exit code, it indicates a validation failure or an error during execution |
| 36 | + if err != nil { |
| 37 | + |
| 38 | + if strings.Contains(string(output), "JSON Validation Script") { |
| 39 | + |
| 40 | + trace.Fatal().Msg("ADJ Validator failed with error, check the output for details.") |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + trace.Fatal().Msgf("Error executing ADJ Validator with command '%s %s'. Ensure that you have installed jsonschema with 'pip install jsonschema==4.25.0' and is accessible in your PATH.", pyCmd, validatorPath) |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +// pythonCommand returns the name of a Python interpreter executable |
| 51 | +// available in the current system PATH. |
| 52 | +// |
| 53 | +// It checks a list of common Python command names in order of preference: |
| 54 | +// - "python3" (typical on Linux/macOS) |
| 55 | +// - "python" (may point to Python 3 on many systems) |
| 56 | +// - "py" (Python launcher commonly available on Windows) |
| 57 | +// |
| 58 | +// For each candidate, exec.LookPath is used to determine whether the |
| 59 | +// executable can be found in the PATH. The function returns the first |
| 60 | +// command that exists. |
| 61 | +// |
| 62 | +// If none of the candidates are found, an empty string is returned, |
| 63 | +// indicating that no Python interpreter is available. |
| 64 | +func pythonCommand() string { |
| 65 | + candidates := []string{"python3", "python", "py"} |
| 66 | + |
| 67 | + for _, c := range candidates { |
| 68 | + _, err := exec.LookPath(c) |
| 69 | + if err == nil { |
| 70 | + return c |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return "" |
| 75 | +} |
0 commit comments