Bash script get version from setup.py or from PKG-INFO file and export as environment variable

Question:

I need to get version value from setup.py or from PKG-INFO using bash and extract environment variable with the version value for later use. (Actually I need version value for GitHub Action)

from setuptools import setup, find_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="helloworld",
    version="0.0.3",
    author="John",
    author_email="[email protected]",
    url="https://github.com/google/helloworld",
    description="Hello World!",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    install_requires=["click"],
    python_requires='>=3.6',
    py_modules=["helloworld"],
    entry_points={"console_scripts": ["helloworld = src.main:main"]},
)

PKG-INFO:

Metadata-Version: 2.1
Name: helloworld
Version: 0.0.3
Summary: Hello World!
Home-page: https://github.com/google/helloworld
Author: John
Author-email: [email protected]
License: UNKNOWN
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown

# helloworld

Hello World Python
...
Asked By: ybonda

||

Answers:

I need to get version value from setup.py or from PKG-INFO using bash and extract environment variable with the version value for later use.

It’s probably easier to get the wanted data from PKG-INFO than from setup.py. Provided that there is only one Version: entry, you can do it with sed:

the_version=$(sed -n 's/^Version: *//p' PKG-INFO) ||
  echo 'no version found' 1>&2
export the_version

Explanation:

  • The -n option to sed suppresses its default behavior of printing the current line at the end of each cycle.
  • The s command removes the Version: label from any line that starts with that. The p suffix causes the result of the substitution to be printed (only) for those lines where a substitution was actually performed.
  • the $() construct around the sed command captures and expands to that command’s standard output, which will be the tail of the Version: line. That is then assigned to shell variable the_version.
  • in the event that sed terminates with non-zero exit status, the echo command is executed to print a diagnostic, which is redirected to standard error instead of standard out
  • the shell variable is exported so that subsequent commands run by the script will receive it in their environments.
  • in the event that the command exits with non-zero exit status, a diagnostic is printed to the standard error stream
Answered By: John Bollinger

It was easier than I though:

VERSION=$(python setup.py --version)
echo $VERSION

In the same manner you can also get the module name:

MODULE_NAME=$(python setup.py --name)
echo $MODULE_NAME
Answered By: ybonda

In order to use setup.cfg you can use grep.

grep version setup.cfg | cut -d '=' -f2 | xargs

Explanation:

grep version setup.cfg        # return a line containing version
cut -d '=' -f2                # extract second element delimited by =
xargs                         # trim

Example:

[metadata]
name = my_app
version = 2.0.4
author = John Doe
author_email = [email protected]

Output: 2.0.4

Combining it all together you could set environment variable by:

export VERSION=$(grep version setup.cfg| cut -d '=' -f2 | xargs)
echo $VERSION
# 2.0.4
Answered By: J.Wincewicz
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.