-
Notifications
You must be signed in to change notification settings - Fork 192
/
commit.py
115 lines (93 loc) · 3.63 KB
/
commit.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
import os
import sys
import random
import re
try:
from hashlib import md5
except ImportError:
from md5 import md5
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.escape import xhtml_unescape
from tornado.options import define, options
define("port", default=5000, help="run on the given port", type=int)
names = ['Nick', 'Steve', 'Andy', 'Qi', 'Fanny', 'Sarah', 'Cord', 'Todd',
'Chris', 'Pasha', 'Gabe', 'Tony', 'Jason', 'Randal', 'Ali', 'Kim',
'Rainer', 'Guillaume', 'Kelan', 'David', 'John', 'Stephen', 'Tom', 'Steven',
'Jen', 'Marcus', 'Edy', 'Rachel']
humans_file = os.path.join(os.path.dirname(__file__), 'static', 'humans.txt')
messages_file = os.path.join(os.path.dirname(__file__), 'commit_messages.txt')
messages = {}
# Create a hash table of all commit messages
with open(messages_file) as messages_input:
for line in messages_input.readlines():
messages[md5(line).hexdigest()] = line
with open(humans_file) as humans_input:
humans_content = humans_input.read()
for line in humans_content.split("\n"):
if "Name:" in line:
data = line[6:].rstrip()
if data.find("github:") == 0:
names.append(data[7:])
else:
names.append(data.split(" ")[0])
num_re = re.compile(r"XNUM([0-9,]*)X")
def fill_line(message):
message = message.replace('XNAMEX', random.choice(names))
message = message.replace('XUPPERNAMEX', random.choice(names).upper())
message = message.replace('XLOWERNAMEX', random.choice(names).lower())
nums = num_re.findall(message)
while nums:
start = 1
end = 999
value = nums.pop(0) or str(end)
if "," in value:
position = value.index(",")
if position == 0: # XNUM,5X
end = int(value[1:])
elif position == len(value) - 1: # XNUM5,X
start = int(value[:position])
else: # XNUM1,5X
start = int(value[:position])
end = int(value[position+1:])
else:
end = int(value)
if start > end:
end = start * 2
randint = random.randint(start, end)
message = num_re.sub(str(randint), message, count=1)
return message
class MainHandler(tornado.web.RequestHandler):
def get(self, message_hash=None):
if not message_hash:
message_hash = random.choice(messages.keys())
elif message_hash not in messages:
raise tornado.web.HTTPError(404)
message = fill_line(messages[message_hash])
self.output_message(message, message_hash)
def output_message(self, message, message_hash):
self.render('index.html', message=message, message_hash=message_hash)
class PlainTextHandler(MainHandler):
def output_message(self, message, message_hash):
self.set_header('Content-Type', 'text/plain')
self.write(xhtml_unescape(message).replace('<br/>', '\n'))
class HumansHandler(tornado.web.RequestHandler):
def get(self):
self.set_header('Content-Type', 'text/plain')
self.write(humans_content)
settings = {
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
}
application = tornado.web.Application([
(r'/', MainHandler),
(r'/([a-z0-9]+)', MainHandler),
(r'/index.txt', PlainTextHandler),
(r'/([a-z0-9]+)/index.txt', PlainTextHandler),
(r'/humans.txt', HumansHandler),
], **settings)
if __name__ == '__main__':
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(os.environ.get("PORT", 5000))
tornado.ioloop.IOLoop.instance().start()