forked from codeforamerica/git-jekyll-preview
-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
182 lines (147 loc) · 5.59 KB
/
util.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
from __future__ import division
from fcntl import flock, LOCK_EX, LOCK_UN
from contextlib import contextmanager
from traceback import format_exc
from datetime import timedelta
from logging import getLogger
from functools import wraps
from urllib import urlencode
from urlparse import urlparse, urlunparse, parse_qsl
from os import environ
from flask import make_response, Response, render_template
from requests.exceptions import RequestException, ReadTimeout
import raven
jlogger = getLogger('precog')
ERR_NO_REPOSITORY = 'Missing repository'
ERR_TESTS_PENDING = 'Test in progress'
ERR_TESTS_FAILED = 'Test failed'
ERR_NO_REF_STATUS = 'Missing statuses for ref'
err_codes = dict(NO_REPOSITORY=ERR_NO_REPOSITORY, TESTS_PENDING=ERR_TESTS_PENDING,
TESTS_FAILED=ERR_TESTS_FAILED, NO_REF_STATUS=ERR_NO_REF_STATUS)
@contextmanager
def locked_file(path):
''' Create a file, lock it, then unlock it. Use as a context manager.
Yields nothing.
'''
jlogger.debug('Locking ' + path)
try:
file = open(path, 'a')
flock(file, LOCK_EX)
yield
finally:
jlogger.debug('Unlocking ' + path)
flock(file, LOCK_UN)
def errors_logged(route_function):
'''
'''
def report(args, kwargs):
if 'SENTRY_DSN' in environ:
client = raven.Client(environ['SENTRY_DSN'])
client.user_context({'args': args, 'kwargs': kwargs})
client.captureException()
jlogger.error(format_exc())
@wraps(route_function)
def wrapper(*args, **kwargs):
try:
result = route_function(*args, **kwargs)
except RequestException as error:
report(args, kwargs)
jlogger.error('Failed to {} {}'.format(error.request.method, error.request.url))
hostname = urlparse(error.request.url).netloc
message = 'An upstream connection to {} failed'.format(hostname)
kwargs = dict(codes=err_codes, error=Exception(message))
return make_response(render_template('error-runtime.html', **kwargs), 500)
except Exception as e:
report(args, kwargs)
raise
return Response('Nope.', headers={'Content-Type': 'text/plain'}, status=500)
else:
return result
return wrapper
def nice_relative_time(delta):
'''
>>> nice_relative_time(timedelta(days=2))
'2 days'
>>> nice_relative_time(timedelta(hours=37))
'2 days'
>>> nice_relative_time(timedelta(hours=36))
'36 hours'
>>> nice_relative_time(timedelta(days=1))
'24 hours'
>>> nice_relative_time(timedelta(hours=2))
'2 hours'
>>> nice_relative_time(timedelta(minutes=91))
'2 hours'
>>> nice_relative_time(timedelta(minutes=90))
'90 minutes'
>>> nice_relative_time(timedelta(hours=1))
'60 minutes'
>>> nice_relative_time(timedelta(minutes=2))
'2 minutes'
>>> nice_relative_time(timedelta(seconds=91))
'2 minutes'
>>> nice_relative_time(timedelta(seconds=90))
'90 seconds'
>>> nice_relative_time(timedelta(minutes=1))
'60 seconds'
'''
seconds = delta.seconds + delta.days * 86400
if seconds > 1.5 * 86400:
return '{:.0f} days'.format(seconds / 86400)
if seconds > 1.5 * 3600:
return '{:.0f} hours'.format(seconds / 3600)
if seconds > 1.5 * 60:
return '{:.0f} minutes'.format(seconds / 60)
return '{:.0f} seconds'.format(seconds)
def parse_webhook_config(*strings):
'''
>>> parse_webhook_config('')
{}
>>> parse_webhook_config()
{}
>>> parse_webhook_config('mapzen/blog:abc:def') \
== {'mapzen/blog': dict(secret='abc', token='def')}
True
>>> parse_webhook_config('mapzen/blog:abc:def:ghi') \
== {'mapzen/blog': dict(secret='abc', token='def:ghi')}
True
>>> parse_webhook_config('mapzen/blog:abc:def mapzen/style:ghi:jkl') \
== {'mapzen/blog': dict(secret='abc', token='def'), \
'mapzen/style': dict(secret='ghi', token='jkl')}
True
>>> parse_webhook_config('mapzen/blog:abc:def', 'mapzen/style:ghi:jkl') \
== {'mapzen/blog': dict(secret='abc', token='def'), \
'mapzen/style': dict(secret='ghi', token='jkl')}
True
>>> parse_webhook_config('mapzen/blog:abc:def', 'mapzen/style:ghi:jkl', 'mapzen/blog:mno:pqr') \
== {'mapzen/blog': dict(secret='mno', token='pqr'), \
'mapzen/style': dict(secret='ghi', token='jkl')}
True
'''
sites = dict()
for site in ' '.join(strings).split():
name, secret, token = site.split(':', 2)
sites[name] = dict(secret=secret, token=token)
return sites
def extend_querystring(url, new_args):
'''
>>> extend_querystring('http://example.com/path', dict())
'http://example.com/path'
>>> extend_querystring('http://example.com/path', dict(foo='bar'))
'http://example.com/path?foo=bar'
>>> extend_querystring('http://example.com/path?foo=bar', dict())
'http://example.com/path?foo=bar'
>>> extend_querystring('http://example.com/path?foo=bar', dict(foo='new'))
'http://example.com/path?foo=new'
>>> 'foo=bar' in extend_querystring('http://example.com/path?foo=bar', dict(doo='new'))
True
>>> 'doo=new' in extend_querystring('http://example.com/path?foo=bar', dict(doo='new'))
True
'''
scheme, host, path, params, query, frag = urlparse(url)
query_dict = dict(parse_qsl(query))
query_dict.update(new_args)
return urlunparse((scheme, host, path, params, urlencode(query_dict), frag))
if __name__ == '__main__':
import doctest
doctest.testmod()