Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / www / crash / crash.py
blobb8a1bf7db677858cef83abaa185502877e507b3e
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 import cgi
5 import os
6 # Regular Expressions
7 import re
8 import smtplib
9 import email
10 from email.MIMEText import MIMEText
11 import sqlite3
13 from traceback import format_exception
14 from sys import exc_info
15 import logging
17 logging.basicConfig(level=logging.DEBUG,
18 format="%(asctime)s %(levelname)-8s %(message)s",
19 datefmt="%a, %d %b %Y %H:%M:%S",
20 filename="crash.log",
21 filemode="a")
23 # create table crash(ip TEXT, crashlog TEXT);
24 db = 'crash.sqlite'
27 def mailreport(crashlog, ip, revision):
28 mail = MIMEText(crashlog)
29 mail["To"] = "bugs@cyberduck.ch"
30 mail["From"] = "noreply@cyberduck.ch"
31 if revision:
32 mail["Subject"] = "Cyberduck Crash Report (r" + revision + ") from " + ip
33 else:
34 mail["Subject"] = "Cyberduck Crash Report from " + ip
35 mail["Date"] = email.Utils.formatdate(localtime=1)
36 mail["Message-ID"] = email.Utils.make_msgid()
37 s = smtplib.SMTP()
38 s.connect("localhost")
39 s.sendmail("noreply@cyberduck.ch", "bugs@cyberduck.ch", mail.as_string())
40 s.quit()
43 if __name__ == "__main__":
44 print "Content-type: text/html"
45 print
46 try:
47 form = cgi.FieldStorage()
48 revision = None
49 if form.has_key("revision"):
50 revision = form["revision"].value
51 platform = None
52 if form.has_key("os"):
53 platform = form["os"].value
54 if not revision:
55 useragent = cgi.escape(os.environ["HTTP_USER_AGENT"])
56 revision = re.match(r"Cyberduck \(([0-9]+)\)", useragent).group(1)
57 ip = cgi.escape(os.environ["REMOTE_ADDR"])
58 if form.has_key("crashlog"):
59 logging.info("Crash Report from %s for OS %s and revision %s", ip, platform, revision)
60 crashlog = form["crashlog"].value
62 #add database entry
63 conn = sqlite3.connect(db)
64 c = conn.cursor()
66 row = (ip, crashlog, revision, platform)
67 try:
68 c.execute('insert into crash values(?,?,?,?)', row)
69 except sqlite3.IntegrityError, (ErrorMessage):
70 logging.error('Error adding crashlog from IP %s:%s', ip, ErrorMessage)
71 pass
72 finally:
73 # Save (commit) the changes
74 conn.commit()
75 # We can also close the cursor if we are done with it
76 c.close()
78 #send mail
79 if revision:
80 mailreport(crashlog, ip, revision)
81 except:
82 logging.error("Unexpected error:".join(format_exception(*exc_info())))
83 cgi.print_exception()