Chris@87: #!/usr/bin/env python Chris@87: """ Chris@87: exec_command Chris@87: Chris@87: Implements exec_command function that is (almost) equivalent to Chris@87: commands.getstatusoutput function but on NT, DOS systems the Chris@87: returned status is actually correct (though, the returned status Chris@87: values may be different by a factor). In addition, exec_command Chris@87: takes keyword arguments for (re-)defining environment variables. Chris@87: Chris@87: Provides functions: Chris@87: exec_command --- execute command in a specified directory and Chris@87: in the modified environment. Chris@87: find_executable --- locate a command using info from environment Chris@87: variable PATH. Equivalent to posix `which` Chris@87: command. Chris@87: Chris@87: Author: Pearu Peterson Chris@87: Created: 11 January 2003 Chris@87: Chris@87: Requires: Python 2.x Chris@87: Chris@87: Succesfully tested on: Chris@87: os.name | sys.platform | comments Chris@87: --------+--------------+---------- Chris@87: posix | linux2 | Debian (sid) Linux, Python 2.1.3+, 2.2.3+, 2.3.3 Chris@87: PyCrust 0.9.3, Idle 1.0.2 Chris@87: posix | linux2 | Red Hat 9 Linux, Python 2.1.3, 2.2.2, 2.3.2 Chris@87: posix | sunos5 | SunOS 5.9, Python 2.2, 2.3.2 Chris@87: posix | darwin | Darwin 7.2.0, Python 2.3 Chris@87: nt | win32 | Windows Me Chris@87: Python 2.3(EE), Idle 1.0, PyCrust 0.7.2 Chris@87: Python 2.1.1 Idle 0.8 Chris@87: nt | win32 | Windows 98, Python 2.1.1. Idle 0.8 Chris@87: nt | win32 | Cygwin 98-4.10, Python 2.1.1(MSC) - echo tests Chris@87: fail i.e. redefining environment variables may Chris@87: not work. FIXED: don't use cygwin echo! Chris@87: Comment: also `cmd /c echo` will not work Chris@87: but redefining environment variables do work. Chris@87: posix | cygwin | Cygwin 98-4.10, Python 2.3.3(cygming special) Chris@87: nt | win32 | Windows XP, Python 2.3.3 Chris@87: Chris@87: Known bugs: Chris@87: - Tests, that send messages to stderr, fail when executed from MSYS prompt Chris@87: because the messages are lost at some point. Chris@87: """ Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: __all__ = ['exec_command', 'find_executable'] Chris@87: Chris@87: import os Chris@87: import sys Chris@87: import shlex Chris@87: Chris@87: from numpy.distutils.misc_util import is_sequence, make_temp_file Chris@87: from numpy.distutils import log Chris@87: from numpy.distutils.compat import get_exception Chris@87: Chris@87: from numpy.compat import open_latin1 Chris@87: Chris@87: def temp_file_name(): Chris@87: fo, name = make_temp_file() Chris@87: fo.close() Chris@87: return name Chris@87: Chris@87: def get_pythonexe(): Chris@87: pythonexe = sys.executable Chris@87: if os.name in ['nt', 'dos']: Chris@87: fdir, fn = os.path.split(pythonexe) Chris@87: fn = fn.upper().replace('PYTHONW', 'PYTHON') Chris@87: pythonexe = os.path.join(fdir, fn) Chris@87: assert os.path.isfile(pythonexe), '%r is not a file' % (pythonexe,) Chris@87: return pythonexe Chris@87: Chris@87: def splitcmdline(line): Chris@87: import warnings Chris@87: warnings.warn('splitcmdline is deprecated; use shlex.split', Chris@87: DeprecationWarning) Chris@87: return shlex.split(line) Chris@87: Chris@87: def find_executable(exe, path=None, _cache={}): Chris@87: """Return full path of a executable or None. Chris@87: Chris@87: Symbolic links are not followed. Chris@87: """ Chris@87: key = exe, path Chris@87: try: Chris@87: return _cache[key] Chris@87: except KeyError: Chris@87: pass Chris@87: log.debug('find_executable(%r)' % exe) Chris@87: orig_exe = exe Chris@87: Chris@87: if path is None: Chris@87: path = os.environ.get('PATH', os.defpath) Chris@87: if os.name=='posix': Chris@87: realpath = os.path.realpath Chris@87: else: Chris@87: realpath = lambda a:a Chris@87: Chris@87: if exe.startswith('"'): Chris@87: exe = exe[1:-1] Chris@87: Chris@87: suffixes = [''] Chris@87: if os.name in ['nt', 'dos', 'os2']: Chris@87: fn, ext = os.path.splitext(exe) Chris@87: extra_suffixes = ['.exe', '.com', '.bat'] Chris@87: if ext.lower() not in extra_suffixes: Chris@87: suffixes = extra_suffixes Chris@87: Chris@87: if os.path.isabs(exe): Chris@87: paths = [''] Chris@87: else: Chris@87: paths = [ os.path.abspath(p) for p in path.split(os.pathsep) ] Chris@87: Chris@87: for path in paths: Chris@87: fn = os.path.join(path, exe) Chris@87: for s in suffixes: Chris@87: f_ext = fn+s Chris@87: if not os.path.islink(f_ext): Chris@87: f_ext = realpath(f_ext) Chris@87: if os.path.isfile(f_ext) and os.access(f_ext, os.X_OK): Chris@87: log.info('Found executable %s' % f_ext) Chris@87: _cache[key] = f_ext Chris@87: return f_ext Chris@87: Chris@87: log.warn('Could not locate executable %s' % orig_exe) Chris@87: return None Chris@87: Chris@87: ############################################################ Chris@87: Chris@87: def _preserve_environment( names ): Chris@87: log.debug('_preserve_environment(%r)' % (names)) Chris@87: env = {} Chris@87: for name in names: Chris@87: env[name] = os.environ.get(name) Chris@87: return env Chris@87: Chris@87: def _update_environment( **env ): Chris@87: log.debug('_update_environment(...)') Chris@87: for name, value in env.items(): Chris@87: os.environ[name] = value or '' Chris@87: Chris@87: def _supports_fileno(stream): Chris@87: """ Chris@87: Returns True if 'stream' supports the file descriptor and allows fileno(). Chris@87: """ Chris@87: if hasattr(stream, 'fileno'): Chris@87: try: Chris@87: r = stream.fileno() Chris@87: return True Chris@87: except IOError: Chris@87: return False Chris@87: else: Chris@87: return False Chris@87: Chris@87: def exec_command( command, Chris@87: execute_in='', use_shell=None, use_tee = None, Chris@87: _with_python = 1, Chris@87: **env ): Chris@87: """ Return (status,output) of executed command. Chris@87: Chris@87: command is a concatenated string of executable and arguments. Chris@87: The output contains both stdout and stderr messages. Chris@87: The following special keyword arguments can be used: Chris@87: use_shell - execute `sh -c command` Chris@87: use_tee - pipe the output of command through tee Chris@87: execute_in - before run command `cd execute_in` and after `cd -`. Chris@87: Chris@87: On NT, DOS systems the returned status is correct for external commands. Chris@87: Wild cards will not work for non-posix systems or when use_shell=0. Chris@87: """ Chris@87: log.debug('exec_command(%r,%s)' % (command,\ Chris@87: ','.join(['%s=%r'%kv for kv in env.items()]))) Chris@87: Chris@87: if use_tee is None: Chris@87: use_tee = os.name=='posix' Chris@87: if use_shell is None: Chris@87: use_shell = os.name=='posix' Chris@87: execute_in = os.path.abspath(execute_in) Chris@87: oldcwd = os.path.abspath(os.getcwd()) Chris@87: Chris@87: if __name__[-12:] == 'exec_command': Chris@87: exec_dir = os.path.dirname(os.path.abspath(__file__)) Chris@87: elif os.path.isfile('exec_command.py'): Chris@87: exec_dir = os.path.abspath('.') Chris@87: else: Chris@87: exec_dir = os.path.abspath(sys.argv[0]) Chris@87: if os.path.isfile(exec_dir): Chris@87: exec_dir = os.path.dirname(exec_dir) Chris@87: Chris@87: if oldcwd!=execute_in: Chris@87: os.chdir(execute_in) Chris@87: log.debug('New cwd: %s' % execute_in) Chris@87: else: Chris@87: log.debug('Retaining cwd: %s' % oldcwd) Chris@87: Chris@87: oldenv = _preserve_environment( list(env.keys()) ) Chris@87: _update_environment( **env ) Chris@87: Chris@87: try: Chris@87: # _exec_command is robust but slow, it relies on Chris@87: # usable sys.std*.fileno() descriptors. If they Chris@87: # are bad (like in win32 Idle, PyCrust environments) Chris@87: # then _exec_command_python (even slower) Chris@87: # will be used as a last resort. Chris@87: # Chris@87: # _exec_command_posix uses os.system and is faster Chris@87: # but not on all platforms os.system will return Chris@87: # a correct status. Chris@87: if (_with_python and _supports_fileno(sys.stdout) and Chris@87: sys.stdout.fileno() == -1): Chris@87: st = _exec_command_python(command, Chris@87: exec_command_dir = exec_dir, Chris@87: **env) Chris@87: elif os.name=='posix': Chris@87: st = _exec_command_posix(command, Chris@87: use_shell=use_shell, Chris@87: use_tee=use_tee, Chris@87: **env) Chris@87: else: Chris@87: st = _exec_command(command, use_shell=use_shell, Chris@87: use_tee=use_tee,**env) Chris@87: finally: Chris@87: if oldcwd!=execute_in: Chris@87: os.chdir(oldcwd) Chris@87: log.debug('Restored cwd to %s' % oldcwd) Chris@87: _update_environment(**oldenv) Chris@87: Chris@87: return st Chris@87: Chris@87: def _exec_command_posix( command, Chris@87: use_shell = None, Chris@87: use_tee = None, Chris@87: **env ): Chris@87: log.debug('_exec_command_posix(...)') Chris@87: Chris@87: if is_sequence(command): Chris@87: command_str = ' '.join(list(command)) Chris@87: else: Chris@87: command_str = command Chris@87: Chris@87: tmpfile = temp_file_name() Chris@87: stsfile = None Chris@87: if use_tee: Chris@87: stsfile = temp_file_name() Chris@87: filter = '' Chris@87: if use_tee == 2: Chris@87: filter = r'| tr -cd "\n" | tr "\n" "."; echo' Chris@87: command_posix = '( %s ; echo $? > %s ) 2>&1 | tee %s %s'\ Chris@87: % (command_str, stsfile, tmpfile, filter) Chris@87: else: Chris@87: stsfile = temp_file_name() Chris@87: command_posix = '( %s ; echo $? > %s ) > %s 2>&1'\ Chris@87: % (command_str, stsfile, tmpfile) Chris@87: #command_posix = '( %s ) > %s 2>&1' % (command_str,tmpfile) Chris@87: Chris@87: log.debug('Running os.system(%r)' % (command_posix)) Chris@87: status = os.system(command_posix) Chris@87: Chris@87: if use_tee: Chris@87: if status: Chris@87: # if command_tee fails then fall back to robust exec_command Chris@87: log.warn('_exec_command_posix failed (status=%s)' % status) Chris@87: return _exec_command(command, use_shell=use_shell, **env) Chris@87: Chris@87: if stsfile is not None: Chris@87: f = open_latin1(stsfile, 'r') Chris@87: status_text = f.read() Chris@87: status = int(status_text) Chris@87: f.close() Chris@87: os.remove(stsfile) Chris@87: Chris@87: f = open_latin1(tmpfile, 'r') Chris@87: text = f.read() Chris@87: f.close() Chris@87: os.remove(tmpfile) Chris@87: Chris@87: if text[-1:]=='\n': Chris@87: text = text[:-1] Chris@87: Chris@87: return status, text Chris@87: Chris@87: Chris@87: def _exec_command_python(command, Chris@87: exec_command_dir='', **env): Chris@87: log.debug('_exec_command_python(...)') Chris@87: Chris@87: python_exe = get_pythonexe() Chris@87: cmdfile = temp_file_name() Chris@87: stsfile = temp_file_name() Chris@87: outfile = temp_file_name() Chris@87: Chris@87: f = open(cmdfile, 'w') Chris@87: f.write('import os\n') Chris@87: f.write('import sys\n') Chris@87: f.write('sys.path.insert(0,%r)\n' % (exec_command_dir)) Chris@87: f.write('from exec_command import exec_command\n') Chris@87: f.write('del sys.path[0]\n') Chris@87: f.write('cmd = %r\n' % command) Chris@87: f.write('os.environ = %r\n' % (os.environ)) Chris@87: f.write('s,o = exec_command(cmd, _with_python=0, **%r)\n' % (env)) Chris@87: f.write('f=open(%r,"w")\nf.write(str(s))\nf.close()\n' % (stsfile)) Chris@87: f.write('f=open(%r,"w")\nf.write(o)\nf.close()\n' % (outfile)) Chris@87: f.close() Chris@87: Chris@87: cmd = '%s %s' % (python_exe, cmdfile) Chris@87: status = os.system(cmd) Chris@87: if status: Chris@87: raise RuntimeError("%r failed" % (cmd,)) Chris@87: os.remove(cmdfile) Chris@87: Chris@87: f = open_latin1(stsfile, 'r') Chris@87: status = int(f.read()) Chris@87: f.close() Chris@87: os.remove(stsfile) Chris@87: Chris@87: f = open_latin1(outfile, 'r') Chris@87: text = f.read() Chris@87: f.close() Chris@87: os.remove(outfile) Chris@87: Chris@87: return status, text Chris@87: Chris@87: def quote_arg(arg): Chris@87: if arg[0]!='"' and ' ' in arg: Chris@87: return '"%s"' % arg Chris@87: return arg Chris@87: Chris@87: def _exec_command( command, use_shell=None, use_tee = None, **env ): Chris@87: log.debug('_exec_command(...)') Chris@87: Chris@87: if use_shell is None: Chris@87: use_shell = os.name=='posix' Chris@87: if use_tee is None: Chris@87: use_tee = os.name=='posix' Chris@87: using_command = 0 Chris@87: if use_shell: Chris@87: # We use shell (unless use_shell==0) so that wildcards can be Chris@87: # used. Chris@87: sh = os.environ.get('SHELL', '/bin/sh') Chris@87: if is_sequence(command): Chris@87: argv = [sh, '-c', ' '.join(list(command))] Chris@87: else: Chris@87: argv = [sh, '-c', command] Chris@87: else: Chris@87: # On NT, DOS we avoid using command.com as it's exit status is Chris@87: # not related to the exit status of a command. Chris@87: if is_sequence(command): Chris@87: argv = command[:] Chris@87: else: Chris@87: argv = shlex.split(command) Chris@87: Chris@87: if hasattr(os, 'spawnvpe'): Chris@87: spawn_command = os.spawnvpe Chris@87: else: Chris@87: spawn_command = os.spawnve Chris@87: argv[0] = find_executable(argv[0]) or argv[0] Chris@87: if not os.path.isfile(argv[0]): Chris@87: log.warn('Executable %s does not exist' % (argv[0])) Chris@87: if os.name in ['nt', 'dos']: Chris@87: # argv[0] might be internal command Chris@87: argv = [os.environ['COMSPEC'], '/C'] + argv Chris@87: using_command = 1 Chris@87: Chris@87: _so_has_fileno = _supports_fileno(sys.stdout) Chris@87: _se_has_fileno = _supports_fileno(sys.stderr) Chris@87: so_flush = sys.stdout.flush Chris@87: se_flush = sys.stderr.flush Chris@87: if _so_has_fileno: Chris@87: so_fileno = sys.stdout.fileno() Chris@87: so_dup = os.dup(so_fileno) Chris@87: if _se_has_fileno: Chris@87: se_fileno = sys.stderr.fileno() Chris@87: se_dup = os.dup(se_fileno) Chris@87: Chris@87: outfile = temp_file_name() Chris@87: fout = open(outfile, 'w') Chris@87: if using_command: Chris@87: errfile = temp_file_name() Chris@87: ferr = open(errfile, 'w') Chris@87: Chris@87: log.debug('Running %s(%s,%r,%r,os.environ)' \ Chris@87: % (spawn_command.__name__, os.P_WAIT, argv[0], argv)) Chris@87: Chris@87: argv0 = argv[0] Chris@87: if not using_command: Chris@87: argv[0] = quote_arg(argv0) Chris@87: Chris@87: so_flush() Chris@87: se_flush() Chris@87: if _so_has_fileno: Chris@87: os.dup2(fout.fileno(), so_fileno) Chris@87: Chris@87: if _se_has_fileno: Chris@87: if using_command: Chris@87: #XXX: disabled for now as it does not work from cmd under win32. Chris@87: # Tests fail on msys Chris@87: os.dup2(ferr.fileno(), se_fileno) Chris@87: else: Chris@87: os.dup2(fout.fileno(), se_fileno) Chris@87: try: Chris@87: status = spawn_command(os.P_WAIT, argv0, argv, os.environ) Chris@87: except OSError: Chris@87: errmess = str(get_exception()) Chris@87: status = 999 Chris@87: sys.stderr.write('%s: %s'%(errmess, argv[0])) Chris@87: Chris@87: so_flush() Chris@87: se_flush() Chris@87: if _so_has_fileno: Chris@87: os.dup2(so_dup, so_fileno) Chris@87: if _se_has_fileno: Chris@87: os.dup2(se_dup, se_fileno) Chris@87: Chris@87: fout.close() Chris@87: fout = open_latin1(outfile, 'r') Chris@87: text = fout.read() Chris@87: fout.close() Chris@87: os.remove(outfile) Chris@87: Chris@87: if using_command: Chris@87: ferr.close() Chris@87: ferr = open_latin1(errfile, 'r') Chris@87: errmess = ferr.read() Chris@87: ferr.close() Chris@87: os.remove(errfile) Chris@87: if errmess and not status: Chris@87: # Not sure how to handle the case where errmess Chris@87: # contains only warning messages and that should Chris@87: # not be treated as errors. Chris@87: #status = 998 Chris@87: if text: Chris@87: text = text + '\n' Chris@87: #text = '%sCOMMAND %r FAILED: %s' %(text,command,errmess) Chris@87: text = text + errmess Chris@87: print (errmess) Chris@87: if text[-1:]=='\n': Chris@87: text = text[:-1] Chris@87: if status is None: Chris@87: status = 0 Chris@87: Chris@87: if use_tee: Chris@87: print (text) Chris@87: Chris@87: return status, text Chris@87: Chris@87: Chris@87: def test_nt(**kws): Chris@87: pythonexe = get_pythonexe() Chris@87: echo = find_executable('echo') Chris@87: using_cygwin_echo = echo != 'echo' Chris@87: if using_cygwin_echo: Chris@87: log.warn('Using cygwin echo in win32 environment is not supported') Chris@87: Chris@87: s, o=exec_command(pythonexe\ Chris@87: +' -c "import os;print os.environ.get(\'AAA\',\'\')"') Chris@87: assert s==0 and o=='', (s, o) Chris@87: Chris@87: s, o=exec_command(pythonexe\ Chris@87: +' -c "import os;print os.environ.get(\'AAA\')"', Chris@87: AAA='Tere') Chris@87: assert s==0 and o=='Tere', (s, o) Chris@87: Chris@87: os.environ['BBB'] = 'Hi' Chris@87: s, o=exec_command(pythonexe\ Chris@87: +' -c "import os;print os.environ.get(\'BBB\',\'\')"') Chris@87: assert s==0 and o=='Hi', (s, o) Chris@87: Chris@87: s, o=exec_command(pythonexe\ Chris@87: +' -c "import os;print os.environ.get(\'BBB\',\'\')"', Chris@87: BBB='Hey') Chris@87: assert s==0 and o=='Hey', (s, o) Chris@87: Chris@87: s, o=exec_command(pythonexe\ Chris@87: +' -c "import os;print os.environ.get(\'BBB\',\'\')"') Chris@87: assert s==0 and o=='Hi', (s, o) Chris@87: elif 0: Chris@87: s, o=exec_command('echo Hello') Chris@87: assert s==0 and o=='Hello', (s, o) Chris@87: Chris@87: s, o=exec_command('echo a%AAA%') Chris@87: assert s==0 and o=='a', (s, o) Chris@87: Chris@87: s, o=exec_command('echo a%AAA%', AAA='Tere') Chris@87: assert s==0 and o=='aTere', (s, o) Chris@87: Chris@87: os.environ['BBB'] = 'Hi' Chris@87: s, o=exec_command('echo a%BBB%') Chris@87: assert s==0 and o=='aHi', (s, o) Chris@87: Chris@87: s, o=exec_command('echo a%BBB%', BBB='Hey') Chris@87: assert s==0 and o=='aHey', (s, o) Chris@87: s, o=exec_command('echo a%BBB%') Chris@87: assert s==0 and o=='aHi', (s, o) Chris@87: Chris@87: s, o=exec_command('this_is_not_a_command') Chris@87: assert s and o!='', (s, o) Chris@87: Chris@87: s, o=exec_command('type not_existing_file') Chris@87: assert s and o!='', (s, o) Chris@87: Chris@87: s, o=exec_command('echo path=%path%') Chris@87: assert s==0 and o!='', (s, o) Chris@87: Chris@87: s, o=exec_command('%s -c "import sys;sys.stderr.write(sys.platform)"' \ Chris@87: % pythonexe) Chris@87: assert s==0 and o=='win32', (s, o) Chris@87: Chris@87: s, o=exec_command('%s -c "raise \'Ignore me.\'"' % pythonexe) Chris@87: assert s==1 and o, (s, o) Chris@87: Chris@87: s, o=exec_command('%s -c "import sys;sys.stderr.write(\'0\');sys.stderr.write(\'1\');sys.stderr.write(\'2\')"'\ Chris@87: % pythonexe) Chris@87: assert s==0 and o=='012', (s, o) Chris@87: Chris@87: s, o=exec_command('%s -c "import sys;sys.exit(15)"' % pythonexe) Chris@87: assert s==15 and o=='', (s, o) Chris@87: Chris@87: s, o=exec_command('%s -c "print \'Heipa\'"' % pythonexe) Chris@87: assert s==0 and o=='Heipa', (s, o) Chris@87: Chris@87: print ('ok') Chris@87: Chris@87: def test_posix(**kws): Chris@87: s, o=exec_command("echo Hello",**kws) Chris@87: assert s==0 and o=='Hello', (s, o) Chris@87: Chris@87: s, o=exec_command('echo $AAA',**kws) Chris@87: assert s==0 and o=='', (s, o) Chris@87: Chris@87: s, o=exec_command('echo "$AAA"',AAA='Tere',**kws) Chris@87: assert s==0 and o=='Tere', (s, o) Chris@87: Chris@87: Chris@87: s, o=exec_command('echo "$AAA"',**kws) Chris@87: assert s==0 and o=='', (s, o) Chris@87: Chris@87: os.environ['BBB'] = 'Hi' Chris@87: s, o=exec_command('echo "$BBB"',**kws) Chris@87: assert s==0 and o=='Hi', (s, o) Chris@87: Chris@87: s, o=exec_command('echo "$BBB"',BBB='Hey',**kws) Chris@87: assert s==0 and o=='Hey', (s, o) Chris@87: Chris@87: s, o=exec_command('echo "$BBB"',**kws) Chris@87: assert s==0 and o=='Hi', (s, o) Chris@87: Chris@87: Chris@87: s, o=exec_command('this_is_not_a_command',**kws) Chris@87: assert s!=0 and o!='', (s, o) Chris@87: Chris@87: s, o=exec_command('echo path=$PATH',**kws) Chris@87: assert s==0 and o!='', (s, o) Chris@87: Chris@87: s, o=exec_command('python -c "import sys,os;sys.stderr.write(os.name)"',**kws) Chris@87: assert s==0 and o=='posix', (s, o) Chris@87: Chris@87: s, o=exec_command('python -c "raise \'Ignore me.\'"',**kws) Chris@87: assert s==1 and o, (s, o) Chris@87: Chris@87: s, o=exec_command('python -c "import sys;sys.stderr.write(\'0\');sys.stderr.write(\'1\');sys.stderr.write(\'2\')"',**kws) Chris@87: assert s==0 and o=='012', (s, o) Chris@87: Chris@87: s, o=exec_command('python -c "import sys;sys.exit(15)"',**kws) Chris@87: assert s==15 and o=='', (s, o) Chris@87: Chris@87: s, o=exec_command('python -c "print \'Heipa\'"',**kws) Chris@87: assert s==0 and o=='Heipa', (s, o) Chris@87: Chris@87: print ('ok') Chris@87: Chris@87: def test_execute_in(**kws): Chris@87: pythonexe = get_pythonexe() Chris@87: tmpfile = temp_file_name() Chris@87: fn = os.path.basename(tmpfile) Chris@87: tmpdir = os.path.dirname(tmpfile) Chris@87: f = open(tmpfile, 'w') Chris@87: f.write('Hello') Chris@87: f.close() Chris@87: Chris@87: s, o = exec_command('%s -c "print \'Ignore the following IOError:\','\ Chris@87: 'open(%r,\'r\')"' % (pythonexe, fn),**kws) Chris@87: assert s and o!='', (s, o) Chris@87: s, o = exec_command('%s -c "print open(%r,\'r\').read()"' % (pythonexe, fn), Chris@87: execute_in = tmpdir,**kws) Chris@87: assert s==0 and o=='Hello', (s, o) Chris@87: os.remove(tmpfile) Chris@87: print ('ok') Chris@87: Chris@87: def test_svn(**kws): Chris@87: s, o = exec_command(['svn', 'status'],**kws) Chris@87: assert s, (s, o) Chris@87: print ('svn ok') Chris@87: Chris@87: def test_cl(**kws): Chris@87: if os.name=='nt': Chris@87: s, o = exec_command(['cl', '/V'],**kws) Chris@87: assert s, (s, o) Chris@87: print ('cl ok') Chris@87: Chris@87: if os.name=='posix': Chris@87: test = test_posix Chris@87: elif os.name in ['nt', 'dos']: Chris@87: test = test_nt Chris@87: else: Chris@87: raise NotImplementedError('exec_command tests for ', os.name) Chris@87: Chris@87: ############################################################ Chris@87: Chris@87: if __name__ == "__main__": Chris@87: Chris@87: test(use_tee=0) Chris@87: test(use_tee=1) Chris@87: test_execute_in(use_tee=0) Chris@87: test_execute_in(use_tee=1) Chris@87: test_svn(use_tee=1) Chris@87: test_cl(use_tee=1)