Bug 1034722 - Upgrade virtualenv to 1.10.1 for TPS environment creation. r=jgriffin a=testonly DONTBUILD
authorHenrik Skupin <mail@hskupin.info>
Mon, 07 Jul 2014 22:58:17 +0200
changeset 208867 144a5dedcf29c95579ee966646d7af8080fab43e
parent 208866 6a7a6089f21452e78178804c090dc8020f6e7ec2
child 208868 6717ee24f683532fe655f5f40e78d5af74f046e8
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgriffin, testonly
bugs1034722
milestone32.0a2
Bug 1034722 - Upgrade virtualenv to 1.10.1 for TPS environment creation. r=jgriffin a=testonly DONTBUILD
testing/tps/create_venv.py
--- a/testing/tps/create_venv.py
+++ b/testing/tps/create_venv.py
@@ -9,17 +9,19 @@ It's probably best to specify a path NOT
 all the virtualenv files will show up in e.g. hg status.
 """
 
 import optparse
 import os
 import shutil
 import subprocess
 import sys
-import urllib
+import urllib2
+import zipfile
+
 
 here = os.path.dirname(os.path.abspath(__file__))
 usage_message = """
 ***********************************************************************
 
 To run TPS, activate the virtualenv using:
     source {TARGET}/{BIN_NAME}
 
@@ -28,27 +30,84 @@ To change your TPS config, please edit t
 
 To execute tps use:
     runtps --binary=/path/to/firefox
 
 See runtps --help for all options
 
 ***********************************************************************
 """
-virtualenv_url = 'https://raw.github.com/pypa/virtualenv/1.9.1/virtualenv.py'
+
+# Link to the folder, which contains the zip archives of virtualenv
+URL_VIRTUALENV = 'https://codeload.github.com/pypa/virtualenv/zip/'
+VERSION_VIRTUALENV = '1.10.1'
+
 
 if sys.platform == 'win32':
     bin_name = os.path.join('Scripts', 'activate.bat')
     activate_env = os.path.join('Scripts', 'activate_this.py')
     python_env = os.path.join('Scripts', 'python.exe')
 else:
     bin_name = os.path.join('bin', 'activate')
     activate_env = os.path.join('bin', 'activate_this.py')
     python_env = os.path.join('bin', 'python')
 
+
+def download(url, target):
+    """Downloads the specified url to the given target."""
+    response = urllib2.urlopen(url)
+    with open(target, 'wb') as f:
+        f.write(response.read())
+
+    return target
+
+
+def setup_virtualenv(target, python_bin=None):
+    script_path = os.path.join(here, 'virtualenv-%s' % VERSION_VIRTUALENV,
+                               'virtualenv.py')
+
+    print 'Downloading virtualenv %s' % VERSION_VIRTUALENV
+    zip_path = download(URL_VIRTUALENV + VERSION_VIRTUALENV,
+                        os.path.join(here, 'virtualenv.zip'))
+
+    try:
+        with zipfile.ZipFile(zip_path, 'r') as f:
+            f.extractall(here)
+
+        print 'Creating new virtual environment'
+        cmd_args = [sys.executable, script_path, target]
+
+        if python_bin:
+            cmd_args.extend(['-p', python_bin])
+
+        subprocess.check_call(cmd_args)
+    finally:
+        try:
+            os.remove(zip_path)
+        except OSError:
+            pass
+
+        shutil.rmtree(os.path.dirname(script_path), ignore_errors=True)
+
+
+def update_configfile(source, target, replacements):
+    lines = []
+
+    with open(source) as config:
+        for line in config:
+            for source_string, target_string in replacements.iteritems():
+                if target_string:
+                    line = line.replace(source_string, target_string)
+            lines.append(line)
+
+    with open(target, 'w') as config:
+        for line in lines:
+            config.write(line)
+
+
 def main():
     parser = optparse.OptionParser('Usage: %prog [options] path_to_venv')
     parser.add_option('--password',
                       type='string',
                       dest='password',
                       metavar='FX_ACCOUNT_PASSWORD',
                       default=None,
                       help='The Firefox Account password.')
@@ -85,22 +144,17 @@ def main():
 
     (options, args) = parser.parse_args(args=None, values=None)
 
     if len(args) != 1:
          parser.error('Path to the environment has to be specified')
     target = args[0]
     assert(target)
 
-    # Create a virtual environment
-    urllib.urlretrieve(virtualenv_url, 'virtualenv.py')
-    cmd_args = [sys.executable, 'virtualenv.py', target]
-    if options.python:
-        cmd_args.extend(['-p', options.python])
-    subprocess.check_call(cmd_args)
+    setup_virtualenv(target, python_bin=options.python)
 
     # Activate tps environment
     tps_env = os.path.join(target, activate_env)
     execfile(tps_env, dict(__file__=tps_env))
 
     # Install TPS in environment
     subprocess.check_call([os.path.join(target, python_env),
                            os.path.join(here, 'setup.py'), 'install'])
@@ -111,56 +165,30 @@ def main():
                                             'sync'))
     if os.path.exists(sync_dir):
         testdir = os.path.join(sync_dir, 'tests', 'tps')
         extdir = os.path.join(sync_dir, 'tps', 'extensions')
     else:
         testdir = os.path.join(here, 'tests')
         extdir = os.path.join(here, 'extensions')
 
-    assert(os.path.exists(testdir))
-    assert(os.path.exists(extdir))
-
-    # Update config file
-    config_in_path = os.path.join(here, 'config', 'config.json.in')
-    replacements = {'__TESTDIR__': testdir.replace('\\','/'),
-                    '__EXTENSIONDIR__': extdir.replace('\\','/')}
-    if options.username and options.password:
-        replacements.update({
-            '__FX_ACCOUNT_USERNAME__': options.username,
-            '__FX_ACCOUNT_PASSWORD__': options.password})
-    else:
-        print 'Firefox Account credentials not specified. Please update the ' \
-              'config file manually.'
+    update_configfile(os.path.join(here, 'config', 'config.json.in'),
+                      os.path.join(target, 'config.json'),
+                      replacements={
+                      '__TESTDIR__': testdir.replace('\\','/'),
+                      '__EXTENSIONDIR__': extdir.replace('\\','/'),
+                      '__FX_ACCOUNT_USERNAME__': options.username,
+                      '__FX_ACCOUNT_PASSWORD__': options.password,
+                      '__SYNC_ACCOUNT_USERNAME__': options.sync_username,
+                      '__SYNC_ACCOUNT_PASSWORD__': options.sync_password,
+                      '__SYNC_ACCOUNT_PASSPHRASE__': options.sync_passphrase})
 
-    if options.sync_username and options.sync_password and options.passphrase:
-        replacements.update({
-            '__SYNC_ACCOUNT_USERNAME__': options.sync_username,
-            '__SYNC_ACCOUNT_PASSWORD__': options.sync_password,
-            '__SYNC_ACCOUNT_PASSPHRASE__': options.sync_passphrase})
-    else:
-        print "Firefox Sync account credentials not specified. Please " \
-              "update the config file manually."
-
-    lines = []
-    with open(config_in_path) as config:
-        for line in config:
-            for source_string, target_string in replacements.iteritems():
-                line = line.replace(source_string, target_string)
-            lines.append(line)
-
-    with open(os.path.join(target, 'config.json'), 'w') as config:
-        for line in lines:
-            config.write(line)
-
-    # Clean up files created by setup.py
-    shutil.rmtree(os.path.join(here, 'build'))
-    shutil.rmtree(os.path.join(here, 'dist'))
-    shutil.rmtree(os.path.join(here, 'tps.egg-info'))
-    os.remove(os.path.join(here, 'virtualenv.py'))
-    os.remove(os.path.join(here, 'virtualenv.pyc'))
+    if not (options.username and options.password):
+        print '\nFirefox Account credentials not specified.'
+    if not (options.sync_username and options.sync_password and options.passphrase):
+        print '\nFirefox Sync account credentials not specified.'
 
     # Print the user instructions
     print usage_message.format(TARGET=target,
                                BIN_NAME=bin_name)
 
 if __name__ == "__main__":
     main()