0

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:[email protected]/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?

2
  • 1
    You should use subprocess instead see here. Commented Sep 25, 2020 at 5:54
  • What's with the nested useless echos? Commented Sep 25, 2020 at 6:47

4 Answers 4

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

I have private git hub repository. So, it seems like I need to add authentication mechanism with this library as well.
Maybe have a look at stackoverflow.com/a/62796479/8505949. I haven't verified it myself but seems to be a good point to start from.
1

Try like this

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

1 Comment

You should not provide Python 2 answers to questions which are explicitly tagged python-3.x.
0

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.

1 Comment

You are linking to Python 2 documentation. It was reinstated sometime in Python 3.x, though now it's simply an (unnecessary) wrapper for subprocess.
0

For python3-X:

from subprocess
git_tag = subprocess.run(['sh', 'start.sh'], capture_output=True, text=True)
print(git_tag.stdout)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.