Skip to content

Commit

Permalink
Merge pull request #2191 from jimklimov/python-pkg
Browse files Browse the repository at this point in the history
Fix contents and recipes for Python packaging
  • Loading branch information
jimklimov authored Nov 18, 2023
2 parents 77348ef + 112581d commit cb16ff1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
26 changes: 10 additions & 16 deletions .github/workflows/PyNUTClient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,37 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: '3.10'
- name: Install pypa/setuptools
- name: Extract python interpreter path name
id: pythoncmd
run: >-
python -m
pip install wheel build
echo "PYTHON=$(command -v python)" >> $GITHUB_OUTPUT
- name: Extract tag name
id: tag
# Note: this is all a single shell line in the end,
# so we need semicolons between commands!
run: >-
TAG_NAME="$(echo $GITHUB_REF | cut -d / -f 3)" ;
if [ x"$TAG_NAME" = xmaster ]; then
TAG_NAME="$(git describe --tags --match 'v[0-9]*.[0-9]*.[0-9]' --exclude '*-signed' --exclude '*rc*' --exclude '*alpha*' --exclude '*beta*' 2>/dev/null || git describe --tags --exclude '*rc*' --exclude '*alpha*' --exclude '*beta*' --exclude '*Windows*' --exclude '*IPM*' 2>/dev/null ) | sed -e 's/^v\([0-9]\)/\1/' -e 's,^.*/,,')" \
TAG_NAME="$(git describe --tags --match 'v[0-9]*.[0-9]*.[0-9]' --exclude '*-signed' --exclude '*rc*' --exclude '*alpha*' --exclude '*beta*' 2>/dev/null || git describe --tags --exclude '*rc*' --exclude '*alpha*' --exclude '*beta*' --exclude '*Windows*' --exclude '*IPM*' 2>/dev/null | sed -e 's/^v\([0-9]\)/\1/' -e 's,^.*/,,')" \
&& test -n "${TAG_NAME}" \
|| TAG_NAME="2.8.1-`TZ=UTC date +%s`" ;
fi ;
TAG_NAME="$(echo "$TAG_NAME" | sed -e 's/^v//' -e 's/-g.*$//' -e 's/-/./g')" ;
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_OUTPUT
- name: Install pypa/setuptools
run: >-
${{ steps.pythoncmd.outputs.PYTHON }} -m
${{ steps.pythoncmd.outputs.PYTHON }} -m pip install wheel build
- name: Update version in setup.py
run: >-
if [ ! -s scripts/python/module/setup.py ] ; then
sed -e "s/@NUT_SOURCE_GITREV_NUMERIC@/${{ steps.tag.outputs.TAG_NAME }}/g" < scripts/python/module/setup.py.in > scripts/python/module/setup.py ;
fi
- name: Prepare source layout
- name: Prepare source layout and Build a binary wheel
run: >-
set -e ;
cd scripts/python/module ;
mkdir src ;
for F in *.py.in ; do sed -e "s,@PYTHON@,$(command -v python)," < "$F" > "src/`basename "$F" .in`" ; done ;
cp README.adoc README.txt ;
cp ../../../LICENSE-GPL3 . ;
chmod +x src/test*.py ;
- name: Build a binary wheel
run: >-
cd scripts/python/module ;
python -m build --skip-dependency-check --no-isolation
|| { rm -rf build dist *egg* ; python setup.py sdist bdist_wheel ; }
|| { rm -rf build dist *egg* ; python -m pip wheel . -w dist --no-deps ; } ;
make -f Makefile.am clean-local dist NUT_SOURCE_GITREV_NUMERIC="${{ steps.tag.outputs.TAG_NAME }}" PYTHON="${{ steps.pythoncmd.outputs.PYTHON }}" top_srcdir="../../.." srcdir="." builddir="." ;
find . -ls
- name: Publish master distribution 📦 to Test PyPI
# https://github.com/pypa/gh-action-pypi-publish
Expand Down
1 change: 1 addition & 0 deletions scripts/python/module/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/build
/dist
/src
/.tox
/README.txt
/.pypi-*
/LICENSE-GPL3
38 changes: 29 additions & 9 deletions scripts/python/module/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,43 @@

# Non-maintainers can at most generate the source layout for python setuptools
# (having only shell scripting as a prerequisite suffices for that)
all: src
all: PyNUTClient

EXTRA_DIST = tox.ini

NUT_SOURCE_GITREV_NUMERIC = @NUT_SOURCE_GITREV_NUMERIC@
PYTHON = @PYTHON@

GENERATED_DIST = dist build *.egg-info
GENERATED_SRC = src README.txt
GENERATED_SRC = PyNUTClient README.txt LICENSE-GPL3

# These are normally generated by a NUT build, but if we want to iterate
# specifically PyNUTClient packaging - `make clean dist` should do it here:
GENERATED_PY = test_nutclient.py PyNUT.py setup.py

# (Re-)generate files normally made by `configure` from .in templates
# No touch-files here, intended for manual use in developer iterations
py-in: $(GENERATED_PY)
redist: clean py-in dist

$(GENERATED_DIST): .pypi-dist

# NOTE: We only clean .pypi-tools* in MAINTAINERCLEANFILES to avoid regular
# re-detection of the probably unchanging environment!
clean-local:
rm -rf $(GENERATED_SRC) $(GENERATED_DIST)
rm -f .pypi-src .pypi-dist LICENSE-GPL3
rm -f .pypi-src .pypi-dist*
rm -f $(GENERATED_PY)

# Python test envs take a while to populate, so maybe better not clean
# them too enthusiastically. Can revise (move to "clean-local") later,
# if this choice proves a problem.
distclean-local:
rm -rf .tox

MAINTAINERCLEANFILES = Makefile.in .dirstamp .pypi-tools*

src: .pypi-src
PyNUTClient: .pypi-src

# Tagged releases should only have three blocks of digits separated by dots
upload publish:
Expand All @@ -35,7 +55,7 @@ upload publish:
.pypi-src: test_nutclient.py.in PyNUT.py.in setup.py.in README.adoc Makefile $(top_srcdir)/LICENSE-GPL3
@echo " PYPI Generate PyPI module source"
@rm -rf $(GENERATED_SRC) "$@"
@mkdir src
@mkdir -p PyNUTClient
@for S in "$(srcdir)"/*.py.in ; do \
B="`basename "$${S}" .in`" ; \
if test x"$${B}" = xsetup.py ; then \
Expand All @@ -45,15 +65,15 @@ upload publish:
continue; \
fi; \
if test -s "$${B}" ; then \
cp -pf "$${B}" src/ || exit ; \
cp -pf "$${B}" PyNUTClient/ || exit ; \
continue; \
fi ; \
sed -e "s,[@]PYTHON[@],@PYTHON@," < "$(srcdir)/$${B}.in" > "src/$${B}" || exit ; \
if test -x "$(srcdir)/$${B}.in" ; then chmod +x "src/$${B}"; fi ; \
sed -e "s,[@]PYTHON[@],@PYTHON@," < "$(srcdir)/$${B}.in" > "PyNUTClient/$${B}" || exit ; \
if test -x "$(srcdir)/$${B}.in" ; then chmod +x "PyNUTClient/$${B}"; fi ; \
done ; \
cp -pf "$(srcdir)/README.adoc" README.txt || exit ; \
cp -pf "$(top_srcdir)/LICENSE-GPL3" . || exit ; \
echo "from . import PyNUT" > src/__init__.py || exit
echo "from . import PyNUT" > PyNUTClient/__init__.py || exit
@touch "$@"

.pypi-tools-python:
Expand Down
11 changes: 8 additions & 3 deletions scripts/python/module/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ setup(
name = "PyNUTClient",
version = '@NUT_SOURCE_GITREV_NUMERIC@',
author = "The Network UPS Tools project",
license_files = ('LICENSE-GPL3',),
license_files = ('LICENSE-GPL3',),
author_email = "[email protected]",
description = "Python client bindings for NUT",
url = "https://github.com/networkupstools/nut/tree/master/scripts/python/module",
long_description_content_type = "text/plain", # NOTE: No asciidoc so far, see https://packaging.python.org/en/latest/specifications/core-metadata/
long_description = long_description,
packages = find_packages(),
packages = find_packages(),
#py_modules = ['PyNUT'],
package_dir = {'PyNUT': 'PyNUTClient'},
data_files = [('', ['tox.ini'])],
#scripts = ['PyNUTClient/test_nutclient.py', 'PyNUTClient/__init__.py'],
python_requires = '>=2.6',
# install_requires = ['telnetlib'], # NOTE: telnetlib.py is part of Python core for tested 2.x and 3.x versions, not something 'pip' can download
keywords = ['pypi', 'cicd', 'python'],
keywords = ['pypi', 'cicd', 'python', 'nut', 'Network UPS Tools'],
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down
25 changes: 25 additions & 0 deletions scripts/python/module/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Configuration for Python test environment manager
# https://tox.wiki/en/latest/user_guide.html
#
# Note that to run `test_nutclient.py` you must prepare a running NUT data
# server (upsd) with a connected driver. You can use a dummy-ups driver for
# that, see e.g. NUT tests/NIT/nit.sh for how the test beds are prepared.
# Further you may need to export `NUT_HOST` and `NUT_PORT` (if not default),
# and a `NUT_USER` and `NUT_PASS` for tests with logged-in session behaviors.
# Then just run `tox` (may have to `pip install tox` first though).

[tox]
envlist =
py2{6,7}
py3{5,5,6,7,8,9,10,11,12,13}

[testenv]
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/src/PyNUTClient

commands_pre =
python -m pip install -U pip

commands =
python src/PyNUTClient/test_nutclient.py
#py.test --basetemp={envtmpdir}

0 comments on commit cb16ff1

Please sign in to comment.