Skip to content

Commit 510ada1

Browse files
committed
Minor improvements
1 parent 22451b6 commit 510ada1

6 files changed

Lines changed: 108 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ release/
44
release.tar.gz
55
release-v*.tar.gz
66
.lsp
7+
__pycache__
78
.env

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ preview:
44
@pnpm --dir preview install --silent
55
@cd preview && pnpm dev
66

7-
build-private:
7+
build-private: drop-release
88
@$(PYTHON) test_solutions.py private
99

10+
drop-release:
11+
@rm -rf release
12+
1013
check-names:
1114
@$(PYTHON) check_task_names.py
1215

13-
build: check-names
16+
build: check-names drop-release
1417
@$(PYTHON) test_solutions.py tasks
1518

1619
build-and-preview: build preview

check_task_names.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
Check uniqueness of task names across tasks/ and private/ directories.
44
Task names are checked case-insensitively to prevent confusion.
5+
Also verifies that file names match the 'name' field in each TOML file.
56
"""
67

78
import os
@@ -31,16 +32,22 @@ def find_toml_files(directory: str) -> List[str]:
3132
return toml_files
3233

3334

34-
def extract_task_names(toml_files: List[str], base_dir: str) -> Dict[str, List[str]]:
35+
def extract_task_names(
36+
toml_files: List[str], base_dir: str
37+
) -> Tuple[Dict[str, List[str]], List[Tuple[str, str, str]]]:
3538
"""
3639
Extract task names from TOML files.
37-
Returns mapping of lowercase_name -> [file_paths]
40+
Returns:
41+
- mapping of lowercase_name -> [file_paths]
42+
- list of mismatches: [(file_path, file_name, toml_name), ...]
3843
Raises ValueError if any file is missing 'name' field.
3944
"""
4045
name_to_files = defaultdict(list)
46+
mismatches = []
4147

4248
for toml_path in toml_files:
4349
rel_path = os.path.relpath(toml_path, base_dir)
50+
file_name = os.path.basename(toml_path).replace(".toml", "")
4451

4552
# Parse TOML file
4653
try:
@@ -60,11 +67,15 @@ def extract_task_names(toml_files: List[str], base_dir: str) -> Dict[str, List[s
6067
)
6168
sys.exit(1)
6269

70+
# Check if file name matches the name field
71+
if file_name != name:
72+
mismatches.append((rel_path, file_name, name))
73+
6374
# Store with lowercase key for case-insensitive comparison
6475
lowercase_name = name.lower()
6576
name_to_files[lowercase_name].append(toml_path)
6677

67-
return dict(name_to_files)
78+
return dict(name_to_files), mismatches
6879

6980

7081
def check_uniqueness(
@@ -87,11 +98,11 @@ def check_uniqueness(
8798
}
8899

89100
if not duplicates:
90-
print(colored("All task names are unique!", "green", attrs=["bold"]))
101+
print(colored("All task names are unique!", "green"))
91102
return (True, total_files, 0)
92103

93104
# Report duplicates
94-
print(colored("DUPLICATE TASK NAMES FOUND:", "red", attrs=["bold"]))
105+
print(colored("DUPLICATE TASK NAMES FOUND:", "red", attrs=["bold"]))
95106
print()
96107

97108
for name, files in sorted(duplicates.items()):
@@ -103,7 +114,7 @@ def check_uniqueness(
103114

104115
# Print summary
105116
duplicate_file_count = sum(len(files) for files in duplicates.values())
106-
print(colored("" * 70, "blue"))
117+
print(colored("=" * 70, "blue"))
107118
print(
108119
colored(
109120
f"Summary: Found {len(duplicates)} duplicate task name(s) across {duplicate_file_count} files",
@@ -115,6 +126,36 @@ def check_uniqueness(
115126
return (False, total_files, len(duplicates))
116127

117128

129+
def check_filename_matches(mismatches: List[Tuple[str, str, str]]) -> bool:
130+
"""
131+
Check for file name / TOML name mismatches and print report.
132+
Returns True if all match, False otherwise.
133+
"""
134+
if not mismatches:
135+
print(colored("All file names match their TOML 'name' field!", "green"))
136+
return True
137+
138+
print(colored("FILE NAME MISMATCHES FOUND:", "red", attrs=["bold"]))
139+
print()
140+
141+
for rel_path, file_name, toml_name in sorted(mismatches):
142+
print(colored(f"File: {rel_path}", "yellow"))
143+
print(f" file name: '{file_name}'")
144+
print(f" toml name: '{toml_name}'")
145+
print()
146+
147+
print(colored("=" * 70, "blue"))
148+
print(
149+
colored(
150+
f"Summary: Found {len(mismatches)} file name mismatch(es)",
151+
"red",
152+
attrs=["bold"],
153+
)
154+
)
155+
156+
return False
157+
158+
118159
def main():
119160
"""Main entry point."""
120161
# Get base directory (project root)
@@ -133,18 +174,22 @@ def main():
133174
print(colored("WARNING: No TOML files found in tasks/ or private/", "yellow"))
134175
sys.exit(0)
135176

136-
# Extract task names (case-insensitive)
177+
# Extract task names (case-insensitive) and check for file name mismatches
137178
try:
138-
name_to_files = extract_task_names(toml_files, base_dir)
179+
name_to_files, mismatches = extract_task_names(toml_files, base_dir)
139180
except Exception as e:
140181
print(colored(f"ERROR: {str(e)}", "red"))
141182
sys.exit(1)
142183

143184
# Check for duplicates
144185
is_unique, total_files, duplicate_count = check_uniqueness(name_to_files, base_dir)
145186

187+
# Check for file name mismatches
188+
print()
189+
names_match = check_filename_matches(mismatches)
190+
146191
# Exit with appropriate code
147-
sys.exit(0 if is_unique else 1)
192+
sys.exit(0 if (is_unique and names_match) else 1)
148193

149194

150195
if __name__ == "__main__":

push_task_packs.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,41 @@ def publish_task_pack(
9898
return False
9999

100100

101+
def load_env_file(env_path: str = ".env") -> dict:
102+
"""
103+
Load environment variables from a .env file.
104+
105+
Args:
106+
env_path: Path to the .env file
107+
108+
Returns:
109+
Dictionary of environment variables
110+
"""
111+
env_vars = {}
112+
env_file = Path(env_path)
113+
114+
if not env_file.exists():
115+
return env_vars
116+
117+
try:
118+
with open(env_file, "r", encoding="utf-8") as f:
119+
for line in f:
120+
line = line.strip()
121+
# Skip empty lines and comments
122+
if not line or line.startswith("#"):
123+
continue
124+
# Parse KEY=VALUE format
125+
if "=" in line:
126+
key, value = line.split("=", 1)
127+
# Remove quotes if present
128+
value = value.strip().strip("\"'")
129+
env_vars[key.strip()] = value
130+
except Exception as e:
131+
print(f"Warning: Error reading .env file: {e}", file=sys.stderr)
132+
133+
return env_vars
134+
135+
101136
def main():
102137
parser = argparse.ArgumentParser(
103138
description="Publish task pack JSON files to a remote URL"
@@ -131,11 +166,15 @@ def main():
131166

132167
visibility = "public" if args.public else "hidden"
133168

134-
# Get auth token from environment
169+
# Get auth token from environment variable first, then fall back to .env file
135170
auth_token = os.environ.get("CODEBATTLE_AUTH_TOKEN", "")
171+
if not auth_token:
172+
env_vars = load_env_file()
173+
auth_token = env_vars.get("CODEBATTLE_AUTH_TOKEN", "")
174+
136175
if not auth_token:
137176
print(
138-
"Warning: CODEBATTLE_AUTH_TOKEN environment variable not set",
177+
"Warning: CODEBATTLE_AUTH_TOKEN not found in environment or .env file",
139178
file=sys.stderr,
140179
)
141180

push_tasks.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,15 @@ def main():
199199

200200
visibility = "public" if args.public else "hidden"
201201

202-
# Load .env file
203-
env_vars = load_env_file()
202+
# Get auth token from environment variable first, then fall back to .env file
203+
auth_token = os.environ.get("CODEBATTLE_AUTH_TOKEN", "")
204+
if not auth_token:
205+
env_vars = load_env_file()
206+
auth_token = env_vars.get("CODEBATTLE_AUTH_TOKEN", "")
204207

205-
# Get auth token from .env file or environment variable
206-
auth_token = env_vars.get("CODEBATTLE_AUTH_TOKEN") or os.environ.get(
207-
"CODEBATTLE_AUTH_TOKEN", ""
208-
)
209208
if not auth_token:
210209
print(
211-
"Warning: CODEBATTLE_AUTH_TOKEN not found in .env file or environment",
210+
"Warning: CODEBATTLE_AUTH_TOKEN not found in environment or .env file",
212211
file=sys.stderr,
213212
)
214213

tasks/medium/math/euler_totient_function.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
level = "hard"
2-
name = "medium"
2+
name = "euler_totient_function"
33
tags = ["math"]
44
time_to_solve_sec = 900
55

0 commit comments

Comments
 (0)