-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython.go
90 lines (83 loc) · 2.07 KB
/
python.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
87
88
89
90
package main
import (
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/AlecAivazis/survey/v2"
)
// runPythonCode runs the given python file of spec.yaml and returns the output.
func runPythonCode(spec Spec) error {
for _, d := range spec.Dependencies {
if e := checkPackage(d.PackageName, d.InstalledVersion); e == ExistSameVersion {
continue
}
if err := installPackage(d.PackageName, d.InstalledVersion); err != nil {
return err
}
}
return runExecCommand([]string{vpython, filepath.Join("src", spec.EntryFile)})
}
// searchPackageVersion searches the given package version.
func searchPackageVersion(name string) error {
version, err := selectPackageVersion(name)
if err != nil {
return err
}
reply := false
if err := survey.AskOne(&survey.Confirm{Message: fmt.Sprintf("Do you want to install %v version %v?", name, version)}, &reply, survey.WithValidator(survey.Required)); err != nil {
return err
}
if reply {
spec, err := readSpecFromPath(".")
if err != nil {
return err
}
if err := appendDependencyToSpec(&spec, name, version); err != nil {
return err
}
if err := writeSpecToPath(".", spec); err != nil {
return err
}
}
return nil
}
// getPythonVersion returns the python version.
func getPythonVersion() (string, error) {
bs, err := exec.Command(python, "-V").Output()
if err != nil {
return "3.7.2", err
}
return strings.Split(strings.Trim(string(bs), "\r\n"), " ")[1], nil
}
// convertVersionToIntArray converts the given version to int array.
func convertVersionToIntArray(version string) [3]int {
v := [3]int{0, 0, 0}
s := strings.Split(strings.TrimSpace(version), ".")
var err error = nil
for i := 0; i < 3; i++ {
if len(s) <= i {
v[i] = 0
continue
}
v[i], err = strconv.Atoi(s[i])
if err != nil {
return v
}
}
return v
}
// compareVersion compares the given version.
func compareVersion(a, b string) int {
av := convertVersionToIntArray(a)
bv := convertVersionToIntArray(b)
for i := 0; i < 3; i++ {
if av[i] > bv[i] {
return -1
} else if av[i] < bv[i] {
return 1
}
}
return 0
}