Attachment 'wikiup.py'
Download 1 #!/usr/bin/python
2
3 """
4 """
5 import xmlrpclib, re, sys, time
6 from getopt import getopt
7
8 def help():
9 print """\
10 -h,--help This help text.
11 -u,--url URL of the wiki to inject into
12 -f,--file supply a file to read page(s) from.
13 -B,--batch pages are separated by '-'*79 and the first line is the name
14 -n,--dry-run actually do the updates
15 -F,--force do the updates even if it's already there (and equal)
16 -v,--verbose verbose operation
17 -b,--base base page from which all pages are made subpages
18 -r,--region name to use in the delimiter regex
19 [ pages ]
20 """
21 sys.exit(0)
22
23 def error(s):
24 print >>sys.stderr, s
25 sys.exit(0)
26
27 opt = {'u:': 'url=', 'r:': 'region=', 'b:': 'base=', 'f:': 'file=',
28 'h': 'help', 'F': 'force', 'u': 'update', 'v': 'verbose',
29 'B': 'batch'}
30
31 try: opts, pages = getopt(sys.argv[1:], ''.join(opt.keys()), opt.values())
32 except: help()
33 #opts = [ (o[1] != '-' and o[1:] or opt[o[2:]], v or 1) for o,v in opts ]
34
35 batch, force, dry_run = False, False, False
36 region, base, fn, url, verb = '','','wiki.wok','http://localhost:8000/wiki', 0
37
38 for k,v in opts:
39 if k in ('--help', '-h'): help() # does not return
40 elif k in ('--verbose','-v'): verb = verb + 1
41 elif k in ('--force', '-F'): force = True
42 elif k in ('--dry-run','-n'): dry_run = True
43 elif k in ('--batch', '-B'): batch = True
44 elif k in ('--url', '-u'): url = v
45 elif k in ('--region', '-r'): region = v+' '
46 elif k in ('--base', '-b'): base = v
47 elif k in ('--file', '-f'): fn = v
48
49 if verb > 2: print opts
50
51 if '?' not in url:
52 url = url + '?action=xmlrpc2'
53 print >>sys.stderr, 'assuming you meant', url
54
55 ab = u'##AUTOMATIC %sBEGINS\n\n' % region
56 ae = u'\n\n##AUTOMATIC %sENDS\n' % region
57
58 rr = re.compile(u"(?s)(?P<before>.*)"+ab+
59 u"(?:~-(?P<time>\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)-~\n\n)?"+
60 u"(?P<auto>.*)"+
61 ae+u"(?P<after>.*)",
62 re.DOTALL)
63
64 try: z = file(fn).read().decode('utf-8')
65 except IOError: error("Couldn't read file \"%s\"" % fn)
66 if not z: error("File \"%s\" appears to be empty" % fn)
67
68 if batch:
69 l = [ x.split('\n', 1) for x in z.split('-'*79+'\n') ]
70 else:
71 try: l = [(pages[0], z)]
72 except: help()
73 pages = False
74
75 try: W = xmlrpclib.ServerProxy(url)
76 except IOError, e:
77 print >>sys.stderr, str(e)
78 sys.exit(5)
79
80 for name, page in l:
81 if pages and name not in pages:
82 continue
83
84 if verb > 1: print name
85
86 while u'\n'*3 in page: page = page.replace(u'\n'*3, u'\n'*2)
87
88 try: it = W.getPage(base+name)
89 except: it = ''
90
91 if type(it) == dict :
92 t = time.strftime(u'%F %T\n\n')
93 rc = W.putPage(base+name, ab + t + page + ae)
94 if verb: print 'N', rc, base+name
95 else:
96 mo = rr.match(it)
97 if mo:
98 d = mo.groupdict()
99 diff = page != d['auto']
100 if verb: print '=?'[diff], d['time'], base+name, len(page)
101
102 else:
103 d = {'before': it, 'after': '', 'auto': page, 'time': 'N/A'}
104 diff = True
105 if verb: print 'n', base+name
106
107 if diff and verb > 2:
108 print d['auto']
109 print '-'*79
110 print page
111
112 d['auto'] = page
113
114 if not diff and not force: continue
115
116 if dry_run: continue
117
118 t = unicode(time.strftime('%F %T'))
119
120 try: rc = W.putPage(base+name,
121 (u"%(before)s" % d)+ ab +
122 u'~-'+t+u'-~'+(u"\n\n%(auto)s" % d) +
123 ae + (u"%(after)s" % d))
124 except Exception,e:
125 print e
126 rc = 0
127 print 'U', ['ok!','KO!'][not rc], base+name
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.