From 2cc599afddbf0d9e2874276421c9f3512559db54 Mon Sep 17 00:00:00 2001 From: "Sotiris Papatheodorou - sotiris at papatheodorou.xyz" Date: Wed, 12 Apr 2023 01:14:28 +0300 Subject: [PATCH] Change the Gopher index filename to gophermap This seems to be the most common convention among gopher servers (e.g. Gophernicus, go-gopher). Added a script to migrate the cache to the new format. Alternatively the following command can be used for migration in a POSIX system: find ~/.cache/offpunk/gopher/ -type f -name 'index.txt' -exec sh -c 'mv {} "$(dirname {})/gophermap"' \; --- CHANGELOG | 1 + migrate-offpunk-cache | 47 +++++++++++++++++++++++++++++++++++++++++++ offpunk.py | 4 +++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100755 migrate-offpunk-cache diff --git a/CHANGELOG b/CHANGELOG index 3b54ff5..0a90d62 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ ## 1.10 - unreleased - IMPORTANT : new optional dependency : python-chardet +- IMPORTANT : Gopher directory index filename changed from "index.txt" to "gophermap". To update the cache to the new format run the `migrate-offpunk-cache` script (Sotiris Papatheodorou) - "set accept_bad_ssl_certificates True" now also used for Gemini expired certificates - Add missing chardet module (Sotiris Papatheodorou) - Fix merging dictionaries with common keys (Sotiris Papatheodorou) diff --git a/migrate-offpunk-cache b/migrate-offpunk-cache new file mode 100755 index 0000000..de3bbb7 --- /dev/null +++ b/migrate-offpunk-cache @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: 2023 Sotiris Papatheodorou +# SPDX-License-Identifier: BSD-2-Clause + +""" +A script to migrate the offpunk cache to the newest version. + +For each new version of offpunk that requires changes to the cache a migration +function should be written. The name of the function should have the format +v__ and it should accept the +offpunk cache directory as a string. The function should perform a migration +from the immediately previous cache format. All migration functions must be +called at the end of this script from oldest to newest. +""" + +import argparse +import os +import os.path + + +def v1_10_0(cache_dir: str) -> None: + """ + Rename index.txt to gophermap in the Gopher protocol cache. + """ + for root, _, files in os.walk(os.path.join(cache_dir, 'gopher')): + for f in files: + if f == 'index.txt': + src = os.path.join(root, f) + dst = os.path.join(root, 'gophermap') + os.rename(src, dst) + + +if __name__ == '__main__': + # Compute the default offpunk cache directory. + xdg_cache_home = os.environ.get('XDG_CACHE_HOME') + if xdg_cache_home: + default_cache_dir = os.path.join(xdg_cache_home, 'offpunk') + else: + default_cache_dir = os.path.join(os.path.expanduser('~'), '.cache/offpunk') + # Parse command line arguments. + parser = argparse.ArgumentParser( + description='Migrate the offpunk cache to a newer version.') + parser.add_argument('cache_dir', metavar='CACHE_DIR', nargs='?', + default=default_cache_dir, help='the path to the cache') + args = parser.parse_args() + # Run the cache migration functions from the oldest to the newest version. + v1_10_0(args.cache_dir) diff --git a/offpunk.py b/offpunk.py index 46bf6c7..3c7770a 100755 --- a/offpunk.py +++ b/offpunk.py @@ -1439,8 +1439,10 @@ class GeminiItem(): # finish by "/". Else, the cache will create a file, not a folder. if self.scheme.startswith("http"): index = "index.html" - elif self.scheme in ["gopher", "finger"]: + elif self.scheme == "finger": index = "index.txt" + elif self.scheme == "gopher": + index = "gophermap" else: index = "index.gmi" if self.path == "" or os.path.isdir(self._cache_path):