OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 # action_makenames.py is a harness script to connect actions sections of |
| 8 # gyp-based builds to make_names.pl. |
| 9 # |
| 10 # usage: action_makenames.py OUTPUTS -- INPUTS [-- OPTIONS] |
| 11 # |
| 12 # Multiple OUTPUTS, INPUTS, and OPTIONS may be listed. The sections are |
| 13 # separated by -- arguments. |
| 14 # |
| 15 # The directory name of the first output is chosen as the directory in which |
| 16 # make_names will run. If the directory name for any subsequent output is |
| 17 # different, those files will be moved to the desired directory. |
| 18 # |
| 19 # Multiple INPUTS may be listed. An input with a basename matching |
| 20 # "make_names.pl" is taken as the path to that script. Inputs with names |
| 21 # ending in TagNames.in or tags.in are taken as tag inputs. Inputs with names |
| 22 # ending in AttributeNames.in or attrs.in are taken as attribute inputs. There |
| 23 # may be at most one tag input and one attribute input. A make_names.pl input |
| 24 # is required and at least one tag or attribute input must be present. |
| 25 # |
| 26 # OPTIONS is a list of additional options to pass to make_names.pl. This |
| 27 # section need not be present. |
| 28 |
| 29 |
| 30 import os |
| 31 import posixpath |
| 32 import shutil |
| 33 import subprocess |
| 34 import sys |
| 35 |
| 36 |
| 37 def SplitArgsIntoSections(args): |
| 38 sections = [] |
| 39 while len(args) > 0: |
| 40 if not '--' in args: |
| 41 # If there is no '--' left, everything remaining is an entire section. |
| 42 dashes = len(args) |
| 43 else: |
| 44 dashes = args.index('--') |
| 45 |
| 46 sections.append(args[:dashes]) |
| 47 |
| 48 # Next time through the loop, look at everything after this '--'. |
| 49 if dashes + 1 == len(args): |
| 50 # If the '--' is at the end of the list, we won't come back through the |
| 51 # loop again. Add an empty section now corresponding to the nothingness |
| 52 # following the final '--'. |
| 53 args = [] |
| 54 sections.append(args) |
| 55 else: |
| 56 args = args[dashes + 1:] |
| 57 |
| 58 return sections |
| 59 |
| 60 |
| 61 def main(args): |
| 62 sections = SplitArgsIntoSections(args[1:]) |
| 63 assert len(sections) == 2 or len(sections) == 3 |
| 64 (outputs, inputs) = sections[:2] |
| 65 if len(sections) == 3: |
| 66 options = sections[2] |
| 67 else: |
| 68 options = [] |
| 69 |
| 70 # Make all output pathnames absolute so that they can be accessed after |
| 71 # changing directory. |
| 72 for index in xrange(0, len(outputs)): |
| 73 outputs[index] = os.path.abspath(outputs[index]) |
| 74 |
| 75 output_dir = os.path.dirname(outputs[0]) |
| 76 |
| 77 # Look at the inputs and figure out which ones are make_names.pl, tags, and |
| 78 # attributes. There can be at most one of each, and those are the only |
| 79 # input types supported. make_names.pl is required and at least one of tags |
| 80 # and attributes is required. |
| 81 make_names_input = None |
| 82 tag_input = None |
| 83 attr_input = None |
| 84 for input in inputs: |
| 85 # Make input pathnames absolute so they can be accessed after changing |
| 86 # directory. On Windows, convert \ to / for inputs to the perl script to |
| 87 # work around the intermix of activepython + cygwin perl. |
| 88 input_abs = os.path.abspath(input) |
| 89 input_abs_posix = input_abs.replace(os.path.sep, posixpath.sep) |
| 90 input_basename = os.path.basename(input) |
| 91 if input_basename == 'make_names.pl': |
| 92 assert make_names_input == None |
| 93 make_names_input = input_abs |
| 94 elif input_basename.endswith('TagNames.in') or \ |
| 95 input_basename.endswith('tags.in'): |
| 96 assert tag_input == None |
| 97 tag_input = input_abs_posix |
| 98 elif input_basename.endswith('AttributeNames.in') or \ |
| 99 input_basename.endswith('attrs.in'): |
| 100 assert attr_input == None |
| 101 attr_input = input_abs_posix |
| 102 else: |
| 103 assert False |
| 104 |
| 105 assert make_names_input != None |
| 106 assert tag_input != None or attr_input != None |
| 107 |
| 108 # scripts_path is a Perl include directory, located relative to |
| 109 # make_names_input. |
| 110 scripts_path = os.path.normpath( |
| 111 os.path.join(os.path.dirname(make_names_input), os.pardir, |
| 112 'bindings', 'scripts')) |
| 113 |
| 114 # Change to the output directory because make_names.pl puts output in its |
| 115 # working directory. |
| 116 os.chdir(output_dir) |
| 117 |
| 118 # Build up the command. |
| 119 command = ['perl', '-I', scripts_path, make_names_input] |
| 120 if tag_input != None: |
| 121 command.extend(['--tags', tag_input]) |
| 122 if attr_input != None: |
| 123 command.extend(['--attrs', attr_input]) |
| 124 command.extend(options) |
| 125 |
| 126 # Do it. check_call is new in 2.5, so simulate its behavior with call and |
| 127 # assert. |
| 128 return_code = subprocess.call(command) |
| 129 assert return_code == 0 |
| 130 |
| 131 # Go through the outputs. Any output that belongs in a different directory |
| 132 # is moved. Do a copy and delete instead of rename for maximum portability. |
| 133 # Note that all paths used in this section are still absolute. |
| 134 for output in outputs: |
| 135 this_output_dir = os.path.dirname(output) |
| 136 if this_output_dir != output_dir: |
| 137 output_basename = os.path.basename(output) |
| 138 src = os.path.join(output_dir, output_basename) |
| 139 dst = os.path.join(this_output_dir, output_basename) |
| 140 shutil.copyfile(src, dst) |
| 141 os.unlink(src) |
| 142 |
| 143 return return_code |
| 144 |
| 145 |
| 146 if __name__ == '__main__': |
| 147 sys.exit(main(sys.argv)) |
OLD | NEW |