aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pyproject.toml10
-rw-r--r--scripts/version.py34
-rw-r--r--snekbox/__init__.py6
3 files changed, 47 insertions, 3 deletions
diff --git a/pyproject.toml b/pyproject.toml
index ac28b6e..c839da5 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,10 +1,9 @@
[build-system]
-requires = ["setuptools>=61"]
-build-backend = "setuptools.build_meta"
+requires = ["setuptools>=61", "setuptools-git-versioning>=1.8"]
+build-backend = "setuptools.build_meta:__legacy__"
[project]
name = "snekbox"
-version = "1.0.0"
description = "HTTP REST API for sanboxed execution of arbitrary Python code."
readme = "README.md"
license = {text = "MIT"}
@@ -22,6 +21,7 @@ classifiers = [
"Topic :: Security",
"Topic :: Software Development :: Interpreters",
]
+dynamic = ["version"]
requires-python = ">=3.10"
dependencies = [
@@ -45,6 +45,10 @@ snekbox = "snekbox.__main__:main"
[tool.setuptools]
packages = ["snekbox"]
+[tool.setuptools-git-versioning]
+enabled = true
+version_callback = "scripts.version:get_version"
+
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
diff --git a/scripts/version.py b/scripts/version.py
new file mode 100644
index 0000000..9076b93
--- /dev/null
+++ b/scripts/version.py
@@ -0,0 +1,34 @@
+import datetime
+import subprocess
+
+__all__ = ("get_version",)
+
+
+def get_version() -> str:
+ """
+ Return a version based on the HEAD commit's date.
+
+ The format is 'year.month.day.commits' and is compliant with PEP 440. 'commits' is the amount of
+ commits made on the same date as HEAD, excluding HEAD. This ensures versions are unique if
+ multiple release occur on the same date.
+ """
+ args = ["git", "show", "-s", "--format=%ct", "HEAD"]
+ stdout = subprocess.check_output(args, text=True)
+ timestamp = float(stdout.strip())
+ date = datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)
+
+ commits = count_commits_on_date(date) - 1 # Exclude HEAD.
+
+ # Don't use strftime because it includes leading zeros, which are against PEP 440.
+ return f"{date.year}.{date.month}.{date.day}.{commits}"
+
+
+def count_commits_on_date(dt: datetime.datetime) -> int:
+ """Return the amount of commits made on the given UTC aware datetime."""
+ dt = dt.combine(dt - datetime.timedelta(days=1), dt.max.time(), dt.tzinfo)
+
+ # git log uses the committer date for this, not the author date.
+ args = ["git", "log", "--oneline", "--after", str(dt.timestamp())]
+ stdout = subprocess.check_output(args, text=True)
+
+ return stdout.strip().count("\n")
diff --git a/snekbox/__init__.py b/snekbox/__init__.py
index ccb8b11..a3a5f09 100644
--- a/snekbox/__init__.py
+++ b/snekbox/__init__.py
@@ -1,7 +1,13 @@
import os
+from importlib import metadata
DEBUG = os.environ.get("DEBUG", False)
+try:
+ __version__ = metadata.version("snekbox")
+except metadata.PackageNotFoundError: # pragma: no cover
+ __version__ = "0.0.0.0+unknown"
+
from snekbox.api import SnekAPI # noqa: E402
from snekbox.nsjail import NsJail # noqa: E402
from snekbox.utils.logging import init_logger, init_sentry # noqa: E402