2 from __future__ import print_function
3 from contextlib import contextmanager
4 from os.path import basename, dirname, realpath
5 from pipes import quote
6 from subprocess import PIPE, Popen, check_call
7 from traceback import extract_stack
8 import subprocess, sys, tempfile
10 from wvtest import WVPASSEQ, wvfailure_count
12 from bup import helpers
16 def no_lingering_errors():
18 if helpers.saved_errors:
20 src_file, src_line, src_func, src_txt = bt[-4]
21 msg = 'saved_errors ' + repr(helpers.saved_errors)
22 print('! %-70s %s' % ('%s:%-4d %s' % (basename(src_file),
28 helpers.clear_errors()
31 helpers.clear_errors()
34 # Assumes (of course) this file is at the top-level of the source tree
35 _bup_tmp = realpath(dirname(__file__) + '/t/tmp')
36 helpers.mkdirp(_bup_tmp)
40 def test_tempdir(prefix):
41 initial_failures = wvfailure_count()
42 tmpdir = tempfile.mkdtemp(dir=_bup_tmp, prefix=prefix)
44 if wvfailure_count() == initial_failures:
45 subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
46 subprocess.call(['rm', '-rf', tmpdir])
50 if isinstance(cmd, basestring):
51 print(cmd, file=sys.stderr)
53 print(' '.join(map(quote, cmd)), file=sys.stderr)
55 def exc(cmd, shell=False):
57 check_call(cmd, shell=shell)
59 def exo(cmd, stdin=None, stdout=True, stderr=False, shell=False, check=True):
63 stdout=(PIPE if stdout else None),
66 out, err = p.communicate()
67 if check and p.returncode != 0:
68 raise Exception('subprocess %r failed with status %d, stderr: %r'
69 % (' '.join(map(quote, cmd)), p.returncode, err))