def _GetRelativePathToSourceDict(root, prefix=''): """Scans a top root directory for .js sources. Args: root: str, Root directory. prefix: str, Prefix for returned paths. Returns: dict, A map of relative paths (with prefix, if given), to source.Source objects. """ # Remember and restore the cwd when we're done. We work from the root so # that paths are relative from the root. start_wd = os.getcwd() os.chdir(root) path_to_source = {} for path in treescan.ScanTreeForJsFiles('.'): prefixed_path = _NormalizePathSeparators(os.path.join(prefix, path)) path_to_source[prefixed_path] = source.Source( source.GetFileContents(path)) os.chdir(start_wd) return path_to_source
def main(): parser = optparse.OptionParser(description=__doc__) parser.add_option( '-w', '--rewrite_prefix', action='append', default=[], dest='prefix_map', metavar='SPEC', help=('Two path prefixes, separated by colons ' + 'specifying that a file whose (relative) path ' + 'name starts with the first prefix should have ' + 'that prefix replaced by the second prefix to ' + 'form a path relative to the output directory. ' + 'The resulting path is used in the deps mapping ' + 'file path to a list of provided and required ' + 'namespaces.')) parser.add_option('-o', '--output_file', action='store', default=[], metavar='SPEC', help=('Where to output the generated deps file.')) options, args = parser.parse_args() path_rewriter = PathRewriter(options.prefix_map) # Write the generated deps file. with open(options.output_file, 'w') as output: for path in args: js_deps = source.Source(source.GetFileContents(path)) path = path_rewriter.RewritePath(path) line = 'goog.addDependency(\'%s\', %s, %s);\n' % ( path, sorted(js_deps.provides), sorted(js_deps.requires)) output.write(line)
def main(): """CLI frontend to MakeDepsFile.""" logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() logging.warning( 'This utility is deprecated! See ' 'https://github.com/google/closure-library/wiki/Migrating-off-Closure-Python-Scripts' ' for more details.') path_to_source = {} # Roots without prefixes for root in options.roots: path_to_source.update(_GetRelativePathToSourceDict(root)) # Roots with prefixes for root_and_prefix in options.roots_with_prefix: root, prefix = _GetPair(root_and_prefix) path_to_source.update(_GetRelativePathToSourceDict(root, prefix=prefix)) # Source paths for path in args: path_to_source[path] = source.Source(source.GetFileContents(path)) # Source paths with alternate deps paths for path_with_depspath in options.paths_with_depspath: srcpath, depspath = _GetPair(path_with_depspath) path_to_source[depspath] = source.Source( source.GetFileContents(srcpath)) # Make our output pipe. if options.output_file: out = open(options.output_file, 'w') else: out = sys.stdout out.write(('// This file was autogenerated by %s.\n' % os.path.basename(__file__))) out.write('// Please do not edit.\n') out.write(MakeDepsFile(path_to_source))
def __init__(self, path): """Initialize a source. Args: path: str, Path to a JavaScript file. The source string will be read from this file. """ super(_PathSource, self).__init__(source.GetFileContents(path)) self._path = path
def main(): """CLI frontend to MakeDepsFile.""" logging.basicConfig(format=(sys.argv[0] + ': %(message)s'), level=logging.INFO) options, args = _GetOptionsParser().parse_args() path_to_source = {} # Roots without prefixes for root in options.roots: path_to_source.update(_GetRelativePathToSourceDict(root)) # Roots with prefixes for root_and_prefix in options.roots_with_prefix: root, prefix = _GetPair(root_and_prefix) path_to_source.update(_GetRelativePathToSourceDict(root, prefix=prefix)) # Source paths for path in args: path_to_source[path] = source.Source(source.GetFileContents(path)) # Source paths with alternate deps paths for path_with_depspath in options.paths_with_depspath: srcpath, depspath = _GetPair(path_with_depspath) path_to_source[depspath] = source.Source( source.GetFileContents(srcpath)) # Make our output pipe. if options.output_file: out = open(options.output_file, 'w') else: out = sys.stdout out.write(('// This file was autogenerated by %s.\n' % os.path.basename(__file__))) out.write('// Please do not edit.\n') out.write(MakeDepsFile(path_to_source))
def EnsureSourceLoaded(in_path, sources): if in_path not in sources: out_path = path_rewriter.RewritePath(in_path) sources[in_path] = SourceWithPaths(source.GetFileContents(in_path), in_path, out_path)