Antoine Pitrou | cb4f929 | 2010-11-10 14:01:16 +0000 | [diff] [blame] | 1 | # Common utility functions used by various script execution tests |
| 2 | # e.g. test_cmd_line, test_cmd_line_script and test_runpy |
| 3 | |
| 4 | import sys |
| 5 | import os |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 6 | import re |
Antoine Pitrou | cb4f929 | 2010-11-10 14:01:16 +0000 | [diff] [blame] | 7 | import os.path |
| 8 | import tempfile |
| 9 | import subprocess |
| 10 | import py_compile |
| 11 | import contextlib |
| 12 | import shutil |
| 13 | import zipfile |
| 14 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 15 | from test.support import strip_python_stderr |
Antoine Pitrou | cb4f929 | 2010-11-10 14:01:16 +0000 | [diff] [blame] | 16 | |
| 17 | # Executing the interpreter in a subprocess |
| 18 | def _assert_python(expected_success, *args, **env_vars): |
| 19 | cmd_line = [sys.executable] |
| 20 | if not env_vars: |
| 21 | cmd_line.append('-E') |
| 22 | cmd_line.extend(args) |
| 23 | # Need to preserve the original environment, for in-place testing of |
| 24 | # shared library builds. |
| 25 | env = os.environ.copy() |
| 26 | env.update(env_vars) |
| 27 | p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 28 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 29 | env=env) |
| 30 | try: |
| 31 | out, err = p.communicate() |
| 32 | finally: |
| 33 | subprocess._cleanup() |
| 34 | p.stdout.close() |
| 35 | p.stderr.close() |
| 36 | rc = p.returncode |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 37 | err = strip_python_stderr(err) |
Antoine Pitrou | cb4f929 | 2010-11-10 14:01:16 +0000 | [diff] [blame] | 38 | if (rc and expected_success) or (not rc and not expected_success): |
| 39 | raise AssertionError( |
| 40 | "Process return code is %d, " |
| 41 | "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore'))) |
| 42 | return rc, out, err |
| 43 | |
| 44 | def assert_python_ok(*args, **env_vars): |
| 45 | """ |
| 46 | Assert that running the interpreter with `args` and optional environment |
| 47 | variables `env_vars` is ok and return a (return code, stdout, stderr) tuple. |
| 48 | """ |
| 49 | return _assert_python(True, *args, **env_vars) |
| 50 | |
| 51 | def assert_python_failure(*args, **env_vars): |
| 52 | """ |
| 53 | Assert that running the interpreter with `args` and optional environment |
| 54 | variables `env_vars` fails and return a (return code, stdout, stderr) tuple. |
| 55 | """ |
| 56 | return _assert_python(False, *args, **env_vars) |
| 57 | |
| 58 | def spawn_python(*args): |
| 59 | cmd_line = [sys.executable, '-E'] |
| 60 | cmd_line.extend(args) |
| 61 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 62 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 63 | |
| 64 | def kill_python(p): |
| 65 | p.stdin.close() |
| 66 | data = p.stdout.read() |
| 67 | p.stdout.close() |
| 68 | # try to cleanup the child so we don't appear to leak when running |
| 69 | # with regrtest -R. |
| 70 | p.wait() |
| 71 | subprocess._cleanup() |
| 72 | return data |
| 73 | |
| 74 | # Script creation utilities |
| 75 | @contextlib.contextmanager |
| 76 | def temp_dir(): |
| 77 | dirname = tempfile.mkdtemp() |
| 78 | dirname = os.path.realpath(dirname) |
| 79 | try: |
| 80 | yield dirname |
| 81 | finally: |
| 82 | shutil.rmtree(dirname) |
| 83 | |
| 84 | def make_script(script_dir, script_basename, source): |
| 85 | script_filename = script_basename+os.extsep+'py' |
| 86 | script_name = os.path.join(script_dir, script_filename) |
| 87 | # The script should be encoded to UTF-8, the default string encoding |
| 88 | script_file = open(script_name, 'w', encoding='utf-8') |
| 89 | script_file.write(source) |
| 90 | script_file.close() |
| 91 | return script_name |
| 92 | |
| 93 | def make_pkg(pkg_dir, init_source=''): |
| 94 | os.mkdir(pkg_dir) |
| 95 | make_script(pkg_dir, '__init__', init_source) |
| 96 | |
| 97 | def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 98 | source, depth=1, compiled=False): |
| 99 | unlink = [] |
| 100 | init_name = make_script(zip_dir, '__init__', '') |
| 101 | unlink.append(init_name) |
| 102 | init_basename = os.path.basename(init_name) |
| 103 | script_name = make_script(zip_dir, script_basename, source) |
| 104 | unlink.append(script_name) |
| 105 | if compiled: |
| 106 | init_name = py_compile(init_name, doraise=True) |
| 107 | script_name = py_compile(script_name, doraise=True) |
| 108 | unlink.extend((init_name, script_name)) |
| 109 | pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
| 110 | script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) |
| 111 | zip_filename = zip_basename+os.extsep+'zip' |
| 112 | zip_name = os.path.join(zip_dir, zip_filename) |
| 113 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 114 | for name in pkg_names: |
| 115 | init_name_in_zip = os.path.join(name, init_basename) |
| 116 | zip_file.write(init_name, init_name_in_zip) |
| 117 | zip_file.write(script_name, script_name_in_zip) |
| 118 | zip_file.close() |
| 119 | for name in unlink: |
| 120 | os.unlink(name) |
| 121 | #if test.support.verbose: |
| 122 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 123 | # print 'Contents of %r:' % zip_name |
| 124 | # zip_file.printdir() |
| 125 | # zip_file.close() |
| 126 | return zip_name, os.path.join(zip_name, script_name_in_zip) |