automatic handle of cache migration

This commit is contained in:
Lionel Dricot 2023-08-29 11:47:51 +02:00
parent 68451fbc0a
commit c356a4607c
4 changed files with 30 additions and 62 deletions

View File

@ -21,6 +21,7 @@ This is an an experimental and potentially unstable release. Bug reports are wel
- "file" is now marked as a dependency (thank Guillaume Loret)
- "--images-mode" allow to choose at startup which images should be dowloaded (none,readable,full)
- Support for multi-format rendering (such as RSS feeds with html elements)
- The cache is now automatically upgraded if needed (see .version in your cache)
## 1.10 - July 31st 2023
- IMPORTANT : new optional dependency : python-chardet

View File

@ -18,30 +18,14 @@ import os
import os.path
def v1_10_0(cache_dir: str) -> None:
def upgrade_to_1(cache_dir: str) -> None:
"""
Rename index.txt to gophermap in the Gopher protocol cache.
"""
print("Upgrading cache to version 1: migrating index.txt to gophermap")
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)

View File

@ -1,44 +0,0 @@
.Dd August 11, 2023
.Dt MIGRATE-OFFPUNK-CACHE 1
.Os
.
.Sh NAME
.Nm migrate-offpunk-cache
.Nd migrate offpunk cache to the newest version
.
.Sh SYNOPSIS
.Nm
.Op Ar CACHE_DIR
.Nm
.Fl h | \-help
.
.Sh DESCRIPTION
For each new version of offpunk that requires changes to the cache,
a migration function is provided.
All migration functions will be called by this script,
from the oldest to the newest version.
.Ss Positional arguments
.Bl -tag -width Ds -offset indent
.It CACHE_DIR
This is the path to the cache.
The default value will point to $XDG_CACHE_HOME,
or $HOME/.cache/offpunk if undefined.
.El
.Ss Keyword arguments
.Bl -tag -width Ds -offset indent
.It Fl h , \-help
Show a help message and exit
.El
.
.Sh EXIT STATUS
.Ex -std
.
.Sh SEE ALSO
.Xr ansicat 1 ,
.Xr netcache 1 ,
.Xr offpunk 1 ,
.Xr opnk 1 ,
.Lk https://sr.ht/~lioploum/offpunk/
.
.Sh AUTHORS
.An Lionel Dricot (Ploum) Aq Mt offpunk2 at ploum.eu

View File

@ -13,6 +13,9 @@ import shutil
import shlex
import urllib.parse
import urllib.parse
import cache_migration
CACHE_VERSION = 1
## Config directories
## We implement our own python-xdg to avoid conflict with existing libraries.
@ -34,6 +37,30 @@ cache_home = os.environ.get('XDG_CACHE_HOME') or\
os.path.join(_home,'.cache')
_CACHE_PATH = os.path.join(os.path.expanduser(cache_home),"offpunk/")
#Lets read current version of the cache
version_path = _CACHE_PATH + ".version"
current_version = 0
if os.path.exists(version_path):
current_str = None
with open(version_path) as f:
current_str = f.read()
f.close()
try:
current_version = int(current_str)
except:
current_version = 0
#Now, lets upgrade the cache if needed
while current_version < CACHE_VERSION:
current_version += 1
upgrade_func = getattr(cache_migration,"upgrade_to_"+str(current_version))
upgrade_func(_CACHE_PATH)
with open(version_path,"w") as f:
f.write(str(current_version))
f.close()
## Those two functions add/remove the mode to the
# URLs. This is a gross hack to remember the mode
def mode_url(url,mode):