OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
jbudorick
2016/03/24 17:44:51
Nit: print_python_deps.py instead of print-python-
| |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Prints all non-system dependencies for the given module. | |
7 | |
8 The primary use-case for this script is to genererate the list of python modules | |
9 required for .isolate files. | |
10 """ | |
11 | |
12 import argparse | |
13 import imp | |
14 import os | |
15 import pipes | |
16 import sys | |
17 | |
18 # Don't use any helper modules, or else they will end up in the results. | |
19 | |
20 | |
21 _SRC_ROOT = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir)) | |
jbudorick
2016/03/24 17:44:51
nit:
os.path.dirname(__file__), os.pardir
inst
agrieve
2016/03/24 18:47:39
Done.
| |
22 | |
23 | |
24 def ComputePythonDependencies(root): | |
25 """Gets the paths of imported non-system python modules. | |
26 | |
27 A path is assumed to be a "system" import if it is outside of chromium's | |
28 src/. The paths will be relative to the current directory. | |
29 """ | |
30 module_paths = (m.__file__ for m in sys.modules.values() | |
31 if m is not None and hasattr(m, '__file__')) | |
jbudorick
2016/03/24 17:44:51
nit: Do you need to check against None specificall
agrieve
2016/03/24 18:47:38
Done.
| |
32 | |
33 src_paths = set() | |
34 for path in module_paths: | |
35 if path == __file__: | |
36 continue | |
37 path = os.path.abspath(path) | |
38 if not path.startswith(_SRC_ROOT): | |
39 continue | |
40 | |
41 if path.endswith('.pyc'): | |
42 path = path[:-1] | |
43 src_paths.add(os.path.relpath(path, root)) | |
44 | |
45 return sorted(src_paths) | |
46 | |
47 | |
48 def main(): | |
49 parser = argparse.ArgumentParser( | |
50 description='Prints all non-system dependencies for the given module.') | |
51 parser.add_argument('module', | |
52 help='The python module to analyze.') | |
53 parser.add_argument('--root', default='.', | |
54 help='Directory to make paths relative to.') | |
55 parser.add_argument('--output', | |
56 help='Write output to a file rather than stdout.') | |
57 options = parser.parse_args() | |
58 sys.path.append(os.path.dirname(options.module)) | |
59 imp.load_source('NAME', options.module) | |
60 out = open(options.output, 'w') if options.output else sys.stdout | |
jbudorick
2016/03/24 17:44:51
This file should be closed after you're done writi
agrieve
2016/03/24 18:47:39
Done.
| |
61 out.write('# Generated by //build/print-python-deps.py\n') | |
62 out.write('# root: //%s\n' % os.path.relpath(options.root, _SRC_ROOT)) | |
63 out.write('# target: //%s\n' % os.path.relpath(options.module, _SRC_ROOT)) | |
64 for path in ComputePythonDependencies(options.root): | |
65 out.write(path + "\n") | |
jbudorick
2016/03/24 17:44:51
nit: single quotes
agrieve
2016/03/24 18:47:38
Done.
| |
66 | |
67 | |
68 if __name__ == '__main__': | |
69 sys.exit(main()) | |
OLD | NEW |