2

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="john@gmail.com",
    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: john@gmail.com
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
...
ybonda
  • 1,546
  • 25
  • 38
  • What have you tried? This is probably a duplicate. – tripleee Aug 18 '21 at 13:34
  • Why don't you use the github script instead of bash since you mention that your package is OS independent, you would need a POSIX solution. There is the python solution also described here https://stackoverflow.com/questions/2058802/how-can-i-get-the-version-defined-in-setup-py-setuptools-in-my-package, which you can use in a previous step and get the answer in the next step of your job sequence. – tchar Aug 18 '21 at 13:34

3 Answers3

3

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
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

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
ybonda
  • 1,546
  • 25
  • 38
0

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 = jdoe@example.com

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
J.Wincewicz
  • 849
  • 12
  • 19