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(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 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(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 |
| 61 with out: |
| 62 out.write('# Generated by //build/print_python_deps.py\n') |
| 63 out.write('# root: //%s\n' % os.path.relpath(options.root, _SRC_ROOT)) |
| 64 out.write('# target: //%s\n' % os.path.relpath(options.module, _SRC_ROOT)) |
| 65 for path in ComputePythonDependencies(options.root): |
| 66 out.write(path + '\n') |
| 67 |
| 68 |
| 69 if __name__ == '__main__': |
| 70 sys.exit(main()) |
OLD | NEW |