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" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 interp, err := findInterpreter(c, s, vers, lookPath) | 62 interp, err := findInterpreter(c, s, vers, lookPath) |
63 if err == nil { | 63 if err == nil { |
64 return interp, nil | 64 return interp, nil |
65 } | 65 } |
66 | 66 |
67 logging.WithError(err).Debugf(c, "Could not find Python for: %q.
", s) | 67 logging.WithError(err).Debugf(c, "Could not find Python for: %q.
", s) |
68 lookErrs.Assign(i, err) | 68 lookErrs.Assign(i, err) |
69 } | 69 } |
70 | 70 |
71 // No Python interpreter could be identified. | 71 // No Python interpreter could be identified. |
72 » return nil, errors.Annotate(lookErrs.Get()).Reason("no Python found").Er
r() | 72 » return nil, errors.Annotate(lookErrs.Get(), "no Python found").Err() |
73 } | 73 } |
74 | 74 |
75 func findInterpreter(c context.Context, name string, vers Version, lookPath Look
PathFunc) (*Interpreter, error) { | 75 func findInterpreter(c context.Context, name string, vers Version, lookPath Look
PathFunc) (*Interpreter, error) { |
76 if lookPath == nil { | 76 if lookPath == nil { |
77 lookPath = osExecLookPath | 77 lookPath = osExecLookPath |
78 } | 78 } |
79 lpr, err := lookPath(c, name) | 79 lpr, err := lookPath(c, name) |
80 if err != nil { | 80 if err != nil { |
81 » » return nil, errors.Annotate(err).Reason("could not find executab
le for: %(name)q"). | 81 » » return nil, errors.Annotate(err, "could not find executable for:
%q", name).Err() |
82 » » » D("name", name). | |
83 » » » Err() | |
84 } | 82 } |
85 | 83 |
86 i := Interpreter{ | 84 i := Interpreter{ |
87 Python: lpr.Path, | 85 Python: lpr.Path, |
88 } | 86 } |
89 | 87 |
90 // If our LookPathResult included a target version, install that into th
e | 88 // If our LookPathResult included a target version, install that into th
e |
91 // Interpreter, allowing it to use this cached value when GetVersion is | 89 // Interpreter, allowing it to use this cached value when GetVersion is |
92 // called instead of needing to perform an additional lookup. | 90 // called instead of needing to perform an additional lookup. |
93 // | 91 // |
94 // Note that our LookPathResult may not populate Version, in which case
we | 92 // Note that our LookPathResult may not populate Version, in which case
we |
95 // will not pre-cache it. | 93 // will not pre-cache it. |
96 if !lpr.Version.IsZero() { | 94 if !lpr.Version.IsZero() { |
97 i.cachedVersion = &lpr.Version | 95 i.cachedVersion = &lpr.Version |
98 } | 96 } |
99 if err := i.Normalize(); err != nil { | 97 if err := i.Normalize(); err != nil { |
100 return nil, err | 98 return nil, err |
101 } | 99 } |
102 | 100 |
103 iv, err := i.GetVersion(c) | 101 iv, err := i.GetVersion(c) |
104 if err != nil { | 102 if err != nil { |
105 » » return nil, errors.Annotate(err).Reason("failed to get version f
or: %(interp)q"). | 103 » » return nil, errors.Annotate(err, "failed to get version for: %q"
, i.Python).Err() |
106 » » » D("interp", i.Python). | |
107 » » » Err() | |
108 } | 104 } |
109 if !vers.IsSatisfiedBy(iv) { | 105 if !vers.IsSatisfiedBy(iv) { |
110 » » return nil, errors.Reason("interpreter %(interp)q version %(inte
rpVersion)q does not satisfy %(version)q"). | 106 » » return nil, errors.Reason("interpreter %q version %q does not sa
tisfy %q", i.Python, iv, vers).Err() |
111 » » » D("interp", i.Python). | |
112 » » » D("interpVersion", iv). | |
113 » » » D("version", vers). | |
114 » » » Err() | |
115 } | 107 } |
116 | 108 |
117 return &i, nil | 109 return &i, nil |
118 } | 110 } |
119 | 111 |
120 func osExecLookPath(c context.Context, target string) (*LookPathResult, error) { | 112 func osExecLookPath(c context.Context, target string) (*LookPathResult, error) { |
121 v, err := exec.LookPath(target) | 113 v, err := exec.LookPath(target) |
122 if err != nil { | 114 if err != nil { |
123 return nil, err | 115 return nil, err |
124 } | 116 } |
125 return &LookPathResult{Path: v}, nil | 117 return &LookPathResult{Path: v}, nil |
126 } | 118 } |
OLD | NEW |