blob: f29143057d40188ca9fb5f8e862babdbb8ba3cd2 [file] [log] [blame]
Nick Coghlan49868cb2009-11-15 07:30:34 +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
14# Executing the interpreter in a subprocess
15def python_exit_code(*args):
16 cmd_line = [sys.executable, '-E']
17 cmd_line.extend(args)
18 with open(os.devnull, 'w') as devnull:
19 return subprocess.call(cmd_line, stdout=devnull,
20 stderr=subprocess.STDOUT)
21
22def spawn_python(*args):
23 cmd_line = [sys.executable, '-E']
24 cmd_line.extend(args)
25 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
26 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
27
28def kill_python(p):
29 p.stdin.close()
30 data = p.stdout.read()
31 p.stdout.close()
32 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou8c54e782009-12-08 19:25:51 +000033 # with regrtest -R.
34 p.wait()
Nick Coghlan49868cb2009-11-15 07:30:34 +000035 subprocess._cleanup()
36 return data
37
38def run_python(*args):
39 if __debug__:
40 p = spawn_python(*args)
41 else:
42 p = spawn_python('-O', *args)
43 stdout_data = kill_python(p)
44 return p.wait(), stdout_data
45
46# Script creation utilities
47@contextlib.contextmanager
48def temp_dir():
49 dirname = tempfile.mkdtemp()
50 dirname = os.path.realpath(dirname)
51 try:
52 yield dirname
53 finally:
54 shutil.rmtree(dirname)
55
56def make_script(script_dir, script_basename, source):
57 script_filename = script_basename+os.extsep+'py'
58 script_name = os.path.join(script_dir, script_filename)
59 script_file = open(script_name, 'w')
60 script_file.write(source)
61 script_file.close()
62 return script_name
63
64def compile_script(script_name):
65 py_compile.compile(script_name, doraise=True)
66 if __debug__:
67 compiled_name = script_name + 'c'
68 else:
69 compiled_name = script_name + 'o'
70 return compiled_name
71
72def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
73 zip_filename = zip_basename+os.extsep+'zip'
74 zip_name = os.path.join(zip_dir, zip_filename)
75 zip_file = zipfile.ZipFile(zip_name, 'w')
76 if name_in_zip is None:
77 name_in_zip = os.path.basename(script_name)
78 zip_file.write(script_name, name_in_zip)
79 zip_file.close()
80 #if test.test_support.verbose:
81 # zip_file = zipfile.ZipFile(zip_name, 'r')
82 # print 'Contents of %r:' % zip_name
83 # zip_file.printdir()
84 # zip_file.close()
85 return zip_name, os.path.join(zip_name, name_in_zip)
86
87def make_pkg(pkg_dir):
88 os.mkdir(pkg_dir)
89 make_script(pkg_dir, '__init__', '')
90
91def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
92 source, depth=1, compiled=False):
93 unlink = []
94 init_name = make_script(zip_dir, '__init__', '')
95 unlink.append(init_name)
96 init_basename = os.path.basename(init_name)
97 script_name = make_script(zip_dir, script_basename, source)
98 unlink.append(script_name)
99 if compiled:
100 init_name = compile_script(init_name)
101 script_name = compile_script(script_name)
102 unlink.extend((init_name, script_name))
103 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
104 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
105 zip_filename = zip_basename+os.extsep+'zip'
106 zip_name = os.path.join(zip_dir, zip_filename)
107 zip_file = zipfile.ZipFile(zip_name, 'w')
108 for name in pkg_names:
109 init_name_in_zip = os.path.join(name, init_basename)
110 zip_file.write(init_name, init_name_in_zip)
111 zip_file.write(script_name, script_name_in_zip)
112 zip_file.close()
113 for name in unlink:
114 os.unlink(name)
115 #if test.test_support.verbose:
116 # zip_file = zipfile.ZipFile(zip_name, 'r')
117 # print 'Contents of %r:' % zip_name
118 # zip_file.printdir()
119 # zip_file.close()
120 return zip_name, os.path.join(zip_name, script_name_in_zip)