All checks were successful
/ index (push) Successful in 6s
Signed-off-by: Arija A. <ari@ari.lt>
113 lines
3 KiB
Python
113 lines
3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Generate index.json"""
|
|
|
|
import base64
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import typing as t
|
|
from datetime import datetime
|
|
from warnings import filterwarnings as filter_warnings
|
|
|
|
|
|
def get_git_creation_timestamp(path: str) -> float:
|
|
"""Get the creation timestamp of a file using Git"""
|
|
|
|
timestamp_str: str = (
|
|
subprocess.check_output(
|
|
["git", "log", "--follow", "--format=%aI", path],
|
|
text=True,
|
|
)
|
|
.strip()
|
|
.splitlines()[-1]
|
|
)
|
|
|
|
return datetime.fromisoformat(timestamp_str).timestamp()
|
|
|
|
|
|
def parse_meta(fp) -> t.Dict[str, t.Any]:
|
|
"""Parse metadata"""
|
|
|
|
final: t.Dict[str, t.Any] = {}
|
|
|
|
stated: bool = False
|
|
|
|
for line in fp:
|
|
key, value = line.strip().split(":", 1)
|
|
|
|
if not key or not value:
|
|
raise ValueError(
|
|
f"Failed to parse {fp.name!r} metadata! Missing key or value"
|
|
)
|
|
|
|
key = key.lower()
|
|
|
|
if key in final:
|
|
raise ValueError(f"{key} already defined in metadata {fp.name!r}!")
|
|
|
|
if key == "statement":
|
|
stated = True
|
|
continue
|
|
|
|
final[key] = value.strip()
|
|
|
|
if not stated:
|
|
raise ValueError(f"License statement missing in metadata {fp.name!r}!")
|
|
|
|
for rkey in ("filename", "alt", "author", "contact"):
|
|
if rkey not in final:
|
|
raise ValueError(f"{rkey} missing in metadata {fp.name!r}!")
|
|
|
|
path = os.path.join("img", final["filename"])
|
|
|
|
final["timestamp"] = get_git_creation_timestamp(path)
|
|
final["license"] = "CC BY-NC-SA 4.0"
|
|
|
|
if not os.path.isfile(path):
|
|
raise ValueError(f"{final['filename']!r} is missing in metadata {fp.name!r}!")
|
|
|
|
return final
|
|
|
|
|
|
def main() -> int:
|
|
"""entry / main function"""
|
|
|
|
index: t.List[t.Dict[str, t.Any]] = []
|
|
contributors: t.List[str] = []
|
|
|
|
for metaf in os.listdir("meta"):
|
|
with open(os.path.join("meta", metaf), "r", encoding="utf-8") as fp:
|
|
meta: t.Dict[str, t.Any] = parse_meta(fp)
|
|
contributor: str = f"* {meta['author']} <{meta['contact']}>"
|
|
if contributor not in contributors:
|
|
contributors.append(contributor)
|
|
meta["contact"] = base64.b85encode(meta["contact"].encode("ascii")).decode(
|
|
"ascii"
|
|
)
|
|
index.append(meta)
|
|
|
|
index.sort(key=lambda meta: meta.get("filename", "").lower())
|
|
contributors.sort()
|
|
|
|
with open("index.json", "w") as ifp:
|
|
json.dump(index, ifp, indent=1)
|
|
|
|
print("Created index.json")
|
|
|
|
with open("CONTRIBUTORS", "w") as fp:
|
|
fp.write(
|
|
"Thank you for contributing to Arivertisements. Here is a complete list of contributors with semi-obfuscated e-mail addresses:\n\n"
|
|
)
|
|
fp.write("\n".join(contributors))
|
|
|
|
print("Created CONTRIBUTORS")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
assert main.__annotations__.get("return") is int, "main() should return an integer"
|
|
|
|
filter_warnings("error", category=Warning)
|
|
raise SystemExit(main())
|