Skip to content

Commit 1241bec

Browse files
committed
Improved version check.
1 parent 322cd9f commit 1241bec

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

ctf/__init__.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,67 @@
11
#!/usr/bin/env python3
22
import importlib.metadata
3+
import json
34
import logging
45
import os
6+
import sys
7+
import urllib.request
58

69
import coloredlogs
710

11+
VERSION = importlib.metadata.version("ctf-script")
12+
13+
if len(sys.argv) > 1 and sys.argv[1] == "version":
14+
print(VERSION)
15+
exit(code=0)
16+
817
ENV = {}
918
for k, v in os.environ.items():
1019
ENV[k] = v
1120

12-
VERSION = importlib.metadata.version("ctf-script")
13-
1421
LOG = logging.getLogger()
1522
LOG.setLevel(level=logging.DEBUG)
1623
coloredlogs.install(level="DEBUG", logger=LOG)
1724

1825

26+
def check_tool_version():
27+
with urllib.request.urlopen(
28+
url="https://api.github.com/repos/nsec/ctf-script/releases/latest"
29+
) as r:
30+
if r.getcode() != 200:
31+
LOG.debug(r.read().decode())
32+
LOG.error("Could not verify the latest release.")
33+
else:
34+
try:
35+
latest_version = json.loads(s=r.read().decode())["tag_name"]
36+
except Exception as e:
37+
LOG.debug(e)
38+
LOG.error("Could not verify the latest release.")
39+
40+
compare = 0
41+
for current_part, latest_part in zip(
42+
[int(part) for part in VERSION.split(".")],
43+
[int(part) for part in latest_version.split(".")],
44+
):
45+
if current_part < latest_part:
46+
compare = -1
47+
break
48+
elif current_part > latest_part:
49+
compare = 1
50+
break
51+
52+
match compare:
53+
case 0 | 1:
54+
LOG.debug("Script is up to date.")
55+
case -1:
56+
LOG.critical(
57+
"Script is outdated. Please update to the latest release before continuing."
58+
)
59+
exit(code=1)
60+
61+
62+
check_tool_version()
63+
64+
1965
def find_ctf_root_directory() -> str:
2066
path = os.path.join(os.getcwd(), ".")
2167

ctf/__main__.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import yaml
1818
from tabulate import tabulate
1919

20-
from ctf import CTF_ROOT_DIRECTORY, ENV, LOG, VERSION
20+
from ctf import CTF_ROOT_DIRECTORY, ENV, LOG
2121
from ctf.utils import (
2222
add_tracks_to_terraform_modules,
2323
available_incus_remotes,
@@ -1136,11 +1136,6 @@ def write_badge(name: str, svg: str) -> None:
11361136
f.write(svg)
11371137

11381138

1139-
def version(args: argparse.Namespace) -> None:
1140-
print(VERSION)
1141-
exit(code=0)
1142-
1143-
11441139
def main():
11451140
# Command line parsing.
11461141
parser = argparse.ArgumentParser(
@@ -1150,11 +1145,11 @@ def main():
11501145

11511146
subparsers = parser.add_subparsers(required=True)
11521147

1153-
parser_version = subparsers.add_parser(
1148+
# Create a fake subparser as the version is printed before anything else in the __init__.py file.
1149+
subparsers.add_parser(
11541150
"version",
11551151
help="Script version.",
11561152
)
1157-
parser_version.set_defaults(func=version)
11581153

11591154
parser_flags = subparsers.add_parser(
11601155
"flags",
@@ -1367,9 +1362,6 @@ def main():
13671362

13681363
args = parser.parse_args()
13691364

1370-
if args.func.__name__ == "version":
1371-
args.func(args=args)
1372-
13731365
if "remote" in args and args.remote:
13741366
ENV["INCUS_REMOTE"] = args.remote
13751367

ctf/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Any, Generator
66

77
import jinja2
8-
import tomllib
98
import yaml
109

1110
from ctf import CTF_ROOT_DIRECTORY

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies = [
1818
"black",
1919
"tabulate==0.9.0",
2020
]
21-
version = "2025.5.0"
21+
version = "1.0.1"
2222
classifiers = [
2323
"Programming Language :: Python :: 3",
2424
"Operating System :: OS Independent",

0 commit comments

Comments
 (0)