From 1697e15e523516bc0e9d3778832769020382f3c3 Mon Sep 17 00:00:00 2001 From: Richard Schwab Date: Thu, 7 Apr 2022 22:09:11 +0200 Subject: [PATCH] Use setuptools-scm for versioning (#748) Use setuptools-scm for versioning - generate aiomysql/_scm_version.py at build time, fallback to `unknown` if it doesn't exist - include setuptools-scm-git-archive to support versioning in archives from git tags - install aiomysql package in readthedocs and access generated version to embed in docs - fetch git context in workflow to ensure we have all information for setuptools-scm to work - fetch git context in `make doc` Split from #734, including suggestions from @webknjaz Co-authored-by: Sviatoslav Sydorenko --- .git_archival.txt | 1 + .gitattributes | 5 +++++ .github/workflows/ci.yml | 2 ++ .readthedocs.yaml | 6 ++++-- CHANGES.txt | 1 + MANIFEST.in | 5 ----- Makefile | 9 +++++++++ aiomysql/.gitignore | 1 + aiomysql/__init__.py | 3 ++- aiomysql/_scm_version.pyi | 3 +++ aiomysql/_version.py | 4 ++++ docs/conf.py | 25 ++++--------------------- pyproject.toml | 7 +++++++ 13 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 .git_archival.txt create mode 100644 .gitattributes delete mode 100644 MANIFEST.in create mode 100644 aiomysql/.gitignore create mode 100644 aiomysql/_scm_version.pyi create mode 100644 aiomysql/_version.py diff --git a/.git_archival.txt b/.git_archival.txt new file mode 100644 index 00000000..95cb3eea --- /dev/null +++ b/.git_archival.txt @@ -0,0 +1 @@ +ref-names: $Format:%D$ diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..ec8c3333 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Force LF line endings for text files +* text=auto eol=lf + +# Needed for setuptools-scm-git-archive +.git_archival.txt export-subst diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1c73562..ab2a59f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,6 +58,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Setup Python ${{ matrix.py }} uses: actions/setup-python@v3 diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 9ac30d0d..5fba73ba 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -19,5 +19,7 @@ formats: - epub python: - install: - - requirements: requirements-dev.txt + install: + - requirements: requirements-dev.txt + - method: pip + path: . diff --git a/CHANGES.txt b/CHANGES.txt index 6aaec769..5c8d7b73 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -24,6 +24,7 @@ To be included in 1.0.0 (unreleased) * Required python version is now properly documented in python_requires instead of failing on setup.py execution #731 * Add rsa extras_require depending on PyMySQL[rsa] #557 * Migrate to PEP 517 build system #746 +* Self-reported `__version__` now returns version generated by `setuptools-scm` during build, otherwise `'unknown'` #748 0.0.22 (2021-11-14) diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 95727af4..00000000 --- a/MANIFEST.in +++ /dev/null @@ -1,5 +0,0 @@ -include LICENSE -include CHANGES.txt -include README.rst -graft aiomysql -global-exclude *.pyc *.swp diff --git a/Makefile b/Makefile index 3b0716a8..ae5ceeb0 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,16 @@ start_mysql: stop_mysql: docker-compose -f docker-compose.yml stop mysql +# TODO: this depends on aiomysql being installed, e.g. in a venv. +# TODO: maybe this can be solved better. doc: + @echo "----------------------------------------------------------------" + @echo "Doc builds require installing the aiomysql package in the" + @echo "environment. Make sure you've installed your current dev version" + @echo "into your environment, e.g. using venv, then run this command in" + @echo "the virtual environment." + @echo "----------------------------------------------------------------" + git fetch --tags --all make -C docs html @echo "open file://`pwd`/docs/_build/html/index.html" diff --git a/aiomysql/.gitignore b/aiomysql/.gitignore new file mode 100644 index 00000000..91cb299f --- /dev/null +++ b/aiomysql/.gitignore @@ -0,0 +1 @@ +/_scm_version.py diff --git a/aiomysql/__init__.py b/aiomysql/__init__.py index 1b08ab56..a367fcd2 100644 --- a/aiomysql/__init__.py +++ b/aiomysql/__init__.py @@ -32,8 +32,9 @@ from .connection import Connection, connect from .cursors import Cursor, SSCursor, DictCursor, SSDictCursor from .pool import create_pool, Pool +from ._version import version -__version__ = '0.0.22' +__version__ = version __all__ = [ diff --git a/aiomysql/_scm_version.pyi b/aiomysql/_scm_version.pyi new file mode 100644 index 00000000..deb2e36a --- /dev/null +++ b/aiomysql/_scm_version.pyi @@ -0,0 +1,3 @@ +# This stub file is necessary because `_scm_version.py` +# autogenerated on build and absent on mypy checks time +version: str diff --git a/aiomysql/_version.py b/aiomysql/_version.py new file mode 100644 index 00000000..80af3cd2 --- /dev/null +++ b/aiomysql/_version.py @@ -0,0 +1,4 @@ +try: + from ._scm_version import version +except ImportError: + version = "unknown" diff --git a/docs/conf.py b/docs/conf.py index 4a4019e1..027c81c5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -30,25 +30,8 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -import re, os.path - -def get_release(): - regexp = re.compile(r"^__version__\W*=\W*'([\d.abrc]+)'") - here = os.path.dirname(__file__) - root = os.path.dirname(here) - init_py = os.path.join(root, 'aiomysql', '__init__.py') - with open(init_py) as f: - for line in f: - match = regexp.match(line) - if match is not None: - return match.group(1) - else: - raise RuntimeError('Cannot find version in aiomysql/__init__.py') - - -def get_version(release): - parts = release.split('.') - return '.'.join(parts[:2]) +from aiomysql import __version__ + extensions = [ 'sphinx.ext.autodoc', @@ -82,8 +65,8 @@ def get_version(release): # |version| and |release|, also used in various other places throughout the # built documents. # -release = get_release() -version = get_version(release) +release = __version__ +version = '.'.join(__version__.split('.')[:2]) # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/pyproject.toml b/pyproject.toml index 05e4daa9..4e903b7d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,5 +2,12 @@ requires = [ # Essentials "setuptools >= 42", + + # Plugins + "setuptools_scm[toml] >= 6.4", + "setuptools_scm_git_archive >= 1.1", ] build-backend = "setuptools.build_meta" + +[tool.setuptools_scm] +write_to = "aiomysql/_scm_version.py"