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