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 # usage: 36 # action_useragentstylesheets.py OUTPUTS INPUTS -- MAINSCRIPT MODULES -- OPTIONS 37 # 38 # OUTPUTS must contain two items, in order: a path to UserAgentStyleSheets.h 39 # and a path to UserAgentStyleSheetsData.cpp. 40 # INPUTS contains one or more CSS files. 41 # 42 # MAINSCRIPT is the path to make-css-file-arrays.pl. MODULES may contain 43 # multiple paths to additional perl modules. 44 # 45 # OPTIONS are passed as-is to MAINSCRIPT as additional arguments. 46 47 48 import os 49 import shlex 50 import subprocess 51 import sys 52 53 54 def SplitArgsIntoSections(args): 55 sections = [] 56 while len(args) > 0: 57 if not '--' in args: 58 # If there is no '--' left, everything remaining is an entire section. 59 dashes = len(args) 60 else: 61 dashes = args.index('--') 62 63 sections.append(args[:dashes]) 64 65 # Next time through the loop, look at everything after this '--'. 66 if dashes + 1 == len(args): 67 # If the '--' is at the end of the list, we won't come back through the 68 # loop again. Add an empty section now corresponding to the nothingness 69 # following the final '--'. 70 args = [] 71 sections.append(args) 72 else: 73 args = args[dashes + 1:] 74 75 return sections 76 77 78 def main(args): 79 sections = SplitArgsIntoSections(args[1:]) 80 assert len(sections) == 3 81 (outputsInputs, scripts, options) = sections 82 83 assert len(outputsInputs) >= 3 84 outputH = outputsInputs[0] 85 outputCpp = outputsInputs[1] 86 styleSheets = outputsInputs[2:] 87 88 assert len(scripts) >= 1 89 makeCssFileArrays = scripts[0] 90 perlModules = scripts[1:] 91 92 includeDirs = [] 93 for perlModule in perlModules: 94 includeDir = os.path.dirname(perlModule) 95 if not includeDir in includeDirs: 96 includeDirs.append(includeDir) 97 98 # The defines come in as one flat string. Split it up into distinct arguments. 99 if '--defines' in options: 100 definesIndex = options.index('--defines') 101 if definesIndex + 1 < len(options): 102 splitOptions = shlex.split(options[definesIndex + 1]) 103 if splitOptions: 104 options[definesIndex + 1] = ' '.join(splitOptions) 105 106 # Build up the command. 107 command = ['perl'] 108 for includeDir in includeDirs: 109 command.extend(['-I', includeDir]) 110 command.append(makeCssFileArrays) 111 command.extend(options) 112 command.extend([outputH, outputCpp]) 113 command.extend(styleSheets) 114 115 # Do it. check_call is new in 2.5, so simulate its behavior with call and 116 # assert. 117 returnCode = subprocess.call(command) 118 assert returnCode == 0 119 120 return returnCode 121 122 123 if __name__ == '__main__': 124 sys.exit(main(sys.argv)) 125