From b5bc70815fe20fa901ca64fefe2fc637c865b646 Mon Sep 17 00:00:00 2001 From: jkeifer Date: Tue, 21 Dec 2021 17:43:47 -0800 Subject: [PATCH] setup.py will discover version from git if needed This might seem silly at first glance--for most things a default version of 0.0.0 is fine. However, given the tightly-coupled nature of cirrus-geo and cirrus-lib, we often want to install cirrus-lib to the same dev venv as cirrus-geo, which causes a conflict as cirrus-lib 0.0.0 is below the version specified as a dependency of cirrus-geo. As a result, any reinstallation of cirrus-geo to the venv will cause the local dev version of cirrus-lib to be uninstalled. If instead we find the most recent tag of cirrus-lib and base our version on that, then we do not have an issue--local development changes will simply be appended to the last tagged version and pip will be happy. --- .github/workflows/python-publish.yml | 3 +- setup.py | 46 +++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 4e1ef42..6982aa2 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -5,7 +5,7 @@ name: Upload Python Package on: release: - types: [created] + types: [published] jobs: deploy: @@ -24,6 +24,7 @@ jobs: pip install setuptools wheel twine - name: Build and publish env: + CIRRUS_VERSION: ${{ github.event.release.tag_name }} TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} run: | diff --git a/setup.py b/setup.py index 24b1b36..033e06d 100755 --- a/setup.py +++ b/setup.py @@ -1,12 +1,56 @@ #!/usr/bin/env python import os import os.path +import subprocess from setuptools import setup, find_namespace_packages HERE = os.path.abspath(os.path.dirname(__file__)) -VERSION = os.environ.get('CIRRUS_VERSION', '0.0.0') + + +# gets the version from the latest tag via git describe +# so we don't have to do anything to manage version number +# aside from tagging releases +def git_version(gitdir, default='0.0.0'): + try: + desc = subprocess.run( + [ + 'git', + '--git-dir', + gitdir, + 'describe', + '--long', + '--tags', + '--dirty', + ], + capture_output=True, + ) + except Exception: + return default + + if desc.returncode != 0: + return default + + # example output: v0.5.1-8-gb38722d-dirty + # parts are: + # 0 - last tag + # 1 - commits since last tag (0 if same commit as tag) + # 2 - short hash of current commit + # 3 - dirty (if repo state is dirty) + parts = desc.stdout.decode().strip().lstrip('v').split('-', maxsplit=2) + if int(parts[1]) > 0 or 'dirty' in parts[2]: + return f'{parts[0]}+{parts[1]}.{parts[2].replace("-",".")}' + else: + return parts[0] + + +# in the case of a tagged release, we +# are passed a version in an env var +VERSION = os.environ.get( + 'CIRRUS_VERSION', + git_version(os.path.join(HERE, '.git')), +) with open(os.path.join(HERE, 'README.md'), encoding='utf-8') as f: