Import initial : script pour dumper une partie du wiki (destin
fournir une copie statique des pages wifi sur Nectaris) darcs-hash:20041231221641-d1718-7477c6e13fd65d025ce7a53b5aa48445cc262d33.gz
This commit is contained in:
parent
607b1c44d9
commit
1abec4d73a
1 changed files with 181 additions and 0 deletions
181
dump-wiki.py
Executable file
181
dump-wiki.py
Executable file
|
@ -0,0 +1,181 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: iso-8859-15 -*-
|
||||||
|
#
|
||||||
|
# Dumpe une partie du Wiki dans le répertoire donné pour créer
|
||||||
|
# une copie "statique".
|
||||||
|
#
|
||||||
|
# Exemple : python dump-wiki.py --regex WiFi/AvoirLeWifi".*" ~/macopie
|
||||||
|
# Les pages correspondant à la regex donnée seront placées dans
|
||||||
|
# le répertoire ~/macopie/wiki. Les attachements seront dans
|
||||||
|
# ~/macopie/attach.
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
WIKIDIR='/var/local/lib/wiki'
|
||||||
|
|
||||||
|
__version__ = '0.1'
|
||||||
|
|
||||||
|
import sys
|
||||||
|
sys.path.append(WIKIDIR) # moin_config
|
||||||
|
sys.path.append('/usr/lib/python2.3/site-packages/MoinMoin') # MoinMoin
|
||||||
|
|
||||||
|
# On peut modifier cette template
|
||||||
|
page_template = '''<html>
|
||||||
|
<head>
|
||||||
|
<title>%(pagename)s</title>
|
||||||
|
<link rel="stylesheet" type="text/css" href="../wiki.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p class="avertissement">
|
||||||
|
Cette page est une copie de statique de ce que l'on peut trouver sur le
|
||||||
|
<a href="http://wiki.crans.org">wiki</a> de l'association. Elle est à destination
|
||||||
|
des utilisateurs souhaitant configurer le wifi sur leur machine. Elle ne contient
|
||||||
|
que les pages strictement nécessaires à la configuration. Une fois la configuration effectuée,
|
||||||
|
vous pourrez accéder au <a href="http://wiki.crans.org">wiki</a> complet.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h1 class="pagename">%(pagename)s</h1>
|
||||||
|
|
||||||
|
%(pagehtml)s
|
||||||
|
|
||||||
|
<p class="creation">
|
||||||
|
Cette page a été extraite du wiki le %(timestamp)s.
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
'''
|
||||||
|
|
||||||
|
import os, time, cStringIO
|
||||||
|
from MoinMoin.scripts import _util
|
||||||
|
|
||||||
|
class MoinDump(_util.Script):
|
||||||
|
def __init__(self):
|
||||||
|
_util.Script.__init__(self, __name__, "[options] <target-directory>")
|
||||||
|
|
||||||
|
# --regex=REGEX
|
||||||
|
self.parser.add_option(
|
||||||
|
"--regex", metavar="REGEX", dest="regex",
|
||||||
|
help="Ne copie que les pages correspondant à cette regex"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def mainloop(self):
|
||||||
|
""" moin-dump's main code. """
|
||||||
|
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
self.parser.print_help()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if len(self.args) != 1:
|
||||||
|
self.parser.error("Nombre incorrect d'arguments")
|
||||||
|
|
||||||
|
# Prepare output directory
|
||||||
|
outputdir = self.args[0]
|
||||||
|
outputdir = os.path.abspath("%s/wiki" % outputdir)
|
||||||
|
attachdir = os.path.abspath("%s/../attach" % outputdir)
|
||||||
|
if not os.path.isdir(outputdir):
|
||||||
|
try:
|
||||||
|
os.mkdir(outputdir)
|
||||||
|
_util.log("Created output directory '%s'!" % outputdir)
|
||||||
|
except OSError:
|
||||||
|
_util.fatal("Cannot create output directory '%s'!" % outputdir)
|
||||||
|
if not os.path.isdir(attachdir):
|
||||||
|
try:
|
||||||
|
os.mkdir(attachdir)
|
||||||
|
_util.log("Created attach directory '%s'!" % attachdir)
|
||||||
|
except OSError:
|
||||||
|
_util.fatal("Cannot create attach directory '%s'!" % attachdir)
|
||||||
|
|
||||||
|
from MoinMoin import config
|
||||||
|
if config.default_config:
|
||||||
|
_util.fatal("Fichier de configuration moin_config.py introuvable.")
|
||||||
|
|
||||||
|
# fix some values so we get relative paths in output html
|
||||||
|
config.url_prefix = "/wiki"
|
||||||
|
|
||||||
|
# avoid spoiling the cache with url_prefix == "."
|
||||||
|
# we do not use nor update the cache because of that
|
||||||
|
config.caching_formats = []
|
||||||
|
|
||||||
|
# Dump the wiki
|
||||||
|
from MoinMoin.request import RequestCGI
|
||||||
|
request = RequestCGI({'script_name': '.'})
|
||||||
|
request.form = request.args = request.setup_args()
|
||||||
|
|
||||||
|
from MoinMoin import wikiutil, Page
|
||||||
|
pages = list(wikiutil.getPageList(config.text_dir))
|
||||||
|
if self.options.regex:
|
||||||
|
# On ne garde que les pages matchant la regexp
|
||||||
|
import re
|
||||||
|
pages = filter(lambda x: re.match(self.options.regex, x), pages)
|
||||||
|
pages.sort()
|
||||||
|
|
||||||
|
wikiutil.quoteWikiname = lambda pagename, qfn=wikiutil.quoteWikiname: qfn(pagename) + ".html"
|
||||||
|
|
||||||
|
errfile = os.path.join(outputdir, '..', 'error.log')
|
||||||
|
errlog = open(errfile, 'w')
|
||||||
|
errcnt = 0
|
||||||
|
|
||||||
|
for pagename in pages:
|
||||||
|
file = wikiutil.quoteWikiname(pagename)
|
||||||
|
_util.log('Writing "%s"...' % file)
|
||||||
|
try:
|
||||||
|
pagehtml = ''
|
||||||
|
page = Page.Page(pagename)
|
||||||
|
try:
|
||||||
|
request.reset()
|
||||||
|
out = cStringIO.StringIO()
|
||||||
|
request.redirect(out)
|
||||||
|
page.send_page(request, count_hit=0, content_only=1)
|
||||||
|
pagehtml = out.getvalue()
|
||||||
|
request.redirect()
|
||||||
|
except:
|
||||||
|
errcnt = errcnt + 1
|
||||||
|
print >>sys.stderr, "*** Caught exception while writing page!"
|
||||||
|
print >>errlog, "~" * 78
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc(None, errlog)
|
||||||
|
finally:
|
||||||
|
import locale
|
||||||
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
|
timestamp = time.strftime("%A %d %B %Y à %H:%M")
|
||||||
|
filepath = os.path.join(outputdir, file)
|
||||||
|
fileout = open(filepath, 'w')
|
||||||
|
fileout.write(page_template % {
|
||||||
|
'pagename': pagename,
|
||||||
|
'pagehtml': pagehtml.replace('%s/rightsidebar/img' % config.url_prefix, '/img'),
|
||||||
|
'timestamp': timestamp,
|
||||||
|
})
|
||||||
|
fileout.close()
|
||||||
|
# On copie les attachements
|
||||||
|
try:
|
||||||
|
source = "%s/data/pages/%s/attachments" % (WIKIDIR, file.replace(".html",""))
|
||||||
|
if os.path.isdir(source):
|
||||||
|
# Il y a des attachements
|
||||||
|
try:
|
||||||
|
os.makedirs("%s/%s/attachments" % (attachdir, file.replace(".html","")))
|
||||||
|
except OSError, e:
|
||||||
|
# On va dire qu'il existe déjà
|
||||||
|
pass
|
||||||
|
os.system("cp -r %s %s/%s/." %
|
||||||
|
(source, attachdir, file.replace(".html","")))
|
||||||
|
except:
|
||||||
|
errcnt = errcnt + 1
|
||||||
|
print >>sys.stderr, "*** Caught exception while copying attachments!"
|
||||||
|
print >>errlog, "~" * 78
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc(None, errlog)
|
||||||
|
|
||||||
|
# On va copier les images
|
||||||
|
os.system("rsync -r --delete /usr/share/moin/htdocs/rightsidebar/img %s/.." % outputdir)
|
||||||
|
|
||||||
|
errlog.close()
|
||||||
|
if errcnt:
|
||||||
|
print >>sys.stderr, "*** %d error(s) occurred, see '%s'!" % (errcnt, errfile)
|
||||||
|
|
||||||
|
def run():
|
||||||
|
MoinDump().run()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue