From e60ec1699aeeb674d8a504d8c4e279facf369633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 27 Jan 2023 18:04:13 +0100 Subject: [PATCH] only rewrite utility files if contents changed --- scripts/completion_bash.py | 2 +- scripts/completion_fish.py | 2 +- scripts/completion_zsh.py | 2 +- scripts/man.py | 4 ++-- scripts/options.py | 8 ++++---- scripts/supportedsites.py | 11 ++++++++--- scripts/util.py | 36 +++++++++++++++++++++++++++++++++++- 7 files changed, 52 insertions(+), 13 deletions(-) diff --git a/scripts/completion_bash.py b/scripts/completion_bash.py index 6ce2a8d3..d8ceef82 100755 --- a/scripts/completion_bash.py +++ b/scripts/completion_bash.py @@ -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), diff --git a/scripts/completion_fish.py b/scripts/completion_fish.py index 1a1816cf..7ed4cb09 100755 --- a/scripts/completion_fish.py +++ b/scripts/completion_fish.py @@ -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)}) diff --git a/scripts/completion_zsh.py b/scripts/completion_zsh.py index 6ac7b526..f2fcdd62 100755 --- a/scripts/completion_zsh.py +++ b/scripts/completion_zsh.py @@ -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)}) diff --git a/scripts/man.py b/scripts/man.py index e2c65588..47968f15 100755 --- a/scripts/man.py +++ b/scripts/man.py @@ -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__, diff --git a/scripts/options.py b/scripts/options.py index b2fa7f6e..11f7f302 100755 --- a/scripts/options.py +++ b/scripts/options.py @@ -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, )) diff --git a/scripts/supportedsites.py b/scripts/supportedsites.py index 41a6d248..5a2014c5 100755 --- a/scripts/supportedsites.py +++ b/scripts/supportedsites.py @@ -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)) diff --git a/scripts/util.py b/scripts/util.py index 773794ca..629f9b0d 100644 --- a/scripts/util.py +++ b/scripts/util.py @@ -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)