Print bash script output in Python - python

I have a Python code that will run a script file.The script file will output the version tag of specific git repository.
My script file named 'start.sh' is as follows:
#!/bin/sh
git clone https://xxxxxxxxxx:x-oauth-basic#github.com/xxxxxxx/xxxxx.git
cd xxxxxxxx
git config --global user.email "xxxxxxxx"
git config --global user.name "xxxxxxxxx"
IMAGE_TAG=`echo \`git describe --tags\``
echo $IMAGE_TAG
My Python code is as follows:
import os
git_tag = os.popen('sh start.sh')
print(git_tag)
When I run the script file separately, it will return me the git tag. But, Whenever I try to print it in the Python code, it's not working.
How can I solve this issue?

Since you are using Python anyway, you could think of using GitPython as an alternative. You can list all your tags with:
from git import Repo
repo = Repo("path/to/repo")
print(repo.tags)

Try like this
from subprocess import check_output
out = check_output(['sh', 'start.sh'])
print(out)

According to python's documentation, os.popen() function has been deprecated since version 2.6. You might want to use subprocess module.
P.S: I wanted to comment but couldn't hence this answer.

For python3-X:
from subprocess
git_tag = subprocess.run(['sh', 'start.sh'], capture_output=True, text=True)
print(git_tag.stdout)

Related

Discovering git branch from python script

I have a python script.
I need to access the name of the git branch from which I'm running the python script, through python, during runtime.
Is there a way to do this?
Edit:
os.system("git rev-parse --abbrev-ref HEAD") outputs to the cli, I don't see how I would get access to it from python...
I would like to have sth like git_branch = <python commands>
You could use GitPython, something like the following:
>>> import git
>>> import os
>>> git_branch = git.Repo(os.getcwd()).active_branch.name
>>> git_branch
'master'
Otherwise, as already pointed out by Yevhen Kuzmovych in the comments, you could use PyGit.

How do I restart nginx from a python script?

I can use "nginx -s reload" command to restart nginx on the shell.
But, when I use os.system("nginx -s reload") command, It appears error.
/usr/local/bin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
For this error. I already install pcre. Is there some magic problems.
For running such commands in python scripts it's better to use subprocess library.
try this code instead of yours:
import subprocess
subprocess.call('whatever command you want to run it in terminal', shell=True)
Be lucky
hello I recommend that you first send this validation before sending the reset so you avoid headaches
reinicioNGINX = subprocess.getoutput(['nginx -t'])
if 'nginx: the configuration file /etc/nginx/nginx.conf syntax is ok' in reinicioNGINX:
_command_restart
else:
_command_avoid_restart

Shell command not found in python script

I source a dotcshrc file in my python script with :os.system(‘/bin/csh dotcshrc’) and it works,but when I want to use the command I have just put into the env by the source command,like os.system(‘ikvalidate mycase ‘),linux complaints:command not found.
But when I do it all by hand,everything go well.
Where is problem?
If you have a command in linux like ls and you want to use it in your python code do like this:
import os
ls = lambda : os.system('ls')
# This effectively turns that command into a python function.
ls() # skadoosh!
Output is :
FileManip.py Oscar
MySafety PROJECT DOCS
GooSpace Pg Admin
l1_2014 PlatformMavenRepo
l1_2015 R
l1_201617 R64
l2_2014 Resources
os.system runs each command in its own isolated environment. If you are sourcing something in an os.system call, subsequent calls will not see that because they are starting with a fresh shell environment. If you have dependencies like the above, you might be able to combine it into one call:
os.system(‘/bin/csh "dotcshrc; ikvalidate mycase"’)

"make install" with Python

I'm converting a Bash script to Python. I have been looking for a replacement for the "make install" - line. Is there any?
print "Installing from the sources"
urllib.urlretrieve("http://"+backupserver+"/backup-manager.tar.gz","backup-manager.tar.gz")
tar = tarfile.open("backup-manager.tar.gz", "r:gz")
tar.extractall()
tar.close()
os.chdir("Backup-Manager-0.7.10")
make install
import subprocess
subprocess.call(['make', 'install'])
Should do the trick.
If you want the output look at this
You can use subprocess
or else
import os
os.system("make install")
Some information about Calling an external command in Python
use subprocess to run other programs from Python.

How to execute git command in a identified path?

I want to execute git command in python program.
I have tried
os.system("git-command")
As we know, git command can be executed correctly only in the directories which contains repositories. I have tried to print current path and this path is not what I hope for, it does not contains repositories.
Now my question is how to execute git command in a identified path.
Use the subprocess module; pick one of the functions that suits your needs (based on what output you need). The functions all take a cwd argument that lets you specify the directory to operate in:
import subprocess
output = subprocess.check_output(['git', 'status'], cwd='/path/to/git/workingdir')
Using GitPython:
from git import *
repo = Repo("/path/to/repo")
git = repo.git
print git.status()

Categories