def _test_locale(self): child = ipc.Subprocess(['locale'], stdout=ipc.PIPE, stderr=ipc.PIPE ) stdout, stderr = child.communicate() stdout = stdout.splitlines() stderr = stderr.splitlines() assert_equal(stderr, []) data = dict(line.split('=', 1) for line in stdout) has_lc_all = has_lc_ctype = has_lang = 0 for key, value in data.iteritems(): if key == 'LC_ALL': has_lc_all = 1 assert_equal(value, '') elif key == 'LC_CTYPE': has_lc_ctype = 1 if utf8_locale is None: raise SkipTest( 'UTF-8 locale missing ' '({0})'.format(' or '.join(utf8_locale_candidates)) ) assert_equal(value, utf8_locale) elif key == 'LANG': has_lang = 1 assert_equal(value, '') elif key == 'LANGUAGE': assert_equal(value, '') else: assert_equal(value, '"POSIX"') assert_true(has_lc_all) assert_true(has_lc_ctype) assert_true(has_lang)
def _test_locale(self): child = ipc.Subprocess(['locale'], stdout=ipc.PIPE, stderr=ipc.PIPE) stdout, stderr = child.communicate() stdout = stdout.splitlines() stderr = stderr.splitlines() assert_equal(stderr, []) data = dict(line.split('=', 1) for line in stdout) has_lc_all = has_lc_ctype = has_lang = 0 for key, value in data.iteritems(): if key == 'LC_ALL': has_lc_all = 1 assert_equal(value, '') elif key == 'LC_CTYPE': has_lc_ctype = 1 assert_equal(value, 'en_US.UTF-8') elif key == 'LANG': has_lang = 1 assert_equal(value, '') elif key == 'LANGUAGE': assert_equal(value, '') else: assert_equal(value, '"POSIX"') assert_true(has_lc_all) assert_true(has_lc_ctype) assert_true(has_lang)
def test_init_exception(): with assert_raises(OSError) as ecm: ipc.Subprocess([nonexistent_command]) exc_message = "[Errno {errno.ENOENT}] No such file or directory: {cmd!r}".format( errno=errno, cmd=nonexistent_command, ) assert_equal(str(ecm.exception), exc_message)
def _test_signal(self, name): child = ipc.Subprocess( ['cat'], stdin=ipc.PIPE) # Any long-standing process would do. os.kill(child.pid, getattr(signal, name)) with assert_raises(ipc.CalledProcessInterrupted) as ecm: child.wait() assert_equal(str(ecm.exception), "Command 'cat' was interrupted by signal " + name)
def test1(self): with interim_environ(didjvu='42'): child = ipc.Subprocess( ['sh', '-c', 'printf $didjvu'], stdout=ipc.PIPE, stderr=ipc.PIPE, ) stdout, stderr = child.communicate() assert_equal(stdout, '42') assert_equal(stderr, '')
def test1(self): child = ipc.Subprocess(['false']) with assert_raises(ipc.CalledProcessError) as ecm: child.wait() message = str(ecm.exception) if message[-1] == '.': # subprocess32 >= 3.5 message = message[:-1] assert_equal(message, "Command 'false' returned non-zero exit status 1")
def test_init_exc(): # https://bugs.python.org/issue32490 prog = 'ocrodjvu-nonexistent' with assert_raises(EnvironmentError) as ecm: ipc.Subprocess([prog]) msg = '[Errno {err}] {strerr}: {prog!r}'.format(err=errno.ENOENT, strerr=os.strerror( errno.ENOENT), prog=prog) assert_equal(str(ecm.exception), msg)
def test3(self): with interim_environ(ocrodjvu='42'): child = ipc.Subprocess( ['sh', '-c', 'printf $ocrodjvu'], stdout=ipc.PIPE, stderr=ipc.PIPE, env=dict(ocrodjvu='24'), ) stdout, stderr = child.communicate() assert_equal(stdout, '24') assert_equal(stderr, '')
def _test_from_file(base_filename, index): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) djvused_filename = base_filename + '.djvused' with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) assert_equal(args[0], '#') with temporary.directory() as tmpdir: djvu_filename = os.path.join(tmpdir, 'empty.djvu') args += [djvu_filename] shutil.copy( os.path.join(os.path.dirname(__file__), '..', 'data', 'empty.djvu'), djvu_filename) ipc.Subprocess( ['djvused', '-f', djvused_filename, '-s', djvu_filename]).wait() xml_filename = os.path.join(tmpdir, 'output.html') with open(xml_filename, 'w+b') as xml_file: xmllint = ipc.Subprocess(['xmllint', '--format', '-'], stdin=ipc.PIPE, stdout=xml_file) try: with open(os.devnull, 'w') as null: with interim(sys, stdout=xmllint.stdin, stderr=null): with interim(djvu2hocr.logger, handlers=[]): rc = try_run(djvu2hocr.main, args) finally: xmllint.stdin.close() try: xmllint.wait() except ipc.CalledProcessError: # Raising the exception here is likely to hide the real # reason of the failure. pass assert_equal(rc, 0) xml_file.seek(0) output = xml_file.read() assert_multi_line_equal(expected_output, output)
def ddjvu(djvu_file, fmt='ppm'): cmdline = ['ddjvu', '-1', '-format=' + fmt] stdio = dict(stdout=ipc.PIPE, stderr=ipc.PIPE) if isinstance(djvu_file, basestring): djvu_path = djvu_file cmdline += [djvu_path] else: stdio.update(stdin=djvu_file) child = ipc.Subprocess(cmdline, **stdio) stdout, stderr = child.communicate() if child.returncode != 0: raise RuntimeError('ddjvu failed') if stderr != '': raise RuntimeError('ddjvu stderr: ' + stderr) out_file = io.BytesIO(stdout) return pil.open(out_file)
def test_path(self): path = os.getenv('PATH').split(':') with temporary.directory() as tmpdir: command_name = temporary.name(dir=tmpdir) command_path = os.path.join(tmpdir, command_name) with open(command_path, 'wt') as file: print('#!/bin/sh', file=file) print('printf 42', file=file) os.chmod(command_path, stat.S_IRWXU) path[:0] = [tmpdir] path = ':'.join(path) with interim_environ(PATH=path): child = ipc.Subprocess([command_name], stdout=ipc.PIPE, stderr=ipc.PIPE, ) stdout, stderr = child.communicate() assert_equal(stdout, '42') assert_equal(stderr, '')
def run_exiv2(filename, fail_ok=False): try: child = ipc.Subprocess( ['exiv2', 'print', '-P', 'Xkt', filename], stdout=ipc.PIPE, stderr=ipc.PIPE, ) except OSError as ex: raise SkipTest(ex) for line in sorted(child.stdout): yield line stderr = child.stderr.read() if not fail_ok: assert_equal(stderr, '') try: child.wait() except ipc.CalledProcessError: if not fail_ok: raise
def test1(self): child = ipc.Subprocess(['false']) with assert_raises(ipc.CalledProcessError) as ecm: child.wait() assert_equal(str(ecm.exception), "Command 'false' returned non-zero exit status 1")
def test0(self): child = ipc.Subprocess(['true']) child.wait()