patch updated date to preserve determinism

This commit is contained in:
Pim Kunis 2024-04-22 23:29:32 +02:00
parent ec31156864
commit 0051889d04
2 changed files with 64 additions and 0 deletions

View file

@ -75,6 +75,8 @@
};
};
website = pkgs.stdenv.mkDerivation
(
let
@ -83,6 +85,13 @@
name = "static";
gemdir = ./src;
};
patch-feed-date = pkgs.stdenv.mkDerivation {
name = "patch-feed-date";
propagatedBuildInputs = [ pkgs.python3 ];
dontUnpack = true;
installPhase = "install -Dm755 ${./patch-feed-date.py} $out/bin/patch-feed-date";
};
in
{
version = "0.0.1";
@ -93,6 +102,7 @@
buildInputs = [
gems
gems.wrappedRuby
patch-feed-date
];
sourceRoot = "src";
@ -104,6 +114,7 @@
installPhase = ''
mkdir -p $out
cp -r _site/* $out/
patch-feed-date --file _site/feed.xml > $out/feed.xml
'';
}
);

53
patch-feed-date.py Executable file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env python3
import argparse
import re
from datetime import datetime
updated_pattern = r"<updated>(?P<datetime>.{25})</updated>"
datetime_pattern = "%Y-%m-%dT%H:%M:%S%z"
def get_post_datetimes(feed):
regex = re.compile(updated_pattern)
datetimes = list()
first_skipped = False
for match in regex.finditer(feed):
# Skip first found datetime, as this is what we want to replace.
if not first_skipped:
first_skipped = True
continue
dt = datetime.strptime(match.group("datetime"), datetime_pattern)
datetimes.append(dt)
return datetimes
def main():
parser = argparse.ArgumentParser(
description="Patch the updated date field on an Atom feed."
)
parser.add_argument(
"--file", "-f", type=str, required=True, help="Path to the Atom feed file."
)
args = parser.parse_args()
with open(args.file, "r") as f:
feed = f.read()
datetimes = get_post_datetimes(feed)
newest_datetime = max(datetimes)
print(
re.sub(
updated_pattern, newest_datetime.strftime(datetime_pattern), feed, count=1
),
end="",
)
if __name__ == "__main__":
main()