-
Notifications
You must be signed in to change notification settings - Fork 129
/
helpers.py
75 lines (61 loc) · 1.92 KB
/
helpers.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
#!/usr/bin/env python2
#
# developed by Sergey Markelov (2013)
#
import StringIO
import zlib
import gzip
import os
import errno
from datetime import datetime
RESULTS_DIR = "results/"
def getLoggingTime():
dt = datetime.now()
dtStr = dt.strftime("%Y-%m-%d %H:%M:%S") + "." + str(dt.microsecond / 100000)
return dtStr
def createResultsDir(f):
"""
Creates results dir where all output will go based on
__file__ object which is passed through f
Note: results dir is created with 755 mode
RESULTS_DIR global variable will be updated
"""
global RESULTS_DIR
scriptDir = os.path.dirname(os.path.realpath(f))
resultsDir = scriptDir + "/" + RESULTS_DIR
try:
os.makedirs(resultsDir, 0o755)
except OSError, e:
if e.errno != errno.EEXIST:
raise
RESULTS_DIR = resultsDir
def getResponseBody(response):
""" Returns response.read(), but does gzip deflate if appropriate"""
encoding = response.info().get("Content-Encoding")
if encoding in ("gzip", "x-gzip", "deflate"):
page = response.read()
if encoding == "deflate":
return zlib.decompress(page)
else:
fd = StringIO.StringIO(page)
try:
data = gzip.GzipFile(fileobj = fd)
try: content = data.read()
finally: data.close()
finally:
fd.close()
return content
else:
return response.read()
def dumpErrorPage(page):
"""
Dumps page into a file. The resulting file is placed into RESULTS_DIR subfolder
with error_dtStr.html name, where dtStr is current date and time with
microseconds precision
"""
if page is None: raise TypeError("page is None")
dtStr = datetime.now().strftime("%Y%m%d-%H%M%S.%f")
filename = "error_" + dtStr + ".html"
with open(RESULTS_DIR + filename, "w") as fd:
fd.write(page)
return filename