1 #!/usr/bin/env python 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 import optparse 6 import parse_deps 7 import sys 8 import os 9 10 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../src")) 11 12 FILES_TO_IGNORE = ["about_tracing.js"] 13 14 js_warning_message = ( 15 """// Copyright (c) 2012 The Chromium Authors. All rights reserved. 16 // Use of this source code is governed by a BSD-style license that can be 17 // found in the LICENSE file. 18 19 /** 20 * WARNING: This file is generated by generate_deps_js_contents.py 21 * 22 * Do not edit directly. 23 */ 24 """) 25 26 def generate_deps_js(): 27 all_filenames = [] 28 for dirpath, dirnames, filenames in os.walk(srcdir): 29 for f in filenames: 30 all_filenames.append(os.path.join(dirpath, f)) 31 filenames = [x for x in all_filenames if 32 os.path.splitext(x)[1] == ".js"] 33 34 filenames = [os.path.relpath(x) for x in filenames] 35 filenames = [x for x in filenames 36 if x not in FILES_TO_IGNORE] 37 def ignored(x): 38 if os.path.basename(x).startswith('.'): 39 return True 40 return False 41 filenames = [x for x in filenames if not ignored(x)] 42 43 if "deps.js" in filenames: 44 filenames.remove("deps.js") 45 46 load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) 47 48 chunks = [js_warning_message] 49 for module in load_sequence: 50 for dependent_module_name in module.dependent_module_names: 51 chunks.append("base.addModuleDependency(\n '%s',\n '%s');\n" % ( 52 module.name, dependent_module_name)); 53 54 for style_sheet in module.style_sheets: 55 chunks.append("base.addModuleStylesheet(\n '%s',\n '%s');\n" % ( 56 module.name, style_sheet.name)); 57 58 result = "".join(chunks) 59 return result 60 61 def is_out_of_date(): 62 olddir = os.getcwd() 63 try: 64 os.chdir(srcdir) 65 66 o = open(os.path.join(srcdir, "deps.js"), 'r') 67 existing_deps_js = o.read() 68 o.close() 69 70 result_js = generate_deps_js() 71 72 if result_js != existing_deps_js: 73 return True 74 75 finally: 76 os.chdir(olddir) 77 return False 78 79 80 def main(args): 81 parser = optparse.OptionParser() 82 options, args = parser.parse_args(args) 83 84 olddir = os.getcwd() 85 try: 86 os.chdir(srcdir) 87 88 try: 89 deps_js = generate_deps_js() 90 except parse_deps.DepsException, ex: 91 sys.stderr.write("Error: %s\n\n" % str(ex)) 92 return 255 93 94 o = open(os.path.join(srcdir, "deps.js"), 'w') 95 o.write(deps_js) 96 o.close() 97 98 finally: 99 os.chdir(olddir) 100 101 return 0 102 103 if __name__ == "__main__": 104 sys.exit(main(sys.argv)) 105