[scripts] ensure files use 'utf-8' encoding and '\n' newlines (#7872)

This commit is contained in:
Mike Fährmann
2025-07-22 16:53:40 +02:00
parent afc5b9c9c6
commit e8b2a496ba
7 changed files with 21 additions and 16 deletions

View File

@@ -51,7 +51,7 @@ for idx, extr, url, result in tests:
# write test data # write test data
try: try:
with open(os.path.join(path, name), "w") as outfile: with util.open(os.path.join(path, name), "w") as outfile:
job.DataJob(url, file=outfile, ensure_ascii=False).run() job.DataJob(url, file=outfile, ensure_ascii=False).run()
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit() sys.exit()

View File

@@ -14,7 +14,7 @@ from gallery_dl import text, extractor
def read(fname): def read(fname):
path = util.path("docs", fname) path = util.path("docs", fname)
try: try:
with open(path) as fp: with util.open(path) as fp:
return fp.read() return fp.read()
except Exception as exc: except Exception as exc:
sys.exit("Unable to read {} ({}: {})".format( sys.exit("Unable to read {} ({}: {})".format(
@@ -110,7 +110,7 @@ def sleeprequest():
continue continue
min, _, max = value.partition("-") min, _, max = value.partition("-")
tup = (float(min), float(max)) tup = (float(min), float(max)) if max else float(min)
if tup != extr.request_interval: if tup != extr.request_interval:
result[category] = extr.request_interval result[category] = extr.request_interval
return result return result

View File

@@ -71,7 +71,7 @@ def get_test_source(extr, *, cache={}):
tests = cache[extr.__module__] tests = cache[extr.__module__]
except KeyError: except KeyError:
path = sys.modules[extr.__module__].__file__ path = sys.modules[extr.__module__].__file__
with open(path) as fp: with util.open(path) as fp:
lines = fp.readlines() lines = fp.readlines()
tests = cache[extr.__module__] = extract_tests_from_source(lines) tests = cache[extr.__module__] = extract_tests_from_source(lines)
return tests.get(extr.url) or ("",) return tests.get(extr.url) or ("",)

View File

@@ -82,7 +82,7 @@ def insert_test_result(args, result):
path = util.path("test", "results", f"{args.cat}.py") path = util.path("test", "results", f"{args.cat}.py")
LOG.info("Adding '%s:%s' test result into '%s'", args.cat, args.sub, path) LOG.info("Adding '%s:%s' test result into '%s'", args.cat, args.sub, path)
with open(path) as fp: with util.open(path) as fp:
lines = fp.readlines() lines = fp.readlines()
lines.insert(-2, result) lines.insert(-2, result)

View File

@@ -69,7 +69,7 @@ def create_extractor_module(opts=NONE):
else: else:
generate_extractors = generate_extractors_basic generate_extractors = generate_extractors_basic
with open(path, opts["open_mode"], encoding="utf-8") as fp: with util.open(path, opts["open_mode"]) as fp:
if copyright := opts.get("copyright", ""): if copyright := opts.get("copyright", ""):
copyright = f"\n# Copyright {dt.date.today().year} {copyright}\n#" copyright = f"\n# Copyright {dt.date.today().year} {copyright}\n#"
@@ -222,8 +222,7 @@ def create_test_results_file(opts=NONE):
LOG.info("Creating '%s'", trim_path(path)) LOG.info("Creating '%s'", trim_path(path))
import_stmt = generate_test_result_import(opts) import_stmt = generate_test_result_import(opts)
with open(path, "x", encoding="utf-8") as fp: with util.open(path, "x") as fp:
fp.write(f"""\ fp.write(f"""\
{ENCODING} {ENCODING}
{LICENSE} {LICENSE}
@@ -260,7 +259,7 @@ def insert_into_modules_list(opts=NONE):
category) category)
path = util.path("gallery_dl", "extractor", "__init__.py") path = util.path("gallery_dl", "extractor", "__init__.py")
with open(path) as fp: with util.open(path) as fp:
lines = fp.readlines() lines = fp.readlines()
module_name = f' "{category}",\n' module_name = f' "{category}",\n'
@@ -289,7 +288,7 @@ def insert_into_supportedsites(opts):
category) category)
path = util.path("scripts", "supportedsites.py") path = util.path("scripts", "supportedsites.py")
with open(path) as fp: with util.open(path) as fp:
lines = fp.readlines() lines = fp.readlines()
compare = False compare = False

View File

@@ -228,8 +228,8 @@ and https://github.com/mikf/gallery-dl/graphs/contributors
def parse_docs_configuration(): def parse_docs_configuration():
doc_path = util.path("docs", "configuration.rst") path = util.path("docs", "configuration.rst")
with open(doc_path, encoding="utf-8") as fp: with util.open(path) as fp:
doc_lines = fp.readlines() doc_lines = fp.readlines()
sections = {} sections = {}

View File

@@ -7,17 +7,22 @@
import os import os
import io import io
import sys import sys
import builtins
ROOTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.realpath(ROOTDIR)) sys.path.insert(0, os.path.realpath(ROOTDIR))
def path(*segments, join=os.path.join): def path(*segments):
result = join(ROOTDIR, *segments) result = os.path.join(ROOTDIR, *segments)
os.makedirs(os.path.dirname(result), exist_ok=True) os.makedirs(os.path.dirname(result), exist_ok=True)
return result return result
def open(path, mode="r"):
return builtins.open(path, mode, encoding="utf-8", newline="\n")
class lazy(): class lazy():
def __init__(self, path): def __init__(self, path):
@@ -30,7 +35,7 @@ class lazy():
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, exc_type, exc_value, traceback):
# get content of old file # get content of old file
try: try:
with open(self.path, encoding="utf-8") as fp: with builtins.open(self.path, encoding="utf-8", newline="") as fp:
old = fp.read() old = fp.read()
except Exception: except Exception:
old = None old = None
@@ -40,7 +45,8 @@ class lazy():
if new != old: if new != old:
# rewrite entire file # rewrite entire file
with open(self.path, "w", encoding="utf-8") as fp: with builtins.open(
self.path, "w", encoding="utf-8", newline="") as fp:
fp.write(new) fp.write(new)
else: else:
# only update atime and mtime # only update atime and mtime