1 #!/usr/bin/python 2 # 3 # Copyright (C) 2009 Google Inc. All rights reserved. 4 # 5 # Redistribution and use in source and binary forms, with or without 6 # modification, are permitted provided that the following conditions are 7 # met: 8 # 9 # * Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # * Redistributions in binary form must reproduce the above 12 # copyright notice, this list of conditions and the following disclaimer 13 # in the documentation and/or other materials provided with the 14 # distribution. 15 # * Neither the name of Google Inc. nor the names of its 16 # contributors may be used to endorse or promote products derived from 17 # this software without specific prior written permission. 18 # 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 # 31 # Copyright (c) 2009 The Chromium Authors. All rights reserved. 32 # Use of this source code is governed by a BSD-style license that can be 33 # found in the LICENSE file. 34 35 # action_cssvaluekeywords.py is a harness script to connect actions sections of 36 # gyp-based builds to makevalues.pl. 37 # 38 # usage: action_cssvaluekeywords.py OUTPUTS -- INPUTS 39 # 40 # Exactly two outputs must be specified: a path to each of CSSValueKeywords.c 41 # and CSSValueKeywords.h. 42 # 43 # Multiple inputs may be specified. One input must have a basename of 44 # makevalues.pl; this is taken as the path to makevalues.pl. All other inputs 45 # are paths to .in files that are used as input to makevalues.pl; at least 46 # one, CSSValueKeywords.in, is required. 47 48 49 import os 50 import posixpath 51 import shutil 52 import subprocess 53 import sys 54 55 56 def SplitArgsIntoSections(args): 57 sections = [] 58 while len(args) > 0: 59 if not '--' in args: 60 # If there is no '--' left, everything remaining is an entire section. 61 dashes = len(args) 62 else: 63 dashes = args.index('--') 64 65 sections.append(args[:dashes]) 66 67 # Next time through the loop, look at everything after this '--'. 68 if dashes + 1 == len(args): 69 # If the '--' is at the end of the list, we won't come back through the 70 # loop again. Add an empty section now corresponding to the nothingness 71 # following the final '--'. 72 args = [] 73 sections.append(args) 74 else: 75 args = args[dashes + 1:] 76 77 return sections 78 79 80 def main(args): 81 (outputs, inputs) = SplitArgsIntoSections(args[1:]) 82 83 # Make all output pathnames absolute so that they can be accessed after 84 # changing directory. 85 for index in xrange(0, len(outputs)): 86 outputs[index] = os.path.abspath(outputs[index]) 87 88 outputDir = os.path.dirname(outputs[0]) 89 90 # Look at the inputs and figure out which one is makevalues.pl and which are 91 # inputs to that script. 92 makevaluesInput = None 93 inFiles = [] 94 for input in inputs: 95 # Make input pathnames absolute so they can be accessed after changing 96 # directory. On Windows, convert \ to / for inputs to the perl script to 97 # work around the intermix of activepython + cygwin perl. 98 inputAbs = os.path.abspath(input) 99 inputAbsPosix = inputAbs.replace(os.path.sep, posixpath.sep) 100 inputBasename = os.path.basename(input) 101 if inputBasename == 'makevalues.pl': 102 assert makevaluesInput == None 103 makevaluesInput = inputAbs 104 elif inputBasename.endswith('.in'): 105 inFiles.append(inputAbsPosix) 106 else: 107 assert False 108 109 assert makevaluesInput != None 110 assert len(inFiles) >= 1 111 112 # Change to the output directory because makevalues.pl puts output in its 113 # working directory. 114 os.chdir(outputDir) 115 116 # Merge all inFiles into a single file whose name will be the same as the 117 # first listed inFile, but in the output directory. 118 mergedPath = os.path.basename(inFiles[0]) 119 merged = open(mergedPath, 'wb') # 'wb' to get \n only on windows 120 121 # Make sure there aren't any duplicate lines in the in files. Lowercase 122 # everything because CSS values are case-insensitive. 123 lineDict = {} 124 for inFilePath in inFiles: 125 inFile = open(inFilePath) 126 for line in inFile: 127 line = line.rstrip() 128 if line.startswith('#'): 129 line = '' 130 if line == '': 131 continue 132 line = line.lower() 133 if line in lineDict: 134 raise KeyError, 'Duplicate value %s' % line 135 lineDict[line] = True 136 print >>merged, line 137 inFile.close() 138 139 merged.close() 140 141 # Build up the command. 142 command = ['perl', makevaluesInput] 143 144 # Do it. checkCall is new in 2.5, so simulate its behavior with call and 145 # assert. 146 returnCode = subprocess.call(command) 147 assert returnCode == 0 148 149 # Don't leave behind the merged file or the .gperf file created by 150 # makevalues. 151 (root, ext) = os.path.splitext(mergedPath) 152 gperfPath = root + '.gperf' 153 os.unlink(gperfPath) 154 os.unlink(mergedPath) 155 156 # Go through the outputs. Any output that belongs in a different directory 157 # is moved. Do a copy and delete instead of rename for maximum portability. 158 # Note that all paths used in this section are still absolute. 159 for output in outputs: 160 thisOutputDir = os.path.dirname(output) 161 if thisOutputDir != outputDir: 162 outputBasename = os.path.basename(output) 163 src = os.path.join(outputDir, outputBasename) 164 dst = os.path.join(thisOutputDir, outputBasename) 165 shutil.copyfile(src, dst) 166 os.unlink(src) 167 168 return returnCode 169 170 171 if __name__ == '__main__': 172 sys.exit(main(sys.argv)) 173