forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.py
102 lines (89 loc) · 2.49 KB
/
framework.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
# Copyright 2012, Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can
# be found in the LICENSE file.
import os
import shlex
from subprocess import Popen, PIPE
import time
import unittest
import utils
class TestCase(unittest.TestCase):
@classmethod
def setenv(cls, env):
cls.env = env
def assertContains(self, b, a):
self.assertTrue(a in b, "%r not found in %r" % (a, b))
class MultiDict(dict):
def __getattr__(self, name):
v = self[name]
if type(v)==dict:
v=MultiDict(v)
return v
def mget(self, mkey, default=None):
keys = mkey.split(".")
try:
v = self
for key in keys:
v = v[key]
except KeyError:
v = default
if type(v)==dict:
v = MultiDict(v)
return v
class Tailer(object):
def __init__(self, filepath, flush=None, sleep=0, timeout=10.0):
self.filepath = filepath
self.flush = flush
self.sleep = sleep
self.timeout = timeout
self.f = None
self.reset()
def reset(self):
"""Call reset when you want to start using the tailer."""
if self.flush:
self.flush()
else:
time.sleep(self.sleep)
# Re-open the file if open.
if self.f:
self.f.close()
self.f = None
# Wait for file to exist.
timeout = self.timeout
while not os.path.exists(self.filepath):
timeout = utils.wait_step('file exists: ' + self.filepath, timeout)
self.f = open(self.filepath)
self.f.seek(0, os.SEEK_END)
self.pos = self.f.tell()
def read(self):
"""Returns a string which may contain multiple lines."""
if self.flush:
self.flush()
else:
time.sleep(self.sleep)
self.f.seek(0, os.SEEK_END)
newpos = self.f.tell()
if newpos < self.pos:
return ""
self.f.seek(self.pos, os.SEEK_SET)
size = newpos-self.pos
self.pos = newpos
return self.f.read(size)
def readLines(self):
"""Returns a list of read lines."""
return self.read().splitlines()
# FIXME: Hijacked from go/vt/tabletserver/test.py
# Reuse when things come together
def execute(cmd, trap_output=False, verbose=False, **kargs):
args = shlex.split(cmd)
if trap_output:
kargs['stdout'] = PIPE
kargs['stderr'] = PIPE
if verbose:
print "Execute:", cmd, ', '.join('%s=%s' % x for x in kargs.iteritems())
proc = Popen(args, **kargs)
proc.args = args
stdout, stderr = proc.communicate()
if proc.returncode:
raise Exception('FAIL: %s %s %s' % (args, stdout, stderr))
return stdout, stderr