Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 """Used by a js_binary action to compile javascript files. | |
| 5 | |
| 6 This script takes in a list of sources and dependencies and compiles them all | |
| 7 together into a single compiled .js file. The dependencies are ordered in a | |
| 8 post-order, left-to-right traversal order. If multiple instances of the same | |
| 9 source file are read, only the first is kept. The script can also take in | |
| 10 optional --defs argument which will add custom flags to the comiler. Any extern | |
| 11 files can also be passed in using the --extern flag. | |
| 12 """ | |
| 13 | |
| 14 from argparse import ArgumentParser | |
| 15 import os | |
| 16 import subprocess | |
| 17 import sys | |
| 18 | |
| 19 EXIT_SUCCESS = 0 | |
| 20 EXIT_FAILURE = 1 | |
| 21 | |
| 22 def IsExecutable(path): | |
| 23 """Returns whether file at |path| exists and is executable.""" | |
| 24 return os.path.isfile(path) and os.access(path, os.X_OK) | |
| 25 | |
| 26 def FindCommand(command): | |
| 27 """Looks up for |commmand| in PATH.""" | |
| 28 filepath, _ = os.path.split(command) | |
| 29 if filepath: | |
|
slan
2017/04/07 23:08:39
nit: Use "and" here.
damargulis
2017/04/08 00:19:49
Done.
| |
| 30 if IsExecutable(command): | |
|
mbjorge
2017/04/07 23:11:19
should this be IsExecutable(filepath)?
damargulis
2017/04/08 00:19:49
I don't think so. I borrowed most of this code fr
| |
| 31 return command | |
| 32 | |
| 33 if sys.platform == 'win32': | |
| 34 # On Windows, if the command does not have an extension, cmd.exe will | |
| 35 # try all extensions from PATHEXT when resolving the full path. | |
| 36 command, ext = os.path.splitext(command) | |
| 37 if not ext: | |
|
mbjorge
2017/04/07 23:30:19
exts = [ext] if ext else os.environ['PATHEXT'].spl
damargulis
2017/04/08 00:19:49
Done.
| |
| 38 exts = os.environ['PATHEXT'].split(os.path.pathsep) | |
| 39 else: | |
| 40 exts = [ext] | |
| 41 else: | |
| 42 exts = [''] | |
| 43 | |
| 44 for path in os.environ['PATH'].split(os.path.pathsep): | |
| 45 for ext in exts: | |
| 46 path = os.path.join(path, command) + ext | |
| 47 if IsExecutable(path): | |
| 48 return path | |
| 49 | |
| 50 return None | |
| 51 | |
| 52 | |
| 53 def RunCompiler(args): | |
| 54 """Runs the closure compiler with |args|.""" | |
| 55 java_path = FindCommand('java') | |
| 56 if not java_path: | |
| 57 sys.stderr.write('java: command not found\n') | |
| 58 sys.exit(EXIT_FAILURE) | |
| 59 return subprocess.check_call([java_path, '-jar'] + args) | |
| 60 | |
| 61 def ParseDepList(dep): | |
| 62 """Parses a depenency list, returns |sources, deps|.""" | |
| 63 with open(dep, 'r') as dep_list: | |
| 64 lines = [line.strip() for line in dep_list.readlines()] | |
| 65 split = lines.index('deps:') | |
| 66 return lines[1:split], lines[split+1:] | |
| 67 | |
| 68 def PostOrder(deps, sources): | |
| 69 """Parses the dependency tree creating a post-order listing of sources.""" | |
| 70 for dep in deps: | |
| 71 new_sources, new_deps = ParseDepList(dep) | |
| 72 sources = PostOrder(new_deps, sources) | |
| 73 sources = sources + [source for source in new_sources | |
| 74 if source not in sources] | |
| 75 return sources | |
| 76 | |
| 77 def main(): | |
| 78 parser = ArgumentParser() | |
| 79 parser.add_argument('-c', '--compiler', required=True, | |
| 80 help='Path to compiler') | |
| 81 parser.add_argument('-s', '--sources', nargs='*', default=[], | |
| 82 help='List of js source files') | |
| 83 parser.add_argument('-o', '--output', required=True, | |
| 84 help='Compile to output') | |
| 85 parser.add_argument('-d', '--deps', nargs='*', default = [], | |
| 86 help='List of js_libarary dependencies') | |
| 87 parser.add_argument('-b', '--bootstrap', | |
| 88 help='A file to include before all others') | |
| 89 parser.add_argument('-cf', '--config', nargs='*', default = [], | |
| 90 help='A list of files to include after bootstrap and' \ | |
| 91 'before all others') | |
| 92 parser.add_argument('-df', '--defs', nargs='*', default = [], | |
| 93 help='A list of custom flags to pass to the compiler. ' \ | |
| 94 'Do not include leading dashes') | |
| 95 parser.add_argument('-e', '--externs', nargs='*', default = [], | |
| 96 help='A list of extern files to pass to the compiler') | |
| 97 | |
| 98 args = parser.parse_args() | |
| 99 sources = PostOrder(args.deps, args.sources) | |
| 100 | |
| 101 defs = ['--' + flag for flag in args.defs] | |
| 102 | |
| 103 compiler_args = [ | |
| 104 args.compiler, | |
| 105 ] + defs | |
| 106 | |
| 107 if(args.externs): | |
| 108 compiler_args += ['--externs'] + args.externs | |
| 109 | |
| 110 compiler_args += [ | |
| 111 '--js_output_file', | |
| 112 args.output, | |
| 113 '--js', | |
| 114 ] | |
| 115 if(args.bootstrap): | |
| 116 compiler_args += [args.bootstrap] | |
| 117 compiler_args += args.config | |
| 118 compiler_args += sources | |
| 119 RunCompiler(compiler_args) | |
| 120 | |
| 121 if __name__ == '__main__': | |
| 122 sys.exit(main()) | |
| OLD | NEW |