Skip to content

Commit 11cbe72

Browse files
committed
Introduce development version numbers.
This prevents developement versions of websocket from advertising themselves as the previous release while they may contain backwards-incompatible changes since that release.
1 parent e6b8522 commit 11cbe72

File tree

2 files changed

+79
-16
lines changed

2 files changed

+79
-16
lines changed

docs/conf.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
copyright = f"2013-{datetime.date.today().year}, Aymeric Augustin and contributors"
2626
author = "Aymeric Augustin"
2727

28-
# The full version, including alpha/beta/rc tags
29-
release = "10.0"
28+
from websockets.version import tag as version, version as release
3029

3130

3231
# -- General configuration ---------------------------------------------------
@@ -91,20 +90,9 @@
9190
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
9291

9392
# Configure viewcode extension.
94-
try:
95-
git_sha1 = subprocess.run(
96-
"git rev-parse --short HEAD",
97-
capture_output=True,
98-
shell=True,
99-
check=True,
100-
text=True,
101-
).stdout.strip()
102-
except subprocess.SubprocessError as exc:
103-
print("Cannot get git commit, disabling linkcode:", exc)
104-
extensions.remove("sphinx.ext.linkcode")
105-
else:
106-
code_url = f"https://github.com/aaugustin/websockets/blob/{git_sha1}"
93+
from websockets.version import commit
10794

95+
code_url = f"https://github.com/aaugustin/websockets/blob/{commit}"
10896

10997
def linkcode_resolve(domain, info):
11098
assert domain == "py"

src/websockets/version.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1-
version = "10.0"
1+
from __future__ import annotations
2+
3+
4+
__all__ = ["tag", "version", "commit"]
5+
6+
7+
# ========= =========== ===================
8+
# release development
9+
# ========= =========== ===================
10+
# tag X.Y X.Y (upcoming)
11+
# version X.Y X.Y.dev1+g5678cde
12+
# commit X.Y 5678cde
13+
# ========= =========== ===================
14+
15+
16+
# When tagging a release, set `released = True`.
17+
# After tagging a release, set `released = False` and increment `tag`.
18+
19+
released = False
20+
21+
tag = version = commit = "10.1"
22+
23+
24+
if not released: # pragma: no cover
25+
import pathlib
26+
import re
27+
import subprocess
28+
29+
def get_version(tag: str) -> str:
30+
# Since setup.py executes the contents of src/websockets/version.py,
31+
# __file__ can point to either of these two files.
32+
file_path = pathlib.Path(__file__)
33+
root_dir = file_path.parents[0 if file_path.name == "setup.py" else 2]
34+
35+
# Read version from git if available. This prevents reading stale
36+
# information from src/websockets.egg-info after building a sdist.
37+
try:
38+
description = subprocess.run(
39+
["git", "describe", "--dirty", "--tags", "--long"],
40+
capture_output=True,
41+
cwd=root_dir,
42+
check=True,
43+
text=True,
44+
).stdout.strip()
45+
except subprocess.CalledProcessError:
46+
pass
47+
else:
48+
description_re = r"[0-9.]+-([0-9]+)-(g[0-9a-f]{7}(?:-dirty)?)"
49+
match = re.fullmatch(description_re, description)
50+
assert match is not None
51+
distance, remainder = match.groups()
52+
remainder = remainder.replace("-", ".") # required by PEP 440
53+
return f"{tag}.dev{distance}+{remainder}"
54+
55+
# Read version from package metadata if it is installed.
56+
try:
57+
import importlib.metadata # move up when dropping Python 3.7
58+
59+
return importlib.metadata.version("websockets")
60+
except ImportError:
61+
pass
62+
63+
# Avoid crashing if the development version cannot be determined.
64+
return f"{tag}.dev0+gunknown"
65+
66+
version = get_version(tag)
67+
68+
def get_commit(tag: str, version: str) -> str:
69+
# Extract commit from version, falling back to tag if not available.
70+
version_re = r"[0-9.]+\.dev[0-9]+\+g([0-9a-f]{7}|unknown)(?:\.dirty)?"
71+
match = re.fullmatch(version_re, version)
72+
assert match is not None
73+
(commit,) = match.groups()
74+
return tag if commit == "unknown" else commit
75+
76+
commit = get_commit(tag, version)

0 commit comments

Comments
 (0)