def is_virtualenv_installed_in_user(): """ 检查virtualenv是否在系统目录安装 """ with settings(warn_only=True): return exists('~/.local/bin/virtualenv')
def kill_by_name(name): """ 停止指定特征的进程 """ with settings(warn_only=True): run("ps aux | grep '%s' | grep -v 'grep' | awk '{print $2}' | xargs kill -9" % name)
def is_virtualenv_installed_in_system(): """ 检查virtualenv是否在系统目录安装 """ with settings(warn_only=True): return 'no virtualenv' not in run('which virtualenv') or \ 'which' not in run('which virtualenv')
def _supervisor_command(command, venv_dir=None): if venv_dir: with virtualenv.activate(venv_dir): _supervisor_command(command) if not 'CURRENT_VIRTUAL_ENV_DIR' in env: raise Exception(u'只可以在虚拟环境安装Python包') venv_dir = env.CURRENT_VIRTUAL_ENV_DIR # 停止supervisor管理的进程 with settings(warn_only=True), cd(venv_dir): run('bin/supervisorctl -c etc/supervisord.conf ' + command)
def start(venv_dir=None): """重启指定虚拟环境的supervisor""" if venv_dir: with virtualenv.activate(venv_dir): start() if not 'CURRENT_VIRTUAL_ENV_DIR' in env: raise Exception(u'只可以在虚拟环境安装Python包') venv_dir = env.CURRENT_VIRTUAL_ENV_DIR with settings(warn_only=True), cd(venv_dir): # 停止supervisor管理的进程 run('bin/supervisord -c etc/supervisord.conf ')
def is_installed(package): """检查Python包是否被安装 注意:只能在虚拟Python环境中执行 """ if not 'CURRENT_VIRTUAL_ENV_DIR' in env: raise Exception(u'只可以在虚拟环境安装Python包') venv_dir = env.CURRENT_VIRTUAL_ENV_DIR with settings(warn_only=True): res = run('%(venv_dir)s/bin/pip freeze' % locals()) packages = [line.split('==')[0].lower() for line in res.splitlines()] return package.lower() in packages
def sync(*packages): """从http://pypi.python.org同步包 用法: fab pypi.sync:django==1.3,tornado """ config.check('PYPI_HOST', 'PYPI_USER', 'PYPI_ROOT') with settings(host_string=env.PYPI_HOST, user=env.PYPI_USER): cmd = ["pip", "-q", "install", "--no-deps", "-i", "https://pypi.python.org/simple", "-d", env.PYPI_ROOT, ' '.join(packages)] run(" ".join(cmd))
def sync(*packages): """从http://pypi.python.org同步包 用法: fab pypi.sync:django==1.3,tornado """ config.check('PYPI_HOST', 'PYPI_USER', 'PYPI_ROOT') with settings(host_string=env.PYPI_HOST, user=env.PYPI_USER): cmd = [ "pip", "-q", "install", "--no-deps", "-i", "https://pypi.python.org/simple", "-d", env.PYPI_ROOT, ' '.join(packages) ] run(" ".join(cmd))