| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 # for details. All rights reserved. Use of this source code is governed by a | 4 # for details. All rights reserved. Use of this source code is governed by a |
| 5 # BSD-style license that can be found in the LICENSE file. | 5 # BSD-style license that can be found in the LICENSE file. |
| 6 | 6 |
| 7 """ | 7 """ |
| 8 Wrapper around a build action that should only be executed in "release" mode. | 8 Wrapper around a build action that should only be executed in release mode. |
| 9 | 9 |
| 10 The following options are accepted: | 10 The mode is defined via an environment varible DART_BUILD_MODE. |
| 11 | 11 |
| 12 --mode=[release,debug] | 12 The arguments to the script are: |
| 13 | 13 |
| 14 --outputs=files... | 14 only_in_release_mode.py files... -- command arguments... |
| 15 | 15 |
| 16 If mode is not 'release', the script will create the files listed in | 16 If mode is not 'release', the script will create the files listed |
| 17 outputs. If mode is release, the script will execute the remaining | 17 before --. If mode is release, the script will execute the command |
| 18 command line arguments as a command. | 18 after --. |
| 19 | |
| 20 Files are a list of quoted filenames separated by space. For example, | |
| 21 '"file1.ext" "file2.ext"' | |
| 22 """ | 19 """ |
| 23 | 20 |
| 24 import optparse | 21 import os |
| 25 import subprocess | 22 import subprocess |
| 26 import sys | 23 import sys |
| 27 | 24 |
| 28 | 25 |
| 29 def BuildOptions(): | |
| 30 result = optparse.OptionParser() | |
| 31 result.add_option('-m', '--mode') | |
| 32 result.add_option('-o', '--outputs') | |
| 33 return result | |
| 34 | |
| 35 | |
| 36 def Main(): | 26 def Main(): |
| 37 (options, arguments) = BuildOptions().parse_args() | 27 # Throws an error if '--' is not in the argument list. |
| 38 if options.mode != 'release': | 28 separator_index = sys.argv.index('--') |
| 39 print >> sys.stderr, 'Not running %s in mode=%s' % (arguments, | 29 outputs = sys.argv[1:separator_index] |
| 40 options.mode) | 30 arguments = sys.argv[separator_index + 1:] |
| 41 for output in options.outputs.strip('"').split('" "'): | 31 mode = os.getenv('DART_BUILD_MODE', default='release') |
| 32 if mode != 'release': |
| 33 print >> sys.stderr, 'Not running %s in mode=%s' % (arguments, mode) |
| 34 for output in outputs: |
| 42 with open(output, 'w'): | 35 with open(output, 'w'): |
| 43 # Create an empty file to ensure that we don't rerun this | 36 # Create an empty file to ensure that we don't rerun this |
| 44 # command unnecessarily. | 37 # command unnecessarily. |
| 45 pass | 38 pass |
| 46 return 0 | 39 return 0 |
| 47 else: | 40 else: |
| 48 try: | 41 try: |
| 49 subprocess.check_call(arguments) | 42 subprocess.check_call(arguments) |
| 50 except subprocess.CalledProcessError as e: | 43 except subprocess.CalledProcessError as e: |
| 51 return e.returncode | 44 return e.returncode |
| 52 return 0 | 45 return 0 |
| 53 | 46 |
| 54 | 47 |
| 55 if __name__ == '__main__': | 48 if __name__ == '__main__': |
| 56 sys.exit(Main()) | 49 sys.exit(Main()) |
| OLD | NEW |