def testMultipleRequires(self): a = MockSource(['A'], ['B']) b = MockSource(['B'], ['C']) c = MockSource(['C'], []) d = MockSource(['D'], ['B']) tree = depstree.DepsTree([a, b, c, d]) self.AssertValidDependencies(tree.GetDependencies(['D', 'A']))
def testDepsForMissingNamespace(self): a = MockSource(['A'], ['B']) b = MockSource(['B'], []) tree = depstree.DepsTree([a, b]) # There is no C. self.assertRaises(depstree.NamespaceNotFoundError, tree.GetDependencies, 'C')
def testCircularDependency(self): # Circular deps a = MockSource(['A'], ['B']) b = MockSource(['B'], ['C']) c = MockSource(['C'], ['A']) tree = depstree.DepsTree([a, b, c]) self.assertRaises(depstree.CircularDependencyError, tree.GetDependencies, 'A')
def testSimpleDepsTree(self): a = MockSource(['A'], ['B', 'C']) b = MockSource(['B'], []) c = MockSource(['C'], ['D']) d = MockSource(['D'], ['E']) e = MockSource(['E'], []) tree = depstree.DepsTree([a, b, c, d, e]) self.AssertValidDependencies(tree.GetDependencies('A')) self.AssertValidDependencies(tree.GetDependencies('B')) self.AssertValidDependencies(tree.GetDependencies('C')) self.AssertValidDependencies(tree.GetDependencies('D')) self.AssertValidDependencies(tree.GetDependencies('E'))
def CalcDeps(bundle, sources, top_level): '''Calculates dependencies for a set of top-level files. Args: bundle: Bundle to add the sources to. sources, dict: Mapping from input path to SourceWithPaths objects. top_level, list: List of top-level input paths to calculate dependencies for. ''' providers = [s for s in sources.itervalues() if len(s.provides) > 0] deps = depstree.DepsTree(providers) namespaces = [] for path in top_level: namespaces.extend(sources[path].requires) # base.js is an implicit dependency that always goes first. bundle.Add(_GetBase(sources)) bundle.Add(deps.GetDependencies(namespaces))
def CalcDeps(bundle, sources, top_level): '''Calculates dependencies for a set of top-level files. Args: bundle: Bundle to add the sources to. sources, dict: Mapping from input path to SourceWithPaths objects. top_level, list: List of top-level input paths to calculate dependencies for. ''' def GetBase(sources): for source in sources.itervalues(): if (os.path.basename(source.GetInPath()) == 'base.js' and 'goog' in source.provides): return source Die('goog.base not provided by any file') providers = [s for s in sources.itervalues() if len(s.provides) > 0] deps = depstree.DepsTree(providers) namespaces = [] for path in top_level: namespaces.extend(sources[path].requires) # base.js is an implicit dependency that always goes first. bundle.Add(GetBase(sources)) bundle.Add(deps.GetDependencies(namespaces))
def MakeDepsTree(): return depstree.DepsTree([a, b, c])
def main(): logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() # Make our output pipe. if options.output_file: out = open(options.output_file, 'w') else: out = sys.stdout sources = set() logging.info('Scanning paths...') for path in options.roots: for js_path in treescan.ScanTreeForJsFiles(path): sources.add(_PathSource(js_path)) # Add scripts specified on the command line. for js_path in args: sources.add(_PathSource(js_path)) logging.info('%s sources scanned.', len(sources)) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = options.inputs or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(options.namespaces) if not input_namespaces: logging.error('No namespaces found. At least one namespace must be ' 'specified with the --namespace or --input flags.') sys.exit(2) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources) deps = [base] + tree.GetDependencies(input_namespaces) output_mode = options.output_mode if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in deps]) elif output_mode == 'script': for js_source in deps: src = js_source.GetSource() if js_source.is_goog_module: src = _WrapGoogModuleSource(src) out.write(src + '\n') elif output_mode == 'compiled': logging.warning("""\ Closure Compiler now natively understands and orders Closure dependencies and is prefererred over using this script for performing JavaScript compilation. Please migrate your codebase. See: https://github.com/google/closure-compiler/wiki/Manage-Closure-Dependencies """) # Make sure a .jar is specified. if not options.compiler_jar: logging.error( '--compiler_jar flag must be specified if --output is ' '"compiled"') sys.exit(2) # Will throw an error if the compilation fails. compiled_source = jscompiler.Compile( options.compiler_jar, [js_source.GetPath() for js_source in deps], jvm_flags=options.jvm_flags, compiler_flags=options.compiler_flags) logging.info('JavaScript compilation succeeded.') out.write(compiled_source) else: logging.error('Invalid value for --output flag.') sys.exit(2)
def main(): logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() # Make our output pipe. if options.output_file: out = open(options.output_file, 'w') else: out = sys.stdout sources = set() logging.info('Scanning paths...') for path in options.roots: for js_path in treescan.ScanTreeForJsFiles(path): sources.add(_PathSource(js_path)) # Add scripts specified on the command line. for path in args: sources.add(source.Source(_PathSource(path))) logging.info('%s sources scanned.', len(sources)) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = options.inputs or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(options.namespaces) if not input_namespaces: logging.error('No namespaces found. At least one namespace must be ' 'specified with the --namespace or --input flags.') sys.exit(2) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources) deps = [base] + tree.GetDependencies(input_namespaces) output_mode = options.output_mode if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in deps]) elif output_mode == 'script': out.writelines([js_source.GetSource() for js_source in deps]) elif output_mode == 'compiled': # Make sure a .jar is specified. if not options.compiler_jar: logging.error( '--compiler_jar flag must be specified if --output is ' '"compiled"') sys.exit(2) compiled_source = jscompiler.Compile( options.compiler_jar, [js_source.GetPath() for js_source in deps], options.compiler_flags) if compiled_source is None: logging.error('JavaScript compilation failed.') sys.exit(1) else: logging.info('JavaScript compilation succeeded.') out.write(compiled_source) else: logging.error('Invalid value for --output flag.') sys.exit(2)
def make( myIncludes=[], inputAry=[], namespaceAry=[], rootAry=[], output_mode='list', compiler_jar=None, compiler_flags=[], jvm_flags=[], output_file='', baseFileName='base.js', ): logging.basicConfig(format=sys.argv[0] + ': %(message)s', level=logging.INFO) if output_mode: out = open(output_file, 'w') else: out = sys.stdout sources = set() logging.info('Scanning paths...') for path in rootAry: for js_path in treescan.ScanTreeForJsFiles(path): sources.add(_PathSource(js_path)) logging.info('sources added by rootPath scan: %s', len(sources)) # Add scripts specified on the command line. # logging.info('Adding myIncludes...') # for js_path in myIncludes: # sources.add(_PathSource(js_path)) # logging.info('sources added by myIncludes: %s', len(myIncludes)) logging.info('sources added total: %s', len(sources)) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = inputAry or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(namespaceAry) if not namespaceAry: logging.error( 'No namespaces found. At least one namespace must be specified with the --namespace or --input flags.' ) sys.exit(2) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources, baseFileName) logging.info('Using base file %s', base) deps = [base] + tree.GetDependencies(input_namespaces) # Add scripts specified on the command line. logging.info('Adding myIncludes...') myIncludesPathSource = [] for js_path in myIncludes: myIncludesPathSource.append(_PathSource(js_path)) newDeps = [] newDeps.extend(myIncludesPathSource) newDeps.extend(deps) #for theFile in myIncludes: # deps.insert(0,_PathSource(theFile)) #deps.insert(0, _PathSource(theFile)) logging.info('sources added by myIncludes: %s', len(myIncludes)) if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in newDeps]) # out.write('test \n') out.close() exists = os.path.exists(output_file) if exists: logging.info('Wrote source list to %s', output_file) else: logging.error('failed to write source list to %s', output_file) elif output_mode == 'script': for js_source in newDeps: src = js_source.GetSource() if js_source.is_goog_module: src = _WrapGoogModuleSource(src) out.write(src + '\n') exists = os.path.exists(output_file) if exists: logging.info('Wrote source script to %s', output_file) else: logging.error('failed to write source script to %s', output_file) elif output_mode == 'compiled': logging.warning("""\ Closure Compiler now natively understands and orders Closure dependencies and is prefererred over using this script for performing JavaScript compilation. Please migrate your codebase. See: https://github.com/google/closure-compiler/wiki/Manage-Closure-Dependencies """) # Make sure a .jar is specified. if not compiler_jar: logging.error( '--compiler_jar flag must be specified if --output is "compiled"' ) sys.exit(2) # Will throw an error if the compilation fails. compiled_source = jscompiler.Compile( compiler_jar, [js_source.GetPath() for js_source in newDeps], jvm_flags=jvm_flags, compiler_flags=compiler_flags) logging.info('JavaScript compilation succeeded.') out.write(compiled_source) out.close() exists = os.path.exists(output_file) if exists: logging.info('Wrote compiled script to %s', output_file) else: logging.error('failed to write compiled script to %s', output_file) else: logging.error('Invalid value for --output flag.') sys.exit(2)
def main(): logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() output_file = options.output_file roots = options.roots inputs = options.inputs namespaces = options.namespaces output_mode = options.output_mode output_dir = options.output_dir root_with_prefix = options.root_with_prefix compiler_jar_path = options.compiler_jar compiler_flags = options.compiler_flags update_build_version = options.update_build_version == 'True' # Make our output pipe. if output_file: svn.try_lock(output_file) out = open(output_file, 'w') else: out = sys.stdout sources = getSources(roots, args) # Though deps output doesn't need to query the tree, we still build it # to validate dependencies. logging.info('Building dependency tree..') tree = depstree.DepsTree(sources) input_namespaces = set() inputs = inputs or [] for input_path in inputs: js_input = _GetInputByPath(input_path, sources) if not js_input: logging.error('No source matched input %s', input_path) sys.exit(1) input_namespaces.update(js_input.provides) input_namespaces.update(namespaces) if not input_namespaces: logging.error('No namespaces found. At least one namespace must be ' 'specified with the --namespace or --input flags.') sys.exit(2) deps = tree.GetDependencies(input_namespaces) # The Closure Library base file must go first. base = _GetClosureBaseFile(sources) if base: deps = [base] + deps if output_mode == 'list': out.writelines([js_source.GetPath() + '\n' for js_source in deps]) elif output_mode == 'calcdepsIndependent': write_deps_file(deps, input_namespaces, output_dir, root_with_prefix) elif output_mode == 'calcdepsIndependentDetail': for dep in deps: print dep.provides.copy().pop(), '\t', dep.GetPath(), '\t', len(dep.GetSource()) elif output_mode == 'calcAndOrganizeDepsIndependent': calcAndOrganizeDepsIndependent(deps, input_namespaces, output_dir, root_with_prefix, sources) elif output_mode == 'genModuleJsEntryPoint': modulejs = options.modulejs genModuleJsEntryPoint(deps, input_namespaces, modulejs, output_dir, root_with_prefix, options.host) elif output_mode == 'script': out.writelines([js_source.GetSource() for js_source in deps]) elif output_mode == 'compiled': min_js, min_js_expired = compile(compiler_jar_path, deps, inputs, compiler_flags, roots[0], update_build_version=update_build_version) if min_js: min_js_expired = version.updater.get_rel_path(min_js_expired, roots[0]) html_paths = version.updater.update(version.updater.get_rel_path(min_js, roots[0]), roots, min_js_expired) paths = [min_js] for path in html_paths: paths.append(path) if options.eos: add_eos_mission(paths, min_js, roots[0]) else: sys.exit(1) elif output_mode == 'compiledByModule': namespace_target = input_namespaces.copy().pop() sources = tree.GetLeafSourcesByNameSpace(namespace_target) paths = [] for dep in sources: namespace = dep.provides.copy().pop() if not re.compile("^test").match(namespace): min_js, min_js_expired = compile(compiler_jar_path, [base] + tree.GetDependencies(namespace), [dep.GetPath()], compiler_flags, roots[0], update_build_version=update_build_version) if min_js: min_js_expired = version.updater.get_rel_path(min_js_expired, roots[0]) html_paths = version.updater.update(version.updater.get_rel_path(min_js, roots[0]), roots, min_js_expired) paths.append(min_js) for path in html_paths: paths.append(path) if options.eos: add_eos_mission(paths, namespace_target, roots[0]) elif output_mode == 'compiledSimple': compileSimple(compiler_jar_path, deps, inputs, compiler_flags, roots, options.eos, update_build_version=update_build_version) elif output_mode == 'compiledSimpleByModule': namespace_target = input_namespaces.copy().pop() sources = tree.GetLeafSourcesByNameSpace(namespace_target) paths = [] for dep in sources: namespace = dep.provides.copy().pop() if not re.compile("^test").match(namespace): min_js, html_paths = compileSimple(compiler_jar_path, [base] + tree.GetDependencies(namespace), [dep.GetPath()], compiler_flags, roots, False, update_build_version=update_build_version) if min_js: paths.append(min_js) for path in html_paths: paths.append(path) if options.eos: add_eos_mission(paths, namespace_target, roots[0]) elif output_mode == 'findEntriesByModule': sources = tree.GetLeafSourcesByNameSpace(input_namespaces.copy().pop()) print 'length:', str(len(sources)) for dep in sources: print dep.provides.copy().pop(), '\t', dep.GetPath() elif output_mode == 'findModulesByModule': sources = tree.GetDirectSourcesByNameSpace(input_namespaces.copy().pop()) for dep in sources: print dep.provides.copy().pop(), '\t', dep.GetPath() elif output_mode == 'calcdepsIndependentByModule': sources = tree.GetLeafSourcesByNameSpace(input_namespaces.copy().pop()) for dep in sources: write_deps_file([base] + tree.GetDependencies(dep.provides.copy().pop()), dep.provides, output_dir, root_with_prefix) elif output_mode == 'convertToLegosByEntrypoint': for dep in deps: convertToLegos(dep) elif output_mode == 'convertToLegos': convertToLegos(_PathSource(inputs[0])) elif output_mode == 'pubDeps2Eos': abs_paths = [] for dep in deps: path = dep.GetPath() abs_paths.append(path) add_eos_mission(abs_paths, 'deps for : ' + input_namespaces.copy().pop(), roots[0], False) else: logging.error('Invalid value for --output flag.') sys.exit(2)