2 from __future__ import absolute_import, print_function
3 from collections import namedtuple
4 from contextlib import contextmanager
5 from os.path import abspath, basename, dirname, realpath
6 from pipes import quote
7 from subprocess import PIPE, Popen
8 from traceback import extract_stack
9 import errno, os, subprocess, sys, tempfile
11 from wvtest import WVPASSEQ, wvfailure_count
13 from bup import helpers
17 def no_lingering_errors():
19 if helpers.saved_errors:
21 src_file, src_line, src_func, src_txt = bt[-4]
22 msg = 'saved_errors ' + repr(helpers.saved_errors)
23 print('! %-70s %s' % ('%s:%-4d %s' % (basename(src_file),
29 helpers.clear_errors()
32 helpers.clear_errors()
35 # Assumes (of course) this file is at the top-level of the source tree
36 _bup_tmp = realpath(dirname(__file__) + '/t/tmp')
40 if e.errno != errno.EEXIST:
45 def test_tempdir(prefix):
46 initial_failures = wvfailure_count()
47 tmpdir = tempfile.mkdtemp(dir=_bup_tmp, prefix=prefix)
49 if wvfailure_count() == initial_failures:
50 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
51 subprocess.call(['rm', '-rf', tmpdir])
54 ex_res = namedtuple('SubprocResult', ['out', 'err', 'proc', 'rc'])
56 def run(cmd, check=True, input=None, **kwargs):
57 """Run a subprocess as per subprocess.Popen(cmd, **kwargs) followed by
58 communicate(input=input). If check is true, then throw an
59 exception if the subprocess exits with non-zero status. Return a
64 assert 'stdin' not in kwargs
65 kwargs['stdin'] = PIPE
66 p = Popen(cmd, **kwargs)
67 out, err = p.communicate(input=input)
68 if check and p.returncode != 0:
69 raise Exception('subprocess %r failed with status %d%s'
70 % (' '.join(map(quote, cmd)), p.returncode,
71 (', stderr: %r' % err) if err else ''))
72 return ex_res(out=out, err=err, proc=p, rc=p.returncode)
75 if isinstance(cmd, basestring):
76 print(cmd, file=sys.stderr)
78 print(' '.join(map(quote, cmd)), file=sys.stderr)
80 def ex(cmd, **kwargs):
81 """Print cmd to stderr and then run it as per ex(...).
82 Print the subprocess stderr to stderr if stderr=PIPE and there's
86 result = run(cmd, **kwargs)
88 sys.stderr.write(result.err)
91 def exo(cmd, **kwargs):
92 """Print cmd to stderr and then run it as per ex(..., stdout=PIPE).
93 Print the subprocess stderr to stderr if stderr=PIPE and there's
97 assert 'stdout' not in kwargs
98 kwargs['stdout'] = PIPE
99 return ex(cmd, **kwargs)