OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2013 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 # This script is a wrapper around the GN binary that is pulled from Google | |
M-A Ruel
2013/11/08 00:48:31
Make that a docstring
"""Wrapper around the GN bi
| |
7 # Cloud Storage when you sync Chrome. The binaries go into platform-specific | |
8 # subdirectories in the source tree. | |
9 # | |
10 # This script makes there be one place that we can forward to the correct | |
M-A Ruel
2013/11/08 00:48:31
unusual English structure to me. I should read mor
| |
11 # platform's binary. It will also automatically try to find the gn binary when | |
12 # run inside the chrome source tree, so users can just type "gn" on the command | |
13 # line (normally depot_tools is on the path). | |
14 | |
15 import os | |
16 import subprocess | |
17 import sys | |
18 | |
M-A Ruel
2013/11/08 00:48:31
2 lines between file level symbols
| |
19 class SourceTreeNotFoundError(IOError): | |
20 pass | |
21 | |
22 class PlatformUnknownError(IOError): | |
23 pass | |
24 | |
25 def HasDotfile(path): | |
26 """Returns True if the given path has a .gn file in it.""" | |
27 return os.path.exists(path + '/.gn') | |
28 | |
29 def FindSourceRootOnPath(): | |
30 """Searches upward from the current directory for the root of the source | |
31 tree and returns the found path. Returns nil if no source root could | |
M-A Ruel
2013/11/08 00:48:31
s/nil/None/
| |
32 be found.""" | |
33 cur = os.getcwd() | |
34 while True: | |
35 if HasDotfile(cur): | |
36 return cur | |
37 up_one = os.path.dirname(cur) | |
38 if up_one == cur: | |
39 return None # Reached the top of the directory tree | |
40 cur = up_one | |
41 | |
42 def RunGN(sourceroot): | |
43 # The binaries in platform-specific subdirectories in src/tools/gn/bin. | |
44 gnpath = sourceroot + '/tools/gn/bin/' | |
45 if sys.platform == 'win32': | |
46 gnpath += 'win/gn.exe' | |
47 elif sys.platform.startswith('linux'): | |
48 gnpath += 'linux/gn' | |
49 elif sys.platform == 'darwin': | |
50 gnpath += 'mac/gn' | |
51 else: | |
52 raise PlatformUnknownError('Unknown platform for GN: ' + sys.platform) | |
M-A Ruel
2013/11/08 00:48:31
I'd recommend to just print >> sys.stderr and retu
brettw
2013/11/08 20:49:16
I kept this exception. I'm going to use this as a
| |
53 | |
54 return subprocess.call([gnpath] + sys.argv[1:]) | |
55 | |
56 def main(args): | |
57 # We don't use optparse since that does things like handle --help that | |
58 # we explicitly want to pass to the GN binary. | |
59 # | |
60 # The only command line argument we support is sourceroot which allows us to | |
61 # find the GN binary. This also gets passed to the GN binary which it will | |
62 # interpret as the same thing. | |
63 ROOT_ARG = '--root=' | |
64 sourceroot = None | |
M-A Ruel
2013/11/08 00:48:31
I think it is not a bad idea to not support this a
brettw
2013/11/08 20:49:16
Okay, I removed this.
| |
65 for arg in sys.argv[1:]: | |
66 if arg.startswith(ROOT_ARG): | |
67 sourceroot = arg[len(ROOT_ARG):] | |
68 break | |
69 | |
70 if not sourceroot: | |
71 sourceroot = FindSourceRootOnPath() | |
72 if not sourceroot: | |
73 raise SourceTreeNotFoundError( | |
M-A Ruel
2013/11/08 00:48:31
I'd recommend to just print >> sys.stderr and retu
| |
74 '.gn file not found in any directory in the current path.' + | |
75 'If you want to run it outside the source tree, use --root') | |
76 return RunGN(sourceroot) | |
77 | |
78 if __name__ == '__main__': | |
79 sys.exit(main(sys.argv)) | |
OLD | NEW |