1#!/usr/bin/env python 2# 3# Copyright (C) 2010 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 32# This script concatenates CSS files in the order specified 33# as @import url(...) in the input stylesheet. 34 35from cStringIO import StringIO 36import os.path 37import re 38import sys 39 40 41import_regex = re.compile(r"@import\s*\url\(\s*\"([^\"]+)\"\s*\)") 42 43 44def extract_css_files(stylesheet_name): 45 result = [] 46 line_number = 1 47 with open(stylesheet_name, 'r') as input: 48 for line in input.readlines(): 49 match = re.search(import_regex, line) 50 if match: 51 result.append((match.group(1), line_number)) 52 line_number += 1 53 return result 54 55 56def main(argv): 57 58 if len(argv) != 3: 59 print('usage: %s input.css output.css' % argv[0]) 60 return 1 61 62 input_stylesheet_name = argv[1] 63 output_file_name = argv[2] 64 input_directory = os.path.dirname(input_stylesheet_name) 65 output = StringIO() 66 67 for input_file_name, line_number in extract_css_files(input_stylesheet_name): 68 full_path = os.path.join(input_directory, input_file_name) 69 if not os.path.isfile(full_path): 70 raise Exception('File %s referenced in %s:%d was not found, ' 71 'check source tree for consistency' % 72 (input_file_name, input_stylesheet_name, line_number)) 73 output.write('/* %s */\n\n' % input_file_name) 74 with open(full_path, 'r') as input_file: 75 output.write(input_file.read()) 76 output.write('\n') 77 78 if os.path.exists(output_file_name): 79 os.remove(output_file_name); 80 with open(output_file_name, 'w') as output_file: 81 output_file.write(output.getvalue()) 82 output.close() 83 84 # Touch output file directory to make sure that Xcode will copy 85 # modified resource files. 86 if sys.platform == 'darwin': 87 output_dir_name = os.path.dirname(output_file_name) 88 os.utime(output_dir_name, None) 89 90 91if __name__ == '__main__': 92 sys.exit(main(sys.argv)) 93