Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
setup.py will discover version from git if needed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkeifer committed Dec 22, 2021
1 parent 5c016a7 commit b5bc708
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Upload Python Package

on:
release:
types: [created]
types: [published]

jobs:
deploy:
Expand All @@ -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: |
Expand Down
46 changes: 45 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit b5bc708

Please sign in to comment.