1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#!/usr/bin/env python3.5
# -*- mode: python -*-
# Copyright 2012-2016 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Find imported modules.
For example, to find dependency packages for the provisioning
server, try the following:
$ find src/provisioningserver \\
> -name '*.py' ! -path '*/test*' -print0 | \\
> xargs -r0 utilities/finder.py --null | \\
> xargs -r0 dpkg -S | cut -d: -f1 | sort -u
"""
import argparse
from modulefinder import ModuleFinder
from os import (
getcwd,
path,
)
import sys
sys.path.insert(0, path.dirname(__file__))
from python_standard_libs import python_standard_libs
def find_standard_library_modules(seed=python_standard_libs):
"""Find all standard-library modules."""
finder = ModuleFinder()
for name in seed:
finder.import_module(name, name, None)
return set(finder.modules)
argument_parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__)
argument_parser.add_argument(
"-0", "--null", help="delimit output with null bytes",
action="store_true", default=False)
argument_parser.add_argument(
"filenames", nargs="+", metavar="FILENAME")
if __name__ == '__main__':
options = argument_parser.parse_args()
standard_libs = find_standard_library_modules()
finder = ModuleFinder()
for filename in options.filenames:
finder.load_file(filename)
# Collect modules from the finder, eliminating those from the standard
# library.
modules = (
module for name, module in finder.modules.items()
if name not in standard_libs
)
# Collect the absolute paths for each module, eliminating those modules
# with no filename.
filenames = (
path.abspath(module.__file__) for module in modules
if module.__file__ is not None
)
# Narrow down to those modules not in the nearby source tree.
here = getcwd()
filenames = (
filename for filename in filenames
if not filename.startswith(here)
)
# Write it all out.
end = "\0" if options.null else None
for filename in sorted(filenames):
print(filename, end=end)
|