Bumping manifests a=b2g-bump
[gecko.git] / python / mozboot / bin / bootstrap.py
blobc743fbea4915c0d4e9b2d53f0574623b7ad28225
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/.
6 # This script provides one-line bootstrap support to configure systems to build
7 # the tree.
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.
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
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
26 from optparse import OptionParser
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'
33 REPOSITORY_PATHS = [
34 'mozboot/__init__.py',
35 'mozboot/android-ndk.rb',
36 'mozboot/android.py',
37 'mozboot/base.py',
38 'mozboot/bootstrap.py',
39 'mozboot/centos.py',
40 'mozboot/debian.py',
41 'mozboot/fedora.py',
42 'mozboot/freebsd.py',
43 'mozboot/gentoo.py',
44 'mozboot/openbsd.py',
45 'mozboot/osx.py',
46 'mozboot/ubuntu.py',
49 TEMPDIR = None
51 def setup_proxy():
52 # Some Linux environments define ALL_PROXY, which is a SOCKS proxy
53 # intended for all protocols. Python doesn't currently automatically
54 # detect this like it does for http_proxy and https_proxy.
55 if 'ALL_PROXY' in os.environ and 'https_proxy' not in os.environ:
56 os.environ['https_proxy'] = os.environ['ALL_PROXY']
57 if 'ALL_PROXY' in os.environ and 'http_proxy' not in os.environ:
58 os.environ['http_proxy'] = os.environ['ALL_PROXY']
60 def fetch_files(repo_url, repo_type):
61 setup_proxy()
62 repo_url = repo_url.rstrip('/')
64 files = {}
66 if repo_type == 'hgweb':
67 for path in REPOSITORY_PATHS:
68 url = repo_url + '/raw-file/default/python/mozboot/' + path
70 req = urlopen(url=url, timeout=30)
71 files[path] = req.read()
72 else:
73 raise NotImplementedError('Not sure how to handle repo type.', repo_type)
75 return files
77 def ensure_environment(repo_url=None, repo_type=None):
78 """Ensure we can load the Python modules necessary to perform bootstrap."""
80 try:
81 from mozboot.bootstrap import Bootstrapper
82 return Bootstrapper
83 except ImportError:
84 # The first fallback is to assume we are running from a tree checkout
85 # and have the files in a sibling directory.
86 pardir = os.path.join(os.path.dirname(__file__), os.path.pardir)
87 include = os.path.normpath(pardir)
89 sys.path.append(include)
90 try:
91 from mozboot.bootstrap import Bootstrapper
92 return Bootstrapper
93 except ImportError:
94 sys.path.pop()
96 # The next fallback is to download the files from the source
97 # repository.
98 files = fetch_files(repo_url, repo_type)
100 # Install them into a temporary location. They will be deleted
101 # after this script has finished executing.
102 global TEMPDIR
103 TEMPDIR = tempfile.mkdtemp()
105 for relpath in files.keys():
106 destpath = os.path.join(TEMPDIR, relpath)
107 destdir = os.path.dirname(destpath)
109 if not os.path.exists(destdir):
110 os.makedirs(destdir)
112 with open(destpath, 'wb') as fh:
113 fh.write(files[relpath])
115 # This should always work.
116 sys.path.append(TEMPDIR)
117 from mozboot.bootstrap import Bootstrapper
118 return Bootstrapper
120 def main(args):
121 parser = OptionParser()
122 parser.add_option('-r', '--repo-url', dest='repo_url',
123 default='https://hg.mozilla.org/mozilla-central/',
124 help='Base URL of source control repository where bootstrap files can '
125 'be downloaded.')
127 parser.add_option('--repo-type', dest='repo_type',
128 default='hgweb',
129 help='The type of the repository. This defines how we fetch file '
130 'content. Like --repo, you should not need to set this.')
132 options, leftover = parser.parse_args(args)
134 try:
135 try:
136 cls = ensure_environment(options.repo_url, options.repo_type)
137 except Exception as e:
138 print('Could not load the bootstrap Python environment.\n')
139 print('This should never happen. Consider filing a bug.\n')
140 print('\n')
141 print(e)
142 return 1
144 dasboot = cls()
145 dasboot.bootstrap()
147 return 0
148 finally:
149 if TEMPDIR is not None:
150 shutil.rmtree(TEMPDIR)
153 if __name__ == '__main__':
154 sys.exit(main(sys.argv))