getPythonVersion returns the python version available on this machine
(vm string)
| 15 | |
| 16 | // getPythonVersion returns the python version available on this machine |
| 17 | func getPythonVersion(vm string) (int, error) { |
| 18 | py, err := exec.LookPath(vm) |
| 19 | if err != nil { |
| 20 | return 0, fmt.Errorf( |
| 21 | "gopy: could not locate 'python' executable (err: %v)", |
| 22 | err, |
| 23 | ) |
| 24 | } |
| 25 | |
| 26 | out, err := exec.Command(py, "-c", "import sys; print(sys.version_info.major)").Output() |
| 27 | if err != nil { |
| 28 | return 0, errors.Wrapf(err, "gopy: error retrieving python version") |
| 29 | } |
| 30 | |
| 31 | vers, err := strconv.Atoi(strings.TrimSpace(string(out))) |
| 32 | if err != nil { |
| 33 | return 0, errors.Wrapf(err, "gopy: error retrieving python version") |
| 34 | } |
| 35 | |
| 36 | return vers, nil |
| 37 | } |