env.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5 
6 # Modified from go/env.py in Chromium infrastructure's repository to patch out
7 # everything but the core toolchain.
8 #
9 # https://chromium.googlesource.com/infra/infra/
10 
11 """Used to wrap a command:
12 
13 $ ./env.py go version
14 """
15 
16 assert __name__ == '__main__'
17 
18 import os
19 import subprocess
20 import sys
21 
22 # /path/to/util/bot
23 ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
24 
25 # Platform depended suffix for executable files.
26 EXE_SFX = '.exe' if sys.platform == 'win32' else ''
27 
28 def get_go_environ(goroot):
29  """Returns a copy of os.environ with added GOROOT and PATH variables."""
30  env = os.environ.copy()
31  env['GOROOT'] = goroot
32  gobin = os.path.join(goroot, 'bin')
33  path = env['PATH'].split(os.pathsep)
34  if gobin not in path:
35  env['PATH'] = os.pathsep.join([gobin] + path)
36  return env
37 
38 def find_executable(name, goroot):
39  """Returns full path to an executable in GOROOT."""
40  basename = name
41  if EXE_SFX and basename.endswith(EXE_SFX):
42  basename = basename[:-len(EXE_SFX)]
43  full_path = os.path.join(goroot, 'bin', basename + EXE_SFX)
44  if os.path.exists(full_path):
45  return full_path
46  return name
47 
48 # TODO(davidben): Now that we use CIPD to fetch Go, this script does not do
49 # much. Switch to setting up GOROOT and PATH in the recipe?
50 goroot = os.path.join(ROOT, 'golang')
51 new = get_go_environ(goroot)
52 
53 exe = sys.argv[1]
54 if exe == 'python':
55  exe = sys.executable
56 else:
57  # Help Windows to find the executable in new PATH, do it only when
58  # executable is referenced by name (and not by path).
59  if os.sep not in exe:
60  exe = find_executable(exe, goroot)
61 sys.exit(subprocess.call([exe] + sys.argv[2:], env=new))
env.find_executable
def find_executable(name, goroot)
Definition: env.py:38
env.get_go_environ
def get_go_environ(goroot)
Definition: env.py:28
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
split
static void split(const char *s, char ***ss, size_t *ns)
Definition: debug/trace.cc:111


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:15