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 16assert __name__ == '__main__' 17 18import os 19import subprocess 20import sys 21 22# /path/to/util/bot 23ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 24 25# Platform depended suffix for executable files. 26EXE_SFX = '.exe' if sys.platform == 'win32' else '' 27 28def 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 38def 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? 50goroot = os.path.join(ROOT, 'golang') 51new = get_go_environ(goroot) 52 53exe = sys.argv[1] 54if exe == 'python': 55 exe = sys.executable 56else: 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) 61sys.exit(subprocess.call([exe] + sys.argv[2:], env=new)) 62