848a37a3ec1ac0bfc0024f6ac76bee393bbcd25b
[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("encodings.idna")
28     mg.import_hook("leap.soledad.client")
29     mg.import_hook("leap.mail")
30     mg.import_hook("leap.keymanager")
31     mg.import_hook("argparse")
32     mg.import_hook("srp")
33     mg.import_hook("pkgutil")
34     mg.import_hook("pkg_resources")
35     mg.import_hook("_sre")
36     mg.import_hook("zope.proxy")
37     mg.import_hook("tuf")
38     mg.import_hook("timeit")
39     mg.run_script(root)
40
41     packages = [mg.findNode(i) for i in ["leap.common", "leap.keymanager", "leap.mail", "leap.soledad.client", "leap.soledad.common", "jsonschema"]]
42     other = []
43
44     sorted_pkg = [(os.path.basename(mod.identifier), mod) for mod in mg.flatten()]
45     sorted_pkg.sort()
46     for (name, pkg) in sorted_pkg:
47         # skip namespace packages
48         if name == "leap" or name == "leap.soledad" or name == "google" or name == "zope" or name.endswith("leap/bitmask/app.py"):
49             continue
50         # print pkg
51         if isinstance(pkg, modulegraph.MissingModule):
52             # print "ignoring", pkg.identifier
53             continue
54         elif isinstance(pkg, modulegraph.Package):
55             foundpackage = False
56             for i in packages:
57                 if pkg.identifier.startswith(i.identifier):
58                     # print "skipping", pkg.identifier, "member of", i.identifier
59                     # print "  found in", i.filename
60                     foundpackage = True
61                     break
62             if foundpackage:
63                 continue
64             if pkg.filename is None:
65                 continue
66             if pkg not in packages:
67                 packages.append(pkg)
68         else: #if isinstance(pkg, modulegraph.Extension):
69             foundpackage = False
70             for i in packages:
71                 if pkg.identifier.startswith(i.identifier):
72                     # print "skipping", pkg.identifier, "member of", i.identifier
73                     # print "  found in", i.filename
74                     foundpackage = True
75                     break
76             if foundpackage:
77                 continue
78             if pkg.filename is None:
79                 continue
80             other.append(pkg)
81             # print pkg.identifier
82     #import pdb; pdb.set_trace()
83
84     print "Packages", len(packages)
85     for i in sorted(packages):
86         # if i.identifier == "distutils":
87         #     i.filename = distutils.__file__
88         print i.identifier, i.filename
89         if i.identifier == "leap.bitmask":
90             continue
91         parts = i.identifier.split(".")
92         destdir = os.path.join(*([dest_lib_dir]+parts))
93         mkdir_p(destdir)
94         dir_util.copy_tree(os.path.dirname(i.filename), destdir)
95         before = []
96         for part in parts:
97             before.append(part)
98             current = before + ["__init__.py"]
99             try:
100                 with open(os.path.join(dest_lib_dir, *current), 'a'):
101                     pass
102             except Exception:
103                 pass
104
105     print "Other", len(other)
106     for i in sorted(other):
107         # if i.identifier == "site":
108         #     i.filename = site.__file__
109         print i.identifier, i.filename
110         file_util.copy_file(i.filename, dest_lib_dir)