#!/usr/bin/env python # # See __main__ for details or run help (-h) import os import shutil import sys import tempfile import commands # verbose set in options to main # verbose = False def _print(msg, error=False): """"If verbose flag is set or error is True print msg """ if verbose or error: print msg def _run_cmd(cmd, raiseOnError=True): status, output = commands.getstatusoutput(cmd) if status: msg = 'Error running command [%s]: %s' % (cmd, output) if raiseOnError: raise(Exception(msg)) else: _print(msg, error=True) else: _print(output) def doSourceCheckout(): _print('Automated checkout in directory: %s' % svnPath) svncmd = '' if not os.path.exists(svnPath): os.makedirs(svnPath) svncmd = "%s checkout %s ./" % (svnbin, svnurl) _print('Checking out source from: %s' % svnurl) else: svncmd = '%s update' % svnbin _print('Updating source') os.chdir(svnPath) if os.system(svncmd): raise "Couldn't checkout source with command: %s" % svncmd _print('Checking out completed.') def switchToDevelopmentMode(): _print('Switching to development mode.') from kforge.utils.admin import EditConfiguration substitutes = { 'system_mode' : 'development' } if domainName is not None: substitutes['domain_name'] = domainName etcPath = os.path.join(envPath, 'etc/kforge.conf') _print('Editing configuration file: %s' % etcPath) etcFile = file(etcPath) cmd = EditConfiguration(etcFile, substitutes) cmd.execute() etcFile.close() etcFile = file(etcPath, 'w') etcFile.write(cmd.result) etcFile.close() _print('Switch to development mode completed.') def configureShellEnvironment(): _print('Configuring current environment.') _print('Setting environment variable KFORGEHOME to: %s' % envPath) os.environ['KFORGEHOME'] = envPath pythonPath = os.path.join(kforgeSystemPath, 'lib/python') sys.path.insert(0, pythonPath) if os.environ.has_key('PYTHONPATH'): pythonPath += ':' + os.environ['PYTHONPATH'] _print('Setting environment variable PYTHONPATH to: %s' % pythonPath) os.environ['PYTHONPATH'] = pythonPath djangoSettings = 'kforge.django.settings.main' _print('Setting environment variable DJANGO_SETTINGS_MODULE to: %s' % djangoSettings) os.environ['DJANGO_SETTINGS_MODULE'] = djangoSettings _print('Configuring environment completed.') def runTests(): testcmd = os.path.join(kforgeBin, 'kforge-test kforge.coretest') _print('Testing deployment of core using: %s' % testcmd) _print('****** Core tests starting **********') os.system(testcmd) _print('****** Core testing completed ******') testcmd = os.path.join(kforgeBin, 'kforge-test') _print('Testing deployment of KForge using: %s' % testcmd) _print('****** KForge tests starting **********') os.system(testcmd) _print('****** KForge testing completed ******') def main(): adminCmd = os.path.join(kforgeBin, 'kforge-admin') + ' --env %s --system %s ' % (envPath, kforgeSystemPath) if os.path.exists(envPath): shutil.rmtree(envPath) if os.path.exists(kforgeSystemPath): shutil.rmtree(kforgeSystemPath) doSourceCheckout() configureShellEnvironment() # have to set this to have PYTHONPATH _print('Installing KForge system to: %s' % kforgeSystemPath) instcmd = "python setup.py install --home=%s" % kforgeSystemPath _run_cmd(instcmd) _print('Completed installation of KForge system') createEnvCmd = adminCmd + 'data create' _run_cmd(createEnvCmd) switchToDevelopmentMode() _run_cmd(adminCmd + 'db rebuild') runTests() import optparse if __name__ == '__main__': usage = 'usage: %prog [options]\n\n' usage += 'Automatic checkout, installation, deployment and test of KForge\n\n' usage += 'Uses default configuration as provided in kforge.conf.new\n' usage += 'Attempts to create/rebuild a database using user specified in that file.\n' parser = optparse.OptionParser(usage) parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True, help='Be quiet in printing status messages' ) parser.add_option('--base', action='store', dest='base_path', default='', help='Base path to use for actions (checkout, installation etc will be done into subdirectories of this directory unless explicitly overridden by other options). If not supplied a temporary path will be used.' ) parser.add_option('--svn-path', action='store', dest='svn_path', default='', help='Path to svn checkout. If this already exists a svn update will be performed instead of a checkout.' ) parser.add_option('--kforge-system', action='store', dest='kforge_system', default='', help='Destination path for installation KForge system' ) parser.add_option('--env-path', action='store', dest='env_path', default='', help='Destination path for KForge environment (the test instance)' ) parser.add_option('--domain-name', action='store', dest='domain_name', default=None, help='Domain name to use for the KForge instance' ) (options, args) = parser.parse_args() # url for KForge svn trunk svnurl = 'http://project.knowledgeforge.net/kforge/svn/trunk' svnbin = 'svn' basePath = tempfile.mkdtemp(prefix='kforge-integration-test-') if options.base_path: basePath = options.base_path # set defaults for other directory paths svnPath = os.path.join(basePath, 'svn') kforgeSystemPath = os.path.join(basePath, 'system') envPath = os.path.join(basePath, 'instance') kforgeBin = os.path.join(kforgeSystemPath, 'bin') domainName = options.domain_name verbose = options.verbose if options.svn_path: svnPath = options.svn_path if options.kforge_system: kforgePath = options.kforge_system if options.env_path: envPath = options.env_path try: main() finally: if not options.base_path: # we are using a temp directory shutil.rmtree(basePath) _print('Automated build-and-test completed.')