Skip to content

Instantly share code, notes, and snippets.

@Alexander01998
Created May 1, 2024 18:21
Show Gist options
  • Save Alexander01998/770fcd4e82f6fa8c3f569a7eb1405783 to your computer and use it in GitHub Desktop.
Save Alexander01998/770fcd4e82f6fa8c3f569a7eb1405783 to your computer and use it in GitHub Desktop.
Utility script to move the list of hacks included in each Wurst update to a central data folder
# Place in root folder of WurstClient.net
# Also, make sure you have ruamel.yaml installed: `pip install ruamel.yaml`
import os
from io import StringIO
from ruamel.yaml import YAML
yaml = YAML()
yaml.preserve_quotes = True
cwd = os.path.dirname(os.path.realpath(__file__))
def read_front_matter(path):
front_matter_lines = []
with open(path, "r", encoding="utf-8") as file:
for line in file.readlines():
if line.strip() == "---":
if len(front_matter_lines) == 0:
continue
else:
break
else:
front_matter_lines.append(line)
front_matter = yaml.load("".join(front_matter_lines))
return front_matter
def write_front_matter(path, front_matter):
print(f"Updating front matter of {os.path.basename(path)}...")
with open(path, "r", encoding="utf-8") as file:
post_lines = file.readlines()
new_post_lines = []
has_reached_end_of_front_matter = False
for line in post_lines:
if line.strip() == "---":
if len(new_post_lines) == 0:
new_post_lines.append(line)
stream = StringIO()
yaml.dump(front_matter, stream)
new_post_lines.append(stream.getvalue())
else:
new_post_lines.append(line)
has_reached_end_of_front_matter = True
elif has_reached_end_of_front_matter:
new_post_lines.append(line)
with open(path, "w", encoding="utf-8") as file:
file.write("".join(new_post_lines))
def write_yml(path, data):
with open(path, "w", encoding="utf-8") as file:
yaml.dump(data, file)
updates_folder = os.path.join(cwd, "_updates")
hacks_by_version = {}
file_by_version = {}
front_matter_by_version = {}
for root, dirs, files in os.walk(updates_folder):
for file in files:
update_path = os.path.join(root, file)
front_matter = read_front_matter(update_path)
if "hacks" not in front_matter:
continue
version = front_matter["wurst-version"]
hacks = front_matter["hacks"]
hacks_by_version[version] = hacks
file_by_version[version] = update_path
front_matter_by_version[version] = front_matter
print(f"Hack lists: {len(hacks_by_version.keys())}")
unique_hacks = set()
for hacks in hacks_by_version.values():
unique_hacks.add(tuple(hacks))
print(f"Unique hack lists: {len(unique_hacks)}")
versions_with_same_hacks = {}
for uhacks in unique_hacks:
for version, hacks in hacks_by_version.items():
if tuple(hacks) == uhacks:
if uhacks not in versions_with_same_hacks:
versions_with_same_hacks[uhacks] = []
versions_with_same_hacks[uhacks].append(version)
for versions in versions_with_same_hacks.values():
filename = f"since_v{versions[0].replace('.', '_').replace('-BETA', '_beta')}.yml"
data_file = os.path.join(cwd, "_data", "hacks", filename)
hacks = hacks_by_version[versions[0]]
print(f"{filename}: {versions}")
write_yml(data_file, hacks)
for version in versions:
path = file_by_version[version]
front_matter = front_matter_by_version[version]
del front_matter["hacks"]
write_front_matter(path, front_matter)
print(f"Updating features include for {os.path.basename(path)}...")
with open(path, "r", encoding="utf-8") as file:
content = file.read()
with open(path, "w", encoding="utf-8") as file:
file.write(content.replace("include update/features.html", f"include update/features.html hacks=site.data.hacks.{filename[:-4]}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment