Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 """
7 This scripts sets up a virtualenv and installs TPS into it.
8 It's probably best to specify a path NOT inside the repo, otherwise
9 all the virtualenv files will show up in e.g. hg status.
10 """
12 import optparse
13 import os
14 import shutil
15 import subprocess
16 import sys
17 import urllib2
18 import zipfile
21 here = os.path.dirname(os.path.abspath(__file__))
22 usage_message = """
23 ***********************************************************************
25 To run TPS, activate the virtualenv using:
26 source {TARGET}/{BIN_NAME}
28 To change your TPS config, please edit the file:
29 {TARGET}/config.json
31 To execute tps use:
32 runtps --binary=/path/to/firefox
34 See runtps --help for all options
36 ***********************************************************************
37 """
39 # Link to the folder, which contains the zip archives of virtualenv
40 URL_VIRTUALENV = 'https://codeload.github.com/pypa/virtualenv/zip/'
41 VERSION_VIRTUALENV = '1.11.6'
44 if sys.platform == 'win32':
45 bin_name = os.path.join('Scripts', 'activate.bat')
46 activate_env = os.path.join('Scripts', 'activate_this.py')
47 python_env = os.path.join('Scripts', 'python.exe')
48 else:
49 bin_name = os.path.join('bin', 'activate')
50 activate_env = os.path.join('bin', 'activate_this.py')
51 python_env = os.path.join('bin', 'python')
54 def download(url, target):
55 """Downloads the specified url to the given target."""
56 response = urllib2.urlopen(url)
57 with open(target, 'wb') as f:
58 f.write(response.read())
60 return target
63 def setup_virtualenv(target, python_bin=None):
64 script_path = os.path.join(here, 'virtualenv-%s' % VERSION_VIRTUALENV,
65 'virtualenv.py')
67 print 'Downloading virtualenv %s' % VERSION_VIRTUALENV
68 zip_path = download(URL_VIRTUALENV + VERSION_VIRTUALENV,
69 os.path.join(here, 'virtualenv.zip'))
71 try:
72 with zipfile.ZipFile(zip_path, 'r') as f:
73 f.extractall(here)
75 print 'Creating new virtual environment'
76 cmd_args = [sys.executable, script_path, target]
78 if python_bin:
79 cmd_args.extend(['-p', python_bin])
81 subprocess.check_call(cmd_args)
82 finally:
83 try:
84 os.remove(zip_path)
85 except OSError:
86 pass
88 shutil.rmtree(os.path.dirname(script_path), ignore_errors=True)
91 def update_configfile(source, target, replacements):
92 lines = []
94 with open(source) as config:
95 for line in config:
96 for source_string, target_string in replacements.iteritems():
97 if target_string:
98 line = line.replace(source_string, target_string)
99 lines.append(line)
101 with open(target, 'w') as config:
102 for line in lines:
103 config.write(line)
106 def main():
107 parser = optparse.OptionParser('Usage: %prog [options] path_to_venv')
108 parser.add_option('--password',
109 type='string',
110 dest='password',
111 metavar='FX_ACCOUNT_PASSWORD',
112 default=None,
113 help='The Firefox Account password.')
114 parser.add_option('-p', '--python',
115 type='string',
116 dest='python',
117 metavar='PYTHON_BIN',
118 default=None,
119 help='The Python interpreter to use.')
120 parser.add_option('--sync-passphrase',
121 type='string',
122 dest='sync_passphrase',
123 metavar='SYNC_ACCOUNT_PASSPHRASE',
124 default=None,
125 help='The old Firefox Sync account passphrase.')
126 parser.add_option('--sync-password',
127 type='string',
128 dest='sync_password',
129 metavar='SYNC_ACCOUNT_PASSWORD',
130 default=None,
131 help='The old Firefox Sync account password.')
132 parser.add_option('--sync-username',
133 type='string',
134 dest='sync_username',
135 metavar='SYNC_ACCOUNT_USERNAME',
136 default=None,
137 help='The old Firefox Sync account username.')
138 parser.add_option('--username',
139 type='string',
140 dest='username',
141 metavar='FX_ACCOUNT_USERNAME',
142 default=None,
143 help='The Firefox Account username.')
145 (options, args) = parser.parse_args(args=None, values=None)
147 if len(args) != 1:
148 parser.error('Path to the environment has to be specified')
149 target = args[0]
150 assert(target)
152 setup_virtualenv(target, python_bin=options.python)
154 # Activate tps environment
155 tps_env = os.path.join(target, activate_env)
156 execfile(tps_env, dict(__file__=tps_env))
158 # Install TPS in environment
159 subprocess.check_call([os.path.join(target, python_env),
160 os.path.join(here, 'setup.py'), 'install'])
162 # Get the path to tests and extensions directory by checking check where
163 # the tests and extensions directories are located
164 sync_dir = os.path.abspath(os.path.join(here, '..', '..', 'services',
165 'sync'))
166 if os.path.exists(sync_dir):
167 testdir = os.path.join(sync_dir, 'tests', 'tps')
168 extdir = os.path.join(sync_dir, 'tps', 'extensions')
169 else:
170 testdir = os.path.join(here, 'tests')
171 extdir = os.path.join(here, 'extensions')
173 update_configfile(os.path.join(here, 'config', 'config.json.in'),
174 os.path.join(target, 'config.json'),
175 replacements={
176 '__TESTDIR__': testdir.replace('\\','/'),
177 '__EXTENSIONDIR__': extdir.replace('\\','/'),
178 '__FX_ACCOUNT_USERNAME__': options.username,
179 '__FX_ACCOUNT_PASSWORD__': options.password,
180 '__SYNC_ACCOUNT_USERNAME__': options.sync_username,
181 '__SYNC_ACCOUNT_PASSWORD__': options.sync_password,
182 '__SYNC_ACCOUNT_PASSPHRASE__': options.sync_passphrase})
184 if not (options.username and options.password):
185 print '\nFirefox Account credentials not specified.'
186 if not (options.sync_username and options.sync_password and options.passphrase):
187 print '\nFirefox Sync account credentials not specified.'
189 # Print the user instructions
190 print usage_message.format(TARGET=target,
191 BIN_NAME=bin_name)
193 if __name__ == "__main__":
194 main()