2 from __future__ import print_function
3 from collections import namedtuple
4 from contextlib import contextmanager
5 from os.path import basename, dirname, realpath
6 from pipes import quote
7 from subprocess import PIPE, Popen, check_call
8 from traceback import extract_stack
9 import 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')
37 helpers.mkdirp(_bup_tmp)
41 def test_tempdir(prefix):
42 initial_failures = wvfailure_count()
43 tmpdir = tempfile.mkdtemp(dir=_bup_tmp, prefix=prefix)
45 if wvfailure_count() == initial_failures:
46 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
47 subprocess.call(['rm', '-rf', tmpdir])
51 if isinstance(cmd, basestring):
52 print(cmd, file=sys.stderr)
54 print(' '.join(map(quote, cmd)), file=sys.stderr)
57 SubprocInfo = namedtuple('SubprocInfo', ('out', 'err', 'rc', 'p'))
59 def exo(cmd, input=None, stdin=None, stdout=PIPE, stderr=PIPE,
60 shell=False, check=True):
61 """Print cmd to stderr, run it, and return the resulting SubprocInfo.
62 The keyword arguments are passed to Popen, and the defaults
63 capture both stdout and stderr.
68 stdin=(PIPE if input else stdin),
72 out, err = p.communicate(input=input)
73 if check and p.returncode != 0:
74 raise Exception('subprocess %r failed with status %d%s'
75 % (' '.join(map(quote, cmd)),
77 (', stderr: %r' % err) if stderr else ''))
78 return SubprocInfo(out=out, err=err, rc=p.returncode, p=p)
80 def exc(cmd, input=None, stdout=None, stderr=None, shell=False, check=True):
81 """Print cmd to stderr, run it, and return the resulting SubprocInfo.
82 The keyword arguments are passed to Popen, and the defaults
83 allow stdout and stderr to pass through.
86 return exo(cmd, input=input, stdout=stdout, stderr=stderr, shell=shell,