Bug 1009004 - Re-implement INSTALL.sh script as a python script. r=hskupin a=testonly DONTBUILD FIREFOX_BETA_30_END
authorCosmin Malutan <cosmin.malutan@softvision.ro>
Tue, 03 Jun 2014 18:10:01 +0200
changeset 193498 6c18811bcd1b319801fd97aeb09c41b963863968
parent 193458 6da3dd0a2e7bd557b29842de29b3363afc427601
child 200514 74413aa5465b2571eebc4a564b7b6e6fa87791a4
push id486
push userasasaki@mozilla.com
push dateMon, 14 Jul 2014 18:39:42 +0000
treeherdermozilla-release@d33428174ff1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershskupin, testonly
bugs1009004
milestone30.0
Bug 1009004 - Re-implement INSTALL.sh script as a python script. r=hskupin a=testonly DONTBUILD
testing/tps/INSTALL.sh
testing/tps/create_venv.py
testing/tps/tps/cli.py
deleted file mode 100755
--- a/testing/tps/INSTALL.sh
+++ /dev/null
@@ -1,99 +0,0 @@
-#!/bin/bash
-# This Source Code Form is subject to the terms of the Mozilla Public
-# License, v. 2.0. If a copy of the MPL was not distributed with this
-# file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
-
-# This scripts sets up a virutalenv and installs TPS into it.
-# It's probably best to specify a path NOT inside the repo, otherwise
-# all the virtualenv files will show up in e.g. hg status.
-
-# get target directory
-if [ ! -z "$1" ]
-then
-  TARGET=$1
-else
-  echo "Usage: INSTALL.sh /path/to/create/virtualenv [/path/to/python2.6]"
-  exit 1
-fi
-
-# decide which python to use
-if [ ! -z "$2" ]
-then
-    PYTHON=$2
-else
-    PYTHON=`which python`
-fi
-if [ -z "${PYTHON}" ]
-then
-    echo "No python found"
-    exit 1
-fi
-
-CWD="`pwd`"
-
-# create the destination directory
-mkdir ${TARGET}
-
-if [ "$?" -gt 0 ]
-then
-  exit 1
-fi
-
-if [ "${OS}" = "Windows_NT" ]
-then
-  BIN_NAME=Scripts/activate
-else
-  BIN_NAME=bin/activate
-fi
-
-# Create a virtualenv:
-curl -L https://raw.github.com/pypa/virtualenv/1.9.1/virtualenv.py | ${PYTHON} - ${TARGET}
-cd ${TARGET}
-. $BIN_NAME
-if [ -z "${VIRTUAL_ENV}" ]
-then
-    echo "virtualenv wasn't installed correctly, aborting"
-    exit 1
-fi
-
-# install TPS
-cd ${CWD}
-python setup.py install
-
-# clean up files created by setup.py
-rm -rf build/
-rm -rf dist/
-rm -rf tps.egg-info/
-
-if [ "$?" -gt 0 ]
-then
-  exit 1
-fi
-
-CONFIG="`find ${VIRTUAL_ENV} -name config.json.in`"
-NEWCONFIG=${CONFIG:0:${#CONFIG}-3}
-
-cd "../../services/sync/tests/tps"
-TESTDIR="`pwd`"
-
-cd "../../tps/extensions"
-EXTDIR="`pwd`"
-
-sed 's|__TESTDIR__|'"${TESTDIR}"'|' "${CONFIG}" | sed 's|__EXTENSIONDIR__|'"${EXTDIR}"'|' > "${NEWCONFIG}"
-rm ${CONFIG}
-
-echo
-echo "***********************************************************************"
-echo
-echo "To run TPS, activate the virtualenv using:"
-echo "  source ${TARGET}/${BIN_NAME}"
-echo "then execute tps using:"
-echo "  runtps --binary=/path/to/firefox"
-echo
-echo "See runtps --help for all options"
-echo
-echo "To change your TPS config, please edit the file: "
-echo "${NEWCONFIG}"
-echo
-echo "***********************************************************************"
new file mode 100755
--- /dev/null
+++ b/testing/tps/create_venv.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+"""
+This scripts sets up a virtualenv and installs TPS into it.
+It's probably best to specify a path NOT inside the repo, otherwise
+all the virtualenv files will show up in e.g. hg status.
+"""
+
+import optparse
+import os
+import shutil
+import subprocess
+import sys
+import urllib
+
+here = os.path.dirname(os.path.abspath(__file__))
+usage_message = """
+***********************************************************************
+
+To run TPS, activate the virtualenv using:
+    source {TARGET}/{BIN_NAME}
+
+To change your TPS config, please edit the file:
+    {TARGET}/config.json
+
+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'
+
+if sys.platform == 'win32':
+    bin_name = os.path.join('Scripts', 'activate.bat')
+    activate_env = os.path.join('Scripts', 'activate_this.py')
+else:
+    bin_name = os.path.join('bin', 'activate')
+    activate_env = os.path.join('bin', 'activate_this.py')
+
+def main():
+    parser = optparse.OptionParser('Usage: %prog [options] path_to_venv')
+    parser.add_option('-p', '--python',
+                      type='string',
+                      dest='python',
+                      metavar='PYTHON_BIN',
+                      default=None,
+                      help='The Python interpreter to use.')
+    (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)
+
+    # 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(['python', os.path.join(here, 'setup.py'),
+                           'install'])
+
+    # Get the path to tests and extensions directory by checking check where
+    # the tests and extensions directories are located
+    sync_dir = os.path.abspath(os.path.join(here, '..', '..', 'services',
+                                            '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, '__EXTENSIONDIR__': extdir}
+    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'))
+
+    # Print the user instructions
+    print usage_message.format(TARGET=target,
+                               BIN_NAME=bin_name)
+
+if __name__ == "__main__":
+    main()
--- a/testing/tps/tps/cli.py
+++ b/testing/tps/tps/cli.py
@@ -62,33 +62,35 @@ def main():
                       type='string',
                       dest='resultfile',
                       default='tps_result.json',
                       help='path to the result file [default: %default]')
     parser.add_option('--testfile',
                       action='store',
                       type='string',
                       dest='testfile',
-                      default='../../services/sync/tests/tps/all_tests.json',
+                      default='all_tests.json',
                       help='path to the test file to run [default: %default]')
     (options, args) = parser.parse_args()
 
     configfile = options.configfile
     if configfile is None:
-        if os.environ.get('VIRTUAL_ENV'):
-            configfile = os.path.join(os.path.dirname(__file__), 'config.json')
+        virtual_env = os.environ.get('VIRTUAL_ENV')
+        if virtual_env:
+            configfile = os.path.join(virtual_env, 'config.json')
         if configfile is None or not os.access(configfile, os.F_OK):
             raise Exception('Unable to find config.json in a VIRTUAL_ENV; you must '
                             'specify a config file using the --configfile option')
 
     # load the config file
     f = open(configfile, 'r')
     configcontent = f.read()
     f.close()
     config = json.loads(configcontent)
+    testfile = os.path.join(config.get('testdir', ''), options.testfile)
 
     rlock = RLock()
 
     print 'using result file', options.resultfile
 
     extensionDir = config.get('extensiondir')
     if not extensionDir or extensionDir == '__EXTENSIONDIR__':
         extensionDir = os.path.join(os.getcwd(), '..', '..',
@@ -105,14 +107,14 @@ def main():
                         binary=options.binary,
                         config=config,
                         debug=options.debug,
                         ignore_unused_engines=options.ignore_unused_engines,
                         logfile=options.logfile,
                         mobile=options.mobile,
                         resultfile=options.resultfile,
                         rlock=rlock,
-                        testfile=options.testfile,
+                        testfile=testfile,
                       )
     TPS.run_tests()
 
 if __name__ == '__main__':
     main()