forked from bup/bup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuptest.py
104 lines (87 loc) · 3.2 KB
/
buptest.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
from __future__ import absolute_import, print_function
from collections import namedtuple
from contextlib import contextmanager
from os.path import abspath, basename, dirname, realpath
from pipes import quote
from subprocess import PIPE, Popen
from traceback import extract_stack
import errno, os, subprocess, sys, tempfile
from wvtest import WVPASSEQ, wvfailure_count
from bup import helpers
from bup.compat import fsencode, str_type
from bup.io import byte_stream
@contextmanager
def no_lingering_errors():
def fail_if_errors():
if helpers.saved_errors:
bt = extract_stack()
src_file, src_line, src_func, src_txt = bt[-4]
msg = 'saved_errors ' + repr(helpers.saved_errors)
print('! %-70s %s' % ('%s:%-4d %s' % (basename(src_file),
src_line,
msg),
'FAILED'))
sys.stdout.flush()
fail_if_errors()
helpers.clear_errors()
yield
fail_if_errors()
helpers.clear_errors()
# Assumes (of course) this file is at the top-level of the source tree
_bup_tmp = realpath(dirname(fsencode(__file__))) + b'/t/tmp'
try:
os.makedirs(_bup_tmp)
except OSError as e:
if e.errno != errno.EEXIST:
raise
@contextmanager
def test_tempdir(prefix):
initial_failures = wvfailure_count()
tmpdir = tempfile.mkdtemp(dir=_bup_tmp, prefix=prefix)
yield tmpdir
if wvfailure_count() == initial_failures:
subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
subprocess.call(['rm', '-rf', tmpdir])
ex_res = namedtuple('SubprocResult', ['out', 'err', 'proc', 'rc'])
def run(cmd, check=True, input=None, **kwargs):
"""Run a subprocess as per subprocess.Popen(cmd, **kwargs) followed by
communicate(input=input). If check is true, then throw an
exception if the subprocess exits with non-zero status. Return a
SubprocResult tuple.
"""
if input:
assert 'stdin' not in kwargs
kwargs['stdin'] = PIPE
p = Popen(cmd, **kwargs)
out, err = p.communicate(input=input)
if check and p.returncode != 0:
raise Exception('subprocess %r failed with status %d%s'
% (cmd, p.returncode,
(', stderr: %r' % err) if err else ''))
return ex_res(out=out, err=err, proc=p, rc=p.returncode)
def logcmd(cmd):
s = helpers.shstr(cmd)
if isinstance(cmd, str_type):
print(s, file=sys.stderr)
else:
# bytes - for now just escape it
print(s.decode(errors='backslashreplace'), file=sys.stderr)
def ex(cmd, **kwargs):
"""Print cmd to stderr and then run it as per ex(...).
Print the subprocess stderr to stderr if stderr=PIPE and there's
any data.
"""
logcmd(cmd)
result = run(cmd, **kwargs)
if result.err:
sys.stderr.flush()
byte_stream(sys.stderr).write(result.err)
return result
def exo(cmd, **kwargs):
"""Print cmd to stderr and then run it as per ex(..., stdout=PIPE).
Print the subprocess stderr to stderr if stderr=PIPE and there's
any data.
"""
assert 'stdout' not in kwargs
kwargs['stdout'] = PIPE
return ex(cmd, **kwargs)