[M73 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / build / print_python_deps.py
1 #!/usr/bin/env vpython
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         or (path.endswith('c') and not os.path.splitext(path)[1])):
43       path = path[:-1]
44     src_paths.add(path)
45
46   return src_paths
47
48
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)
53   if root != '.':
54     args.extend(('--root', root))
55   if options.output:
56     args.extend(('--output', os.path.relpath(options.output, _SRC_ROOT)))
57   if options.gn_paths:
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)
63
64
65 def _FindPythonInDirectory(directory):
66   """Returns an iterable of all non-test python files in the given directory."""
67   files = []
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)
72
73
74 def main():
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',
92                       dest='whitelists',
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
97   # module.
98   sys.path[0] = os.path.dirname(options.module)
99   imp.load_source('NAME', options.module)
100
101   if options.inplace:
102     if options.output:
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)
108
109   paths_set = _ComputePythonDependencies()
110   for path in options.whitelists:
111     paths_set.update(os.path.abspath(p) for p in _FindPythonInDirectory(path))
112
113   paths = [os.path.relpath(p, options.root) for p in paths_set]
114
115   normalized_cmdline = _NormalizeCommandLine(options)
116   out = open(options.output, 'w') if options.output else sys.stdout
117   with out:
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')
124
125
126 if __name__ == '__main__':
127   sys.exit(main())