from fabric.api import env, task from fabric.operations import run import sys import os import logging from utils import virtualenv_source, virtualenv from utils import print_console, printerr ERROR_BAD_BRANCH_PARAM = -3 ERROR_BAD_PARAM = -2 @task def setup_virtualenv(): configuration = env.config if env.debug: logging.basicConfig( format='\n%(levelname)s: deploy.setup_virtualenv %(message)s', level=logging.DEBUG) mkvirtualenv_cmd = "mkvirtualenv --no-site-packages " \ "{virtualenv_name}".format( virtualenv_name=configuration.virtualenv.name) if env.debug: logging.debug("virtualenv.workon : %s" % configuration.virtualenv.workon) logging.debug("virtualenv.activate : %s" % configuration.virtualenv.activate) logging.debug("virtualenv.name : %s" % configuration.virtualenv.name) logging.debug("virtualenv.paths.bin : %s" % configuration.virtualenv.paths.bin) logging.debug("virtualenv.paths.root : %s" % configuration.virtualenv.paths.root) logging.debug("with virtualenv_source(): run(\"\n\t%s\n\t\")".format( mkvirtualenv_cmd)) else: # run("source virtualenvwrapper.sh; mkvirtualenv " # "--no-site-packages {virtualenv_name}".format( # virtualenv_name=configuration.virtualenv.name)) with virtualenv_source(): run(mkvirtualenv_cmd) @task def setup(): """ install all packages via pip """ configuration = env.config if env.debug: logging.basicConfig( format='\n%(levelname)s: deploy.pip %(message)s', level=logging.DEBUG) pipinstall_cmd = "pip install -r {requirements}".format( requirements=configuration.virtualenv.requirements.filepath) if env.debug: logging.debug("with virtualenv(): run(\"\n\t%s\n\t\")" % pipinstall_cmd) else: with virtualenv(): run(pipinstall_cmd) @task def install(package=None): """ install a packages via pip """ configuration = env.config if not package: printerr("You must specify a package to be installed", ERROR_BAD_PARAM) if package == "--all": pipinstall_cmd = "pip install -r {requirements_file}".format( requirements_file=configuration.virtualenv.requirements.filepath) else: pipinstall_cmd = "pip install {package}".format( package=package) if env.debug: print_console("pipinstall_cmd : %s" % pipinstall_cmd) else: with virtualenv(): run(pipinstall_cmd) @task def freeze(param=False): """ True - update package list and freeze output """ configuration = env.config msg_help = """ pip.freeze takes one of three values: \thelp - this help message \tTrue - update the pip package list the freeze output \tFalse (default) - print the freeze output to the console """ from utils import booleanize, handle_help if handle_help(param, msg_help, 'help'): sys.exit() else: try: param = booleanize(param) except TypeError: printerr( "the parameter value you gave, '{param}' , is not a " "valid parameter\n{msg_help}".format( param=param, msg_help=msg_help), ERROR_BAD_PARAM ) if param: cmd_pipfreeze = "pip freeze > {requirements}".format( requirements=configuration.virtualenv.requirements.filepath) else: cmd_pipfreeze = "pip freeze" with virtualenv(): run(cmd_pipfreeze) @task def copy(branch): """ copy requirements from the specified branch to the current branch this only changes the requirements on the local branches. It does not upload remotely. This is because I want to use deploy.sync to do all remote updates """ configuration = env.config branch_list = ['staging', 'production', 'development'] if branch not in branch_list: printerr( "Branch parameter '{branch}' must be one of {branchlist} " "values.".format(branch=branch, branchlist=branch_list), ERROR_BAD_BRANCH_PARAM) elif configuration.project.branch == 'development': printerr( "This method copies to current branch which is you set to " "development. That is not allowed for sanity reasons.", ERROR_BAD_BRANCH_PARAM) elif configuration.project.branch == branch: printerr( "You have set the source branch to the current branch." "This will simply copy over \n\tthe requirements file for " "this branch with itself", ERROR_BAD_BRANCH_PARAM) print "current: %s" % configuration.virtualenv.requirements.filepath from initialize import get_config branch_config = get_config(branch) print "current filepath: %s" % \ configuration.virtualenv.requirements.filepath print "current filename: %s" % \ configuration.virtualenv.requirements.filename print print "branch filepath: %s" % \ branch_config.virtualenv.requirements.filepath print "branch filename: %s" % \ branch_config.virtualenv.requirements.filename current_local_path = os.path.join( configuration.virtualenv.requirements.local, configuration.virtualenv.requirements.filename) branch_local_path = os.path.join( configuration.virtualenv.requirements.local, branch_config.virtualenv.requirements.filename) print "current_local : %s" % current_local_path print "branch_local : %s" % branch_local_path print print print message = "Copying file from branch {branch_src} to " \ "branch {branch_dst}. Continue? Y/n ".format( branch_src=branch, branch_dst=configuration.project.branch) from utils import prompt_continue prompt_continue(message=message)