Skip to content

Instantly share code, notes, and snippets.

@N-Parsons
Last active October 12, 2017 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save N-Parsons/9a99d9be6df07e470faf7a9310abbd23 to your computer and use it in GitHub Desktop.
Save N-Parsons/9a99d9be6df07e470faf7a9310abbd23 to your computer and use it in GitHub Desktop.
Script for checking version numbering in test files for exercism/python
import json
import os
import re
# There are probably others that don't require canonical data
NO_DATA = {"beer-song", "house", "robot-name", "zebra-puzzle", "twelve-days"}
def check_versions():
status = {"up-to-date": [], "up-to-date (x-common)": [],
"out-of-date": {}, "unknown": [], "unimplemented": [],
"no-canonical-data": [], "no-data-required": [],
"deprecated": []}
avail_exercises = set(os.listdir("./problem-specifications/exercises"))
impl_exercises = set(os.listdir("./python/exercises"))
status["unimplemented"] = sorted(avail_exercises - impl_exercises)
for exercise in sorted(impl_exercises):
if exercise in NO_DATA:
status["no-data-required"].append(exercise)
continue
if is_deprecated(exercise):
status["deprecated"].append(exercise)
continue
test_file, data_file = get_filepaths(exercise)
test_version, version_string = get_version(test_file)
try:
data_version = get_canonical_version(data_file)
except FileNotFoundError:
status["no-canonical-data"].append(exercise)
continue
if test_version == data_version:
if "x-common" in version_string:
status["up-to-date (x-common)"].append(exercise)
else:
status["up-to-date"].append(exercise)
elif not test_version or not data_version:
status["unknown"].append(exercise)
else:
status["out-of-date"].update({exercise: (test_version,
data_version)})
with open("version_status.json", "w") as outfile:
json.dump(status, outfile, indent=2)
def get_filepaths(exercise):
"""Return a tuple of the filepaths for *_test.py and canonical-data.json"""
test_file = os.path.join("./python/exercises",
exercise,
exercise.replace("-", "_") + "_test.py")
data_file = os.path.join("./problem-specifications/exercises",
exercise,
"canonical-data.json")
return (test_file, data_file)
def get_version(test_file):
"""Return a tuple of (version, version_string)"""
version = ""
version_string = ""
with open(test_file, "r") as f:
for line in f:
if line.startswith("#"):
version = parse_version(line)
if version:
version_string = line
break
return (version, version_string)
def parse_version(line):
"""Parse a possible version string, return None if no version found"""
version = re.findall("[0-9][.][0-9][.][0-9]", line)
return version[-1] if version else ""
def get_canonical_version(data_file):
"""Get the current version from the canonical-data file"""
with open(data_file, "r") as data:
return json.load(data)["version"]
def is_deprecated(exercise):
with open("./python/config.json", "r") as config:
data = json.load(config)
ex_data = [ex for ex in data["exercises"] if ex["slug"] == exercise]
try:
deprecated = ex_data[0]["deprecated"]
except KeyError:
deprecated = False
except IndexError:
print("{} not found in config.json".format(exercise))
deprecated = False
return deprecated
if __name__ == '__main__':
check_versions()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment