Add manually the 'service-identity' dep.
[bitmask_bundler.git] / bundler / depcollector.py
1 import sys
2 import os
3 import errno
4
5 from distutils import dir_util, file_util
6 from modulegraph import modulegraph
7
8
9 def mkdir_p(path):
10     try:
11         os.makedirs(path)
12     except OSError as exc:
13         if exc.errno == errno.EEXIST and os.path.isdir(path):
14             pass
15         else:
16             raise
17
18
19 def collect_deps(root, dest_lib_dir, path_file):
20     mg = modulegraph.ModuleGraph(
21         [sys.path[0]] +
22         [x.strip() for x in open(path_file, 'r').readlines()] +
23         sys.path[1:])  # , debug=3)
24
25     mg.import_hook("distutils")
26     mg.import_hook("site")
27     mg.import_hook("jsonschema")
28     mg.import_hook("scrypt")
29     mg.import_hook("_scrypt")
30     mg.import_hook("ConfigParser")
31     mg.import_hook("encodings.idna")
32     mg.import_hook("leap.soledad.client")
33     mg.import_hook("leap.mail")
34     mg.import_hook("leap.keymanager")
35     mg.import_hook("argparse")
36     mg.import_hook("srp")
37     mg.import_hook("pkgutil")
38     mg.import_hook("pkg_resources")
39     mg.import_hook("_sre")
40     mg.import_hook("zope.proxy")
41     mg.import_hook("tuf")
42     mg.import_hook("timeit")
43     mg.import_hook("daemon")  # for leap/bitmask/util/polkit_agent.py
44
45     # this import ensures the inclusion of the 'service-identity' dependency
46     # since we don't import it implicitly anywhere
47     mg.import_hook("service_identity")
48
49     mg.run_script(root)
50
51     packages = [mg.findNode(i) for i in ["leap.common", "leap.keymanager",
52                                          "leap.mail", "leap.soledad.client",
53                                          "leap.soledad.common", "jsonschema"]]
54     other = []
55
56     sorted_pkg = [(os.path.basename(m.identifier), m) for m in mg.flatten()]
57     sorted_pkg.sort()
58
59     for (name, pkg) in sorted_pkg:
60         # skip namespace packages
61         if name in ("leap", "leap.soledad", "google", "zope") or \
62                 name.endswith("leap/bitmask/app.py"):
63             continue
64
65         # print pkg
66         if isinstance(pkg, modulegraph.MissingModule):
67             # print "ignoring", pkg.identifier
68             continue
69         elif isinstance(pkg, modulegraph.Package):
70             foundpackage = False
71             for i in packages:
72                 if pkg.identifier.startswith(i.identifier):
73                     # print "skipping", pkg.identifier, "member of", i.identifier
74                     # print "  found in", i.filename
75                     foundpackage = True
76                     break
77             if foundpackage:
78                 continue
79             if pkg.filename is None:
80                 continue
81             if pkg not in packages:
82                 packages.append(pkg)
83         else:  # if isinstance(pkg, modulegraph.Extension):
84             foundpackage = False
85             for i in packages:
86                 if pkg.identifier.startswith(i.identifier):
87                     # print "skipping", pkg.identifier, "member of", i.identifier
88                     # print "  found in", i.filename
89                     foundpackage = True
90                     break
91             if foundpackage:
92                 continue
93             if pkg.filename is None:
94                 continue
95             other.append(pkg)
96             # print pkg.identifier
97
98     print "Packages", len(packages)
99     for i in sorted(packages):
100         # if i.identifier == "distutils":
101         #     i.filename = distutils.__file__
102         print i.identifier, i.filename
103         if i.identifier == "leap.bitmask":
104             continue
105         parts = i.identifier.split(".")
106         destdir = os.path.join(*([dest_lib_dir]+parts))
107         mkdir_p(destdir)
108         dir_util.copy_tree(os.path.dirname(i.filename), destdir)
109         before = []
110         for part in parts:
111             before.append(part)
112             current = before + ["__init__.py"]
113             try:
114                 with open(os.path.join(dest_lib_dir, *current), 'a'):
115                     pass
116             except Exception:
117                 pass
118
119     print "Other", len(other)
120     for i in sorted(other):
121         # if i.identifier == "site":
122         #     i.filename = site.__file__
123         print i.identifier, i.filename
124         file_util.copy_file(i.filename, dest_lib_dir)