1#!/usr/bin/env python
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"""Can be used to point environment variable to hermetic Go toolset.
12
13Usage (on linux and mac):
14$ eval `./env.py`
15$ go version
16
17Or it can be used to wrap a command:
18
19$ ./env.py go version
20"""
21
22assert __name__ == '__main__'
23
24import imp
25import os
26import subprocess
27import sys
28
29# Do not want to mess with sys.path, load the module directly.
30bootstrap = imp.load_source(
31    'bootstrap', os.path.join(os.path.dirname(__file__), 'bootstrap.py'))
32
33old = os.environ.copy()
34new = bootstrap.prepare_go_environ()
35
36if len(sys.argv) == 1:
37  for key, value in sorted(new.iteritems()):
38    if old.get(key) != value:
39      print 'export %s="%s"' % (key, value)
40else:
41  exe = sys.argv[1]
42  if exe == 'python':
43    exe = sys.executable
44  else:
45    # Help Windows to find the executable in new PATH, do it only when
46    # executable is referenced by name (and not by path).
47    if os.sep not in exe:
48      exe = bootstrap.find_executable(exe, [bootstrap.WORKSPACE])
49  sys.exit(subprocess.call([exe] + sys.argv[2:], env=new))
50