X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=geekigeeki.py;h=f6ba26c967419f5c219d98d313bdc19932630883;hb=4e1ebf231d757ce8887705fe2a271c68a2eed1e4;hp=081517a64853ee460f3346b59ff04b201d706c8a;hpb=cd485ccea608860b133c50432d5ffb51aaf0eb8b;p=geekigeeki.git diff --git a/geekigeeki.py b/geekigeeki.py index 081517a..f6ba26c 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -3,7 +3,7 @@ # # Copyright 1999, 2000 Martin Pool # Copyright 2002 Gerardo Poggiali -# Copyright 2007, 2008 Bernie Innocenti +# Copyright 2007, 2008, 2009 Bernie Innocenti # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -33,7 +33,8 @@ file_re = re.compile(r"^\b([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)\b$") img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE) video_re = re.compile(r"^.*\.(ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE) url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$") -link_re = re.compile("(?:\[\[|{{)([^\s\|]+)(?:\s*\|\s*([^\]]+)|)(?:\]\]|}})") +link_re = re.compile(r"(?:\[\[|{{)([^\s\|]+)(?:\s*\|\s*([^\]]+)|)(?:\]\]|}})") +ext_re = re.compile(r"\.([^\./]+)$") title_done = False @@ -77,7 +78,7 @@ def relative_url(pathname, privileged=False): else: url = script_name() pathname = url + '/' + pathname - return pathname + return cgi.escape(pathname, quote=True) def permalink(s): return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip()) @@ -86,13 +87,6 @@ def permalink(s): def emit_header(mime_type="text/html"): print "Content-type: " + mime_type + "; charset=utf-8\n" -def sendfile(dest_file, src_file): - """Efficiently copy file data between file descriptors""" - while 1: - data = src_file.read(65536) - if not data: break - dest_file.write(data) - def send_guru(msg_text, msg_type): if not msg_text: return print '
'
@@ -100,12 +94,10 @@ def send_guru(msg_text, msg_type):
         print '    Software Failure.  Press left mouse button to continue.\n'
     print msg_text
     if msg_type == 'error':
-        print '\n      Guru Meditation #DEADBEEF.ABADC0DE'
+        print '\n           Guru Meditation #DEADBEEF.ABADC0DE'
     print '
' - try: - sendfile(sys.stdout, open('gurumeditation.js', 'rb')) - except IOError, err: - pass + print '' \ + % relative_url('sys/GuruMeditation.js') def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False): global title_done @@ -151,7 +143,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal # Navbar print '" send_footer() - def send_raw(self, mimetype='text/plain'): - if self.can_read(): - body = self.get_raw_body() - emit_header(mimetype) - print body - else: + def send_raw(self, mimetype='text/plain', args=[]): + if not self.can_read(): send_title(None, msg_text='Read access denied by ACLs', msg_type='notice') + return - def send_image(self, mimetype, args=[]): if 'maxwidth' in args: import subprocess emit_header(mimetype) @@ -752,7 +756,9 @@ class Page: subprocess.check_call(['gm', 'convert', self._filename(), '-scale', args['maxwidth'].value + ' >', '-']) else: - self.send_raw(mimetype) + body = self.get_raw_body() + emit_header(mimetype) + print body def _write_file(self, data): tmp_filename = self._tmp_filename() @@ -775,19 +781,13 @@ class Page: self._write_file(newdata) rc = 0 if post_edit_hook: - # FIXME: what's the std way to perform shell quoting in python? - cmd = ( post_edit_hook - + " '" + data_dir + '/' + self.page_name - + "' '" + remote_user() - + "' '" + remote_host() - + "' '" + changelog + "'" - ) - out = os.popen(cmd) - output = out.read() - rc = out.close() + import subprocess + cmd = [ post_edit_hook, data_dir + '/' + self.page_name, remote_user(), remote_host(), changelog] + child = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) + output = child.stdout.read() + rc = child.wait() if rc: - self.msg_text += "Post-editing hook returned %d.\n" % rc - self.msg_text += 'Command was: ' + cmd + '\n' + self.msg_text += "Post-editing hook returned %d. Command was:\n'%s'\n" % (rc, "' '".join(cmd)) if output: self.msg_text += 'Output follows:\n' + output else: @@ -803,18 +803,13 @@ def main(): else: query = query_string() if file_re.match(query): - if word_re.match(query): - Page(query).format() + # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension! + from mimetypes import MimeTypes + mimetype, encoding = MimeTypes().guess_type(query) + if mimetype: + Page(query).send_raw(mimetype=mimetype, args=form) else: - from mimetypes import MimeTypes - mimetype, encoding = MimeTypes().guess_type(query) - if mimetype: - if mimetype.startswith('image/'): - Page(query).send_image(mimetype=mimetype, args=form) - else: - Page(query).send_raw(mimetype=mimetype) - else: - Page(query).format() + Page(query).format() else: send_httperror("403 Forbidden", query)