Remove unused code
[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 from utils import IS_WIN
8
9
10 def mkdir_p(path):
11     try:
12         os.makedirs(path)
13     except OSError as exc:
14         if exc.errno == errno.EEXIST and os.path.isdir(path):
15             pass
16         else: raise
17
18 def collect_deps(root, dest_lib_dir, path_file):
19     mg = modulegraph.ModuleGraph([sys.path[0]] + [x.strip() for x in open(path_file, 'r').readlines()] + sys.path[1:])#, debug=3)
20
21     mg.import_hook("distutils")
22     mg.import_hook("site")
23     mg.import_hook("jsonschema")
24     mg.import_hook("scrypt")
25     mg.import_hook("_scrypt")
26     mg.import_hook("ConfigParser")
27     mg.import_hook("Crypto")
28     mg.import_hook("encodings.idna")
29     mg.import_hook("leap.soledad.client")
30     mg.import_hook("leap.mail")
31     mg.import_hook("leap.keymanager")
32     mg.import_hook("argparse")
33     mg.import_hook("srp")
34     mg.import_hook("pkgutil")
35     mg.import_hook("pkg_resources")
36     mg.import_hook("_sre")
37     mg.import_hook("zope.proxy")
38     mg.import_hook("tuf")
39     mg.import_hook("timeit")
40     mg.run_script(root)
41
42     packages = [mg.findNode(i) for i in ["leap.common", "leap.keymanager", "leap.mail", "leap.soledad.client", "leap.soledad.common", "jsonschema"]]
43     other = []
44
45     sorted_pkg = [(os.path.basename(mod.identifier), mod) for mod in mg.flatten()]
46     sorted_pkg.sort()
47     for (name, pkg) in sorted_pkg:
48         # skip namespace packages
49         if name == "leap" or name == "leap.soledad" or name == "google" or name == "zope" or name.endswith("leap/bitmask/app.py"):
50             continue
51         # print pkg
52         if isinstance(pkg, modulegraph.MissingModule):
53             # print "ignoring", pkg.identifier
54             continue
55         elif isinstance(pkg, modulegraph.Package):
56             foundpackage = False
57             for i in packages:
58                 if pkg.identifier.startswith(i.identifier):
59                     # print "skipping", pkg.identifier, "member of", i.identifier
60                     # print "  found in", i.filename
61                     foundpackage = True
62                     break
63             if foundpackage:
64                 continue
65             if pkg.filename is None:
66                 continue
67             if pkg not in packages:
68                 packages.append(pkg)
69         else: #if isinstance(pkg, modulegraph.Extension):
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             other.append(pkg)
82             # print pkg.identifier
83     #import pdb; pdb.set_trace()
84
85     print "Packages", len(packages)
86     for i in sorted(packages):
87         # if i.identifier == "distutils":
88         #     i.filename = distutils.__file__
89         print i.identifier, i.filename
90         if i.identifier == "leap.bitmask":
91             continue
92         parts = i.identifier.split(".")
93         destdir = os.path.join(*([dest_lib_dir]+parts))
94         mkdir_p(destdir)
95         dir_util.copy_tree(os.path.dirname(i.filename), destdir)
96         before = []
97         for part in parts:
98             before.append(part)
99             current = before + ["__init__.py"]
100             try:
101                 with open(os.path.join(dest_lib_dir, *current), 'a'):
102                     pass
103             except Exception:
104                 pass
105
106     print "Other", len(other)
107     for i in sorted(other):
108         # if i.identifier == "site":
109         #     i.filename = site.__file__
110         print i.identifier, i.filename
111         file_util.copy_file(i.filename, dest_lib_dir)