| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package python |
| 6 |
| 7 import ( |
| 8 "os/exec" |
| 9 |
| 10 "github.com/luci/luci-go/common/errors" |
| 11 |
| 12 "golang.org/x/net/context" |
| 13 ) |
| 14 |
| 15 // Find attempts to find a Python interpreter matching the supplied version |
| 16 // using PATH. |
| 17 // |
| 18 // In order to accommodate multiple configurations on operating systems, Find |
| 19 // will attempt to identify versions that appear on the path |
| 20 func Find(c context.Context, vers Version) (*Interpreter, error) { |
| 21 // pythonM.m, pythonM, python |
| 22 searches := make([]string, 0, 3) |
| 23 pv := vers |
| 24 pv.Patch = 0 |
| 25 if pv.Minor > 0 { |
| 26 searches = append(searches, pv.PythonBase()) |
| 27 pv.Minor = 0 |
| 28 } |
| 29 if pv.Major > 0 { |
| 30 searches = append(searches, pv.PythonBase()) |
| 31 pv.Major = 0 |
| 32 } |
| 33 searches = append(searches, pv.PythonBase()) |
| 34 |
| 35 for _, s := range searches { |
| 36 p, err := exec.LookPath(s) |
| 37 if err != nil { |
| 38 if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNo
tFound { |
| 39 // Not found is okay. |
| 40 continue |
| 41 } |
| 42 return nil, errors.Annotate(err).Reason("failed to searc
h PATH for: %(interp)q"). |
| 43 D("interp", s). |
| 44 Err() |
| 45 } |
| 46 |
| 47 i := Interpreter{Python: p} |
| 48 iv, err := i.GetVersion(c) |
| 49 if err != nil { |
| 50 return nil, errors.Annotate(err).Reason("failed to get v
ersion for: %(interp)q"). |
| 51 D("interp", p). |
| 52 Err() |
| 53 } |
| 54 if vers.IsSatisfiedBy(iv) { |
| 55 return &i, nil |
| 56 } |
| 57 } |
| 58 |
| 59 return nil, errors.New("no Python found") |
| 60 } |
| OLD | NEW |