#!/usr/bin/env python3 ''' Shell scripting with python : - https://www.freecodecamp.org/news/python-for-system-administration-tutorial - https://github.com/ninjaaron/replacing-bash-scripting-with-python#if-the-shell-is-so-great-what-s-the-problem - https://linuxize.com/post/python-get-change-current-working-directory ''' DEBUG = False #DEBUG = True #print(DEBUG) import sys import os from shutil import which import subprocess def run(command: str) : #print(command.split(' ')) return subprocess.run(command.split(' ')) #result = subprocess.run(command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Guess Context (3 possibilities) : # - NO DOCKER, # or # - DOCKER OUT of container, # or # - or DOCKER IN container WITH_DOCKER_IS_SET = False if os.getenv('WITH_DOCKER') is None else True #print(WITH_DOCKER_IS_SET) #APP_FOLDER=False #[ -d ../app/ ] && APP_FOLDER=true APP_FOLDER = os.path.exists('../app/') VENV = os.path.exists('./venv/') # test if docker is installed #[ -x "$(command -v docker)" ] && DOCKER_CMD=true DOCKER_CMD = which('docker') is not None # Pas utile vu qu'on va redémarrer systématiquement le container # test if container is running ##DOCKER_CONTAINER_STARTED=false ##$DOCKER_CMD && [ $(docker ps | grep 'pyros' | wc -l) -eq 2 ] && DOCKER_CONTAINER_STARTED=true # # SYNTHESIS # #DOCKER=false #[[ $VENV == false && $DOCKER_CMD == true ]] && DOCKER=true DOCKER = DOCKER_CMD and not VENV DOCKER_OUT_CONTAINER = DOCKER and not WITH_DOCKER_IS_SET #[[ $DOCKER == false && $WITH_DOCKER_IS_SET == true ]] && DOCKER_IN_CONTAINER=true if DEBUG : print(APP_FOLDER) print(VENV) print(DOCKER_CMD) #print(container) # Synthesis print(DOCKER) print(DOCKER_OUT_CONTAINER) print(sys.argv) exit(0) # no container ? => start container first #[ $container == false ] && cd docker/ && docker-compose up -d args = sys.argv[1:] args = ' '.join(args) #print(args) #PYROS_CMD = "python3 pyros.py --help" #PYROS_CMD = "python3 pyros.py $*" PYROS_CMD = "python3 pyros.py " + args PYROS_CMD = PYROS_CMD.rstrip() # DOCKER_OUT_CONTAINER true ? => docker exec #docker exec -it pyros python3 pyros.py $* #[ $DOCKER_OUT_CONTAINER == true ] && cd docker/ && docker-compose up -d && PREFIX='docker exec -it pyros' if DOCKER_OUT_CONTAINER : #cd docker/ os.chdir('docker') #docker-compose up -d run('docker-compose up -d') PYROS_CMD = 'docker exec -it pyros ' + PYROS_CMD #PYROS_CMD print("\n Executing command :", PYROS_CMD, "\n") res = run(PYROS_CMD) ''' #Print the stdout and stderr print() print(result.args) print() print(result.stdout) print() print(result.stderr) ''' ''' ########################## # Script BASH equivalent # ########################## #!/usr/bin/env bash DEBUG=true DEBUG=false # test if user passed a command as parameter #if test $# -lt 1 ; then #echo "Missing command, use one of the commands below" #python3 pyros.py --help #exit #fi # Guess Context : # - NO DOCKER, # or # - DOCKER OUT of container, # or # - or DOCKER IN container WITH_DOCKER_IS_SET=true [ -z $WITH_DOCKER ] && WITH_DOCKER_IS_SET=false APP_FOLDER=false [ -d ../app/ ] && APP_FOLDER=true VENV=false [ -d ./venv/ ] && VENV=true # test if docker is installed DOCKER_CMD=false [ -x "$(command -v docker)" ] && DOCKER_CMD=true # test if container is running DOCKER_CONTAINER_STARTED=false $DOCKER_CMD && [ $(docker ps | grep 'pyros' | wc -l) -eq 2 ] && DOCKER_CONTAINER_STARTED=true # # SYNTHESIS # DOCKER=false [[ $VENV == false && $DOCKER_CMD == true ]] && DOCKER=true DOCKER_OUT_CONTAINER=false [[ $DOCKER == true && $WITH_DOCKER_IS_SET == false ]] && DOCKER_OUT_CONTAINER=true #[[ $DOCKER == false && $WITH_DOCKER_IS_SET == true ]] && DOCKER_IN_CONTAINER=true if $DEBUG ; then echo $APP_FOLDER echo $VENV echo $DOCKER_CMD echo $container # Synthesis echo $DOCKER echo $DOCKER_OUT_CONTAINER exit fi # no container ? => start container first #[ $container == false ] && cd docker/ && docker-compose up -d # DOCKER_OUT_CONTAINER true ? docker exec PREFIX='' #docker exec -it pyros python3 pyros.py $* [ $DOCKER_OUT_CONTAINER == true ] && cd docker/ && docker-compose up -d && PREFIX='docker exec -it pyros' echo $PREFIX $PREFIX python3 pyros.py $* '''