Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Unified Diff: vpython/python/find.go

Issue 2701073002: vpython: Add Python interpreter handling package. (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | vpython/python/interpreter.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: vpython/python/find.go
diff --git a/vpython/python/find.go b/vpython/python/find.go
new file mode 100644
index 0000000000000000000000000000000000000000..65c62ede0fba69bf302191a76ce57bf9dd8c0c2e
--- /dev/null
+++ b/vpython/python/find.go
@@ -0,0 +1,60 @@
+// 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}
+ iv, err := i.GetVersion(c)
+ if err != nil {
+ return nil, errors.Annotate(err).Reason("failed to get version for: %(interp)q").
+ D("interp", p).
+ Err()
+ }
+ if vers.IsSatisfiedBy(iv) {
+ return &i, nil
+ }
+ }
+
+ return nil, errors.New("no Python found")
+}
« no previous file with comments | « no previous file | vpython/python/interpreter.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698