]> Lady’s Gitweb - x_status_git/blob - post-receive
Fail on git clone error
[x_status_git] / post-receive
1 #!/usr/bin/env python3
2 from glob import iglob
3 from itertools import starmap
4 import json
5 from os import mkdir
6 from os.path import exists
7 from pathlib import Path
8 import re
9 from shutil import copy2, rmtree
10 from subprocess import run
11 from sys import stdin
12 from warnings import warn
13 from xml.dom import XHTML_NAMESPACE
14 from xml.dom.minidom import getDOMImplementation
15
16 GIT_DIRECTORY = "/home/USERNAME/Status.git"
17 BUILD_DIRECTORY = "/home/USERNAME/status.site.example/.build"
18 PUBLIC_DIRECTORY = "/home/USERNAME/status.site.example/public"
19 PUBLIC_URL = "https://status.site.example"
20 LANG = "en"
21 LIVE_BRANCH = "live"
22
23 if stdin.read().split()[-1] == f"refs/heads/{LIVE_BRANCH}":
24
25 print(f"This is an update to the '{LIVE_BRANCH}' branch; regenerating site…")
26
27 # Set up the build directory.
28 if exists(BUILD_DIRECTORY):
29 rmtree(BUILD_DIRECTORY)
30 cloneresult = run(["git", "clone", "--local", "--branch", LIVE_BRANCH, GIT_DIRECTORY, BUILD_DIRECTORY], capture_output=True, encoding="utf-8")
31 # if cloneresult.stderr:
32 # print(cloneresult.stderr)
33 cloneresult.check_returncode()
34
35 # Set up various containers.
36 months = {}
37 topics = {}
38
39 # Create an XML representation of the provided status text.
40 def statusxml (text, version="1.0"):
41 doc = getDOMImplementation().createDocument(None, "article", None)
42 articleElt = doc.documentElement
43 articleElt.setAttribute("xmlns", XHTML_NAMESPACE)
44 articleElt.setAttribute("lang", LANG)
45 for para in text.split("\n\n"):
46 paraElt = articleElt.appendChild(doc.createElement("p"))
47 for component in re.findall(r'<[a-z]+:[^\s]*>(?:="[^\n"]+")?|\n|[^<\n]+|<(?![a-z]+:[^\s]*>)', para):
48 if component == "\n":
49 paraElt.appendChild(doc.createElement("br"))
50 elif re.fullmatch(r'<[a-z]+:[^\s]*>(?:="[^\n"]+")?', component):
51 href = component.split(">", maxsplit=1)[0][1:]
52 anchorElt = paraElt.appendChild(doc.createElement("a"))
53 anchorElt.setAttribute("href", href)
54 anchorElt.setAttribute("rel", "noreferrer")
55 anchorElt.appendChild(doc.createTextNode(component if len(href) == len(component) - 2 else component[len(href)+4:-1]))
56 else:
57 paraElt.appendChild(doc.createTextNode(component))
58 return articleElt.toxml()
59
60 # Map status paths to status objects, or None if there is an error.
61 #
62 # The provided path must be to a `text` object.
63 def statusmap (topic, path):
64 status = { "@type": "MicroblogPost" }
65 version_path = next(path.parent.glob("0=*"))
66 if version_path and version_path.name != "0=x_status_git_1.0":
67 warn(f"Unrecognized version for {path}; skipping.")
68 return None
69 if topic:
70 status["subject"] = topic
71 author_path = next(path.parent.glob("1=*"))
72 if author_path:
73 status["author"] = { "name": author_path.name[2:] }
74 with author_path.open("r", encoding="utf-8") as text:
75 status["author"]["@id"] = text.read().strip()
76 title_path = next(path.parent.glob("2=*"))
77 if title_path:
78 with title_path.open("r", encoding="utf-8") as text:
79 title = text.read().strip()
80 status["title"] = title
81 date_path = next(path.parent.glob("3=*"))
82 datetime = ""
83 if date_path:
84 with date_path.open("r", encoding="utf-8") as text:
85 datetime = text.read().strip()
86 status["created"] = datetime
87 else:
88 warn(f"Missing date for {path}; skipping.")
89 return None
90 identifier_path = next(path.parent.glob("4=*"))
91 identifier = ""
92 if identifier_path:
93 identifier = identifier_path.name[2:]
94 status["@id"] = f"{PUBLIC_URL}/topics/{topic}/{identifier}" if topic else f"{PUBLIC_URL}/statuses/{datetime[0:7]}/{identifier}"
95 with identifier_path.open("r", encoding="utf-8") as text:
96 status["identifier"] = text.read().strip()
97 else:
98 warn(f"Missing identifier for {path}; skipping.")
99 return None
100 with path.open("r", encoding="utf-8") as text:
101 status["content"] = statusxml(text.read().strip())
102 return (datetime, identifier, status)
103
104 # Get status paths.
105 status_paths = []
106 for yearpath in Path(f"{BUILD_DIRECTORY}/").glob("[0-9][0-9][0-9][0-9]"):
107 for monthpath in yearpath.glob("[0-9][0-9]"):
108 for daypath in monthpath.glob("[0-9][0-9]"):
109 for statuspath in daypath.glob("*/text"):
110 status_paths.append((None, statuspath))
111 for topicpath in Path(f"{BUILD_DIRECTORY}/").glob("topic/*"):
112 for hash0path in topicpath.glob("[0-9a-f]"):
113 for hash1path in hash0path.glob("[0-9a-f]"):
114 for hash2path in hash1path.glob("[0-9a-f]"):
115 for hash3path in hash2path.glob("[0-9a-f]"):
116 for statuspath in hash3path.glob("*/text"):
117 status_paths.append((topicpath.name, statuspath))
118
119 # Build status objects and listings.
120 for (datetime, identifier, status) in sorted(filter(None, starmap(statusmap, status_paths))):
121 if "subject" in status:
122 topic = status["subject"]
123 if topic not in topics:
124 topics[topic] = { "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "foaf": "http://xmlns.com/foaf/0.1/", "sioc": "http://rdfs.org/sioc/ns#", "sioct": "http://rdfs.org/sioc/types#", "OrderedCollection": "activity:OrderedCollection", "Thread": "sioc:Thread", "MicroblogPost": "sioct:MicroblogPost", "items": { "@id": "activity:items", "@type": "@id", "@container": "@list" }, "created": { "@id": "dct:created", "@type": "http://www.w3.org/2001/XMLSchema#dateTime" }, "creator": { "@id": "dct:creator", "@type": "@id" }, "identifier": { "@id": "dct:identifier", "@type": "http://www.w3.org/2001/XMLSchema#anyURI" }, "subject": "dct:subject", "name": "foaf:name", "title": "dct:title", "content": { "@id": "sioc:content", "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral" } }, "@id": f"{PUBLIC_URL}/topics/{topic}", "@type": ["OrderedCollection", "Thread"], "items": [], "subject": topic }
125 topics[topic]["items"].append(status)
126 else:
127 yyyy_mm = datetime[0:7]
128 if yyyy_mm not in months:
129 months[yyyy_mm] = { "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "foaf": "http://xmlns.com/foaf/0.1/", "sioc": "http://rdfs.org/sioc/ns#", "sioct": "http://rdfs.org/sioc/types#", "OrderedCollectionPage": "activity:OrderedCollectionPage", "Thread": "sioc:Thread", "MicroblogPost": "sioct:MicroblogPost", "current": { "@id": "activity:current", "@type": "@id" }, "first": { "@id": "activity:first", "@type": "@id" }, "items": { "@id": "activity:items", "@type": "@id", "@container": "@list" }, "partOf": { "@id": "activity:partOf", "@type": "@id" }, "prev": { "@id": "activity:prev", "@type": "@id" }, "next": { "@id": "activity:next", "@type": "@id" }, "created": { "@id": "dct:created", "@type": "http://www.w3.org/2001/XMLSchema#dateTime" }, "creator": { "@id": "dct:creator", "@type": "@id" }, "identifier": { "@id": "dct:identifier", "@type": "http://www.w3.org/2001/XMLSchema#anyURI" }, "name": "foaf:name", "title": "dct:title", "content": { "@id": "sioc:content", "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral" } }, "@id": f"{PUBLIC_URL}/statuses/{yyyy_mm}", "@type": ["OrderedCollectionPage", "Thread"], "items": [], "partOf": f"{PUBLIC_URL}/statuses" }
130 months[yyyy_mm]["items"].append(status)
131
132 # Set up the public directory.
133 if exists(PUBLIC_DIRECTORY):
134 rmtree(PUBLIC_DIRECTORY)
135 mkdir(PUBLIC_DIRECTORY)
136
137 # Copy H·T·M·L files to their expected locations.
138 copy2(f"{BUILD_DIRECTORY}/index.html", f"{PUBLIC_DIRECTORY}/index.html")
139 copy2(f"{BUILD_DIRECTORY}/about.html", f"{PUBLIC_DIRECTORY}/.about.html")
140 copy2(f"{BUILD_DIRECTORY}/status.html", f"{PUBLIC_DIRECTORY}/.status.html")
141 copy2(f"{BUILD_DIRECTORY}/statuses.html", f"{PUBLIC_DIRECTORY}/.statuses.html")
142 copy2(f"{BUILD_DIRECTORY}/topic.html", f"{PUBLIC_DIRECTORY}/.topic.html")
143 copy2(f"{BUILD_DIRECTORY}/topics.html", f"{PUBLIC_DIRECTORY}/.topics.html")
144
145 # Output “about” metadata
146 if not exists(f"{PUBLIC_DIRECTORY}/about"):
147 mkdir(f"{PUBLIC_DIRECTORY}/about")
148 with open(f"{PUBLIC_DIRECTORY}/about/index.jsonld", "w", encoding="utf-8") as f:
149 json.dump({ "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "sioc": "http://rdfs.org/sioc/ns#", "sioct": "http://rdfs.org/sioc/types#", "Forum": "sioc:Forum", "Thread": "sioc:Thread", "Microblog": "sioct:Microblog", "streams": { "@id": "activity:streams", "@type": "@id" } }, "@id": f"{PUBLIC_URL}", "@type": "Microblog", "streams": [{ "@id": f"{PUBLIC_URL}/statuses", "@type": "Thread" }, { "@id": f"{PUBLIC_URL}/topics", "@type": "Forum" }] }, f, ensure_ascii=False, allow_nan=False)
150
151 # Output month‐based listings and the non‐topic index
152 if not exists(f"{PUBLIC_DIRECTORY}/statuses"):
153 mkdir(f"{PUBLIC_DIRECTORY}/statuses")
154 statuspairs = list(enumerate(months.items()))
155 for (index, (yyyy_mm, ld)) in statuspairs:
156 if not exists(f"{PUBLIC_DIRECTORY}/statuses/{yyyy_mm}"):
157 mkdir(f"{PUBLIC_DIRECTORY}/statuses/{yyyy_mm}")
158 ld["first"] = f"{PUBLIC_URL}/statuses/{statuspairs[0][1][0]}"
159 ld["current"] = f"{PUBLIC_URL}/statuses/{statuspairs[-1][1][0]}"
160 if index > 0:
161 ld["prev"] = f"{PUBLIC_URL}/statuses/{statuspairs[index - 1][1][0]}"
162 if index < len(statuspairs) - 1:
163 ld["next"] = f"{PUBLIC_URL}/statuses/{statuspairs[index + 1][1][0]}"
164 with open(f"{PUBLIC_DIRECTORY}/statuses/{yyyy_mm}/index.jsonld", "w", encoding="utf-8") as f:
165 json.dump(ld, f, ensure_ascii=False, allow_nan=False)
166 with open(f"{PUBLIC_DIRECTORY}/statuses/index.jsonld", "w", encoding="utf-8") as f:
167 json.dump({ "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "sioc": "http://rdfs.org/sioc/ns#", "OrderedCollection": "activity:OrderedCollection", "Thread": "sioc:Thread", "current": { "@id": "activity:current", "@type": "@id" }, "first": { "@id": "activity:first", "@type": "@id" }, "has_parent": { "@id": "sioc:has_parent", "@type": "id" } }, "@id": f"{PUBLIC_URL}/statuses", "@type": ["OrderedCollection", "Thread"], "first": f"{PUBLIC_URL}/statuses/{statuspairs[0][1][0]}", "current": f"{PUBLIC_URL}/statuses/{statuspairs[-1][1][0]}", "has_parent": f"{PUBLIC_URL}" }, f, ensure_ascii=False, allow_nan=False)
168
169 # Output topic‐based listings and the topic index
170 if not exists(f"{PUBLIC_DIRECTORY}/topics"):
171 mkdir(f"{PUBLIC_DIRECTORY}/topics")
172 for (topic, ld) in topics.items():
173 if not exists(f"{PUBLIC_DIRECTORY}/topics/{topic}"):
174 mkdir(f"{PUBLIC_DIRECTORY}/topics/{topic}")
175 with open(f"{PUBLIC_DIRECTORY}/topics/{topic}/index.jsonld", "w", encoding="utf-8") as f:
176 json.dump(ld, f, ensure_ascii=False, allow_nan=False)
177 with open(f"{PUBLIC_DIRECTORY}/topics/index.jsonld", "w", encoding="utf-8") as f:
178 json.dump({ "@context": { "@language": LANG, "activity": "https://www.w3.org/ns/activitystreams#", "dct": "http://purl.org/dc/terms/", "sioc": "http://rdfs.org/sioc/ns#", "Collection": "activity:Collection", "Forum": "sioc:Forum", "items": { "@id": "activity:items", "@type": "@id" }, "has_parent": { "@id": "sioc:has_parent", "@type": "id" }, "subject": "dct:subject" }, "@id": f"{PUBLIC_URL}/topics", "@type": ["Collection", "Forum"], "items": list(map(lambda a: { "@id": a["@id"], "subject": a["subject"] }, topics.values())), "has_parent": f"{PUBLIC_URL}" }, f, ensure_ascii=False, allow_nan=False)
179
180 # Remove the build directory.
181 rmtree(BUILD_DIRECTORY)
This page took 0.210627 seconds and 5 git commands to generate.