Date photo uploads from capture metadata
This commit is contained in:
@@ -4,6 +4,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -15,6 +16,13 @@ from posts import publish_post
|
||||
log = logging.getLogger("publisher")
|
||||
|
||||
|
||||
def entry_date(path):
|
||||
match = re.search(r'^date: "(\d{4}-\d{2}-\d{2})', path.read_text(), re.MULTILINE)
|
||||
if not match:
|
||||
raise ValueError(f"Photo entry has no valid date: {path.name}")
|
||||
return match.group(1)
|
||||
|
||||
|
||||
class Publisher:
|
||||
def __init__(self):
|
||||
self.data = Path(os.environ.get("PHOTO_DATA", "/data"))
|
||||
@@ -50,6 +58,7 @@ class Publisher:
|
||||
self.state(job, "publishing")
|
||||
try:
|
||||
outcomes = {}
|
||||
replaced_photos = {}
|
||||
if not (self.repo / ".git").is_dir():
|
||||
self.git("clone", "--branch", self.branch, "--single-branch", "--",
|
||||
self.remote, str(self.repo), directory=self.work)
|
||||
@@ -85,6 +94,14 @@ class Publisher:
|
||||
continue
|
||||
entry = Path("_art") / f"{manifest['date'][:10]}-photo-{job}.md"
|
||||
images = Path("img/arts/uploads") / job
|
||||
replaces = manifest.get("replaces", "")
|
||||
if re.fullmatch(r"[a-f0-9]{64}", replaces) and replaces != job:
|
||||
for previous in (self.repo / "_art").glob(f"*-photo-{replaces}*.md"):
|
||||
self.git("rm", "--ignore-unmatch", "--",
|
||||
str(previous.relative_to(self.repo)))
|
||||
previous_images = Path("img/arts/uploads") / replaces
|
||||
self.git("rm", "-r", "--ignore-unmatch", "--", str(previous_images))
|
||||
replaced_photos[job] = replaces
|
||||
(self.repo / entry).parent.mkdir(parents=True, exist_ok=True)
|
||||
entries = submission / "entries"
|
||||
if entries.is_dir():
|
||||
@@ -92,7 +109,7 @@ class Publisher:
|
||||
# is republished by a newer service version.
|
||||
self.git("rm", "--ignore-unmatch", "--", str(entry))
|
||||
for source in sorted(entries.glob("*.md")):
|
||||
destination = Path("_art") / f"{manifest['date'][:10]}-photo-{job}-{source.stem}.md"
|
||||
destination = Path("_art") / f"{entry_date(source)}-photo-{job}-{source.stem}.md"
|
||||
shutil.copyfile(source, self.repo / destination)
|
||||
self.git("add", "--", str(destination))
|
||||
else:
|
||||
@@ -106,6 +123,8 @@ class Publisher:
|
||||
commit = self.git("rev-parse", "HEAD")
|
||||
for job in jobs:
|
||||
self.state(job, "pushed", commit=commit, **outcomes.get(job, {}))
|
||||
if job in replaced_photos:
|
||||
self.state(replaced_photos[job], "superseded", replaced_by=job)
|
||||
# A subsequent revision replaces the old one in the rendered site.
|
||||
for job, outcome in outcomes.items():
|
||||
job_date = json.loads((self.data / "submissions" / job / "manifest.json").read_text())["date"]
|
||||
|
||||
Reference in New Issue
Block a user