Home > OS >  bash shell commands run through python to be universal with windows, mac, and linux
bash shell commands run through python to be universal with windows, mac, and linux

Time:08-17

I need to have bash shell commands run through python in order to be universal with pc and mac/linux. ./bin/production doesn't work in powershell and putting 'bash' in front would give an error that it doesn't recognize 'docker' command

./bin/production contents:

#!/bin/bash
docker run --rm -it \
    --volume ${PWD}/prime:/app \
    $(docker build -q docker/prime) \
    npm run build

This is the python script:

import subprocess
from python_on_whales import docker
cmd = docker.run('docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
print(out)

This is the error I get when running the python script:

python_on_whales.exceptions.NoSuchImage: The docker command executed was C:\Program Files\Docker\Docker\resources\bin\docker.EXE image inspect docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build. It returned with code 1 The content of stdout is '[] ' The content of stderr is 'Error response from daemon: no such image: docker run --rm -it --volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build: invalid reference format: repository name must be lowercase '

Running the command, docker run --rm -it--volume ${PWD}/prime:/app $(docker build -q docker/prime) npm run build in one long line in powershell works but we want a universal standard command for both pc and mac/linux

CodePudding user response:

The Python on Whales docker.run() function doesn't take a docker run ... command line. It is a native Python API where you need to express the various Docker options as function parameters.

In principle you could rewrite this Python script using that API:

from pathlib import Path
from python_on_whales import docker

# build the image, returns an Image object
image = docker.build(Path.cwd() / 'docker' / 'prime')

# start the container; like `docker run ...`
docker.run(image,
           command=['npm', 'run', 'build'],
           volumes=[(Path.cwd() / 'prime', '/app')], # -v $(PWD)/prime:/app
           interactive=True,                         # -i (required?)
           tty=True,                                 # -t (required?)
           remove=True)                              # --rm

The return value from docker.run() (without detach=True) is the container's stdout, and the examples print() that data.

CodePudding user response:

This might not be what you're looking for but you can always try this:

import platform
import subprocess
import os

cur_os = platform.system()

if cur_os == "Windows":
    print("You are on windows")
    os.system('Command here') # for windows
elif cur_os == "Darwin":
    print("You are on mac")
    subprocess.call('Command goes here') # for mac

Edit: I'm intermediate with python so don't judge, if I did something wrong please give me feedback. Thanks.

  • Related