Skip to content

Commit 7084308

Browse files
committed
build: Invert version fetching.
A lot of systems expect the version number to be in the package metadata so make that the source of truth and update the code to pull the version variable from there instead of the other way around.
1 parent 6035bdf commit 7084308

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

backend/docs/conf.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,13 @@
1818
from subprocess import check_call
1919

2020
from django import setup as django_setup
21-
22-
23-
def get_version(*file_paths):
24-
"""
25-
Extract the version string from the file.
26-
27-
Input:
28-
- file_paths: relative path fragments to file with
29-
version string
30-
"""
31-
filename = os.path.join(os.path.dirname(__file__), *file_paths)
32-
version_file = open(filename, encoding="utf8").read()
33-
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
34-
if version_match:
35-
return version_match.group(1)
36-
raise RuntimeError('Unable to find version string.')
21+
from importlib.metadata import version as get_version
3722

3823

3924
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
4025
sys.path.append(REPO_ROOT)
4126

42-
VERSION = get_version('../sample_plugin', '__init__.py')
27+
VERSION = get_version('openedx-sample-plugin')
4328
# Configure Django for autodoc usage
4429
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_settings'
4530
django_setup()

backend/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ name = "openedx-sample-plugin"
77
description = "A sample backend plugin for the Open edX Platform"
88
requires-python = ">=3.11"
99
license="Apache-2.0"
10+
version = "0.1.0"
1011
authors = [
1112
{name = "Open edX Project", email = "[email protected]"},
1213
]
@@ -24,7 +25,7 @@ keywords= [
2425
"edx",
2526
]
2627

27-
dynamic = ["version", "readme", "dependencies"]
28+
dynamic = ["readme", "dependencies"]
2829

2930
[project.entry-points."lms.djangoapp"]
3031
sample_plugin = "sample_plugin.apps:SamplePluginConfig"
@@ -37,7 +38,6 @@ Homepage = "https://openedx.org/openedx/sample-plugin"
3738
Repository = "https://openedx.org/openedx/sample-plugin"
3839

3940
[tool.setuptools.dynamic]
40-
version = {attr = "sample_plugin.__version__"}
4141
readme = {file = ["README.rst", "CHANGELOG.rst"]}
4242
dependencies = {file = "requirements/base.in"}
4343

backend/sample_plugin/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
A sample backend plugin for the Open edX Platform.
33
"""
44

5-
__version__ = "0.1.0"
5+
from importlib.metadata import version as get_version
6+
7+
# The name of the package is `openedx-sample-plugin` but __package__ is `sample_plugin` so we hardcode the name of the
8+
# package here so that the version fetching works correctly. A lot of examples will show using `__package__`.
9+
__version__ = get_version('openedx-sample-plugin')

0 commit comments

Comments
 (0)