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
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
22 from urllib2
import urlopen
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
31 REPOSITORY_PATH_PREFIX
= 'python/mozboot'
34 'mozboot/__init__.py',
35 'mozboot/android-ndk.rb',
38 'mozboot/bootstrap.py',
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
):
62 repo_url
= repo_url
.rstrip('/')
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()
73 raise NotImplementedError('Not sure how to handle repo type.', repo_type
)
77 def ensure_environment(repo_url
=None, repo_type
=None):
78 """Ensure we can load the Python modules necessary to perform bootstrap."""
81 from mozboot
.bootstrap
import Bootstrapper
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
)
91 from mozboot
.bootstrap
import Bootstrapper
96 # The next fallback is to download the files from the source
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.
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
):
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
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 '
127 parser
.add_option('--repo-type', dest
='repo_type',
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
)
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')
149 if TEMPDIR
is not None:
150 shutil
.rmtree(TEMPDIR
)
153 if __name__
== '__main__':
154 sys
.exit(main(sys
.argv
))