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 compiler. Any | |
| 11 extern files can also be passed in using the --extern flag. | |
| 12 """ | |
| 13 | |
| 14 from argparse import ArgumentParser | |
| 15 import os | |
| 16 import subprocess | |
|
Dan Beam
2017/04/18 02:05:50
it seems more common in chrome to use subprocess2
damargulis
2017/04/19 00:42:12
I based most of the code to run the jar from java_
Dan Beam
2017/04/19 17:59:45
why are you duplicating the code rather than just
| |
| 17 import sys | |
| 18 | |
| 19 EXIT_SUCCESS = 0 | |
|
Dan Beam
2017/04/18 02:05:50
unused
damargulis
2017/04/19 00:42:12
Done.
| |
| 20 EXIT_FAILURE = 1 | |
| 21 | |
| 22 def IsExecutable(path): | |
| 23 return os.path.isfile(path) and os.access(path, os.X_OK) | |
| 24 | |
|
Dan Beam
2017/04/18 02:05:49
file-level globals should have 2 \n between them
damargulis
2017/04/19 00:42:12
Done.
| |
| 25 def FindCommand(command): | |
| 26 filepath, _ = os.path.split(command) | |
| 27 if filepath and IsExecutable(command): | |
| 28 return command | |
| 29 | |
| 30 if sys.platform == 'win32': | |
| 31 # On Windows, if the command does not have an extension, cmd.exe will | |
| 32 # try all extensions from PATHEXT when resolving the full path. | |
| 33 command, ext = os.path.splitext(command) | |
| 34 exts = [ext] if ext else os.environ['PATHEXT'].split(os.path.pathsep) | |
| 35 else: | |
| 36 exts = [''] | |
| 37 | |
| 38 for path in os.environ['PATH'].split(os.path.pathsep): | |
| 39 for ext in exts: | |
| 40 path = os.path.join(path, command) + ext | |
| 41 if IsExecutable(path): | |
| 42 return path | |
| 43 | |
| 44 return None | |
| 45 | |
| 46 | |
| 47 def RunCompiler(args): | |
| 48 java_path = FindCommand('java') | |
| 49 if not java_path: | |
| 50 sys.stderr.write('java: command not found\n') | |
| 51 sys.exit(EXIT_FAILURE) | |
| 52 return subprocess.check_call([java_path, '-jar'] + args) | |
|
Dan Beam
2017/04/18 02:05:49
can you share this code across compile.py and comp
damargulis
2017/04/19 00:42:12
compile.py and compile2.py use the Checker class,
Dan Beam
2017/04/19 17:59:45
that's obviously easy to change
| |
| 53 | |
| 54 def ParseDepList(dep): | |
| 55 """Parses a depenency list, returns |sources, deps|.""" | |
| 56 assert os.path.isfile(dep), dep[:-11] + ' is not a js_library target' | |
|
Dan Beam
2017/04/18 02:05:50
what does -11 mean here?
damargulis
2017/04/19 00:42:12
This removes ".js_library" from the name of the fi
| |
| 57 with open(dep, 'r') as dep_list: | |
| 58 lines = [line.strip() for line in dep_list.readlines()] | |
|
Dan Beam
2017/04/18 02:05:49
use dep_list.splitlines() instead of readlines() +
damargulis
2017/04/19 00:42:12
splitlines() is only available on strings, not fil
| |
| 59 split = lines.index('deps:') | |
|
Dan Beam
2017/04/18 02:05:49
what if this fails?
damargulis
2017/04/19 00:42:12
If the file exists, it should never fail, because
| |
| 60 return lines[1:split], lines[split+1:] | |
| 61 | |
| 62 def PostOrder(deps, sources): | |
|
Dan Beam
2017/04/18 02:05:50
can we name this something else? this sounds like
damargulis
2017/04/19 00:42:12
Changed to CrawlDepsTree
| |
| 63 """Parses the dependency tree creating a post-order listing of sources.""" | |
| 64 for dep in deps: | |
| 65 new_sources, new_deps = ParseDepList(dep) | |
| 66 | |
| 67 sources = PostOrder(new_deps, sources) | |
| 68 sources = sources + [source for source in new_sources | |
| 69 if source not in sources] | |
| 70 return sources | |
| 71 | |
| 72 def main(): | |
| 73 parser = ArgumentParser() | |
| 74 parser.add_argument('-c', '--compiler', required=True, | |
| 75 help='Path to compiler') | |
| 76 parser.add_argument('-s', '--sources', nargs='*', default=[], | |
| 77 help='List of js source files') | |
| 78 parser.add_argument('-o', '--output', required=True, | |
| 79 help='Compile to output') | |
| 80 parser.add_argument('-d', '--deps', nargs='*', default = [], | |
| 81 help='List of js_libarary dependencies') | |
| 82 parser.add_argument('-b', '--bootstrap', | |
| 83 help='A file to include before all others') | |
| 84 parser.add_argument('-cf', '--config', nargs='*', default = [], | |
| 85 help='A list of files to include after bootstrap and' \ | |
| 86 'before all others') | |
| 87 parser.add_argument('-df', '--defs', nargs='*', default = [], | |
| 88 help='A list of custom flags to pass to the compiler. ' \ | |
| 89 'Do not include leading dashes') | |
| 90 parser.add_argument('-e', '--externs', nargs='*', default = [], | |
| 91 help='A list of extern files to pass to the compiler') | |
| 92 | |
| 93 args = parser.parse_args() | |
| 94 sources = PostOrder(args.deps, []) + args.sources | |
| 95 | |
| 96 defs = ['--' + flag for flag in args.defs] | |
| 97 | |
| 98 compiler_args = [ | |
| 99 args.compiler, | |
| 100 ] + defs | |
|
Dan Beam
2017/04/18 02:05:49
what is doing this formatting? this would fit on
damargulis
2017/04/19 00:42:12
Done.
| |
| 101 | |
| 102 for extern in args.externs: | |
| 103 compiler_args += ['--externs=' + extern] | |
|
Dan Beam
2017/04/18 02:05:49
compiler_args += ['--externs=%s' % e for e in args
Dan Beam
2017/04/18 02:05:50
the python style guide discourages use of + for st
Dan Beam
2017/04/18 02:07:42
oh, i guess it's a little more nuanced than that:
damargulis
2017/04/19 00:42:13
I switched it to use substitution, as it turns it
| |
| 104 | |
| 105 compiler_args += [ | |
| 106 '--js_output_file', | |
| 107 args.output, | |
| 108 '--js', | |
| 109 ] | |
| 110 if(args.bootstrap): | |
|
Dan Beam
2017/04/18 02:05:49
space after if
Dan Beam
2017/04/18 02:05:49
no () (this ain't C)
damargulis
2017/04/19 00:42:12
Done.
damargulis
2017/04/19 00:42:12
Done.
| |
| 111 compiler_args += [args.bootstrap] | |
| 112 compiler_args += args.config | |
| 113 compiler_args += sources | |
| 114 RunCompiler(compiler_args) | |
| 115 | |
| 116 if __name__ == '__main__': | |
| 117 sys.exit(main()) | |
|
Dan Beam
2017/04/18 02:05:49
it doesn't look like main() actually returns anyth
damargulis
2017/04/19 00:42:12
Done.
| |
| OLD | NEW |