Chromium Code Reviews| 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{ | |
| 48 Python: p, | |
| 49 Isolated: true, | |
| 50 } | |
| 51 iv, err := i.GetVersion(c) | |
| 52 if err != nil { | |
| 53 return nil, errors.Annotate(err).Reason("failed to get v eriso for: %(interp)q"). | |
|
iannucci
2017/02/21 10:08:16
versio
dnj
2017/02/21 23:21:35
Vers I/O
| |
| 54 D("interp", p). | |
| 55 Err() | |
| 56 } | |
| 57 if vers.IsSatisfiedBy(iv) { | |
| 58 return &i, nil | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 return nil, errors.New("no Python found") | |
| 63 } | |
| OLD | NEW |