-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiup.py
executable file
·246 lines (132 loc) · 5.37 KB
/
wikiup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import distutils, os, pkg_resources, urllib2
from setuptools import Command
from distutils.util import convert_path
from distutils import log
from distutils.errors import *
class wikiup(Command):
"""Upload pages to a wiki"""
description = "upload wiki pages specified in wikiup.cfg"
user_options = [
('config-file=', 'C', "file with page info (default: wikiup.cfg)"),
('comment=', 'c', "Revision comment to use (default: none)"),
('changed-file=', 'f', "File to upload (default: all files)"),
]
def initialize_options(self):
self.config_file = self.comment = self.changed_file = None
self.wikis = {}
def finalize_options(self):
if self.config_file is None:
self.config_file = 'wikiup.cfg'
def run(self):
wiki_pages = list(self.get_pages())
if not wiki_pages:
log.warn("No wiki pages defined -- skipping upload")
else:
for wiki, pagename, filename in wiki_pages:
if self.changed_file and filename!=self.changed_file: continue
f = open(convert_path(filename))
page = f.read()
f.close()
plugin = self.get_wiki(wiki)
log.info("uploading %s to %s::%s", filename, wiki, pagename)
plugin.upload_page(pagename, page)
def get_wiki(self, wiki):
"""Return a Wiki object for the named wiki"""
try:
return self.wikis[wiki]
except KeyError:
pass
key = 'wikiup-'+wiki
if key not in self.distribution.command_options:
raise DistutilsOptionError(
"No configuration for wiki %r" % (wiki,))
options = self.distribution.command_options[key]
if 'type' not in options:
raise DistutilsOptionError(
"%r section must include a 'type' setting" % (key,))
plugin_name = options['type'][1]
for ep in pkg_resources.iter_entry_points('wikiup.plugins', plugin_name):
plugin = self.wikis[wiki] = ep.load()(self, wiki, options)
plugin.login()
return plugin
raise DistutilsModuleError(
"No plugin found to handle wiki %r of type %r (found in %r)"
% ((wiki,) + options['type'][::-1])
)
def get_pages(self):
"""Yield wiki,page,file triples from our config file"""
if not os.path.exists(self.config_file):
return # no pages here
for wiki,pages in pkg_resources.split_sections(file(self.config_file)):
if wiki is None and pages:
raise DistutilsOptionError(
"Unlabelled section in "+self.config_file
)
for line in pages:
if '=' in line:
pagename, filename = line.split('=', 1)
else:
pagename = filename = line
yield wiki, pagename.strip(), filename.strip()
class Wiki(object):
"""Base class for wikis"""
required_options = dict(
url = "Base URL of the Wiki",
)
url = None
def __init__(self, command, name, options):
for cls in type(self).__mro__:
if issubclass(cls, Wiki):
for k in cls.required_options:
if k not in options and getattr(self, k, None) is None:
raise DistutilsOptionError(
"Missing option %r for wiki %r: %s" %
(k, name, cls.required_options[k])
)
cls = type(self)
for k in options:
v = getattr(cls, k, None)
if v is not None and not isinstance(v, basestring):
raise DistutilsOptionError(
"Unrecognized option %r for wiki %r: %s" %
(k, name, options[k])
)
setattr(self, k, options[k][1])
self.command = command
self.name = name
def login(self):
raise NotImplementedError
def upload_page(self, pagename, page):
raise NotImplementedError
class OldMoin(Wiki):
"""Old MoinMoin, as used on PEAK DevCenter"""
uid = None
page_format = r"#format rst\n%s"
def login(self):
from mechanize import Browser
self.browser = Browser()
if self.uid:
log.debug("Logging in to %s wiki", self.name)
self.browser.open(self.url+'?action=userform&uid='+self.uid)
def upload_page(self, pagename, page):
url = self.url
if not url.endswith('/'):
url+='/'
url += pagename
self.browser.open(url+'?action=edit')
self.browser.select_form(nr=0)
page = self.page_format.decode('string_escape') % (page,)
if self.browser['savetext'].splitlines()==page.splitlines():
log.info("Page %s is unchanged; not saving", pagename)
return
if self.command.comment:
self.browser['comment'] = self.command.comment
#self.browser['notify']=[]
self.browser['savetext'] = page
self.browser.submit()
def additional_tests():
import doctest
return doctest.DocFileSuite(
'README.txt',
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
)