| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package python | 5 package python |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "os/exec" | 8 "os/exec" |
| 9 | 9 |
| 10 "github.com/luci/luci-go/common/errors" | 10 "github.com/luci/luci-go/common/errors" |
| 11 | 11 |
| 12 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
| 13 ) | 13 ) |
| 14 | 14 |
| 15 // LookPathResult is the result of LookPathFunc. |
| 16 type LookPathResult struct { |
| 17 // Path, if not zero, is the absolute path to the identified target |
| 18 // executable. |
| 19 Path string |
| 20 |
| 21 // Version, if not zero, is the Python version string. Standard "vpython
" |
| 22 // wrapper identification may choose to call Python with "--version" to |
| 23 // identify it. If so, it may choose to populate this field to avoid red
undant |
| 24 // calls to "--version". |
| 25 Version Version |
| 26 } |
| 27 |
| 28 // LookPathFunc attempts to find a file identified by "target", similar to |
| 29 // exec.LookPath. |
| 30 // |
| 31 // "target" will be the name of the program being looked up. It will NOT be a |
| 32 // path, relative or absolute. It also should not include an extension where |
| 33 // otherwise applicable (e.g., "python" instead of "python.exe"); the lookup |
| 34 // function is responsible for trying known extensions as part of the lookup. |
| 35 // |
| 36 // A nil LookPathFunc will use exec.LookPath. |
| 37 type LookPathFunc func(c context.Context, target string) (*LookPathResult, error
) |
| 38 |
| 15 // Find attempts to find a Python interpreter matching the supplied version | 39 // Find attempts to find a Python interpreter matching the supplied version |
| 16 // using PATH. | 40 // using PATH. |
| 17 // | 41 // |
| 18 // In order to accommodate multiple configurations on operating systems, Find | 42 // In order to accommodate multiple configurations on operating systems, Find |
| 19 // will attempt to identify versions that appear on the path | 43 // will attempt to identify versions that appear on the path |
| 20 func Find(c context.Context, vers Version) (*Interpreter, error) { | 44 func Find(c context.Context, vers Version, lookPath LookPathFunc) (*Interpreter,
error) { |
| 45 » if lookPath == nil { |
| 46 » » lookPath = func(c context.Context, target string) (*LookPathResu
lt, error) { |
| 47 » » » v, err := exec.LookPath(target) |
| 48 » » » if err != nil { |
| 49 » » » » return nil, err |
| 50 » » » } |
| 51 » » » return &LookPathResult{Path: v}, nil |
| 52 » » } |
| 53 » } |
| 54 |
| 21 // pythonM.m, pythonM, python | 55 // pythonM.m, pythonM, python |
| 22 searches := make([]string, 0, 3) | 56 searches := make([]string, 0, 3) |
| 23 pv := vers | 57 pv := vers |
| 24 pv.Patch = 0 | 58 pv.Patch = 0 |
| 25 if pv.Minor > 0 { | 59 if pv.Minor > 0 { |
| 26 searches = append(searches, pv.PythonBase()) | 60 searches = append(searches, pv.PythonBase()) |
| 27 pv.Minor = 0 | 61 pv.Minor = 0 |
| 28 } | 62 } |
| 29 if pv.Major > 0 { | 63 if pv.Major > 0 { |
| 30 searches = append(searches, pv.PythonBase()) | 64 searches = append(searches, pv.PythonBase()) |
| 31 pv.Major = 0 | 65 pv.Major = 0 |
| 32 } | 66 } |
| 33 searches = append(searches, pv.PythonBase()) | 67 searches = append(searches, pv.PythonBase()) |
| 34 | 68 |
| 35 for _, s := range searches { | 69 for _, s := range searches { |
| 36 » » p, err := exec.LookPath(s) | 70 » » lpr, err := lookPath(c, s) |
| 37 if err != nil { | 71 if err != nil { |
| 38 if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNo
tFound { | 72 if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNo
tFound { |
| 39 // Not found is okay. | 73 // Not found is okay. |
| 40 continue | 74 continue |
| 41 } | 75 } |
| 42 return nil, errors.Annotate(err).Reason("failed to searc
h PATH for: %(interp)q"). | 76 return nil, errors.Annotate(err).Reason("failed to searc
h PATH for: %(interp)q"). |
| 43 D("interp", s). | 77 D("interp", s). |
| 44 Err() | 78 Err() |
| 45 } | 79 } |
| 46 | 80 |
| 47 » » i := Interpreter{Python: p} | 81 » » i := Interpreter{ |
| 82 » » » Python: lpr.Path, |
| 83 » » } |
| 84 |
| 85 » » // If our LookPathResult included a target version, install that
into the |
| 86 » » // Interpreter, allowing it to use this cached value when GetVer
sion is |
| 87 » » // called instead of needing to perform an additional lookup. |
| 88 » » // |
| 89 » » // Note that our LookPathResult may not populate Version, in whi
ch case we |
| 90 » » // will not pre-cache it. |
| 91 » » if !lpr.Version.IsZero() { |
| 92 » » » i.cachedVersion = &lpr.Version |
| 93 » » } |
| 48 if err := i.Normalize(); err != nil { | 94 if err := i.Normalize(); err != nil { |
| 49 return nil, err | 95 return nil, err |
| 50 } | 96 } |
| 51 | 97 |
| 52 iv, err := i.GetVersion(c) | 98 iv, err := i.GetVersion(c) |
| 53 if err != nil { | 99 if err != nil { |
| 54 return nil, errors.Annotate(err).Reason("failed to get v
ersion for: %(interp)q"). | 100 return nil, errors.Annotate(err).Reason("failed to get v
ersion for: %(interp)q"). |
| 55 D("interp", i.Python). | 101 D("interp", i.Python). |
| 56 Err() | 102 Err() |
| 57 } | 103 } |
| 58 if vers.IsSatisfiedBy(iv) { | 104 if vers.IsSatisfiedBy(iv) { |
| 59 return &i, nil | 105 return &i, nil |
| 60 } | 106 } |
| 61 } | 107 } |
| 62 | 108 |
| 63 return nil, errors.New("no Python found") | 109 return nil, errors.New("no Python found") |
| 64 } | 110 } |
| OLD | NEW |