blob: af5252c8c4f67835aec255ab0eb986a5f476dee2 [file] [log] [blame]
Nick Coghlan260bd3e2009-11-16 06:49:25 +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
33 # with regrtest -R. This should be a no-op on Windows.
34 subprocess._cleanup()
35 return data
36
37def run_python(*args):
38 if __debug__:
39 p = spawn_python(*args)
40 else:
41 p = spawn_python('-O', *args)
42 stdout_data = kill_python(p)
43 return p.wait(), stdout_data
44
45# Script creation utilities
46@contextlib.contextmanager
47def temp_dir():
48 dirname = tempfile.mkdtemp()
49 dirname = os.path.realpath(dirname)
50 try:
51 yield dirname
52 finally:
53 shutil.rmtree(dirname)
54
55def make_script(script_dir, script_basename, source):
56 script_filename = script_basename+os.extsep+'py'
57 script_name = os.path.join(script_dir, script_filename)
58 script_file = open(script_name, 'w')
59 script_file.write(source)
60 script_file.close()
61 return script_name
62
63def compile_script(script_name):
64 py_compile.compile(script_name, doraise=True)
65 if __debug__:
66 compiled_name = script_name + 'c'
67 else:
68 compiled_name = script_name + 'o'
69 return compiled_name
70
71def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
72 zip_filename = zip_basename+os.extsep+'zip'
73 zip_name = os.path.join(zip_dir, zip_filename)
74 zip_file = zipfile.ZipFile(zip_name, 'w')
75 if name_in_zip is None:
76 name_in_zip = os.path.basename(script_name)
77 zip_file.write(script_name, name_in_zip)
78 zip_file.close()
79 #if test.test_support.verbose:
80 # zip_file = zipfile.ZipFile(zip_name, 'r')
81 # print 'Contents of %r:' % zip_name
82 # zip_file.printdir()
83 # zip_file.close()
84 return zip_name, os.path.join(zip_name, name_in_zip)
85
86def make_pkg(pkg_dir):
87 os.mkdir(pkg_dir)
88 make_script(pkg_dir, '__init__', '')
89
90def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
91 source, depth=1, compiled=False):
92 unlink = []
93 init_name = make_script(zip_dir, '__init__', '')
94 unlink.append(init_name)
95 init_basename = os.path.basename(init_name)
96 script_name = make_script(zip_dir, script_basename, source)
97 unlink.append(script_name)
98 if compiled:
99 init_name = compile_script(init_name)
100 script_name = compile_script(script_name)
101 unlink.extend((init_name, script_name))
102 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
103 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
104 zip_filename = zip_basename+os.extsep+'zip'
105 zip_name = os.path.join(zip_dir, zip_filename)
106 zip_file = zipfile.ZipFile(zip_name, 'w')
107 for name in pkg_names:
108 init_name_in_zip = os.path.join(name, init_basename)
109 zip_file.write(init_name, init_name_in_zip)
110 zip_file.write(script_name, script_name_in_zip)
111 zip_file.close()
112 for name in unlink:
113 os.unlink(name)
114 #if test.test_support.verbose:
115 # zip_file = zipfile.ZipFile(zip_name, 'r')
116 # print 'Contents of %r:' % zip_name
117 # zip_file.printdir()
118 # zip_file.close()
119 return zip_name, os.path.join(zip_name, script_name_in_zip)