Skip to content

Commit 322cd9f

Browse files
committed
Added version command and revamped a few things
1 parent 0b66154 commit 322cd9f

File tree

5 files changed

+63
-52
lines changed

5 files changed

+63
-52
lines changed

ctf/__init__.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python3
2+
import importlib.metadata
3+
import logging
4+
import os
5+
6+
import coloredlogs
7+
8+
ENV = {}
9+
for k, v in os.environ.items():
10+
ENV[k] = v
11+
12+
VERSION = importlib.metadata.version("ctf-script")
13+
14+
LOG = logging.getLogger()
15+
LOG.setLevel(level=logging.DEBUG)
16+
coloredlogs.install(level="DEBUG", logger=LOG)
17+
18+
19+
def find_ctf_root_directory() -> str:
20+
path = os.path.join(os.getcwd(), ".")
21+
22+
while path != (path := os.path.dirname(p=path)):
23+
dir = os.listdir(path=path)
24+
25+
if ".git" not in dir:
26+
continue
27+
if ".deploy" not in dir:
28+
continue
29+
if "challenges" not in dir:
30+
continue
31+
break
32+
33+
if path == "/":
34+
if "CTF_ROOT_DIR" not in os.environ:
35+
LOG.critical(
36+
msg='Could not automatically find the root directory nor the "CTF_ROOT_DIR" environment variable.'
37+
)
38+
exit(1)
39+
return os.environ.get("CTF_ROOT_DIR", default=".")
40+
41+
LOG.debug(msg=f"Found root directory: {path}")
42+
return path
43+
44+
45+
CTF_ROOT_DIRECTORY = find_ctf_root_directory()

ctf/__main__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import csv
44
import io
55
import json
6-
import logging
76
import os
87
import re
98
import secrets
@@ -14,17 +13,16 @@
1413
from enum import Enum, unique
1514

1615
import argcomplete
17-
import coloredlogs
1816
import jinja2
1917
import yaml
2018
from tabulate import tabulate
2119

20+
from ctf import CTF_ROOT_DIRECTORY, ENV, LOG, VERSION
2221
from ctf.utils import (
2322
add_tracks_to_terraform_modules,
2423
available_incus_remotes,
2524
check_git_lfs,
2625
create_terraform_modules_file,
27-
find_ctf_root_directory,
2826
get_all_available_tracks,
2927
get_ctf_script_schemas_directory,
3028
get_ctf_script_templates_directory,
@@ -47,13 +45,6 @@
4745
except ImportError:
4846
_has_pybadges = False
4947

50-
LOG = logging.getLogger()
51-
LOG.setLevel(level=logging.DEBUG)
52-
coloredlogs.install(level="DEBUG", logger=LOG)
53-
54-
ENV = {}
55-
56-
CTF_ROOT_DIRECTORY = find_ctf_root_directory()
5748
TEMPLATES_ROOT_DIRECTORY = get_ctf_script_templates_directory()
5849
SCHEMAS_ROOT_DIRECTORY = get_ctf_script_schemas_directory()
5950
AVAILABLE_INCUS_REMOTES = available_incus_remotes()
@@ -1145,6 +1136,11 @@ def write_badge(name: str, svg: str) -> None:
11451136
f.write(svg)
11461137

11471138

1139+
def version(args: argparse.Namespace) -> None:
1140+
print(VERSION)
1141+
exit(code=0)
1142+
1143+
11481144
def main():
11491145
# Command line parsing.
11501146
parser = argparse.ArgumentParser(
@@ -1154,6 +1150,12 @@ def main():
11541150

11551151
subparsers = parser.add_subparsers(required=True)
11561152

1153+
parser_version = subparsers.add_parser(
1154+
"version",
1155+
help="Script version.",
1156+
)
1157+
parser_version.set_defaults(func=version)
1158+
11571159
parser_flags = subparsers.add_parser(
11581160
"flags",
11591161
help="Get flags from tracks",
@@ -1365,8 +1367,8 @@ def main():
13651367

13661368
args = parser.parse_args()
13671369

1368-
for k, v in os.environ.items():
1369-
ENV[k] = v
1370+
if args.func.__name__ == "version":
1371+
args.func(args=args)
13701372

13711373
if "remote" in args and args.remote:
13721374
ENV["INCUS_REMOTE"] = args.remote

ctf/utils.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import logging
21
import os
32
import re
43
import subprocess
54
import textwrap
65
from typing import Any, Generator
76

8-
import coloredlogs
97
import jinja2
8+
import tomllib
109
import yaml
1110

12-
LOG = logging.getLogger()
13-
LOG.addHandler(hdlr=logging.StreamHandler())
14-
LOG.setLevel(level=logging.DEBUG)
15-
coloredlogs.install(level="DEBUG", logger=LOG)
11+
from ctf import CTF_ROOT_DIRECTORY
1612

1713

1814
def available_incus_remotes() -> list[str]:
@@ -31,35 +27,6 @@ def check_git_lfs() -> bool:
3127
return not bool(subprocess.run(args=["git", "lfs"], capture_output=True).returncode)
3228

3329

34-
def find_ctf_root_directory() -> str:
35-
path = os.path.join(os.getcwd(), ".")
36-
37-
while path != (path := os.path.dirname(p=path)):
38-
dir = os.listdir(path=path)
39-
40-
if ".git" not in dir:
41-
continue
42-
if ".deploy" not in dir:
43-
continue
44-
if "challenges" not in dir:
45-
continue
46-
break
47-
48-
if path == "/":
49-
if "CTF_ROOT_DIR" not in os.environ:
50-
LOG.critical(
51-
msg='Could not automatically find the root directory nor the "CTF_ROOT_DIR" environment variable.'
52-
)
53-
exit(1)
54-
return os.environ.get("CTF_ROOT_DIR", default=".")
55-
56-
LOG.debug(msg=f"Found root directory: {path}")
57-
return path
58-
59-
60-
CTF_ROOT_DIRECTORY = find_ctf_root_directory()
61-
62-
6330
def get_all_available_tracks() -> set[str]:
6431
tracks = set()
6532

ctf/validators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
import re
55
from dataclasses import dataclass
66

7+
from ctf import CTF_ROOT_DIRECTORY
78
from ctf.utils import (
8-
find_ctf_root_directory,
99
get_all_file_paths_recursively,
1010
parse_post_yamls,
1111
parse_track_yaml,
1212
remove_ctf_script_root_directory_from_path,
1313
)
1414

15-
CTF_ROOT_DIRECTORY = find_ctf_root_directory()
16-
1715

1816
@dataclass
1917
class ValidationError:

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 = "1.0.0"
21+
version = "2025.5.0"
2222
classifiers = [
2323
"Programming Language :: Python :: 3",
2424
"Operating System :: OS Independent",

0 commit comments

Comments
 (0)