Skip to content

Commit 75c5787

Browse files
committed
restore python code for doc versioning
1 parent c96e1e8 commit 75c5787

File tree

8 files changed

+176
-8
lines changed

8 files changed

+176
-8
lines changed

Pipfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ verify_ssl = true
66
[dev-packages]
77
# Pinning black stops us having to allow pre-releases globally
88
black = "==19.10b0"
9-
109
# Pins to make lockfile usable on multiple Python versions and platforms
1110
mypy = "*"
1211
atomicwrites = "*"
1312
typing-extensions = "*"
1413
importlib-metadata = "*"
15-
1614
# Switch to main repo after PR https://github.com/Holzhaus/sphinx-multiversion/pull/64 is merged
1715
sphinx-multiversion = {editable = true,git = "https://github.com/dls-controls/sphinx-multiversion.git",ref = "only-arg"}
18-
1916
# List wheel here to make sure we make the same version to make wheels on gitlab and github CI
2017
wheel = "0.33.1"
21-
2218
# Test and docs dependencies
2319
pytest-cov = "*"
2420
pytest-mypy = "*"
@@ -30,7 +26,7 @@ sphinx-rtd-theme = "*"
3026

3127
[packages]
3228
# All other package requirements from setup.cfg
33-
k8s_epics_docs = {editable = true,path = "."}
29+
k8s_epics_docs = {editable = true, path = "."}
3430

3531
[scripts]
3632
tests = "python -m pytest"

docs/conf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import os
1313
import sys
1414

15-
1615
sys.path.insert(0, os.path.abspath(os.path.join(__file__, "..", "..")))
1716

18-
import k8s_epics_docs # noqa
1917

2018
# -- General configuration ------------------------------------------------
2119

@@ -30,7 +28,7 @@
3028
# The short X.Y version.
3129
if "+" in release:
3230
# Not on a tag
33-
version = "master"
31+
version = "main"
3432
else:
3533
version = release
3634

k8s_epics_docs/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from k8s_epics_docs._version_git import __version__
2+
3+
__all__ = ["__version__"]

k8s_epics_docs/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from k8s_epics_docs import cli
2+
3+
# test with:
4+
# pipenv run python -m k8s_epics_docs
5+
if __name__ == "__main__":
6+
cli.main()

k8s_epics_docs/_version_git.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Compute a version number from a git repo or archive
2+
3+
# This file is released into the public domain. Generated by:
4+
# versiongit-1.0 (https://github.com/dls-controls/versiongit)
5+
import os
6+
import re
7+
import sys
8+
from subprocess import STDOUT, CalledProcessError, check_output
9+
10+
# These will be filled in if git archive is run or by setup.py cmdclasses
11+
GIT_REFS = "$Format:%D$"
12+
GIT_SHA1 = "$Format:%h$"
13+
14+
# Git describe gives us sha1, last version-like tag, and commits since then
15+
CMD = "git describe --tags --dirty --always --long --match=[0-9]*[-.][0-9]*"
16+
17+
18+
def get_version_from_git(path=None):
19+
"""Try to parse version from git describe, fallback to git archive tags"""
20+
tag, plus, suffix = "0.0", "untagged", ""
21+
if not GIT_SHA1.startswith("$"):
22+
# git archive or the cmdclasses below have filled in these strings
23+
sha1 = GIT_SHA1
24+
for ref_name in GIT_REFS.split(", "):
25+
if ref_name.startswith("tag: "):
26+
# git from 1.8.3 onwards labels archive tags "tag: TAGNAME"
27+
tag, plus = ref_name[5:], "0"
28+
else:
29+
if path is None:
30+
# If no path to git repo, choose the directory this file is in
31+
path = os.path.dirname(os.path.abspath(__file__))
32+
# output is TAG-NUM-gHEX[-dirty] or HEX[-dirty]
33+
try:
34+
cmd_out = check_output(CMD.split(), stderr=STDOUT, cwd=path)
35+
except Exception as e:
36+
sys.stderr.write("%s: %s\n" % (type(e).__name__, str(e)))
37+
if isinstance(e, CalledProcessError):
38+
sys.stderr.write("-> %s" % e.output.decode())
39+
return "0.0+unknown", None, e
40+
else:
41+
out = cmd_out.decode().strip()
42+
if out.endswith("-dirty"):
43+
out = out[:-6]
44+
suffix = ".dirty"
45+
if "-" in out:
46+
# There is a tag, extract it and the other pieces
47+
match = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", out)
48+
tag, plus, sha1 = match.groups()
49+
else:
50+
# No tag, just sha1
51+
sha1 = out
52+
# Replace dashes in tag for dots
53+
tag = tag.replace("-", ".")
54+
if plus != "0" or suffix:
55+
# Not on a tag, add additional info
56+
tag = "%(tag)s+%(plus)s.g%(sha1)s%(suffix)s" % locals()
57+
return tag, sha1, None
58+
59+
60+
__version__, git_sha1, git_error = get_version_from_git()
61+
62+
63+
def get_cmdclass(build_py=None, sdist=None):
64+
"""Create cmdclass dict to pass to setuptools.setup that will write a
65+
_version_static.py file in our resultant sdist, wheel or egg"""
66+
if build_py is None:
67+
from setuptools.command.build_py import build_py
68+
if sdist is None:
69+
from setuptools.command.sdist import sdist
70+
71+
def make_version_static(base_dir, pkg):
72+
vg = os.path.join(base_dir, pkg.split(".")[0], "_version_git.py")
73+
if os.path.isfile(vg):
74+
lines = open(vg).readlines()
75+
with open(vg, "w") as f:
76+
for line in lines:
77+
# Replace GIT_* with static versions
78+
if line.startswith("GIT_SHA1 = "):
79+
f.write("GIT_SHA1 = '%s'\n" % git_sha1)
80+
elif line.startswith("GIT_REFS = "):
81+
f.write("GIT_REFS = 'tag: %s'\n" % __version__)
82+
else:
83+
f.write(line)
84+
85+
class BuildPy(build_py):
86+
def run(self):
87+
build_py.run(self)
88+
for pkg in self.packages:
89+
make_version_static(self.build_lib, pkg)
90+
91+
class Sdist(sdist):
92+
def make_release_tree(self, base_dir, files):
93+
sdist.make_release_tree(self, base_dir, files)
94+
for pkg in self.distribution.packages:
95+
make_version_static(base_dir, pkg)
96+
97+
return dict(build_py=BuildPy, sdist=Sdist)

k8s_epics_docs/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from argparse import ArgumentParser
2+
3+
from k8s_epics_docs import __version__
4+
5+
6+
def main(args=None):
7+
parser = ArgumentParser()
8+
parser.add_argument("--version", action="version", version=__version__)
9+
args = parser.parse_args(args)
10+
11+
# dummy CLI entry point

setup.cfg

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[metadata]
2+
name = k8s_epics_docs
3+
description = One line description of your module
4+
url = https://github.com/epics-containers/k8s_epics_docs
5+
author = Giles Knap
6+
author_email = [email protected]
7+
license = Apache License 2.0
8+
long_description = file: README.rst
9+
long_description_content_type = text/x-rst
10+
classifiers =
11+
Development Status :: 4 - Beta
12+
License :: OSI Approved :: Apache Software License
13+
Programming Language :: Python :: 3.7
14+
Programming Language :: Python :: 3.8
15+
Programming Language :: Python :: 3.9
16+
17+
[options]
18+
packages = find:
19+
# install_requires =
20+
# numpy
21+
# scipy
22+
23+
# If you want to include data files in packages,
24+
# either define [options.package_data] or
25+
# set this to True and include a MANIFEST.in file.
26+
include_package_data = False
27+
28+
[options.packages.find]
29+
# Don't include our tests directory in the distribution
30+
exclude = tests
31+
32+
[tool:pytest]
33+
# Run pytest with all our checkers, and don't spam us with massive tracebacks on error
34+
addopts =
35+
--tb=native -vv --flake8 --black --mypy --doctest-modules --doctest-glob="*.rst"
36+
--cov=k8s_epics_docs --cov-report term --cov-report xml:cov.xml
37+
38+
[coverage:run]
39+
# This is covered in the versiongit test suite so exclude it here
40+
omit = */_version_git.py

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
import sys
3+
4+
from setuptools import setup
5+
6+
7+
# Place the directory containing _version_git on the path
8+
for path, _, filenames in os.walk(os.path.dirname(os.path.abspath(__file__))):
9+
if "_version_git.py" in filenames:
10+
sys.path.append(path)
11+
break
12+
13+
from _version_git import __version__, get_cmdclass # noqa
14+
15+
# Setup information is stored in setup.cfg but this function call
16+
# is still necessary.
17+
setup(cmdclass=get_cmdclass(), version=__version__)

0 commit comments

Comments
 (0)