Python 运行 git pull 然后启动真实应用程序的启动器

Python launcher that runs git pull and then launches the real application

我想为我拥有的 python qt 应用程序编写一个启动器脚本。这个想法是它会 运行 git pull, pip install -r requirements, launch the real application and then quit.

我知道如何做所有 git/pip 的事情,我知道几种启动应用程序的方法,但是这里更新应用程序然后 运行ning 它的最佳实践是什么无需用户做任何事情。

应用程序安装在我们办公室的工作站上,所有工作站都 运行宁 windows 安装了 python。他们使用的应用程序是否与 git 运行ning 一起安装在 virtualenv 中。

我过去所做的是检查数据库中的版本,如果版本不正确,然后 运行 git/pip 进程并退出并向用户发送一条消息以重新启动应用程序。我宁愿重新启动应用程序。

TIA

我建议使用像 (fabric/fabtools) 这样的项目自动化设置工具:安装它们 pip install fabric fabtools

在你的问题中,你没有指定你是想 运行 这些东西在本地还是在远程服务器上,无论如何,找到下面两种情况:

from fabric.api import local, cd, run, env, roles
from fabric.context_managers import prefix


env.project_name = "project name"
env.repo = "your_repo.git"

REMOTEHOST = "Ip or domaine"

REMOTEUSER = "user"
REMOTEPASSWORD = "password"

env.roledefs.update({
    "remote": [REMOTEHOST]
})

def remote():
    """Defines the Development Role
    """
    env.user = REMOTEUSER
    env.password = REMOTEPASSWORD
    env.forward_agent = True  # Your local machine has access, and the remote not, so you forward you identity to the
                              # remote, and then the remote gets access

def install_requirements(environment="local"):
    """Install the packages required by our each environment
    """
    if environment == "local":
        with cd("/your project/"):
            with prefix("source activate {0}".format("YOUR VIRTUALENV NAME")):
                run("pip install -r requirements/local.txt")
    elif environment == "remote":
        with cd("your project"):
            with prefix("source activate {0}".format("YOUR VIRTUALENV NAME")):
                run("pip install -r requirements/remote.txt")

def bootstrap_local():
    """Do your job locally
    """
    env.warn_only = True
    with cd("your directory"):
        with prefix("source activate {0}".format("YOUR VIRTUALENV NAME")):
            local("git checkout {0}".format("YOUR BRANCH"))
            local("git pull origin {0}".format("YOUR BRANCH"))
    install_requirements(environment="local")
    local("the command line of the Application you wanna launch")

@roles('remote')
def bootstrap_remote():
    """do your job in the Remote server
    """
    env.warn_only = True
    remote()
    with cd("your directory"):
        with prefix("source activate {0}".format("YOUR VIRTUALENV NAME")):
            run("git checkout {0}".format("YOUR BRANCH"))
            run("git pull origin {0}".format("YOUR BRANCH"))
    install_requirements(environment="remote")
    run("the command line of the Application you wanna launch")

将此脚本写入 "fabfile.py" 后,从终端转到包含此脚本的目录并:

  • 运行 fab bootstrap_local 到 运行 本地工作
  • 或运行fab bootstrap_remote到运行远程作业