blob: 0699cf61a2154065d5645e4f224dc68bdafd9003 [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
R. David Murray43b2f452011-02-11 03:13:19 +00006import re
Antoine Pitroucb4f9292010-11-10 14:01:16 +00007import os.path
8import tempfile
9import subprocess
10import py_compile
11import contextlib
12import shutil
13import zipfile
14
R. David Murray43b2f452011-02-11 03:13:19 +000015from test.support import strip_python_stderr
Antoine Pitroucb4f9292010-11-10 14:01:16 +000016
17# Executing the interpreter in a subprocess
18def _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 Murray43b2f452011-02-11 03:13:19 +000037 err = strip_python_stderr(err)
Antoine Pitroucb4f9292010-11-10 14:01:16 +000038 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
44def 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
51def 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
58def 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
64def 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
76def temp_dir():
77 dirname = tempfile.mkdtemp()
78 dirname = os.path.realpath(dirname)
79 try:
80 yield dirname
81 finally:
82 shutil.rmtree(dirname)
83
84def 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
93def make_pkg(pkg_dir, init_source=''):
94 os.mkdir(pkg_dir)
95 make_script(pkg_dir, '__init__', init_source)
96
97def 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)