Back to index

moin  1.9.0~rc2
cleanpage.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - display unused or trash page directories in data/pages
00004 
00005 @copyright: 2005-2006 MoinMoin:ThomasWaldmann
00006 @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import os
00010 
00011 from MoinMoin.script import MoinScript
00012 
00013 class PluginScript(MoinScript):
00014     """\
00015 Purpose:
00016 ========
00017 This tool outputs a shell script which upon execution will remove unused or
00018 trashed pages from the wiki.
00019 
00020 Detailed Instructions:
00021 ======================
00022 General syntax: moin [options] maint cleanpage [cleanpage-options]
00023 
00024 [options] usually should be:
00025     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00026 
00027 [cleanpage-options] see below:
00028     0. Verify the outputted shell script before running it.
00029 
00030     1. This script takes no command line arguments.
00031 """
00032 
00033     def __init__(self, argv, def_values):
00034         MoinScript.__init__(self, argv, def_values)
00035 
00036     def qualify(self, p):
00037         """ look at page directory p and return its state """
00038         dir = os.listdir(p)
00039         if not dir:
00040             return 'empty'
00041 
00042         # check if we have something of potential value
00043         revs = []
00044         if 'revisions' in dir:
00045             revs = os.listdir(os.path.join(p, 'revisions'))
00046         atts = []
00047         if 'attachments' in dir:
00048             atts = os.listdir(os.path.join(p, 'attachments'))
00049 
00050         if not revs and not atts:
00051             return 'trash'
00052 
00053         if 'current-locked' in dir:
00054             return 'current-locked'
00055         elif 'current' in dir:
00056             try:
00057                 current = open(os.path.join(p, 'current')).read().strip()
00058                 int(current)
00059             except:
00060                 return 'current damaged'
00061             if current not in revs:
00062                 return 'deleted'
00063         else:
00064             return 'no current'
00065 
00066         return 'ok'
00067 
00068     def mainloop(self):
00069         self.init_request()
00070         base = self.request.cfg.data_dir
00071         pagesdir = os.path.join(base, 'pages')
00072         for p in os.listdir(pagesdir):
00073             pagedir = os.path.join(pagesdir, p)
00074             status = self.qualify(pagedir)
00075             if status in ['trash', 'empty', ]:
00076                 print "mv '%s' trash # %s" % (pagedir, status)
00077             elif status in ['deleted', ]:
00078                 print "mv '%s' deleted # %s" % (pagedir, status)
00079             else:
00080                 print "# %s: '%s'" % (status, pagedir)
00081