Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 1 | # Common utility functions used by various script execution tests |
| 2 | # e.g. test_cmd_line, test_cmd_line_script and test_runpy |
| 3 | |
| 4 | import sys |
| 5 | import os |
| 6 | import os.path |
| 7 | import tempfile |
| 8 | import subprocess |
| 9 | import py_compile |
| 10 | import contextlib |
| 11 | import shutil |
| 12 | import zipfile |
| 13 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 14 | from imp import source_from_cache |
| 15 | from test.support import make_legacy_pyc |
| 16 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 17 | # Executing the interpreter in a subprocess |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 18 | def _assert_python(expected_success, *args): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 19 | cmd_line = [sys.executable, '-E'] |
| 20 | cmd_line.extend(args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 21 | p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 22 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 23 | try: |
| 24 | out, err = p.communicate() |
| 25 | finally: |
| 26 | subprocess._cleanup() |
Brian Curtin | c4ac887 | 2010-11-01 14:00:33 +0000 | [diff] [blame^] | 27 | p.stdout.close() |
| 28 | p.stderr.close() |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 29 | rc = p.returncode |
| 30 | if (rc and expected_success) or (not rc and not expected_success): |
| 31 | raise AssertionError( |
| 32 | "Process return code is %d, " |
| 33 | "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore'))) |
| 34 | return rc, out, err |
| 35 | |
| 36 | def assert_python_ok(*args): |
| 37 | return _assert_python(True, *args) |
| 38 | |
| 39 | def assert_python_failure(*args): |
| 40 | return _assert_python(False, *args) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 41 | |
| 42 | def spawn_python(*args): |
| 43 | cmd_line = [sys.executable, '-E'] |
| 44 | cmd_line.extend(args) |
| 45 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 46 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 47 | |
| 48 | def kill_python(p): |
| 49 | p.stdin.close() |
| 50 | data = p.stdout.read() |
| 51 | p.stdout.close() |
| 52 | # try to cleanup the child so we don't appear to leak when running |
Antoine Pitrou | 4e7dc5f | 2009-12-08 19:27:24 +0000 | [diff] [blame] | 53 | # with regrtest -R. |
| 54 | p.wait() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 55 | subprocess._cleanup() |
| 56 | return data |
| 57 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 58 | # Script creation utilities |
| 59 | @contextlib.contextmanager |
| 60 | def temp_dir(): |
| 61 | dirname = tempfile.mkdtemp() |
| 62 | dirname = os.path.realpath(dirname) |
| 63 | try: |
| 64 | yield dirname |
| 65 | finally: |
| 66 | shutil.rmtree(dirname) |
| 67 | |
| 68 | def make_script(script_dir, script_basename, source): |
| 69 | script_filename = script_basename+os.extsep+'py' |
| 70 | script_name = os.path.join(script_dir, script_filename) |
Florent Xicluna | 8de42e2 | 2010-02-27 16:12:22 +0000 | [diff] [blame] | 71 | # The script should be encoded to UTF-8, the default string encoding |
| 72 | script_file = open(script_name, 'w', encoding='utf-8') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 73 | script_file.write(source) |
| 74 | script_file.close() |
| 75 | return script_name |
| 76 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 77 | def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): |
| 78 | zip_filename = zip_basename+os.extsep+'zip' |
| 79 | zip_name = os.path.join(zip_dir, zip_filename) |
| 80 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 81 | if name_in_zip is None: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 82 | parts = script_name.split(os.sep) |
| 83 | if len(parts) >= 2 and parts[-2] == '__pycache__': |
| 84 | legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) |
| 85 | name_in_zip = os.path.basename(legacy_pyc) |
| 86 | script_name = legacy_pyc |
| 87 | else: |
| 88 | name_in_zip = os.path.basename(script_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 89 | zip_file.write(script_name, name_in_zip) |
| 90 | zip_file.close() |
Florent Xicluna | 02ea12b | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 91 | #if test.support.verbose: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 92 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 93 | # print 'Contents of %r:' % zip_name |
| 94 | # zip_file.printdir() |
| 95 | # zip_file.close() |
| 96 | return zip_name, os.path.join(zip_name, name_in_zip) |
| 97 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 98 | def make_pkg(pkg_dir, init_source=''): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 99 | os.mkdir(pkg_dir) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 100 | make_script(pkg_dir, '__init__', init_source) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 101 | |
| 102 | def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 103 | source, depth=1, compiled=False): |
| 104 | unlink = [] |
| 105 | init_name = make_script(zip_dir, '__init__', '') |
| 106 | unlink.append(init_name) |
| 107 | init_basename = os.path.basename(init_name) |
| 108 | script_name = make_script(zip_dir, script_basename, source) |
| 109 | unlink.append(script_name) |
| 110 | if compiled: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 111 | init_name = py_compile(init_name, doraise=True) |
| 112 | script_name = py_compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 113 | unlink.extend((init_name, script_name)) |
| 114 | pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
| 115 | script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) |
| 116 | zip_filename = zip_basename+os.extsep+'zip' |
| 117 | zip_name = os.path.join(zip_dir, zip_filename) |
| 118 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 119 | for name in pkg_names: |
| 120 | init_name_in_zip = os.path.join(name, init_basename) |
| 121 | zip_file.write(init_name, init_name_in_zip) |
| 122 | zip_file.write(script_name, script_name_in_zip) |
| 123 | zip_file.close() |
| 124 | for name in unlink: |
| 125 | os.unlink(name) |
Florent Xicluna | 02ea12b | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 126 | #if test.support.verbose: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 127 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 128 | # print 'Contents of %r:' % zip_name |
| 129 | # zip_file.printdir() |
| 130 | # zip_file.close() |
| 131 | return zip_name, os.path.join(zip_name, script_name_in_zip) |