-1

Any “proper” way to automate with python for build scripts?

I want to have my script do something like this:

cd /somewhere
git pull
npm run build
make deploy

Everywhere on google I see: os.system("xxx") or subprocess.call(...).

In BASH, the above is simple, but I wanna create a cli python app to create all of that stuff for me.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82
  • 1
    you can make use of python libraries such as `os, shutils, psutils, subprocess` – Jeril Feb 05 '19 at 07:23
  • Write to a .sh file, and execute it via [subprocess](https://stackoverflow.com/questions/3777301/how-to-call-a-shell-script-from-python-code). – felipe Feb 05 '19 at 07:33
  • or, you can use `os.system` to execute the command directly on Python. Import `os` and run `os.systems('ls')` to see results. – felipe Feb 05 '19 at 07:35
  • @MichaWiedenmann You will see my first comment speaks directly about `subprocess`, and frankly, I'm not sure as to why using `os.system` would be wrong in this situation. The `ls` command was just for him to see that he can execute bash commands on Python. – felipe Feb 05 '19 at 22:05

2 Answers2

0

In the spirit of do not reinvent the wheel, there are lots of way to automate builds with python, getting for free things like dependencies management.

One useful tool is doit.

To get an idea, this is a very simple example similar to your use case:

import os

MY_PRJ_ROOT='/home/myname/my_project_dir'

def task_cd():
    def cd_to_somewhere():
        os.chdir(MY_PRJ_ROOT)
    return {
        'actions': [cd_to_somewhere]
    }

def task_git_pull():
    """pull my git repo"""
    return {
        'actions': ['git pull'],
    }

def task_build_rust_app():
    """build by awesome rust app"""
    return {
        'actions': ['cargo build']
    }

supposing the above is a file named dodo.py, the default name for doit tasks, run it as:

> doit

Others resources

Also worth noting (to my knowledge, they are not an exhaustive list of pythons automation tools):

SCons - a software construction tool

ShutIt - A versatile automation framework

attdona
  • 17,196
  • 7
  • 49
  • 60
0

os.system call the shell and send the command to the shell, so you can easily do it:

import os

cmd == """\
cd /somewhere
git pull
npm run build
make deploy
""""

os.system(cmd)

It is so easy. We tend to forget that os.system do no directly execute the commands, but it dispatch command to the shell. So we can use redirection and pipe.

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32