OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env 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(os.path.dirname(__file__), os.pardir)) |
| 22 |
| 23 |
| 24 def _ComputePythonDependencies(): |
| 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 and hasattr(m, '__file__')) |
| 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(path) |
| 44 |
| 45 return src_paths |
| 46 |
| 47 |
| 48 def _NormalizeCommandLine(options): |
| 49 """Returns a string that when run from SRC_ROOT replicates the command.""" |
| 50 args = ['build/print_python_deps.py'] |
| 51 root = os.path.relpath(options.root, _SRC_ROOT) |
| 52 if root != '.': |
| 53 args.extend(('--root', root)) |
| 54 if options.output: |
| 55 args.extend(('--output', os.path.relpath(options.output, _SRC_ROOT))) |
| 56 for whitelist in sorted(options.whitelists): |
| 57 args.extend(('--whitelist', os.path.relpath(whitelist, _SRC_ROOT))) |
| 58 args.append(os.path.relpath(options.module, _SRC_ROOT)) |
| 59 return ' '.join(pipes.quote(x) for x in args) |
| 60 |
| 61 |
| 62 def _FindPythonInDirectory(directory): |
| 63 """Returns an iterable of all non-test python files in the given directory.""" |
| 64 files = [] |
| 65 for root, _dirnames, filenames in os.walk(directory): |
| 66 for filename in filenames: |
| 67 if filename.endswith('.py') and not filename.endswith('_test.py'): |
| 68 yield os.path.join(root, filename) |
| 69 |
| 70 |
| 71 def main(): |
| 72 parser = argparse.ArgumentParser( |
| 73 description='Prints all non-system dependencies for the given module.') |
| 74 parser.add_argument('module', |
| 75 help='The python module to analyze.') |
| 76 parser.add_argument('--root', default='.', |
| 77 help='Directory to make paths relative to.') |
| 78 parser.add_argument('--output', |
| 79 help='Write output to a file rather than stdout.') |
| 80 parser.add_argument('--whitelist', default=[], action='append', |
| 81 dest='whitelists', |
| 82 help='Recursively include all non-test python files ' |
| 83 'within this directory. May be specified multiple times.') |
| 84 options = parser.parse_args() |
| 85 sys.path.append(os.path.dirname(options.module)) |
| 86 imp.load_source('NAME', options.module) |
| 87 |
| 88 paths_set = _ComputePythonDependencies() |
| 89 for path in options.whitelists: |
| 90 paths_set.update(os.path.abspath(p) for p in _FindPythonInDirectory(path)) |
| 91 |
| 92 paths = [os.path.relpath(p, options.root) for p in paths_set] |
| 93 |
| 94 normalized_cmdline = _NormalizeCommandLine(options) |
| 95 out = open(options.output, 'w') if options.output else sys.stdout |
| 96 with out: |
| 97 out.write('# Generated by running:\n') |
| 98 out.write('# %s\n' % normalized_cmdline) |
| 99 for path in sorted(paths): |
| 100 out.write(path + '\n') |
| 101 |
| 102 |
| 103 if __name__ == '__main__': |
| 104 sys.exit(main()) |
OLD | NEW |