sync-docs: apply "ruff format" and "ruff check --fix"
This commit is contained in:
+43
-43
@@ -1,21 +1,21 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# SPDX-License-Identifier: LGPL-2.1-or-later
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
|
||||||
import glob
|
import glob
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
BASE_URL = "https://www.freedesktop.org/software/systemd/man/"
|
BASE_URL = 'https://www.freedesktop.org/software/systemd/man/'
|
||||||
JQUERY_URL = "https://code.jquery.com/jquery-3.7.1.min.js"
|
JQUERY_URL = 'https://code.jquery.com/jquery-3.7.1.min.js'
|
||||||
SCRIPT_TAG = '<script src="{}"></script>'
|
SCRIPT_TAG = '<script src="{}"></script>'
|
||||||
|
|
||||||
NAV_JS = """
|
NAV_JS = '''
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
$.getJSON("../index.json", function(data) {
|
$.getJSON("../index.json", function(data) {
|
||||||
data.sort().reverse();
|
data.sort().reverse();
|
||||||
@@ -48,77 +48,77 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
"""
|
'''
|
||||||
|
|
||||||
|
|
||||||
def process_file(filename):
|
def process_file(filename):
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
contents = f.read()
|
contents = f.read()
|
||||||
|
|
||||||
if SCRIPT_TAG.format("../nav.js") in contents:
|
if SCRIPT_TAG.format('../nav.js') in contents:
|
||||||
return
|
return
|
||||||
|
|
||||||
body_tag = re.search("<body[^>]*>", contents)
|
body_tag = re.search('<body[^>]*>', contents)
|
||||||
new_contents = (
|
new_contents = (
|
||||||
contents[: body_tag.end()]
|
contents[: body_tag.end()]
|
||||||
+ SCRIPT_TAG.format(JQUERY_URL)
|
+ SCRIPT_TAG.format(JQUERY_URL)
|
||||||
+ SCRIPT_TAG.format("../nav.js")
|
+ SCRIPT_TAG.format('../nav.js')
|
||||||
+ contents[body_tag.end() :]
|
+ contents[body_tag.end() :]
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(filename, "w") as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(new_contents)
|
f.write(new_contents)
|
||||||
|
|
||||||
|
|
||||||
def update_index_file(version, index_filename):
|
def update_index_file(version, index_filename):
|
||||||
response = requests.get(BASE_URL + "index.json")
|
response = requests.get(BASE_URL + 'index.json')
|
||||||
if response.status_code == 404:
|
if response.status_code == 404:
|
||||||
index = []
|
index = []
|
||||||
elif response.ok:
|
elif response.ok:
|
||||||
index = response.json()
|
index = response.json()
|
||||||
else:
|
else:
|
||||||
sys.exit(f"Error getting index: {response.status_code} {response.reason}")
|
sys.exit(f'Error getting index: {response.status_code} {response.reason}')
|
||||||
|
|
||||||
if version not in index:
|
if version not in index:
|
||||||
index.insert(0, version)
|
index.insert(0, version)
|
||||||
|
|
||||||
with open(index_filename, "w") as f:
|
with open(index_filename, 'w') as f:
|
||||||
json.dump(index, f)
|
json.dump(index, f)
|
||||||
|
|
||||||
|
|
||||||
def get_latest_version():
|
def get_latest_version():
|
||||||
tags = subprocess.check_output(["git", "tag", "-l", "v*"], text=True).split()
|
tags = subprocess.check_output(['git', 'tag', '-l', 'v*'], text=True).split()
|
||||||
versions = []
|
versions = []
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
m = re.match(r"v?(\d+).*", tag)
|
m = re.match(r'v?(\d+).*', tag)
|
||||||
if m:
|
if m:
|
||||||
versions.append(int(m.group(1)))
|
versions.append(int(m.group(1)))
|
||||||
return max(versions)
|
return max(versions)
|
||||||
|
|
||||||
|
|
||||||
def main(version, directory, www_target):
|
def main(version, directory, www_target):
|
||||||
index_filename = os.path.join(directory, "index.json")
|
index_filename = os.path.join(directory, 'index.json')
|
||||||
nav_filename = os.path.join(directory, "nav.js")
|
nav_filename = os.path.join(directory, 'nav.js')
|
||||||
# The upload directory does not contain point release suffixes
|
# The upload directory does not contain point release suffixes
|
||||||
version = re.sub(r"\..+$", "", version)
|
version = re.sub(r'\..+$', '', version)
|
||||||
|
|
||||||
current_branch = subprocess.check_output(["git", "branch", "--show-current"], text=True).strip()
|
current_branch = subprocess.check_output(['git', 'branch', '--show-current'], text=True).strip()
|
||||||
|
|
||||||
if current_branch != 'main' and not current_branch.endswith("-stable"):
|
if current_branch != 'main' and not current_branch.endswith('-stable'):
|
||||||
sys.exit("doc-sync should only be run from main or a stable branch")
|
sys.exit('doc-sync should only be run from main or a stable branch')
|
||||||
|
|
||||||
for filename in glob.glob(os.path.join(directory, "*.html")):
|
for filename in glob.glob(os.path.join(directory, '*.html')):
|
||||||
process_file(filename)
|
process_file(filename)
|
||||||
|
|
||||||
if current_branch == "main":
|
if current_branch == 'main':
|
||||||
version = "devel"
|
version = 'devel'
|
||||||
dirs = ["devel"]
|
dirs = ['devel']
|
||||||
elif int(version) == get_latest_version():
|
elif int(version) == get_latest_version():
|
||||||
dirs = [version, "latest"]
|
dirs = [version, 'latest']
|
||||||
else:
|
else:
|
||||||
dirs = [version]
|
dirs = [version]
|
||||||
|
|
||||||
with open(nav_filename, "w") as f:
|
with open(nav_filename, 'w') as f:
|
||||||
f.write(NAV_JS)
|
f.write(NAV_JS)
|
||||||
|
|
||||||
update_index_file(version, index_filename)
|
update_index_file(version, index_filename)
|
||||||
@@ -126,33 +126,33 @@ def main(version, directory, www_target):
|
|||||||
for d in dirs:
|
for d in dirs:
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
[
|
[
|
||||||
"rsync",
|
'rsync',
|
||||||
"-rlv",
|
'-rlv',
|
||||||
"--delete-excluded",
|
'--delete-excluded',
|
||||||
"--include=*.html",
|
'--include=*.html',
|
||||||
"--exclude=*",
|
'--exclude=*',
|
||||||
"--omit-dir-times",
|
'--omit-dir-times',
|
||||||
directory + "/", # copy contents of directory
|
directory + '/', # copy contents of directory
|
||||||
os.path.join(www_target, "man", d),
|
os.path.join(www_target, 'man', d),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
[
|
[
|
||||||
"rsync",
|
'rsync',
|
||||||
"-v",
|
'-v',
|
||||||
os.path.join(directory, "index.json"),
|
os.path.join(directory, 'index.json'),
|
||||||
os.path.join(directory, "nav.js"),
|
os.path.join(directory, 'nav.js'),
|
||||||
os.path.join(www_target, "man"),
|
os.path.join(www_target, 'man'),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == '__main__':
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument("--version", required=True)
|
parser.add_argument('--version', required=True)
|
||||||
parser.add_argument("directory")
|
parser.add_argument('directory')
|
||||||
parser.add_argument("www_target")
|
parser.add_argument('www_target')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
main(args.version, args.directory, args.www_target)
|
main(args.version, args.directory, args.www_target)
|
||||||
|
|||||||
Reference in New Issue
Block a user