3
3
MoinMoin - Package Generator
5
5
@copyright: 2005 Alexander Schremmer,
6
2006-2009 MoinMoin:ThomasWaldmann,
7
2009 MoinMoin:ReimarBauer
6
2006 MoinMoin:ThomasWaldmann
8
7
@license: GNU GPL, see COPYING for details.
13
13
from datetime import datetime
15
from MoinMoin.support.python_compatibility import set
16
from MoinMoin import wikiutil
17
from MoinMoin.action.AttachFile import _get_files
15
from MoinMoin import wikidicts, wikiutil
18
16
from MoinMoin.Page import Page
19
from MoinMoin.action import AttachFile
20
17
from MoinMoin.packages import packLine, MOIN_PACKAGE_FILE
21
18
from MoinMoin.script import MoinScript
22
from MoinMoin import i18n
23
from MoinMoin.i18n import strings
24
i18n.strings = strings
22
ALL = u'all_languages'
27
23
COMPRESSION_LEVEL = zipfile.ZIP_STORED
29
25
class PluginScript(MoinScript):
37
33
General syntax: moin [options] maint mkpagepacks [mkpagepacks-options]
39
35
[options] usually should be:
40
--config-dir=/path/to/my/cfg/ --wiki-url=http://wiki.example.org/
36
--config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
42
38
[mkpagepacks-options] see below:
43
39
0. THIS SCRIPT SHOULD NEVER BE RUN ON ANYTHING OTHER THAN A TEST WIKI!
51
47
def buildPageSets(self):
52
48
""" Calculates which pages should go into which package. """
53
49
request = self.request
55
all_pages = set(request.rootpage.getPageList())
56
packaged_pages = set()
58
languages = i18n.wikiLanguages()
59
pageset_names = i18n.strings.pagesets
61
for lang in languages:
62
def trans(text, request=request, lang=lang, **kw):
63
return i18n.getText(text, request, lang, **kw)
66
lang_long = languages[lang]['x-language-in-english']
67
lang_long = lang_long.replace('/', '_').replace(' ', '_')
71
for pageset_name in pageset_names:
72
pageset_orig = set(getattr(i18n.strings, pageset_name))
73
pageset_trans = set([trans(pn) for pn in pageset_orig])
74
key = u"%s--%s" % (lang_long, pageset_name)
75
pageset = pageset_trans
77
pageset -= pageset_orig
79
print key, len(pageset)
80
pageSets[key] = pageset
81
packaged_pages |= pageset
83
not_packaged_pages = all_pages - packaged_pages
84
pageSets['00_needs_fixing'] = not_packaged_pages
52
allPages = Set(request.rootpage.getPageList())
54
systemPages = wikidicts.Group(request, "SystemPagesGroup").members()
56
for pagename in systemPages:
57
if pagename.endswith("Group"):
58
#print x + " -> " + repr(wikidicts.Group(request, x).members())
59
self.gd.addgroup(request, pagename)
62
for name, group in self.gd.dictdict.items():
63
groupPages = Set(group.members() + [name])
64
name = name.replace("SystemPagesIn", "").replace("Group", "")
65
pageSets[name] = groupPages
66
langPages |= groupPages
68
specialPages = Set(["SystemPagesGroup"])
70
masterNonSystemPages = allPages - langPages - specialPages
72
moinI18nPages = Set([x for x in masterNonSystemPages if x.startswith("MoinI18n")])
74
nodistPages = moinI18nPages | Set(["InterWikiMap", ])
76
extraPages = masterNonSystemPages - nodistPages
78
pageSets[ALL] = langPages
80
for name in pageSets.keys():
81
if name not in (u"English"):
82
pageSets[name] -= pageSets[u"English"]
83
pageSets[name] -= nodistPages
85
pageSets[EXTRA] = extraPages # stuff that maybe should be in some language group
86
pageSets[NODIST] = nodistPages # we dont want to have them in dist archive
87
89
def packagePages(self, pagelist, filename, function):
91
93
os.remove(filename)
94
# page LanguageSetup needs no packing!
95
existing_pages = [pagename for pagename in pagelist if Page(request, pagename).exists() and pagename != 'LanguageSetup']
96
if not existing_pages:
99
96
zf = zipfile.ZipFile(filename, "w", COMPRESSION_LEVEL)
101
script = [packLine(['MoinMoinPackage', '1']), ]
104
for pagename in existing_pages:
99
script = [packLine(['MoinMoinPackage', '1']), ]
101
for pagename in pagelist:
105
102
pagename = pagename.strip()
106
103
page = Page(request, pagename)
107
files = _get_files(request, pagename)
108
for attname in files:
111
script.append(packLine(["ReplaceUnderlayAttachment", zipname, attname, pagename]))
112
attpath = AttachFile.getFilename(request, pagename, attname)
113
zf.write(attpath, zipname)
117
script.append(packLine([function, zipname, pagename]))
118
timestamp = wikiutil.version2timestamp(page.mtime_usecs())
119
zi = zipfile.ZipInfo(filename=zipname, date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
120
zi.compress_type = COMPRESSION_LEVEL
121
zf.writestr(zi, page.get_raw_body().encode("utf-8"))
106
script.append(packLine([function, str(cnt), pagename]))
107
timestamp = wikiutil.version2timestamp(page.mtime_usecs())
108
zi = zipfile.ZipInfo(filename=str(cnt), date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
109
zi.compress_type = COMPRESSION_LEVEL
110
zf.writestr(zi, page.get_raw_body().encode("utf-8"))
113
#print >>sys.stderr, "Could not find the page %s." % pagename.encode("utf-8")
123
116
script += [packLine(['Print', 'Installed MoinMaster page bundle %s.' % os.path.basename(filename)])]
134
def packageCompoundInstaller(self, bundledict, filename):
135
""" Creates a package which installs all other packages. """
140
zf = zipfile.ZipFile(filename, "w", COMPRESSION_LEVEL)
142
script = [packLine(['MoinMoinPackage', '1']), ]
144
script += [packLine(["InstallPackage", "SystemPagesSetup", name + ".zip"])
145
for name in bundledict if name not in (NODIST, EXTRA, ALL, u"English")]
146
script += [packLine(['Print', 'Installed all MoinMaster page bundles.'])]
148
zf.writestr(MOIN_PACKAGE_FILE, u"\n".join(script).encode("utf-8"))
141
151
def mainloop(self):
142
152
# self.options.wiki_url = 'localhost/'
143
153
if self.options.wiki_url and '.' in self.options.wiki_url:
154
165
print "NEVER EVER RUN THIS ON A REAL WIKI!!! This must be run on a local testwiki."
168
self.gd = wikidicts.GroupDict(request)
157
171
print "Building page sets ..."
158
172
pageSets = self.buildPageSets()
160
174
print "Creating packages ..."
161
package_path = os.path.join('tests', 'wiki', 'underlay', 'pages', 'LanguageSetup', 'attachments')
163
# create attachment dir in case it is not there:
164
os.mkdir(package_path)
167
generate_filename = lambda name: os.path.join(package_path, '%s.zip' % name)
168
[self.packagePages(list(pages), generate_filename(name), "ReplaceUnderlay") for name, pages in pageSets.items()]
170
print "Removing pagedirs of packaged pages ..."
171
dontkill = set(['LanguageSetup'])
172
[self.removePages(list(pages - dontkill)) for name, pages in pageSets.items()]
175
generate_filename = lambda name: os.path.join('tests', 'wiki', 'underlay', 'pages', 'SystemPagesSetup', 'attachments', '%s.zip' % name)
177
self.packageCompoundInstaller(pageSets, generate_filename(ALL))
179
[self.packagePages(list(pages), generate_filename(name), "ReplaceUnderlay")
180
for name, pages in pageSets.items() if not name in (u'English', ALL, NODIST)]
182
[self.removePages(list(pages))
183
for name, pages in pageSets.items() if not name in (u'English', ALL)]
174
185
print "Finished."