#!/usr/bin/python3 '''When a wikiweb is renamed, the TagMePlugin files _tags_*.*.txt in /var/www/foswiki/working/work_areas/TagMePlugin/ corresponding to that wikiweb are not renamed with it. So, here we look for _tag_*.*.txt files in that directory where their corresponding topics may have changed their location and then make proposals for renaming or deleting them. This script creates two shell scripts that may help you cleaning up the mess. Licensed under GNU GPL 3.0 or higher. See https://www.gnu.org/licenses/gpl-3.0.html (c) 2020 Christian Kern, Atos. ''' ### Python modules import sys, os, re import subprocess ### Configuration foswikidir = '/var/www/foswiki' tagdir = os.path.join(foswikidir, 'working/work_areas/TagMePlugin') datadir = os.path.join(foswikidir, 'data') verbose = True ### Helper functions def vprint(*text): print('#', *text) def dict_print(dct, filename): with open(filename, 'w') as fho: for k,v in sorted(dct.items()): print(k,'->',v, file=fho) def list_print(lst, filename): with open(filename, 'w') as fho: for k in sorted(lst): print(k, file=fho) ### Show configuration vprint('config: foswikidir:', foswikidir) vprint('config: tagdir: ', tagdir) vprint('config: datadir: ', datadir) ### Create file lists ##### Tagfiles: ##### - at least two dots in the filename (not "tags_all.txt") ##### - Name e.g. _tags_Web.Topic.txt, _tags_Web.Subweb.Topic.txt, ... tagfiles = dict([(re.sub('^_tags_(.+)\.txt$', r'\1', x).replace('.', '/'), x) for x in sorted(os.listdir(tagdir)) if re.search('^_tags_[^.]+\..+\.txt$', x)]) ### Debugging: Insert an entry for a missing topic which has multiple ### "sisters" of the same name #debug#tagfiles['NoRealWeb/FrequentTopicName'] = '/path/to/NoRealWeb/FrequentTopicName' vprint('Found', len(tagfiles), 'tagfiles') ##### Topicfiles: ##### - also in subdirectories ##### - Name e.g. Web/Topic.txt, Web/Subweb/Topic.txt, ... p = subprocess.Popen(['find', datadir, '-type', 'f', '-name', '*.txt', '!', '-wholename', '*,pfv/*'], stdout=subprocess.PIPE) topicfiles = dict([(x[len(datadir)+1:-4],x) for x in [x.strip().decode('latin1') for x in sorted(p.stdout.readlines())]]) vprint('Found', len(topicfiles), 'topicfiles') if verbose: dict_print(tagfiles, 'dict_tagfiles' ) dict_print(topicfiles, 'dict_topicfiles') ### Basenames basenames = {} for path in sorted(topicfiles.keys()): topic = os.path.basename(path) basenames[topic] = basenames.get(topic, []) + [path] dict_print(basenames, 'dict_basenames') ### See what is here, what was perhaps moved, what is missing. # tagfiles: Web/Topic -> _tags_Web.Topic.txt # topicateien: Web/Topic -> /var/www/foswiki/data/Web/Topic.txt # basenames: Topic -> [Web/Topic, AnderesWeb/Topic, ...] good, perhaps_moved, missing = [], {}, [] for tag,tagpath in tagfiles.items(): if tag in topicfiles: good.append(tag) else: bn = os.path.basename(tag) if bn == 'Ansa': print('JOPF', bn, tag, tagpath) if bn in basenames: perhaps_moved[tag] = basenames[bn] else: missing.append(tag) print() vprint('Found', len(good), 'good ones.') vprint('Found', len(perhaps_moved), 'perhaps moved.') vprint('Found', len(missing), 'missing ones.') if verbose: list_print(good, 'erglist_good') dict_print(perhaps_moved, 'erglist_movd') list_print(missing, 'erglist_miss') with open('action.remove_missing.sh', 'w') as fho: print('#!/bin/bash -e', file=fho) for f in missing: fp = tagfiles[f] assert "'" not in fp print("rm -fv -- '%s'" % os.path.join(tagdir, fp), file=fho) with open('action.rename.sh', 'w') as fho: print('#!/bin/bash -e', file=fho) print('# Carefully uncomment lines as necessary/sensible.', file=fho) print('# Then run this script.', file=fho) assert "'" not in tagdir print("cd -- '%s'" % tagdir, file=fho) for k,vl in sorted(perhaps_moved.items()): kp = tagfiles[k] assert "'" not in kp print('\n### =========== %s ===========' % k, file=fho) for v in sorted(vl): vp = '_tags_%s.txt' % v.replace('/','.') assert "'" not in vp print("#mv -iv -- '%s' '%s'" % (kp, vp), file=fho) ### For cases where no candidate seems fitting, propose removing the tagfile. print("##rm -fv -- '%s'" % kp, file=fho) # vim:expandtab:ts=4