def main(args): parser = optparse.OptionParser(usage="%prog --outdir=<directory>") parser.add_option("--outdir", dest="out_dir", help="Where to place generated content") options, args = parser.parse_args(args) if not options.out_dir: sys.stderr.write("ERROR: Must specify --outdir=<directory>") parser.print_help() return 1 filenames = ["base.js", "about_tracing.js"] load_sequence = parse_deps.calc_load_sequence(filenames, [src_dir]) olddir = os.getcwd() try: try: result_html = generate_html(options.out_dir, load_sequence) except parse_deps.DepsException, ex: sys.stderr.write("Error: %s\n\n" % str(ex)) return 255 o = open(os.path.join(options.out_dir, "about_tracing.html"), 'w') o.write(result_html) o.close() result_js = generate_js(options.out_dir, load_sequence) o = open(os.path.join(options.out_dir, "about_tracing.js"), 'w') o.write(result_js) o.close()
def generate_deps_js(): all_filenames = [] for dirpath, dirnames, filenames in os.walk(srcdir): for f in filenames: all_filenames.append(os.path.join(dirpath, f)) filenames = [x for x in all_filenames if os.path.splitext(x)[1] == ".js"] filenames = [os.path.relpath(x) for x in filenames] filenames = [x for x in filenames if x not in FILES_TO_IGNORE] if "deps.js" in filenames: filenames.remove("deps.js") load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) chunks = [js_warning_message] for module in load_sequence: for dependent_module_name in module.dependent_module_names: chunks.append("base.addModuleDependency(\n '%s',\n '%s');\n" % ( module.name, dependent_module_name)); for style_sheet in module.style_sheets: chunks.append("base.addModuleStylesheet(\n '%s',\n '%s');\n" % ( module.name, style_sheet.name)); result = "".join(chunks) return result
def generate_js(): f = open(os.path.join(srcdir, "about_tracing.js.template"), 'r') template = f.read() f.close() assert template.find("<WARNING_MESSAGE></WARNING_MESSAGE>") != -1 assert template.find("<SCRIPT_CONTENTS></SCRIPT_CONTENTS>") != -1 filenames = [os.path.join(srcdir, x) for x in ["base.js", "profiling_view.js"]] filenames = [os.path.relpath(x) for x in filenames] import parse_deps load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) script_contents = "" script_contents += "window.FLATTENED = {};\n" for module in load_sequence: script_contents += "window.FLATTENED['%s'] = true;\n" % module.name for module in load_sequence: rel_filename = os.path.relpath(module.filename, srcdir) script_contents += """<include src="%s">\n""" % rel_filename result = template result = result.replace("<WARNING_MESSAGE></WARNING_MESSAGE>", js_warning_message) result = result.replace("<SCRIPT_CONTENTS></SCRIPT_CONTENTS>", script_contents) return result
def generate_deps_js(): all_filenames = [] for dirpath, dirnames, filenames in os.walk(srcdir): for f in filenames: all_filenames.append(os.path.join(dirpath, f)) filenames = [x for x in all_filenames if os.path.splitext(x)[1] == ".js"] filenames = [os.path.relpath(x) for x in filenames] def ignored(x): if os.path.basename(x).startswith('.'): return True return False filenames = [x for x in filenames if not ignored(x)] load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) chunks = [] for module in load_sequence: for dependent_module_name in module.dependent_module_names: chunks.append("base.addModuleDependency('%s','%s');\n" % (module.name, dependent_module_name)) for dependent_raw_script_name in module.dependent_raw_script_names: chunks.append("base.addModuleRawScriptDependency('%s','%s');\n" % (module.name, dependent_raw_script_name)) for style_sheet in module.style_sheets: chunks.append("base.addModuleStylesheet('%s','%s');\n" % (module.name, style_sheet.name)) return "".join(chunks)
def generate_js(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) js_chunks = [js_warning_message, '\n'] js_chunks.append("window.FLATTENED = {};\n") js_chunks.append("window.FLATTENED_RAW_SCRIPTS = {};\n") for module in load_sequence: for dependent_raw_script_name in module.dependent_raw_script_names: js_chunks.append("window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" % dependent_raw_script_name) js_chunks.append( "window.FLATTENED['%s'] = true;\n" % module.name) html_encoded = base64.b64encode(generate_templates()) js_chunks.append("var templateData_ = window.atob('" + html_encoded + "');\n"); js_chunks.append("var templateElem_ = document.createElement('div');\n"); js_chunks.append("templateElem_.innerHTML = templateData_;\n"); js_chunks.append("while (templateElem_.hasChildNodes()) {\n"); js_chunks.append(" document.head.appendChild(" + "templateElem_.removeChild(templateElem_.firstChild));\n"); js_chunks.append("}\n\n"); for module in load_sequence: js_chunks.append(module.contents) js_chunks.append("\n") return ''.join(js_chunks)
def generate_html(): f = open(os.path.join(srcdir, "about_tracing.html.template"), 'r') template = f.read() f.close() assert template.find("<WARNING_MESSAGE></WARNING_MESSAGE>") != -1 assert template.find("<STYLE_SHEET_CONTENTS></STYLE_SHEET_CONTENTS>") != -1 filenames = [ os.path.join(srcdir, x) for x in ["base.js", "about_tracing/profiling_view.js"] ] filenames = [os.path.relpath(x, srcdir) for x in filenames] load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) style_sheet_contents = "" for module in load_sequence: for style_sheet in module.style_sheets: rel_filename = os.path.relpath(style_sheet.filename, srcdir) link_tag = """<link rel="stylesheet" href="%s">\n""" % rel_filename style_sheet_contents += link_tag result = template result = result.replace("<WARNING_MESSAGE></WARNING_MESSAGE>", html_warning_message) result = result.replace("<STYLE_SHEET_CONTENTS></STYLE_SHEET_CONTENTS>", style_sheet_contents) return result
def generate_deps_js(): all_filenames = [] for dirpath, dirnames, filenames in os.walk(srcdir): for f in filenames: all_filenames.append(os.path.join(dirpath, f)) filenames = [x for x in all_filenames if os.path.splitext(x)[1] == ".js"] filenames = [os.path.relpath(x) for x in filenames] def ignored(x): if os.path.basename(x).startswith('.'): return True return False filenames = [x for x in filenames if not ignored(x)] load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) chunks = [] for module in load_sequence: for dependent_module_name in module.dependent_module_names: chunks.append("base.addModuleDependency('%s','%s');\n" % ( module.name, dependent_module_name)); for dependent_raw_script_name in module.dependent_raw_script_names: chunks.append( "base.addModuleRawScriptDependency('%s','%s');\n" % ( module.name, dependent_raw_script_name)); for style_sheet in module.style_sheets: chunks.append("base.addModuleStylesheet('%s','%s');\n" % ( module.name, style_sheet.name)); return "".join(chunks)
def generate_js(): f = open(os.path.join(srcdir, "about_tracing.js.template"), 'r') template = f.read() f.close() assert template.find("<WARNING_MESSAGE></WARNING_MESSAGE>") != -1 assert template.find("<SCRIPT_CONTENTS></SCRIPT_CONTENTS>") != -1 filenames = [ os.path.join(srcdir, x) for x in ["base.js", "profiling_view.js"] ] filenames = [os.path.relpath(x) for x in filenames] import parse_deps load_sequence = parse_deps.calc_load_sequence(filenames) script_contents = "" script_contents += "window.FLATTENED = {};\n" for module in load_sequence: script_contents += "window.FLATTENED['%s'] = true;\n" % module.name for module in load_sequence: rel_filename = os.path.relpath(module.filename, srcdir) script_contents += """<include src="%s">\n""" % rel_filename result = template result = result.replace("<WARNING_MESSAGE></WARNING_MESSAGE>", js_warning_message) result = result.replace("<SCRIPT_CONTENTS></SCRIPT_CONTENTS>", script_contents) return result
def generate_html(): f = open(os.path.join(srcdir, "about_tracing.html.template"), 'r') template = f.read() f.close() assert template.find("<WARNING_MESSAGE></WARNING_MESSAGE>") != -1 assert template.find("<STYLE_SHEET_CONTENTS></STYLE_SHEET_CONTENTS>") != -1 filenames = [os.path.join(srcdir, x) for x in ["base.js", "about_tracing/profiling_view.js"]] filenames = [os.path.relpath(x, srcdir) for x in filenames] load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) style_sheet_contents = "" for module in load_sequence: for style_sheet in module.style_sheets: rel_filename = os.path.relpath(style_sheet.filename, srcdir) link_tag = """<link rel="stylesheet" href="%s">\n""" % rel_filename style_sheet_contents += link_tag result = template result = result.replace("<WARNING_MESSAGE></WARNING_MESSAGE>", html_warning_message) result = result.replace("<STYLE_SHEET_CONTENTS></STYLE_SHEET_CONTENTS>", style_sheet_contents) return result
def generate_js(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) js_chunks = [js_warning_message, '\n'] js_chunks.append("window.FLATTENED = {};\n") js_chunks.append("window.FLATTENED_RAW_SCRIPTS = {};\n") for module in load_sequence: for dependent_raw_script_name in module.dependent_raw_script_names: js_chunks.append("window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" % dependent_raw_script_name) js_chunks.append("window.FLATTENED['%s'] = true;\n" % module.name) html_encoded = base64.b64encode(generate_templates()) js_chunks.append("var templateData_ = window.atob('" + html_encoded + "');\n") js_chunks.append("var templateElem_ = document.createElement('div');\n") js_chunks.append("templateElem_.innerHTML = templateData_;\n") js_chunks.append("while (templateElem_.hasChildNodes()) {\n") js_chunks.append(" document.head.appendChild(" + "templateElem_.removeChild(templateElem_.firstChild));\n") js_chunks.append("}\n\n") for module in load_sequence: js_chunks.append(module.contents) js_chunks.append("\n") return ''.join(js_chunks)
def generate_css(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) style_sheet_chunks = [css_warning_message, '\n'] for module in load_sequence: for style_sheet in module.style_sheets: style_sheet_chunks.append("""%s\n""" % style_sheet.contents) # Borrowed from grit html_format.py. def InlineUrl(m): filename = m.group('filename') idx = filename.index('/images') filename = "%s%s" % (srcdir, filename[idx:]) ext = filename[filename.rindex('.') + 1:] with open(filename, 'rb') as f: data = f.read(); data = base64.standard_b64encode(data) return "url(data:image/%s;base64,%s)" % (ext, data) full_style_sheet = ''.join(style_sheet_chunks) # I'm assuming we only have url()'s associated with images return re.sub('url\((?P<quote>"|\'|)(?P<filename>[^"\'()]*)(?P=quote)\)', lambda m: InlineUrl(m), full_style_sheet)
def generate_css(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) style_sheet_chunks = [css_warning_message, '\n'] for module in load_sequence: for style_sheet in module.style_sheets: style_sheet_chunks.append("""%s\n""" % style_sheet.contents) # Borrowed from grit html_format.py. def InlineUrl(m): filename = m.group('filename') idx = filename.index('/images') filename = "%s%s" % (srcdir, filename[idx:]) ext = filename[filename.rindex('.') + 1:] with open(filename, 'rb') as f: data = f.read() data = base64.standard_b64encode(data) return "url(data:image/%s;base64,%s)" % (ext, data) full_style_sheet = ''.join(style_sheet_chunks) # I'm assuming we only have url()'s associated with images return re.sub('url\((?P<quote>"|\'|)(?P<filename>[^"\'()]*)(?P=quote)\)', lambda m: InlineUrl(m), full_style_sheet)
def generate_deps_js(): all_filenames = [] for dirpath, dirnames, filenames in os.walk(srcdir): for f in filenames: all_filenames.append(os.path.join(dirpath, f)) filenames = [x for x in all_filenames if os.path.splitext(x)[1] == ".js"] filenames = [os.path.relpath(x) for x in filenames] filenames = [x for x in filenames if x not in FILES_TO_IGNORE] if "deps.js" in filenames: filenames.remove("deps.js") load_sequence = parse_deps.calc_load_sequence(filenames) chunks = [js_warning_message] for module in load_sequence: for dependent_module_name in module.dependent_module_names: chunks.append( "base.addModuleDependency(\n '%s',\n '%s');\n" % (module.name, dependent_module_name)) for style_sheet in module.style_sheets: chunks.append( "base.addModuleStylesheet(\n '%s',\n '%s');\n" % (module.name, style_sheet.name)) result = "".join(chunks) return result
def main(args): parser = optparse.OptionParser( usage="%prog --js=<filename> --css=<filename>", epilog=""" A script to takes all of the javascript and css files that comprise trace-viewer and merges them together into two giant js and css files, taking into account various ordering restrictions between them. """) parser.add_option("--js", dest="js_file", help="Where to place generated javascript file") parser.add_option("--css", dest="css_file", help="Where to place generated css file") options, args = parser.parse_args(args) if not options.js_file and not options.css_file: sys.stderr.write("ERROR: Must specify one of --js=<filename> or " "--css=<filename>\n\n") parser.print_help() return 1 load_sequence = parse_deps.calc_load_sequence( ['tracing/standalone_timeline_view.js'], [src_dir]) if options.js_file: with _sopen(options.js_file, 'w') as f: f.write(generate.generate_js(load_sequence)) if options.css_file: with _sopen(options.css_file, 'w') as f: f.write(generate.generate_css(load_sequence)) return 0
def generate_css(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) style_sheet_chunks = [css_warning_message, '\n'] for module in load_sequence: for style_sheet in module.style_sheets: style_sheet_chunks.append("""%s\n""" % style_sheet.contents) return ''.join(style_sheet_chunks)
def generate_css(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames) style_sheet_chunks = [css_warning_message, "\n"] for module in load_sequence: for style_sheet in module.style_sheets: style_sheet_chunks.append("""%s\n""" % style_sheet.contents) return "".join(style_sheet_chunks)
def flatten_style_sheet_contents(filenames): out = StringIO.StringIO() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) # Stylesheets should be sourced from topmsot in, not inner-out. load_sequence.reverse() for module in load_sequence: for style_sheet in module.style_sheets: out.write(style_sheet.contents) if style_sheet.contents[-1] != '\n': out.write('\n') return out.getvalue()
def flatten_module_contents(filenames): out = StringIO.StringIO() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) flattened_module_names = ["'%s'" % module.name for module in load_sequence] out.write(" if (!window.FLATTENED) window.FLATTENED = {};\n") for module in load_sequence: out.write(" window.FLATTENED['%s'] = true;\n" % module.name); for module in load_sequence: out.write(module.contents) if module.contents[-1] != '\n': out.write('\n') return out.getvalue()
def flatten_module_contents(filenames): out = StringIO.StringIO() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) flattened_module_names = ["'%s'" % module.name for module in load_sequence] out.write(" if (!window.FLATTENED) window.FLATTENED = {};\n") for module in load_sequence: out.write(" window.FLATTENED['%s'] = true;\n" % module.name) for module in load_sequence: out.write(module.contents) if module.contents[-1] != '\n': out.write('\n') return out.getvalue()
def generate_js(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames) js_chunks = [js_warning_message, "\n"] js_chunks.append("window.FLATTENED = {};\n") for module in load_sequence: js_chunks.append("window.FLATTENED['%s'] = true;\n" % module.name) for module in load_sequence: js_chunks.append(module.contents) js_chunks.append("\n") return "".join(js_chunks)
def generate_js(): filenames = _get_input_filenames() load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) js_chunks = [js_warning_message, '\n'] js_chunks.append("window.FLATTENED = {};\n") for module in load_sequence: js_chunks.append("window.FLATTENED['%s'] = true;\n" % module.name) for module in load_sequence: js_chunks.append(module.contents) js_chunks.append("\n") return ''.join(js_chunks)
def GritCheck(): filenames = [os.path.join(srcdir, x) for x in [ "base.js", "about_tracing/profiling_view.js"]] grit_files = [] load_sequence = parse_deps.calc_load_sequence(filenames, srcdir) for module in load_sequence: for style_sheet in module.style_sheets: # I'm assuming we only have url()'s associated with images grit_files.extend(re.findall( 'url\((?:["|\']?)([^"\'()]*)(?:["|\']?)\)', style_sheet.contents)) for idx, filename in enumerate(grit_files): while filename.startswith("../"): filename = filename[3:] grit_files[idx] = "src/" + filename known_images = [] for (dirpath, dirnames, filenames) in os.walk('src/images'): for name in filenames: known_images.append(os.path.join(dirpath, name)) u = set(grit_files).union(set(known_images)) i = set(grit_files).intersection(set(known_images)) diff = list(u - i) if len(diff) == 0: return '' error = 'Entries in CSS url()s do not match files in src/images:\n' in_grit_only = list(set(grit_files) - set(known_images)) in_known_only = list(set(known_images) - set(grit_files)) if len(in_grit_only) > 0: error += ' In CSS urls()s only:\n ' + '\n '.join(sorted(in_grit_only)) if len(in_known_only) > 0: if len(in_grit_only) > 0: error += '\n\n' error += ' In src/images only:\n ' + '\n '.join(sorted(in_known_only)) return error
def GritCheck(): filenames = ["base.js", "about_tracing/profiling_view.js"] grit_files = [] load_sequence = parse_deps.calc_load_sequence(filenames, [srcdir]) for module in load_sequence: for style_sheet in module.style_sheets: # I'm assuming we only have url()'s associated with images grit_files.extend(re.findall( 'url\((?:["|\']?)([^"\'()]*)(?:["|\']?)\)', style_sheet.contents)) for idx, filename in enumerate(grit_files): while filename.startswith("../"): filename = filename[3:] grit_files[idx] = "src/" + filename known_images = [] for (dirpath, dirnames, filenames) in os.walk('src/images'): for name in filenames: known_images.append(os.path.join(dirpath, name)) if '.svn' in dirnames: dirnames.remove('.svn') u = set(grit_files).union(set(known_images)) i = set(grit_files).intersection(set(known_images)) diff = list(u - i) if len(diff) == 0: return '' error = 'Entries in CSS url()s do not match files in src/images:\n' in_grit_only = list(set(grit_files) - set(known_images)) in_known_only = list(set(known_images) - set(grit_files)) if len(in_grit_only) > 0: error += ' In CSS urls()s only:\n ' + '\n '.join(sorted(in_grit_only)) if len(in_known_only) > 0: if len(in_grit_only) > 0: error += '\n\n' error += ' In src/images only:\n ' + '\n '.join(sorted(in_known_only)) return error
def test_one_toplevel_nodeps(self): load_sequence = parse_deps.calc_load_sequence( [os.path.join(srcdir, 'base', 'guid.js')], srcdir) name_sequence = [x.name for x in load_sequence] self.assertEquals(['base.guid'], name_sequence)
def test_one_toplevel_nodeps(self): load_sequence = parse_deps.calc_load_sequence( [os.path.join(srcdir, "unittest.js")]) name_sequence = [x.name for x in load_sequence] self.assertEquals(["unittest"], name_sequence)
def test_one_toplevel_nodeps(self): load_sequence = parse_deps.calc_load_sequence([os.path.join(srcdir, "base", "guid.js")], srcdir) name_sequence = [x.name for x in load_sequence] self.assertEquals(["base.guid"], name_sequence)