|
1 #!/usr/bin/env python |
|
2 # This Source Code Form is subject to the terms of the Mozilla Public |
|
3 # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 # You can obtain one at http://mozilla.org/MPL/2.0/. |
|
5 |
|
6 # This script provides one-line bootstrap support to configure systems to build |
|
7 # the tree. |
|
8 # |
|
9 # The role of this script is to load the Python modules containing actual |
|
10 # bootstrap support. It does this through various means, including fetching |
|
11 # content from the upstream source repository. |
|
12 |
|
13 # If we add unicode_literals, optparse breaks on Python 2.6.1 (which is needed |
|
14 # to support OS X 10.6). |
|
15 from __future__ import print_function |
|
16 |
|
17 import os |
|
18 import shutil |
|
19 import sys |
|
20 import tempfile |
|
21 try: |
|
22 from urllib2 import urlopen |
|
23 except ImportError: |
|
24 from urllib.request import urlopen |
|
25 |
|
26 from optparse import OptionParser |
|
27 |
|
28 # The next two variables define where in the repository the Python files |
|
29 # reside. This is used to remotely download file content when it isn't |
|
30 # available locally. |
|
31 REPOSITORY_PATH_PREFIX = 'python/mozboot' |
|
32 |
|
33 REPOSITORY_PATHS = [ |
|
34 'mozboot/__init__.py', |
|
35 'mozboot/base.py', |
|
36 'mozboot/bootstrap.py', |
|
37 'mozboot/centos.py', |
|
38 'mozboot/debian.py', |
|
39 'mozboot/fedora.py', |
|
40 'mozboot/freebsd.py', |
|
41 'mozboot/gentoo.py', |
|
42 'mozboot/openbsd.py', |
|
43 'mozboot/osx.py', |
|
44 'mozboot/ubuntu.py', |
|
45 ] |
|
46 |
|
47 TEMPDIR = None |
|
48 |
|
49 def setup_proxy(): |
|
50 # Some Linux environments define ALL_PROXY, which is a SOCKS proxy |
|
51 # intended for all protocols. Python doesn't currently automatically |
|
52 # detect this like it does for http_proxy and https_proxy. |
|
53 if 'ALL_PROXY' in os.environ and 'https_proxy' not in os.environ: |
|
54 os.environ['https_proxy'] = os.environ['ALL_PROXY'] |
|
55 if 'ALL_PROXY' in os.environ and 'http_proxy' not in os.environ: |
|
56 os.environ['http_proxy'] = os.environ['ALL_PROXY'] |
|
57 |
|
58 def fetch_files(repo_url, repo_type): |
|
59 setup_proxy() |
|
60 repo_url = repo_url.rstrip('/') |
|
61 |
|
62 files = {} |
|
63 |
|
64 if repo_type == 'hgweb': |
|
65 for path in REPOSITORY_PATHS: |
|
66 url = repo_url + '/raw-file/default/python/mozboot/' + path |
|
67 |
|
68 req = urlopen(url=url, timeout=30) |
|
69 files[path] = req.read() |
|
70 else: |
|
71 raise NotImplementedError('Not sure how to handle repo type.', repo_type) |
|
72 |
|
73 return files |
|
74 |
|
75 def ensure_environment(repo_url=None, repo_type=None): |
|
76 """Ensure we can load the Python modules necessary to perform bootstrap.""" |
|
77 |
|
78 try: |
|
79 from mozboot.bootstrap import Bootstrapper |
|
80 return Bootstrapper |
|
81 except ImportError: |
|
82 # The first fallback is to assume we are running from a tree checkout |
|
83 # and have the files in a sibling directory. |
|
84 pardir = os.path.join(os.path.dirname(__file__), os.path.pardir) |
|
85 include = os.path.normpath(pardir) |
|
86 |
|
87 sys.path.append(include) |
|
88 try: |
|
89 from mozboot.bootstrap import Bootstrapper |
|
90 return Bootstrapper |
|
91 except ImportError: |
|
92 sys.path.pop() |
|
93 |
|
94 # The next fallback is to download the files from the source |
|
95 # repository. |
|
96 files = fetch_files(repo_url, repo_type) |
|
97 |
|
98 # Install them into a temporary location. They will be deleted |
|
99 # after this script has finished executing. |
|
100 global TEMPDIR |
|
101 TEMPDIR = tempfile.mkdtemp() |
|
102 |
|
103 for relpath in files.keys(): |
|
104 destpath = os.path.join(TEMPDIR, relpath) |
|
105 destdir = os.path.dirname(destpath) |
|
106 |
|
107 if not os.path.exists(destdir): |
|
108 os.makedirs(destdir) |
|
109 |
|
110 with open(destpath, 'wb') as fh: |
|
111 fh.write(files[relpath]) |
|
112 |
|
113 # This should always work. |
|
114 sys.path.append(TEMPDIR) |
|
115 from mozboot.bootstrap import Bootstrapper |
|
116 return Bootstrapper |
|
117 |
|
118 def main(args): |
|
119 parser = OptionParser() |
|
120 parser.add_option('-r', '--repo-url', dest='repo_url', |
|
121 default='https://hg.mozilla.org/mozilla-central/', |
|
122 help='Base URL of source control repository where bootstrap files can ' |
|
123 'be downloaded.') |
|
124 |
|
125 parser.add_option('--repo-type', dest='repo_type', |
|
126 default='hgweb', |
|
127 help='The type of the repository. This defines how we fetch file ' |
|
128 'content. Like --repo, you should not need to set this.') |
|
129 |
|
130 options, leftover = parser.parse_args(args) |
|
131 |
|
132 try: |
|
133 try: |
|
134 cls = ensure_environment(options.repo_url, options.repo_type) |
|
135 except Exception as e: |
|
136 print('Could not load the bootstrap Python environment.\n') |
|
137 print('This should never happen. Consider filing a bug.\n') |
|
138 print('\n') |
|
139 print(e) |
|
140 return 1 |
|
141 |
|
142 dasboot = cls() |
|
143 dasboot.bootstrap() |
|
144 |
|
145 return 0 |
|
146 finally: |
|
147 if TEMPDIR is not None: |
|
148 shutil.rmtree(TEMPDIR) |
|
149 |
|
150 |
|
151 if __name__ == '__main__': |
|
152 sys.exit(main(sys.argv)) |