Chromium Code Reviews| Index: vpython/python/find.go |
| diff --git a/vpython/python/find.go b/vpython/python/find.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4e92b6c7262834d08b51fa90ee4e305f56c8e82a |
| --- /dev/null |
| +++ b/vpython/python/find.go |
| @@ -0,0 +1,63 @@ |
| +// Copyright 2017 The LUCI Authors. All rights reserved. |
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +package python |
| + |
| +import ( |
| + "os/exec" |
| + |
| + "github.com/luci/luci-go/common/errors" |
| + |
| + "golang.org/x/net/context" |
| +) |
| + |
| +// Find attempts to find a Python interpreter matching the supplied version |
| +// using PATH. |
| +// |
| +// In order to accommodate multiple configurations on operating systems, Find |
| +// will attempt to identify versions that appear on the path |
| +func Find(c context.Context, vers Version) (*Interpreter, error) { |
| + // pythonM.m, pythonM, python |
| + searches := make([]string, 0, 3) |
| + pv := vers |
| + pv.Patch = 0 |
| + if pv.Minor > 0 { |
| + searches = append(searches, pv.PythonBase()) |
| + pv.Minor = 0 |
| + } |
| + if pv.Major > 0 { |
| + searches = append(searches, pv.PythonBase()) |
| + pv.Major = 0 |
| + } |
| + searches = append(searches, pv.PythonBase()) |
| + |
| + for _, s := range searches { |
| + p, err := exec.LookPath(s) |
| + if err != nil { |
| + if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound { |
| + // Not found is okay. |
| + continue |
| + } |
| + return nil, errors.Annotate(err).Reason("failed to search PATH for: %(interp)q"). |
| + D("interp", s). |
| + Err() |
| + } |
| + |
| + i := Interpreter{ |
| + Python: p, |
| + Isolated: true, |
| + } |
| + iv, err := i.GetVersion(c) |
| + if err != nil { |
| + return nil, errors.Annotate(err).Reason("failed to get veriso for: %(interp)q"). |
|
iannucci
2017/02/21 10:08:16
versio
dnj
2017/02/21 23:21:35
Vers I/O
|
| + D("interp", p). |
| + Err() |
| + } |
| + if vers.IsSatisfiedBy(iv) { |
| + return &i, nil |
| + } |
| + } |
| + |
| + return nil, errors.New("no Python found") |
| +} |