1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/tps/create_venv.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,194 @@ 1.4 +#!/usr/bin/env python 1.5 +# This Source Code Form is subject to the terms of the Mozilla Public 1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.8 + 1.9 +""" 1.10 +This scripts sets up a virtualenv and installs TPS into it. 1.11 +It's probably best to specify a path NOT inside the repo, otherwise 1.12 +all the virtualenv files will show up in e.g. hg status. 1.13 +""" 1.14 + 1.15 +import optparse 1.16 +import os 1.17 +import shutil 1.18 +import subprocess 1.19 +import sys 1.20 +import urllib2 1.21 +import zipfile 1.22 + 1.23 + 1.24 +here = os.path.dirname(os.path.abspath(__file__)) 1.25 +usage_message = """ 1.26 +*********************************************************************** 1.27 + 1.28 +To run TPS, activate the virtualenv using: 1.29 + source {TARGET}/{BIN_NAME} 1.30 + 1.31 +To change your TPS config, please edit the file: 1.32 + {TARGET}/config.json 1.33 + 1.34 +To execute tps use: 1.35 + runtps --binary=/path/to/firefox 1.36 + 1.37 +See runtps --help for all options 1.38 + 1.39 +*********************************************************************** 1.40 +""" 1.41 + 1.42 +# Link to the folder, which contains the zip archives of virtualenv 1.43 +URL_VIRTUALENV = 'https://codeload.github.com/pypa/virtualenv/zip/' 1.44 +VERSION_VIRTUALENV = '1.11.6' 1.45 + 1.46 + 1.47 +if sys.platform == 'win32': 1.48 + bin_name = os.path.join('Scripts', 'activate.bat') 1.49 + activate_env = os.path.join('Scripts', 'activate_this.py') 1.50 + python_env = os.path.join('Scripts', 'python.exe') 1.51 +else: 1.52 + bin_name = os.path.join('bin', 'activate') 1.53 + activate_env = os.path.join('bin', 'activate_this.py') 1.54 + python_env = os.path.join('bin', 'python') 1.55 + 1.56 + 1.57 +def download(url, target): 1.58 + """Downloads the specified url to the given target.""" 1.59 + response = urllib2.urlopen(url) 1.60 + with open(target, 'wb') as f: 1.61 + f.write(response.read()) 1.62 + 1.63 + return target 1.64 + 1.65 + 1.66 +def setup_virtualenv(target, python_bin=None): 1.67 + script_path = os.path.join(here, 'virtualenv-%s' % VERSION_VIRTUALENV, 1.68 + 'virtualenv.py') 1.69 + 1.70 + print 'Downloading virtualenv %s' % VERSION_VIRTUALENV 1.71 + zip_path = download(URL_VIRTUALENV + VERSION_VIRTUALENV, 1.72 + os.path.join(here, 'virtualenv.zip')) 1.73 + 1.74 + try: 1.75 + with zipfile.ZipFile(zip_path, 'r') as f: 1.76 + f.extractall(here) 1.77 + 1.78 + print 'Creating new virtual environment' 1.79 + cmd_args = [sys.executable, script_path, target] 1.80 + 1.81 + if python_bin: 1.82 + cmd_args.extend(['-p', python_bin]) 1.83 + 1.84 + subprocess.check_call(cmd_args) 1.85 + finally: 1.86 + try: 1.87 + os.remove(zip_path) 1.88 + except OSError: 1.89 + pass 1.90 + 1.91 + shutil.rmtree(os.path.dirname(script_path), ignore_errors=True) 1.92 + 1.93 + 1.94 +def update_configfile(source, target, replacements): 1.95 + lines = [] 1.96 + 1.97 + with open(source) as config: 1.98 + for line in config: 1.99 + for source_string, target_string in replacements.iteritems(): 1.100 + if target_string: 1.101 + line = line.replace(source_string, target_string) 1.102 + lines.append(line) 1.103 + 1.104 + with open(target, 'w') as config: 1.105 + for line in lines: 1.106 + config.write(line) 1.107 + 1.108 + 1.109 +def main(): 1.110 + parser = optparse.OptionParser('Usage: %prog [options] path_to_venv') 1.111 + parser.add_option('--password', 1.112 + type='string', 1.113 + dest='password', 1.114 + metavar='FX_ACCOUNT_PASSWORD', 1.115 + default=None, 1.116 + help='The Firefox Account password.') 1.117 + parser.add_option('-p', '--python', 1.118 + type='string', 1.119 + dest='python', 1.120 + metavar='PYTHON_BIN', 1.121 + default=None, 1.122 + help='The Python interpreter to use.') 1.123 + parser.add_option('--sync-passphrase', 1.124 + type='string', 1.125 + dest='sync_passphrase', 1.126 + metavar='SYNC_ACCOUNT_PASSPHRASE', 1.127 + default=None, 1.128 + help='The old Firefox Sync account passphrase.') 1.129 + parser.add_option('--sync-password', 1.130 + type='string', 1.131 + dest='sync_password', 1.132 + metavar='SYNC_ACCOUNT_PASSWORD', 1.133 + default=None, 1.134 + help='The old Firefox Sync account password.') 1.135 + parser.add_option('--sync-username', 1.136 + type='string', 1.137 + dest='sync_username', 1.138 + metavar='SYNC_ACCOUNT_USERNAME', 1.139 + default=None, 1.140 + help='The old Firefox Sync account username.') 1.141 + parser.add_option('--username', 1.142 + type='string', 1.143 + dest='username', 1.144 + metavar='FX_ACCOUNT_USERNAME', 1.145 + default=None, 1.146 + help='The Firefox Account username.') 1.147 + 1.148 + (options, args) = parser.parse_args(args=None, values=None) 1.149 + 1.150 + if len(args) != 1: 1.151 + parser.error('Path to the environment has to be specified') 1.152 + target = args[0] 1.153 + assert(target) 1.154 + 1.155 + setup_virtualenv(target, python_bin=options.python) 1.156 + 1.157 + # Activate tps environment 1.158 + tps_env = os.path.join(target, activate_env) 1.159 + execfile(tps_env, dict(__file__=tps_env)) 1.160 + 1.161 + # Install TPS in environment 1.162 + subprocess.check_call([os.path.join(target, python_env), 1.163 + os.path.join(here, 'setup.py'), 'install']) 1.164 + 1.165 + # Get the path to tests and extensions directory by checking check where 1.166 + # the tests and extensions directories are located 1.167 + sync_dir = os.path.abspath(os.path.join(here, '..', '..', 'services', 1.168 + 'sync')) 1.169 + if os.path.exists(sync_dir): 1.170 + testdir = os.path.join(sync_dir, 'tests', 'tps') 1.171 + extdir = os.path.join(sync_dir, 'tps', 'extensions') 1.172 + else: 1.173 + testdir = os.path.join(here, 'tests') 1.174 + extdir = os.path.join(here, 'extensions') 1.175 + 1.176 + update_configfile(os.path.join(here, 'config', 'config.json.in'), 1.177 + os.path.join(target, 'config.json'), 1.178 + replacements={ 1.179 + '__TESTDIR__': testdir.replace('\\','/'), 1.180 + '__EXTENSIONDIR__': extdir.replace('\\','/'), 1.181 + '__FX_ACCOUNT_USERNAME__': options.username, 1.182 + '__FX_ACCOUNT_PASSWORD__': options.password, 1.183 + '__SYNC_ACCOUNT_USERNAME__': options.sync_username, 1.184 + '__SYNC_ACCOUNT_PASSWORD__': options.sync_password, 1.185 + '__SYNC_ACCOUNT_PASSPHRASE__': options.sync_passphrase}) 1.186 + 1.187 + if not (options.username and options.password): 1.188 + print '\nFirefox Account credentials not specified.' 1.189 + if not (options.sync_username and options.sync_password and options.passphrase): 1.190 + print '\nFirefox Sync account credentials not specified.' 1.191 + 1.192 + # Print the user instructions 1.193 + print usage_message.format(TARGET=target, 1.194 + BIN_NAME=bin_name) 1.195 + 1.196 +if __name__ == "__main__": 1.197 + main()