-
-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate latest.py from endoflife.date to release-data (#4313)
It makes more sense as it is closely linked to the JSON version file format, which is more subject to change than the product file format. Also took the opportunity to refactor the bulk_update.py script.
- Loading branch information
1 parent
79208bd
commit 68bfaa0
Showing
5 changed files
with
26 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,38 @@ | ||
import sys | ||
import frontmatter | ||
import json | ||
import re | ||
import datetime | ||
from glob import glob | ||
from pathlib import Path | ||
from distutils.version import StrictVersion | ||
from ruamel.yaml import YAML | ||
from io import StringIO | ||
from os.path import exists | ||
from ruamel.yaml.resolver import Resolver | ||
|
||
DEFAULT_POST_TEMPLATE = """\ | ||
--- | ||
{metadata} | ||
--- | ||
|
||
{content} | ||
""" | ||
"""Helper script that can be used to bulk-update product files.""" | ||
|
||
|
||
def yaml_to_str(obj): | ||
yaml = YAML() | ||
yaml.indent(sequence=4) | ||
string_stream = StringIO() | ||
yaml.dump(obj, string_stream) | ||
output_str = string_stream.getvalue() | ||
string_stream.close() | ||
return output_str | ||
def update(yaml_frontmatter): | ||
return yaml_frontmatter | ||
|
||
|
||
""" | ||
Takes a product dict | ||
and return the updated version | ||
""" | ||
# Force YAML to format version numbers as strings, see https://stackoverflow.com/a/71329221/368328. | ||
Resolver.add_implicit_resolver("tag:yaml.org,2002:string", re.compile(r"\d+(\.\d+){0,3}", re.X), list(".0123456789")) | ||
|
||
|
||
def update(obj): | ||
return obj | ||
|
||
|
||
def update_product(name): | ||
fn = "products/%s.md" % name | ||
print(name) | ||
with open(fn, "r+") as f: | ||
for product_path in glob("products/*.md"): | ||
with open(product_path, "r+") as product_file: | ||
# Read YAML | ||
yaml = YAML() | ||
yaml.preserve_quotes = True | ||
data = next(yaml.load_all(f)) | ||
f.seek(0) | ||
_, content = frontmatter.parse(f.read()) | ||
yaml.indent(sequence=4) | ||
data = next(yaml.load_all(product_file)) | ||
product_file.seek(0) | ||
_, content = frontmatter.parse(product_file.read()) | ||
|
||
print(f"Updating {product_path}...") | ||
data = update(data) | ||
final_contents = DEFAULT_POST_TEMPLATE.format( | ||
metadata=yaml_to_str(data), content=content | ||
) | ||
|
||
f.seek(0) | ||
f.truncate() | ||
f.write(final_contents) | ||
|
||
|
||
if __name__ == "__main__": | ||
for x in glob("products/*.md"): | ||
update_product(Path(x).stem) | ||
# Read updated YAML | ||
product_file.seek(0) | ||
product_file.truncate() | ||
product_file.write("---\n") | ||
yaml.dump(data, product_file) | ||
product_file.write("\n---\n\n") | ||
product_file.write(content) | ||
product_file.write("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
python-frontmatter==1.0.1 | ||
ruamel.yaml==0.18.5 | ||
ruamel.yaml.clib==0.2.8 | ||
packaging==23.2 |