Left: | ||
Right: |
OLD | NEW |
---|---|
1 # Common utility functions used by various script execution tests | 1 # Common utility functions used by various script execution tests |
2 # e.g. test_cmd_line, test_cmd_line_script and test_runpy | 2 # e.g. test_cmd_line, test_cmd_line_script and test_runpy |
3 | 3 |
4 import sys | 4 import sys |
5 import os | 5 import os |
6 import os.path | 6 import os.path |
7 import tempfile | 7 import tempfile |
8 import subprocess | 8 import subprocess |
9 import py_compile | 9 import py_compile |
10 import contextlib | 10 import contextlib |
11 import shutil | 11 import shutil |
12 import zipfile | 12 import zipfile |
13 | 13 |
14 from imp import cache_from_source, source_from_cache | |
Benjamin
2010/04/07 02:50:04
cache_from_source isn't actually used anywhere.
barry
2010/04/08 12:53:47
Done.
| |
15 from test.support import make_legacy_pyc | |
16 | |
14 # Executing the interpreter in a subprocess | 17 # Executing the interpreter in a subprocess |
15 def python_exit_code(*args): | 18 def python_exit_code(*args): |
16 cmd_line = [sys.executable, '-E'] | 19 cmd_line = [sys.executable, '-E'] |
17 cmd_line.extend(args) | 20 cmd_line.extend(args) |
18 with open(os.devnull, 'w') as devnull: | 21 with open(os.devnull, 'w') as devnull: |
19 return subprocess.call(cmd_line, stdout=devnull, | 22 return subprocess.call(cmd_line, stdout=devnull, |
20 stderr=subprocess.STDOUT) | 23 stderr=subprocess.STDOUT) |
21 | 24 |
22 def spawn_python(*args): | 25 def spawn_python(*args): |
23 cmd_line = [sys.executable, '-E'] | 26 cmd_line = [sys.executable, '-E'] |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 | 58 |
56 def make_script(script_dir, script_basename, source): | 59 def make_script(script_dir, script_basename, source): |
57 script_filename = script_basename+os.extsep+'py' | 60 script_filename = script_basename+os.extsep+'py' |
58 script_name = os.path.join(script_dir, script_filename) | 61 script_name = os.path.join(script_dir, script_filename) |
59 # The script should be encoded to UTF-8, the default string encoding | 62 # The script should be encoded to UTF-8, the default string encoding |
60 script_file = open(script_name, 'w', encoding='utf-8') | 63 script_file = open(script_name, 'w', encoding='utf-8') |
61 script_file.write(source) | 64 script_file.write(source) |
62 script_file.close() | 65 script_file.close() |
63 return script_name | 66 return script_name |
64 | 67 |
65 def compile_script(script_name): | |
66 py_compile.compile(script_name, doraise=True) | |
67 if __debug__: | |
68 compiled_name = script_name + 'c' | |
69 else: | |
70 compiled_name = script_name + 'o' | |
71 return compiled_name | |
72 | |
73 def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): | 68 def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): |
74 zip_filename = zip_basename+os.extsep+'zip' | 69 zip_filename = zip_basename+os.extsep+'zip' |
75 zip_name = os.path.join(zip_dir, zip_filename) | 70 zip_name = os.path.join(zip_dir, zip_filename) |
76 zip_file = zipfile.ZipFile(zip_name, 'w') | 71 zip_file = zipfile.ZipFile(zip_name, 'w') |
77 if name_in_zip is None: | 72 if name_in_zip is None: |
78 name_in_zip = os.path.basename(script_name) | 73 parts = script_name.split(os.sep) |
74 if len(parts) >= 2 and parts[-2] == '__pycache__': | |
75 legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) | |
76 name_in_zip = os.path.basename(legacy_pyc) | |
77 script_name = legacy_pyc | |
78 else: | |
79 name_in_zip = os.path.basename(script_name) | |
79 zip_file.write(script_name, name_in_zip) | 80 zip_file.write(script_name, name_in_zip) |
80 zip_file.close() | 81 zip_file.close() |
81 #if test.test_support.verbose: | 82 #if test.test_support.verbose: |
82 # zip_file = zipfile.ZipFile(zip_name, 'r') | 83 # zip_file = zipfile.ZipFile(zip_name, 'r') |
83 # print 'Contents of %r:' % zip_name | 84 # print 'Contents of %r:' % zip_name |
84 # zip_file.printdir() | 85 # zip_file.printdir() |
85 # zip_file.close() | 86 # zip_file.close() |
86 return zip_name, os.path.join(zip_name, name_in_zip) | 87 return zip_name, os.path.join(zip_name, name_in_zip) |
87 | 88 |
88 def make_pkg(pkg_dir): | 89 def make_pkg(pkg_dir): |
89 os.mkdir(pkg_dir) | 90 os.mkdir(pkg_dir) |
90 make_script(pkg_dir, '__init__', '') | 91 make_script(pkg_dir, '__init__', '') |
91 | 92 |
92 def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, | 93 def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
93 source, depth=1, compiled=False): | 94 source, depth=1, compiled=False): |
94 unlink = [] | 95 unlink = [] |
95 init_name = make_script(zip_dir, '__init__', '') | 96 init_name = make_script(zip_dir, '__init__', '') |
96 unlink.append(init_name) | 97 unlink.append(init_name) |
97 init_basename = os.path.basename(init_name) | 98 init_basename = os.path.basename(init_name) |
98 script_name = make_script(zip_dir, script_basename, source) | 99 script_name = make_script(zip_dir, script_basename, source) |
99 unlink.append(script_name) | 100 unlink.append(script_name) |
100 if compiled: | 101 if compiled: |
101 init_name = compile_script(init_name) | 102 init_name = py_compile(init_name, doraise=True) |
102 script_name = compile_script(script_name) | 103 script_name = py_compile(script_name, doraise=True) |
103 unlink.extend((init_name, script_name)) | 104 unlink.extend((init_name, script_name)) |
104 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] | 105 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
105 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_nam e)) | 106 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_nam e)) |
106 zip_filename = zip_basename+os.extsep+'zip' | 107 zip_filename = zip_basename+os.extsep+'zip' |
107 zip_name = os.path.join(zip_dir, zip_filename) | 108 zip_name = os.path.join(zip_dir, zip_filename) |
108 zip_file = zipfile.ZipFile(zip_name, 'w') | 109 zip_file = zipfile.ZipFile(zip_name, 'w') |
109 for name in pkg_names: | 110 for name in pkg_names: |
110 init_name_in_zip = os.path.join(name, init_basename) | 111 init_name_in_zip = os.path.join(name, init_basename) |
111 zip_file.write(init_name, init_name_in_zip) | 112 zip_file.write(init_name, init_name_in_zip) |
112 zip_file.write(script_name, script_name_in_zip) | 113 zip_file.write(script_name, script_name_in_zip) |
113 zip_file.close() | 114 zip_file.close() |
114 for name in unlink: | 115 for name in unlink: |
115 os.unlink(name) | 116 os.unlink(name) |
116 #if test.test_support.verbose: | 117 #if test.test_support.verbose: |
117 # zip_file = zipfile.ZipFile(zip_name, 'r') | 118 # zip_file = zipfile.ZipFile(zip_name, 'r') |
118 # print 'Contents of %r:' % zip_name | 119 # print 'Contents of %r:' % zip_name |
119 # zip_file.printdir() | 120 # zip_file.printdir() |
120 # zip_file.close() | 121 # zip_file.close() |
121 return zip_name, os.path.join(zip_name, script_name_in_zip) | 122 return zip_name, os.path.join(zip_name, script_name_in_zip) |
OLD | NEW |