]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/sync-docs.py
Merge pull request #30380 from keszybz/tmpfiles-dry-run
[thirdparty/systemd.git] / tools / sync-docs.py
CommitLineData
3c1f396f
AK
1#!/usr/bin/env python3
2# SPDX-License-Identifier: LGPL-2.1-or-later
3
4from argparse import ArgumentParser
5import glob
6import json
7import os
8import re
9import subprocess
10import sys
11
12import requests
13
14BASE_URL = "https://www.freedesktop.org/software/systemd/man/"
15JQUERY_URL = "https://code.jquery.com/jquery-3.7.1.min.js"
16SCRIPT_TAG = '<script src="{}"></script>'
17
18NAV_JS = """
19$(document).ready(function() {
20 $.getJSON("../index.json", function(data) {
21 data.sort().reverse();
22
23 var [filename, dirname] = window.location.pathname.split("/").reverse();
24
25 var items = [];
26 $.each( data, function(_, version) {
27 if (version == dirname) {
28 items.push( "<option selected value='" + version + "'>" + "systemd " + version + "</option>");
29 } else if (dirname == "latest" && version == data[0]) {
30 items.push( "<option selected value='" + version + "'>" + "systemd " + version + "</option>");
31 } else {
32 items.push( "<option value='" + version + "'>" + "systemd " + version + "</option>");
33 }
34 });
35
36 $("span:first").html($( "<select/>", {
37 id: "version-selector",
38 html: items.join( "" )
39 }));
40
41 $("#version-selector").on("change", function() {
42 window.location.assign("../" + $(this).val() + "/" + filename);
43 });
44 });
45});
46"""
47
48
49def process_file(filename):
50 with open(filename) as f:
51 contents = f.read()
52
53 if SCRIPT_TAG.format("../nav.js") in contents:
54 return
55
56 body_tag = re.search("<body[^>]*>", contents)
57 new_contents = (
58 contents[: body_tag.end()]
59 + SCRIPT_TAG.format(JQUERY_URL)
60 + SCRIPT_TAG.format("../nav.js")
61 + contents[body_tag.end() :]
62 )
63
64 with open(filename, "w") as f:
65 f.write(new_contents)
66
67
68def update_index_file(version, index_filename):
69 response = requests.get(BASE_URL + "index.json")
70 if response.status_code == 404:
71 index = []
72 elif response.ok:
73 index = response.json()
74 else:
75 sys.exit(f"Error getting index: {response.status_code} {response.reason}")
76
77 if version not in index:
78 index.insert(0, version)
79
80 with open(index_filename, "w") as f:
81 json.dump(index, f)
82
83
00fc4a39
AK
84def get_latest_version():
85 tags = subprocess.check_output(["git", "tag", "-l", "v*"], text=True).split()
86 versions = []
87 for tag in tags:
88 m = re.match("v?(\d+).*", tag)
89 if m:
90 versions.append(int(m.group(1)))
91 return max(versions)
92
93
94def main(version, directory, www_target):
3c1f396f
AK
95 index_filename = os.path.join(directory, "index.json")
96 nav_filename = os.path.join(directory, "nav.js")
97
e8868e83
AK
98 current_branch = subprocess.check_output(["git", "branch", "--show-current"], text=True).strip()
99
100 if current_branch != 'main' and not current_branch.endswith("-stable"):
101 sys.exit("doc-sync should only be run from main or a stable branch")
102
3c1f396f
AK
103 for filename in glob.glob(os.path.join(directory, "*.html")):
104 process_file(filename)
105
e8868e83
AK
106 if current_branch == "main":
107 version = "devel"
108 dirs = ["devel"]
109 elif int(version) == get_latest_version():
110 dirs = [version, "latest"]
111 else:
112 dirs = [version]
113
3c1f396f
AK
114 with open(nav_filename, "w") as f:
115 f.write(NAV_JS)
116
117 update_index_file(version, index_filename)
118
3c1f396f
AK
119 for d in dirs:
120 subprocess.check_call(
121 [
122 "rsync",
123 "-rlv",
124 "--delete-excluded",
125 "--include=*.html",
126 "--exclude=*",
127 "--omit-dir-times",
128 directory + "/", # copy contents of directory
75481beb 129 os.path.join(www_target, "man", d),
3c1f396f
AK
130 ]
131 )
132
133 subprocess.check_call(
134 [
135 "rsync",
136 "-v",
137 os.path.join(directory, "index.json"),
138 os.path.join(directory, "nav.js"),
75481beb 139 os.path.join(www_target, "man"),
3c1f396f
AK
140 ]
141 )
142
143
144if __name__ == "__main__":
145 parser = ArgumentParser()
146 parser.add_argument("--version", required=True)
3c1f396f
AK
147 parser.add_argument("directory")
148 parser.add_argument("www_target")
149
150 args = parser.parse_args()
00fc4a39 151 main(args.version, args.directory, args.www_target)