pythonauto-updatebest-in-place

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


I want to write a launcher script to a python qt application i have. The idea is that it would run git pull, pip install -r requirements, launch the real application and then quit.

I know how to do all of the git/pip stuff and i know of a couple ways to launch the application but what is the best practice here for updating the application and then running it without having the user have to do anything.

Application is installed on workstations in our office, all the workstations are running windows with python installed. Are applications that they use are are installed with git running in virtualenv.

What i have done in the past was check versions in db if version isn't the correct one to then run the git/pip process and exit with a message to the user to just restart the application. I would rather just restart the application.

TIA


Solution

  • I suggest using project automation setup tools like (fabric/fabtools) : Install them pip install fabric fabtools

    In your question, you did not specify whether you would like to run these stuffs locally or on a remote server, any way, find bellow both cases:

    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")
    

    After writing this script into "fabfile.py", from terminal go to the directory caontaining this script and: