326e80b991d9e98a9c1f8eed5e41dbc3c3a12a71
[bitmask_bundler.git] / bundler / main.py
1 # TODO:
2 #  - Check if inside a virtualenv, and warn before doing anything
3 #  - Build everything that we are currently expecting as a binary
4 #  - Create complete bundle changelog
5
6 import argparse
7 import os
8 import tempfile
9
10 from contextlib import contextmanager
11 from distutils import dir_util
12
13 from actions import GitCloneAll, PythonSetupAll, CreateDirStructure
14 from actions import CollectAllDeps, CopyBinaries, PLister, SeededConfig
15 from actions import DarwinLauncher, CopyAssets, CopyMisc, FixDylibs
16 from actions import DmgIt, PycRemover, TarballIt, MtEmAll, ZipIt, SignIt
17 from actions import RemoveUnused
18
19 from utils import IS_MAC, IS_WIN
20
21 sorted_repos = [
22     "leap_assets",
23     "leap_pycommon",
24     "keymanager",
25     "soledad",
26     "leap_mail",
27     "bitmask_client",
28     "bitmask_launcher",
29 ]
30
31
32 @contextmanager
33 def new_build_dir(default=None):
34     bd = default
35     if bd is None:
36         bd = tempfile.mkdtemp(prefix="bundler-")
37     yield bd
38     # Only remove if created a temp dir
39     if default is None:
40         dir_util.remove_tree(bd)
41
42
43 def main():
44     parser = argparse.ArgumentParser(description='Bundle creation tool.')
45     parser.add_argument('--workon', help="")
46     parser.add_argument('--skip', nargs="*", default=[], help="")
47     parser.add_argument('--do', nargs="*", default=[], help="")
48     parser.add_argument('--paths-file', help="")
49     parser.add_argument('--binaries', help="")
50     parser.add_argument('--seeded-config', help="")
51     parser.add_argument('--nightly', action="store_true", help="")
52     parser.add_argument('--codesign', default="", help="")
53
54     args = parser.parse_args()
55
56     assert args.paths_file is not None, \
57         "We need a paths file, otherwise you'll get " \
58         "problems with distutils and site"
59     paths_file = os.path.realpath(args.paths_file)
60
61     assert args.binaries is not None, \
62         "We don't support building from source, so you'll need to " \
63         "specify a binaries path"
64     binaries_path = os.path.realpath(args.binaries)
65
66     seeded_config = None
67     if args.seeded_config is not None:
68         seeded_config = os.path.realpath(args.seeded_config)
69
70     with new_build_dir(os.path.realpath(args.workon)) as bd:
71         print "Doing it all in", bd
72
73         def init(t, bd=bd):
74             return t(bd, args.skip, args.do)
75
76         gc = init(GitCloneAll)
77         gc.run(sorted_repos, args.nightly)
78
79         ps = init(PythonSetupAll)
80         ps.run(sorted_repos, binaries_path)
81
82         cd = init(CreateDirStructure, os.path.join(bd, "Bitmask"))
83         cd.run()
84
85         dp = init(CollectAllDeps)
86         dp.run(paths_file)
87
88         if binaries_path is not None:
89             cb = init(CopyBinaries)
90             cb.run(binaries_path)
91
92         if IS_MAC:
93             pl = init(PLister)
94             pl.run()
95             dl = init(DarwinLauncher)
96             dl.run()
97             ca = init(CopyAssets)
98             ca.run()
99             fd = init(FixDylibs)
100             fd.run()
101
102         cm = init(CopyMisc)
103         cm.run(binaries_path)
104
105         pyc = init(PycRemover)
106         pyc.run()
107
108         if IS_WIN:
109             mt = init(MtEmAll)
110             mt.run()
111
112         if IS_MAC:
113             si = init(SignIt)
114             si.run(args.codesign)
115
116         if seeded_config is not None:
117             sc = init(SeededConfig)
118             sc.run(seeded_config)
119
120         if IS_MAC:
121             dm = init(DmgIt)
122             dm.run(sorted_repos, args.nightly)
123         elif IS_WIN:
124             zi = init(ZipIt)
125             zi.run(sorted_repos, args.nightly)
126         else:
127             ru = init(RemoveUnused)
128             ru.run()
129             ti = init(TarballIt)
130             ti.run(sorted_repos, args.nightly)
131
132         # do manifest on windows
133
134 if __name__ == "__main__":
135     main()