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.
6 """Prints all non-system dependencies for the given module.
8 The primary use-case for this script is to genererate the list of python modules
9 required for .isolate files.
18 # Don't use any helper modules, or else they will end up in the results.
21 _SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
24 def _ComputePythonDependencies():
25 """Gets the paths of imported non-system python modules.
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.
30 module_paths = (m.__file__ for m in sys.modules.values()
31 if m and hasattr(m, '__file__'))
34 for path in module_paths:
37 path = os.path.abspath(path)
38 if not path.startswith(_SRC_ROOT):
41 if (path.endswith('.pyc')
42 or (path.endswith('c') and not os.path.splitext(path)[1])):
49 def _NormalizeCommandLine(options):
50 """Returns a string that when run from SRC_ROOT replicates the command."""
51 args = ['build/print_python_deps.py']
52 root = os.path.relpath(options.root, _SRC_ROOT)
54 args.extend(('--root', root))
56 args.extend(('--output', os.path.relpath(options.output, _SRC_ROOT)))
58 args.extend(('--gn-paths',))
59 for whitelist in sorted(options.whitelists):
60 args.extend(('--whitelist', os.path.relpath(whitelist, _SRC_ROOT)))
61 args.append(os.path.relpath(options.module, _SRC_ROOT))
62 return ' '.join(pipes.quote(x) for x in args)
65 def _FindPythonInDirectory(directory):
66 """Returns an iterable of all non-test python files in the given directory."""
68 for root, _dirnames, filenames in os.walk(directory):
69 for filename in filenames:
70 if filename.endswith('.py') and not filename.endswith('_test.py'):
71 yield os.path.join(root, filename)
75 parser = argparse.ArgumentParser(
76 description='Prints all non-system dependencies for the given module.')
77 parser.add_argument('module',
78 help='The python module to analyze.')
79 parser.add_argument('--root', default='.',
80 help='Directory to make paths relative to.')
81 parser.add_argument('--output',
82 help='Write output to a file rather than stdout.')
83 parser.add_argument('--inplace', action='store_true',
84 help='Write output to a file with the same path as the '
85 'module, but with a .pydeps extension. Also sets the '
86 'root to the module\'s directory.')
87 parser.add_argument('--no-header', action='store_true',
88 help='Do not write the "# Generated by" header.')
89 parser.add_argument('--gn-paths', action='store_true',
90 help='Write paths as //foo/bar/baz.py')
91 parser.add_argument('--whitelist', default=[], action='append',
93 help='Recursively include all non-test python files '
94 'within this directory. May be specified multiple times.')
95 options = parser.parse_args()
96 # Replace the path entry for print_python_deps.py with the one for the given
98 sys.path[0] = os.path.dirname(options.module)
99 imp.load_source('NAME', options.module)
103 parser.error('Cannot use --inplace and --output at the same time!')
104 if not options.module.endswith('.py'):
105 parser.error('Input module path should end with .py suffix!')
106 options.output = options.module + 'deps'
107 options.root = os.path.dirname(options.module)
109 paths_set = _ComputePythonDependencies()
110 for path in options.whitelists:
111 paths_set.update(os.path.abspath(p) for p in _FindPythonInDirectory(path))
113 paths = [os.path.relpath(p, options.root) for p in paths_set]
115 normalized_cmdline = _NormalizeCommandLine(options)
116 out = open(options.output, 'w') if options.output else sys.stdout
118 if not options.no_header:
119 out.write('# Generated by running:\n')
120 out.write('# %s\n' % normalized_cmdline)
121 prefix = '//' if options.gn_paths else ''
122 for path in sorted(paths):
123 out.write(prefix + path + '\n')
126 if __name__ == '__main__':