Python启动程序,运行git pull,然后启动真正的应用程序

2024-04-19 14:04:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为我拥有的pythonqt应用程序编写一个启动程序脚本。其想法是,它将运行git pull、pip install-r需求,启动真正的应用程序,然后退出。你知道吗

我知道如何做所有的git/pip工作,我知道一些启动应用程序的方法,但是在这里更新应用程序然后运行它而不需要用户做任何事情的最佳实践是什么。你知道吗

应用程序安装在我们办公室的工作站上,所有的工作站都运行windows,安装了python。它们所使用的应用程序是在git运行于virtualenv的情况下安装的。你知道吗

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

短暂性脑缺血发作


Tags: installpip方法用户git程序版本脚本
1条回答
网友
1楼 · 发布于 2024-04-19 14:04:38

我建议使用像(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")

将此脚本写入“文件.py,从终端转到包含此脚本的目录,然后:

  • 运行fab bootstrap_local在本地运行作业
  • 或者运行fab bootstrap_remote远程运行作业

相关问题 更多 >