only rewrite utility files if contents changed

pull/3589/head
Mike Fährmann 2 years ago
parent 0d818d3540
commit e60ec1699a
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -48,7 +48,7 @@ for action in option.build_parser()._actions:
opts.append(opt)
PATH = util.path("data/completion/gallery-dl")
with open(PATH, "w", encoding="utf-8") as file:
with util.lazy(PATH) as file:
file.write(TEMPLATE % {
"opts" : " ".join(opts),
"diropts" : "|".join(diropts),

@ -41,5 +41,5 @@ for action in option.build_parser()._actions:
opts.append(opt)
PATH = util.path("data/completion/gallery-dl.fish")
with open(PATH, "w", encoding="utf-8") as file:
with util.lazy(PATH) as file:
file.write(TEMPLATE % {"opts": "\n".join(opts)})

@ -46,5 +46,5 @@ for action in option.build_parser()._actions:
PATH = util.path("data/completion/_gallery-dl")
with open(PATH, "w", encoding="utf-8") as file:
with util.lazy(PATH) as file:
file.write(TEMPLATE % {"opts": " \\\n".join(opts)})

@ -100,7 +100,7 @@ and https://github.com/mikf/gallery-dl/graphs/contributors
if not path:
path = util.path("data/man/gallery-dl.1")
with open(path, "w", encoding="utf-8") as file:
with util.lazy(path) as file:
file.write(TEMPLATE.lstrip() % {
"options": "\n".join(options),
"version": gallery_dl.version.__version__,
@ -218,7 +218,7 @@ and https://github.com/mikf/gallery-dl/graphs/contributors
if not path:
path = util.path("data/man/gallery-dl.conf.5")
with open(path, "w", encoding="utf-8") as file:
with util.lazy(path) as file:
file.write(TEMPLATE.lstrip() % {
"options": "\n".join(content),
"version": gallery_dl.version.__version__,

@ -33,10 +33,10 @@ opts = re.sub(r"(?m)^(\w+.*)", "## \\1", opts) # group names to headings
opts = opts.replace("\n ", "\n ") # indent by 4
outfile = sys.argv[1] if len(sys.argv) > 1 else util.path("docs", "options.md")
with open(outfile, "w", encoding="utf-8") as fp:
fp.write(TEMPLATE.format(
PATH = (sys.argv[1] if len(sys.argv) > 1 else
util.path("docs", "options.md"))
with util.lazy(PATH) as file:
file.write(TEMPLATE.format(
"/".join(os.path.normpath(__file__).split(os.sep)[-2:]),
opts,
))

@ -1,6 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Generate a Markdown document listing all supported sites"""
import os
@ -511,6 +515,7 @@ Consider all sites to be NSFW unless otherwise known.
categories, domains = build_extractor_list()
outfile = sys.argv[1] if len(sys.argv) > 1 else "supportedsites.md"
with open(util.path("docs", outfile), "w") as fp:
fp.write(generate_output(COLUMNS, categories, domains))
PATH = (sys.argv[1] if len(sys.argv) > 1 else
util.path("docs", "supportedsites.md"))
with util.lazy(PATH) as file:
file.write(generate_output(COLUMNS, categories, domains))

@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
import os
import io
import sys
import os.path
ROOTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.realpath(ROOTDIR))
@ -11,3 +16,32 @@ def path(*segments, join=os.path.join):
result = join(ROOTDIR, *segments)
os.makedirs(os.path.dirname(result), exist_ok=True)
return result
class lazy():
def __init__(self, path):
self.path = path
self.buffer = io.StringIO()
def __enter__(self):
return self.buffer
def __exit__(self, exc, value, tb):
# get content of old file
try:
with open(self.path, encoding="utf-8") as fp:
old = fp.read()
except Exception:
old = None
# get new content
new = self.buffer.getvalue()
if new != old:
# rewrite entire file
with open(self.path, "w", encoding="utf-8") as fp:
fp.write(new)
else:
# only update atime and mtime
os.utime(self.path)

Loading…
Cancel
Save