~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/script/maint/mkpagepacks.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
MoinMoin - Package Generator
4
4
 
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.
9
8
"""
10
9
 
11
10
import os
12
11
import zipfile
 
12
from sets import Set
13
13
from datetime import datetime
14
14
 
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
25
 
 
26
 
 
 
19
 
 
20
EXTRA = u'extra'
 
21
NODIST = u'nodist'
 
22
ALL = u'all_languages'
27
23
COMPRESSION_LEVEL = zipfile.ZIP_STORED
28
24
 
29
25
class PluginScript(MoinScript):
37
33
General syntax: moin [options] maint mkpagepacks [mkpagepacks-options]
38
34
 
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/
41
37
 
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
54
 
 
55
 
        all_pages = set(request.rootpage.getPageList())
56
 
        packaged_pages = set()
57
 
 
58
 
        languages = i18n.wikiLanguages()
59
 
        pageset_names = i18n.strings.pagesets
60
50
        pageSets = {}
61
 
        for lang in languages:
62
 
            def trans(text, request=request, lang=lang, **kw):
63
 
                return i18n.getText(text, request, lang, **kw)
64
 
 
65
 
            try:
66
 
                lang_long = languages[lang]['x-language-in-english']
67
 
                lang_long = lang_long.replace('/', '_').replace(' ', '_')
68
 
            except KeyError:
69
 
                lang_long = lang
70
 
 
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
76
 
                if lang != 'en':
77
 
                    pageset -= pageset_orig
78
 
                if pageset:
79
 
                    print key, len(pageset)
80
 
                    pageSets[key] = pageset
81
 
                    packaged_pages |= pageset
82
 
 
83
 
        not_packaged_pages = all_pages - packaged_pages
84
 
        pageSets['00_needs_fixing'] = not_packaged_pages
 
51
 
 
52
        allPages = Set(request.rootpage.getPageList())
 
53
 
 
54
        systemPages = wikidicts.Group(request, "SystemPagesGroup").members()
 
55
 
 
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)
 
60
 
 
61
        langPages = Set()
 
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
 
67
 
 
68
        specialPages = Set(["SystemPagesGroup"])
 
69
 
 
70
        masterNonSystemPages = allPages - langPages - specialPages
 
71
 
 
72
        moinI18nPages = Set([x for x in masterNonSystemPages if x.startswith("MoinI18n")])
 
73
 
 
74
        nodistPages = moinI18nPages | Set(["InterWikiMap", ])
 
75
 
 
76
        extraPages = masterNonSystemPages - nodistPages
 
77
 
 
78
        pageSets[ALL] = langPages
 
79
 
 
80
        for name in pageSets.keys():
 
81
            if name not in (u"English"):
 
82
                pageSets[name] -= pageSets[u"English"]
 
83
                pageSets[name] -= nodistPages
 
84
 
 
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
85
87
        return pageSets
86
88
 
87
89
    def packagePages(self, pagelist, filename, function):
91
93
            os.remove(filename)
92
94
        except OSError:
93
95
            pass
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:
97
 
            return
98
 
 
99
96
        zf = zipfile.ZipFile(filename, "w", COMPRESSION_LEVEL)
100
97
 
101
 
        script = [packLine(['MoinMoinPackage', '1']), ]
102
 
 
103
98
        cnt = 0
104
 
        for pagename in existing_pages:
 
99
        script = [packLine(['MoinMoinPackage', '1']), ]
 
100
 
 
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:
 
104
            if page.exists():
109
105
                cnt += 1
110
 
                zipname = "%d" % cnt
111
 
                script.append(packLine(["ReplaceUnderlayAttachment", zipname, attname, pagename]))
112
 
                attpath = AttachFile.getFilename(request, pagename, attname)
113
 
                zf.write(attpath, zipname)
114
 
 
115
 
            cnt += 1
116
 
            zipname = "%d" % cnt
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"))
 
111
            else:
 
112
                #import sys
 
113
                #print >>sys.stderr, "Could not find the page %s." % pagename.encode("utf-8")
 
114
                pass
122
115
 
123
116
        script += [packLine(['Print', 'Installed MoinMaster page bundle %s.' % os.path.basename(filename)])]
124
117
 
138
131
            except:
139
132
                pass
140
133
 
 
134
    def packageCompoundInstaller(self, bundledict, filename):
 
135
        """ Creates a package which installs all other packages. """
 
136
        try:
 
137
            os.remove(filename)
 
138
        except OSError:
 
139
            pass
 
140
        zf = zipfile.ZipFile(filename, "w", COMPRESSION_LEVEL)
 
141
 
 
142
        script = [packLine(['MoinMoinPackage', '1']), ]
 
143
 
 
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.'])]
 
147
 
 
148
        zf.writestr(MOIN_PACKAGE_FILE, u"\n".join(script).encode("utf-8"))
 
149
        zf.close()
 
150
 
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:
147
157
        self.init_request() # this request will work on a test wiki in tests/wiki/ directory
148
158
                            # we assume that there are current moinmaster pages there
149
159
        request = self.request
 
160
        request.form = request.args = request.setup_args()
150
161
 
151
162
        if not ('tests/wiki' in request.cfg.data_dir.replace("\\", "/") and 'tests/wiki' in request.cfg.data_underlay_dir.replace("\\", "/")):
152
163
            import sys
154
165
            print "NEVER EVER RUN THIS ON A REAL WIKI!!! This must be run on a local testwiki."
155
166
            return
156
167
 
 
168
        self.gd = wikidicts.GroupDict(request)
 
169
        self.gd.reset()
 
170
 
157
171
        print "Building page sets ..."
158
172
        pageSets = self.buildPageSets()
159
173
 
160
174
        print "Creating packages ..."
161
 
        package_path = os.path.join('tests', 'wiki', 'underlay', 'pages', 'LanguageSetup', 'attachments')
162
 
        try:
163
 
            # create attachment dir in case it is not there:
164
 
            os.mkdir(package_path)
165
 
        except OSError:
166
 
            pass
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()]
169
 
 
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)
 
176
 
 
177
        self.packageCompoundInstaller(pageSets, generate_filename(ALL))
 
178
 
 
179
        [self.packagePages(list(pages), generate_filename(name), "ReplaceUnderlay")
 
180
            for name, pages in pageSets.items() if not name in (u'English', ALL, NODIST)]
 
181
 
 
182
        [self.removePages(list(pages))
 
183
            for name, pages in pageSets.items() if not name in (u'English', ALL)]
173
184
 
174
185
        print "Finished."
175
186