Chromium Code Reviews| Index: gn.py |
| =================================================================== |
| --- gn.py (revision 0) |
| +++ gn.py (revision 0) |
| @@ -0,0 +1,79 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +# 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
|
| +# Cloud Storage when you sync Chrome. The binaries go into platform-specific |
| +# subdirectories in the source tree. |
| +# |
| +# 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
|
| +# platform's binary. It will also automatically try to find the gn binary when |
| +# run inside the chrome source tree, so users can just type "gn" on the command |
| +# line (normally depot_tools is on the path). |
| + |
| +import os |
| +import subprocess |
| +import sys |
| + |
|
M-A Ruel
2013/11/08 00:48:31
2 lines between file level symbols
|
| +class SourceTreeNotFoundError(IOError): |
| + pass |
| + |
| +class PlatformUnknownError(IOError): |
| + pass |
| + |
| +def HasDotfile(path): |
| + """Returns True if the given path has a .gn file in it.""" |
| + return os.path.exists(path + '/.gn') |
| + |
| +def FindSourceRootOnPath(): |
| + """Searches upward from the current directory for the root of the source |
| + 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/
|
| + be found.""" |
| + cur = os.getcwd() |
| + while True: |
| + if HasDotfile(cur): |
| + return cur |
| + up_one = os.path.dirname(cur) |
| + if up_one == cur: |
| + return None # Reached the top of the directory tree |
| + cur = up_one |
| + |
| +def RunGN(sourceroot): |
| + # The binaries in platform-specific subdirectories in src/tools/gn/bin. |
| + gnpath = sourceroot + '/tools/gn/bin/' |
| + if sys.platform == 'win32': |
| + gnpath += 'win/gn.exe' |
| + elif sys.platform.startswith('linux'): |
| + gnpath += 'linux/gn' |
| + elif sys.platform == 'darwin': |
| + gnpath += 'mac/gn' |
| + else: |
| + 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
|
| + |
| + return subprocess.call([gnpath] + sys.argv[1:]) |
| + |
| +def main(args): |
| + # We don't use optparse since that does things like handle --help that |
| + # we explicitly want to pass to the GN binary. |
| + # |
| + # The only command line argument we support is sourceroot which allows us to |
| + # find the GN binary. This also gets passed to the GN binary which it will |
| + # interpret as the same thing. |
| + ROOT_ARG = '--root=' |
| + 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.
|
| + for arg in sys.argv[1:]: |
| + if arg.startswith(ROOT_ARG): |
| + sourceroot = arg[len(ROOT_ARG):] |
| + break |
| + |
| + if not sourceroot: |
| + sourceroot = FindSourceRootOnPath() |
| + if not sourceroot: |
| + raise SourceTreeNotFoundError( |
|
M-A Ruel
2013/11/08 00:48:31
I'd recommend to just print >> sys.stderr and retu
|
| + '.gn file not found in any directory in the current path.' + |
| + 'If you want to run it outside the source tree, use --root') |
| + return RunGN(sourceroot) |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv)) |