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
|
import errno
import json
import os
import StringIO
import time
import cliapp
import requests
import yaml
import yarnutils
datadir = os.environ['DATADIR']
srcdir = os.environ['SRCDIR']
vars = yarnutils.Variables(datadir)
MAX_CAT_TIME = 5 # seconds
def cat(filename):
start = time.time()
while time.time() - start < MAX_CAT_TIME:
try:
with open(filename) as f:
data = f.read()
if len(data) == 0:
continue
return data
except (IOError, OSError) as e:
if e.errno == errno.ENOENT:
continue
raise
raise Exception("cat took more then %s seconds" % MAX_CAT_TIME)
def write(filename, content):
with open(filename, 'w') as f:
f.write(content)
def git(repo, *argv):
return cliapp.runcmd(['git'] + list(argv), cwd=repo)
def controller_url(port, path):
return 'http://localhost:{}{}'.format(port, path)
def request(method, url, body=None):
funcs = {
'POST': requests.post,
'PUT': requests.put,
'GET': requests.get,
'DELETE': requests.delete,
}
headers = {
'Content-Type': 'application/json',
}
response = funcs[method](
url,
headers=headers,
data=body,
)
return response.status_code, response.text
def parse_json(text):
return json.loads(text)
def parse_yaml(text):
f = StringIO.StringIO(text)
return yaml.safe_load(stream=f)
def unescape(text):
def helper(text):
while text:
if text.startswith('\\n'):
skip = 2
answer = '\n'
else:
skip = 1
answer = text[0]
text = text[skip:]
yield answer
return ''.join(helper(text))
|