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_cssvaluekeywords.py is a harness script to connect actions sections of |
| 8 # gyp-based builds to makevalues.pl. |
| 9 # |
| 10 # usage: action_cssvaluekeywords.py OUTPUTS -- INPUTS |
| 11 # |
| 12 # Exactly two outputs must be specified: a path to each of CSSValueKeywords.c |
| 13 # and CSSValueKeywords.h. |
| 14 # |
| 15 # Multiple inputs may be specified. One input must have a basename of |
| 16 # makevalues.pl; this is taken as the path to makevalues.pl. All other inputs |
| 17 # are paths to .in files that are used as input to makevalues.pl; at least |
| 18 # one, CSSValueKeywords.in, is required. |
| 19 |
| 20 |
| 21 import os |
| 22 import posixpath |
| 23 import shutil |
| 24 import subprocess |
| 25 import sys |
| 26 |
| 27 |
| 28 def SplitArgsIntoSections(args): |
| 29 sections = [] |
| 30 while len(args) > 0: |
| 31 if not '--' in args: |
| 32 # If there is no '--' left, everything remaining is an entire section. |
| 33 dashes = len(args) |
| 34 else: |
| 35 dashes = args.index('--') |
| 36 |
| 37 sections.append(args[:dashes]) |
| 38 |
| 39 # Next time through the loop, look at everything after this '--'. |
| 40 if dashes + 1 == len(args): |
| 41 # If the '--' is at the end of the list, we won't come back through the |
| 42 # loop again. Add an empty section now corresponding to the nothingness |
| 43 # following the final '--'. |
| 44 args = [] |
| 45 sections.append(args) |
| 46 else: |
| 47 args = args[dashes + 1:] |
| 48 |
| 49 return sections |
| 50 |
| 51 |
| 52 def main(args): |
| 53 (outputs, inputs) = SplitArgsIntoSections(args[1:]) |
| 54 |
| 55 # Make all output pathnames absolute so that they can be accessed after |
| 56 # changing directory. |
| 57 for index in xrange(0, len(outputs)): |
| 58 outputs[index] = os.path.abspath(outputs[index]) |
| 59 |
| 60 output_dir = os.path.dirname(outputs[0]) |
| 61 |
| 62 # Look at the inputs and figure out which one is makevalues.pl and which are |
| 63 # inputs to that script. |
| 64 makevalues_input = None |
| 65 in_files = [] |
| 66 for input in inputs: |
| 67 # Make input pathnames absolute so they can be accessed after changing |
| 68 # directory. On Windows, convert \ to / for inputs to the perl script to |
| 69 # work around the intermix of activepython + cygwin perl. |
| 70 input_abs = os.path.abspath(input) |
| 71 input_abs_posix = input_abs.replace(os.path.sep, posixpath.sep) |
| 72 input_basename = os.path.basename(input) |
| 73 if input_basename == 'makevalues.pl': |
| 74 assert makevalues_input == None |
| 75 makevalues_input = input_abs |
| 76 elif input_basename.endswith('.in'): |
| 77 in_files.append(input_abs_posix) |
| 78 else: |
| 79 assert False |
| 80 |
| 81 assert makevalues_input != None |
| 82 assert len(in_files) >= 1 |
| 83 |
| 84 # Change to the output directory because makevalues.pl puts output in its |
| 85 # working directory. |
| 86 os.chdir(output_dir) |
| 87 |
| 88 # Merge all in_files into a single file whose name will be the same as the |
| 89 # first listed in_file, but in the output directory. |
| 90 merged_path = os.path.basename(in_files[0]) |
| 91 merged = open(merged_path, 'wb') # 'wb' to get \n only on windows |
| 92 |
| 93 # Make sure there aren't any duplicate lines in the in files. Lowercase |
| 94 # everything because CSS values are case-insensitive. |
| 95 line_dict = {} |
| 96 for in_file_path in in_files: |
| 97 in_file = open(in_file_path) |
| 98 for line in in_file: |
| 99 line = line.rstrip() |
| 100 if line.startswith('#'): |
| 101 line = '' |
| 102 if line == '': |
| 103 continue |
| 104 line = line.lower() |
| 105 if line in line_dict: |
| 106 raise KeyError, 'Duplicate value %s' % line |
| 107 line_dict[line] = True |
| 108 print >>merged, line |
| 109 in_file.close() |
| 110 |
| 111 merged.close() |
| 112 |
| 113 # Build up the command. |
| 114 command = ['perl', makevalues_input] |
| 115 |
| 116 # Do it. check_call is new in 2.5, so simulate its behavior with call and |
| 117 # assert. |
| 118 return_code = subprocess.call(command) |
| 119 assert return_code == 0 |
| 120 |
| 121 # Don't leave behind the merged file or the .gperf file created by |
| 122 # makevalues. |
| 123 (root, ext) = os.path.splitext(merged_path) |
| 124 gperf_path = root + '.gperf' |
| 125 os.unlink(gperf_path) |
| 126 os.unlink(merged_path) |
| 127 |
| 128 # Go through the outputs. Any output that belongs in a different directory |
| 129 # is moved. Do a copy and delete instead of rename for maximum portability. |
| 130 # Note that all paths used in this section are still absolute. |
| 131 for output in outputs: |
| 132 this_output_dir = os.path.dirname(output) |
| 133 if this_output_dir != output_dir: |
| 134 output_basename = os.path.basename(output) |
| 135 src = os.path.join(output_dir, output_basename) |
| 136 dst = os.path.join(this_output_dir, output_basename) |
| 137 shutil.copyfile(src, dst) |
| 138 os.unlink(src) |
| 139 |
| 140 return return_code |
| 141 |
| 142 |
| 143 if __name__ == '__main__': |
| 144 sys.exit(main(sys.argv)) |
OLD | NEW |