testing/tps/create_venv.py

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rwxr-xr-x

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial