]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | # Common utility functions used by various script execution tests\r |
2 | # e.g. test_cmd_line, test_cmd_line_script and test_runpy\r | |
3 | \r | |
4 | import sys\r | |
5 | import os\r | |
6 | import re\r | |
7 | import os.path\r | |
8 | import tempfile\r | |
9 | import subprocess\r | |
10 | import py_compile\r | |
11 | import contextlib\r | |
12 | import shutil\r | |
13 | import zipfile\r | |
14 | \r | |
15 | from test.test_support import strip_python_stderr\r | |
16 | \r | |
17 | # Executing the interpreter in a subprocess\r | |
18 | def _assert_python(expected_success, *args, **env_vars):\r | |
19 | cmd_line = [sys.executable]\r | |
20 | if not env_vars:\r | |
21 | cmd_line.append('-E')\r | |
22 | cmd_line.extend(args)\r | |
23 | # Need to preserve the original environment, for in-place testing of\r | |
24 | # shared library builds.\r | |
25 | env = os.environ.copy()\r | |
26 | env.update(env_vars)\r | |
27 | p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,\r | |
28 | stdout=subprocess.PIPE, stderr=subprocess.PIPE,\r | |
29 | env=env)\r | |
30 | try:\r | |
31 | out, err = p.communicate()\r | |
32 | finally:\r | |
33 | subprocess._cleanup()\r | |
34 | p.stdout.close()\r | |
35 | p.stderr.close()\r | |
36 | rc = p.returncode\r | |
37 | err = strip_python_stderr(err)\r | |
38 | if (rc and expected_success) or (not rc and not expected_success):\r | |
39 | raise AssertionError(\r | |
40 | "Process return code is %d, "\r | |
41 | "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))\r | |
42 | return rc, out, err\r | |
43 | \r | |
44 | def assert_python_ok(*args, **env_vars):\r | |
45 | """\r | |
46 | Assert that running the interpreter with `args` and optional environment\r | |
47 | variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.\r | |
48 | """\r | |
49 | return _assert_python(True, *args, **env_vars)\r | |
50 | \r | |
51 | def assert_python_failure(*args, **env_vars):\r | |
52 | """\r | |
53 | Assert that running the interpreter with `args` and optional environment\r | |
54 | variables `env_vars` fails and return a (return code, stdout, stderr) tuple.\r | |
55 | """\r | |
56 | return _assert_python(False, *args, **env_vars)\r | |
57 | \r | |
58 | def python_exit_code(*args):\r | |
59 | cmd_line = [sys.executable, '-E']\r | |
60 | cmd_line.extend(args)\r | |
61 | with open(os.devnull, 'w') as devnull:\r | |
62 | return subprocess.call(cmd_line, stdout=devnull,\r | |
63 | stderr=subprocess.STDOUT)\r | |
64 | \r | |
65 | def spawn_python(*args, **kwargs):\r | |
66 | cmd_line = [sys.executable, '-E']\r | |
67 | cmd_line.extend(args)\r | |
68 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,\r | |
69 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT,\r | |
70 | **kwargs)\r | |
71 | \r | |
72 | def kill_python(p):\r | |
73 | p.stdin.close()\r | |
74 | data = p.stdout.read()\r | |
75 | p.stdout.close()\r | |
76 | # try to cleanup the child so we don't appear to leak when running\r | |
77 | # with regrtest -R.\r | |
78 | p.wait()\r | |
79 | subprocess._cleanup()\r | |
80 | return data\r | |
81 | \r | |
82 | def run_python(*args, **kwargs):\r | |
83 | if __debug__:\r | |
84 | p = spawn_python(*args, **kwargs)\r | |
85 | else:\r | |
86 | p = spawn_python('-O', *args, **kwargs)\r | |
87 | stdout_data = kill_python(p)\r | |
88 | return p.wait(), stdout_data\r | |
89 | \r | |
90 | # Script creation utilities\r | |
91 | @contextlib.contextmanager\r | |
92 | def temp_dir():\r | |
93 | dirname = tempfile.mkdtemp()\r | |
94 | dirname = os.path.realpath(dirname)\r | |
95 | try:\r | |
96 | yield dirname\r | |
97 | finally:\r | |
98 | shutil.rmtree(dirname)\r | |
99 | \r | |
100 | def make_script(script_dir, script_basename, source):\r | |
101 | script_filename = script_basename+os.extsep+'py'\r | |
102 | script_name = os.path.join(script_dir, script_filename)\r | |
103 | script_file = open(script_name, 'w')\r | |
104 | script_file.write(source)\r | |
105 | script_file.close()\r | |
106 | return script_name\r | |
107 | \r | |
108 | def compile_script(script_name):\r | |
109 | py_compile.compile(script_name, doraise=True)\r | |
110 | if __debug__:\r | |
111 | compiled_name = script_name + 'c'\r | |
112 | else:\r | |
113 | compiled_name = script_name + 'o'\r | |
114 | return compiled_name\r | |
115 | \r | |
116 | def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):\r | |
117 | zip_filename = zip_basename+os.extsep+'zip'\r | |
118 | zip_name = os.path.join(zip_dir, zip_filename)\r | |
119 | zip_file = zipfile.ZipFile(zip_name, 'w')\r | |
120 | if name_in_zip is None:\r | |
121 | name_in_zip = os.path.basename(script_name)\r | |
122 | zip_file.write(script_name, name_in_zip)\r | |
123 | zip_file.close()\r | |
124 | #if test.test_support.verbose:\r | |
125 | # zip_file = zipfile.ZipFile(zip_name, 'r')\r | |
126 | # print 'Contents of %r:' % zip_name\r | |
127 | # zip_file.printdir()\r | |
128 | # zip_file.close()\r | |
129 | return zip_name, os.path.join(zip_name, name_in_zip)\r | |
130 | \r | |
131 | def make_pkg(pkg_dir):\r | |
132 | os.mkdir(pkg_dir)\r | |
133 | make_script(pkg_dir, '__init__', '')\r | |
134 | \r | |
135 | def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,\r | |
136 | source, depth=1, compiled=False):\r | |
137 | unlink = []\r | |
138 | init_name = make_script(zip_dir, '__init__', '')\r | |
139 | unlink.append(init_name)\r | |
140 | init_basename = os.path.basename(init_name)\r | |
141 | script_name = make_script(zip_dir, script_basename, source)\r | |
142 | unlink.append(script_name)\r | |
143 | if compiled:\r | |
144 | init_name = compile_script(init_name)\r | |
145 | script_name = compile_script(script_name)\r | |
146 | unlink.extend((init_name, script_name))\r | |
147 | pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]\r | |
148 | script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))\r | |
149 | zip_filename = zip_basename+os.extsep+'zip'\r | |
150 | zip_name = os.path.join(zip_dir, zip_filename)\r | |
151 | zip_file = zipfile.ZipFile(zip_name, 'w')\r | |
152 | for name in pkg_names:\r | |
153 | init_name_in_zip = os.path.join(name, init_basename)\r | |
154 | zip_file.write(init_name, init_name_in_zip)\r | |
155 | zip_file.write(script_name, script_name_in_zip)\r | |
156 | zip_file.close()\r | |
157 | for name in unlink:\r | |
158 | os.unlink(name)\r | |
159 | #if test.test_support.verbose:\r | |
160 | # zip_file = zipfile.ZipFile(zip_name, 'r')\r | |
161 | # print 'Contents of %r:' % zip_name\r | |
162 | # zip_file.printdir()\r | |
163 | # zip_file.close()\r | |
164 | return zip_name, os.path.join(zip_name, script_name_in_zip)\r |