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

Side by Side Diff: go/env.py

Issue 2121653002: Fix prompt exporting escaped characters, opt into PS1 mod. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Fix pylint error. Created 4 years, 5 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 unified diff | Download patch
« no previous file with comments | « go/bootstrap.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Can be used to point environment variable to hermetic Go toolset. 6 """Can be used to point environment variable to hermetic Go toolset.
7 7
8 Usage (on linux and mac): 8 Usage (on linux and mac):
9 $ eval `./env.py` 9 $ eval `./env.py`
10 $ go version 10 $ go version
(...skipping 11 matching lines...) Expand all
22 import subprocess 22 import subprocess
23 import sys 23 import sys
24 24
25 # Do not want to mess with sys.path, load the module directly. 25 # Do not want to mess with sys.path, load the module directly.
26 bootstrap = imp.load_source( 26 bootstrap = imp.load_source(
27 'bootstrap', os.path.join(os.path.dirname(__file__), 'bootstrap.py')) 27 'bootstrap', os.path.join(os.path.dirname(__file__), 'bootstrap.py'))
28 28
29 old = os.environ.copy() 29 old = os.environ.copy()
30 new = bootstrap.prepare_go_environ() 30 new = bootstrap.prepare_go_environ()
31 31
32 def _escape_special(v):
33 """Returns (str): The supplied value, with special shell characters escaped.
34
35 Replace special characters with their escaped form. This will allow them
36 to be interpreted by the shell using the $'...' notation.
37
38 Args:
39 v (str): The input value to escape.
40 """
41 for f, r in (
42 ('\n', '\\n'),
43 ('\b', '\\b'),
44 ('\r', '\\r'),
45 ('\t', '\\t'),
46 ('\v', '\\v')):
47 v = v.replace(f, r)
48 return v
49
32 if len(sys.argv) == 1: 50 if len(sys.argv) == 1:
33 for key, value in sorted(new.iteritems()): 51 for key, value in sorted(new.iteritems()):
34 if old.get(key) != value: 52 if old.get(key) != value:
35 print 'export %s=%s' % (key, pipes.quote(value)) 53 orig_value, value = value, _escape_special(value)
54
55 # We will only use the $'...' notation if there was an escaped character
56 # in the string.
57 print 'export %s=%s%s' % (key, ('$') if orig_value != value else (''),
58 pipes.quote(value))
36 else: 59 else:
37 exe = sys.argv[1] 60 exe = sys.argv[1]
38 if exe == 'python': 61 if exe == 'python':
39 exe = sys.executable 62 exe = sys.executable
40 else: 63 else:
41 # Help Windows to find the executable in new PATH, do it only when 64 # Help Windows to find the executable in new PATH, do it only when
42 # executable is referenced by name (and not by path). 65 # executable is referenced by name (and not by path).
43 if os.sep not in exe: 66 if os.sep not in exe:
44 exe = bootstrap.find_executable(exe, [bootstrap.WORKSPACE]) 67 exe = bootstrap.find_executable(exe, [bootstrap.WORKSPACE])
45 sys.exit(subprocess.call([exe] + sys.argv[2:], env=new)) 68 sys.exit(subprocess.call([exe] + sys.argv[2:], env=new))
OLDNEW
« no previous file with comments | « go/bootstrap.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698