Skip to content

Instantly share code, notes, and snippets.

@benjmin-r
Created September 30, 2010 14:19
Show Gist options
  • Save benjmin-r/604650 to your computer and use it in GitHub Desktop.
Save benjmin-r/604650 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Run run_tests.py from the repository root.
Must be called with -n switch the first time, to create virtualenv
"""
import os
import sys
import warnings
import shutil
import subprocess
from optparse import OptionParser
basepath = os.path.abspath(os.path.dirname(__file__))
def set_sys_path():
gae = os.path.abspath(os.path.join(basepath, 'etc', 'parts', 'google_appengine'))
distlib = os.path.abspath(os.path.join(basepath, 'app', 'distlib'))
lib = os.path.abspath(os.path.join(basepath, 'app', 'lib'))
app = os.path.abspath(os.path.join(basepath, 'app'))
sys.path[0:0] = [distlib, lib, app, gae]
def start_nose(want_coverage):
a = __file__
a += ' -d --with-gae -P --with-color --without-sandbox --gae-application=app'
if want_coverage:
a += ' --with-coverage --cover-html --cover-package=cmm,app --cover-html-dir=coverage-report'
a = a.split()
print ">>> Starting Nose!"
print ">>> Args: '%s'" % a
print ">>> ----------------------------------------------------------"
warnings.simplefilter("ignore", DeprecationWarning)
import nose # import here as it's loaded after ve activate
nose.run(argv=a)
def clear_virtualenv(vedir):
if os.path.exists(vedir):
print ">>> ----------------------------------------------------------"
print ">>> Starting fresh!"
print ">>> Removing current virtual environment in '%s'!!!!" % vedir
print ">>> ----------------------------------------------------------"
shutil.rmtree(vedir)
def install_requirements(vedir, req_filename):
print ">>> Calling 'pip' with filename '%s'" % req_filename
subprocess.call(["pip",
"install",
"-E", vedir,
"-r", req_filename ])
def activate_virtualenv():
activate_this = os.path.abspath(os.path.join(basepath, options.vedir, 'bin', 'activate_this.py'))
if not os.path.exists(activate_this):
# on windows, activate_this.py is in 'Scripts' folder
activate_this = os.path.abspath(os.path.join(basepath, options.vedir, 'Scripts', 'activate_this.py'))
print ">>> ----------------------------------------------------------"
print ">>> Activating virtualenv in '%s'" % activate_this
execfile(activate_this, dict(__file__=activate_this))
if __name__ == '__main__':
opt_parser = OptionParser()
opt_parser.add_option("-e", "--virtual-env", dest="vedir", default='ve', help="Directory where to find/put the virtual env")
opt_parser.add_option("-f", "--req-file", dest="reqfiles", action="append", default=[], help="A pip requirements file (in dir specfied by -d)")
opt_parser.add_option("-n", "--new", dest="newenv", action="store_true", default=False, help="Create new virtual env (clearing current if one exists)")
opt_parser.add_option("-c", "--coverage", dest="coverage", action="store_true", default=False, help="create a coverage report")
(options, args) = opt_parser.parse_args()
if options.newenv:
if not options.reqfiles:
rf = os.path.abspath(os.path.join(basepath, 'tests', 'requirements.pip'))
options.reqfiles.append(rf)
clear_virtualenv(options.vedir)
for r in options.reqfiles:
install_requirements(options.vedir, r)
activate_virtualenv()
set_sys_path()
start_nose(options.coverage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment