GeezerVison is showing github.com — gh-109615: Fix test_tools.test_freeze SRCDIR (#109935) · python/cpython@1512d6c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/libregrtest/utils.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -355,7 +355,7 @@ def get_temp_dir(tmp_dir: StrPath | None = None) -> StrPath: | 355 | 355 | if not support.is_wasi: | 356 | 356 | tmp_dir = sysconfig.get_config_var('abs_builddir') | 357 | 357 | if tmp_dir is None: | 358 | | - # bpo-30284: On Windows, only srcdir is available. Using | | 358 | + # gh-74470: On Windows, only srcdir is available. Using | 359 | 359 | # abs_builddir mostly matters on UNIX when building Python | 360 | 360 | # out of the source tree, especially when the source tree | 361 | 361 | # is read only. | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/support/__init__.py +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2565,3 +2565,30 @@ def wrapper(*args, **kwargs): | 2565 | 2565 | finally: | 2566 | 2566 | _testinternalcapi.set_optimizer(save_opt) | 2567 | 2567 | return wrapper | | 2568 | + | | 2569 | + | | 2570 | +_BASE_COPY_SRC_DIR_IGNORED_NAMES = frozenset({ | | 2571 | + # SRC_DIR/.git | | 2572 | + '.git', | | 2573 | + # ignore all __pycache__/ sub-directories | | 2574 | + '__pycache__', | | 2575 | +}) | | 2576 | + | | 2577 | +# Ignore function for shutil.copytree() to copy the Python source code. | | 2578 | +def copy_python_src_ignore(path, names): | | 2579 | + ignored = _BASE_COPY_SRC_DIR_IGNORED_NAMES | | 2580 | + if os.path.basename(path) == 'Doc': | | 2581 | + ignored |= { | | 2582 | + # SRC_DIR/Doc/build/ | | 2583 | + 'build', | | 2584 | + # SRC_DIR/Doc/venv/ | | 2585 | + 'venv', | | 2586 | + } | | 2587 | + | | 2588 | + # check if we are at the root of the source code | | 2589 | + elif 'Modules' in names: | | 2590 | + ignored |= { | | 2591 | + # SRC_DIR/build/ | | 2592 | + 'build', | | 2593 | + } | | 2594 | + return ignored |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_support.py +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | import stat | 8 | 8 | import subprocess | 9 | 9 | import sys | | 10 | +import sysconfig | 10 | 11 | import tempfile | 11 | 12 | import textwrap | 12 | 13 | import unittest | @@ -800,6 +801,27 @@ def test_set_memlimit(self): | 800 | 801 | support.max_memuse = old_max_memuse | 801 | 802 | support.real_max_memuse = old_real_max_memuse | 802 | 803 |
| | 804 | + def test_copy_python_src_ignore(self): | | 805 | + src_dir = sysconfig.get_config_var('srcdir') | | 806 | + src_dir = os.path.abspath(src_dir) | | 807 | + | | 808 | + ignored = {'.git', '__pycache__'} | | 809 | + | | 810 | + # Source code directory | | 811 | + names = os.listdir(src_dir) | | 812 | + self.assertEqual(support.copy_python_src_ignore(src_dir, names), | | 813 | + ignored | {'build'}) | | 814 | + | | 815 | + # Doc/ directory | | 816 | + path = os.path.join(src_dir, 'Doc') | | 817 | + self.assertEqual(support.copy_python_src_ignore(path, os.listdir(path)), | | 818 | + ignored | {'build', 'venv'}) | | 819 | + | | 820 | + # An other directory | | 821 | + path = os.path.join(src_dir, 'Objects') | | 822 | + self.assertEqual(support.copy_python_src_ignore(path, os.listdir(path)), | | 823 | + ignored) | | 824 | + | 803 | 825 | # XXX -follows a list of untested API | 804 | 826 | # make_legacy_pyc | 805 | 827 | # is_resource_enabled | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_venv.py +2-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | skip_if_broken_multiprocessing_synchronize, verbose, | 22 | 22 | requires_subprocess, is_emscripten, is_wasi, | 23 | 23 | requires_venv_with_pip, TEST_HOME_DIR, | 24 | | - requires_resource) | | 24 | + requires_resource, copy_python_src_ignore) | 25 | 25 | from test.support.os_helper import (can_symlink, EnvironmentVarGuard, rmtree) | 26 | 26 | import unittest | 27 | 27 | import venv | @@ -560,12 +560,6 @@ def test_zippath_from_non_installed_posix(self): | 560 | 560 | stdlib_zip) | 561 | 561 | additional_pythonpath_for_non_installed = [] | 562 | 562 |
| 563 | | - # gh-109748: Don't copy __pycache__/ sub-directories, because they can | 564 | | - # be modified by other Python tests running in parallel. | 565 | | - ignored_names = {'__pycache__'} | 566 | | - def ignore_pycache(src, names): | 567 | | - return ignored_names | 568 | | - | 569 | 563 | # Copy stdlib files to the non-installed python so venv can | 570 | 564 | # correctly calculate the prefix. | 571 | 565 | for eachpath in sys.path: | @@ -583,7 +577,7 @@ def ignore_pycache(src, names): | 583 | 577 | shutil.copy(fn, libdir) | 584 | 578 | elif os.path.isdir(fn): | 585 | 579 | shutil.copytree(fn, os.path.join(libdir, name), | 586 | | - ignore=ignore_pycache) | | 580 | + ignore=copy_python_src_ignore) | 587 | 581 | else: | 588 | 582 | additional_pythonpath_for_non_installed.append( | 589 | 583 | eachpath) | |
Collapse file Copy file name to clipboardExpand all lines: Tools/freeze/test/freeze.py +7-47Lines changed: 7 additions & 47 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import os | 2 | 2 | import os.path | 3 | | -import re | 4 | 3 | import shlex | 5 | 4 | import shutil | 6 | 5 | import subprocess | | 6 | +import sysconfig | | 7 | +from test import support | 7 | 8 |
| 8 | 9 |
| 9 | 10 | TESTS_DIR = os.path.dirname(__file__) | 10 | 11 | TOOL_ROOT = os.path.dirname(TESTS_DIR) | 11 | | -SRCDIR = os.path.dirname(os.path.dirname(TOOL_ROOT)) | | 12 | +SRCDIR = os.path.abspath(sysconfig.get_config_var('srcdir')) | 12 | 13 |
| 13 | 14 | MAKE = shutil.which('make') | 14 | 15 | FREEZE = os.path.join(TOOL_ROOT, 'freeze.py') | @@ -75,56 +76,17 @@ def ensure_opt(args, name, value): | 75 | 76 |
| 76 | 77 |
| 77 | 78 | def copy_source_tree(newroot, oldroot): | 78 | | - print(f'copying the source tree into {newroot}...') | | 79 | + print(f'copying the source tree from {oldroot} to {newroot}...') | 79 | 80 | if os.path.exists(newroot): | 80 | 81 | if newroot == SRCDIR: | 81 | 82 | raise Exception('this probably isn\'t what you wanted') | 82 | 83 | shutil.rmtree(newroot) | 83 | 84 |
| 84 | | - def ignore_non_src(src, names): | 85 | | - """Turns what could be a 1000M copy into a 100M copy.""" | 86 | | - # Don't copy the ~600M+ of needless git repo metadata. | 87 | | - # source only, ignore cached .pyc files. | 88 | | - subdirs_to_skip = {'.git', '__pycache__'} | 89 | | - if os.path.basename(src) == 'Doc': | 90 | | - # Another potential ~250M+ of non test related data. | 91 | | - subdirs_to_skip.add('build') | 92 | | - subdirs_to_skip.add('venv') | 93 | | - return subdirs_to_skip | 94 | | - | 95 | | - shutil.copytree(oldroot, newroot, ignore=ignore_non_src) | | 85 | + shutil.copytree(oldroot, newroot, ignore=support.copy_python_src_ignore) | 96 | 86 | if os.path.exists(os.path.join(newroot, 'Makefile')): | 97 | 87 | _run_quiet([MAKE, 'clean'], newroot) | 98 | 88 |
| 99 | 89 |
| 100 | | -def get_makefile_var(builddir, name): | 101 | | - regex = re.compile(rf'^{name} *=\s*(.*?)\s*$') | 102 | | - filename = os.path.join(builddir, 'Makefile') | 103 | | - try: | 104 | | - infile = open(filename, encoding='utf-8') | 105 | | - except FileNotFoundError: | 106 | | - return None | 107 | | - with infile: | 108 | | - for line in infile: | 109 | | - m = regex.match(line) | 110 | | - if m: | 111 | | - value, = m.groups() | 112 | | - return value or '' | 113 | | - return None | 114 | | - | 115 | | - | 116 | | -def get_config_var(builddir, name): | 117 | | - python = os.path.join(builddir, 'python') | 118 | | - if os.path.isfile(python): | 119 | | - cmd = [python, '-c', | 120 | | - f'import sysconfig; print(sysconfig.get_config_var("{name}"))'] | 121 | | - try: | 122 | | - return _run_stdout(cmd) | 123 | | - except subprocess.CalledProcessError: | 124 | | - pass | 125 | | - return get_makefile_var(builddir, name) | 126 | | - | 127 | | - | 128 | 90 | ################################## | 129 | 91 | # freezing | 130 | 92 |
| @@ -151,10 +113,8 @@ def prepare(script=None, outdir=None): | 151 | 113 |
| 152 | 114 | # Run configure. | 153 | 115 | print(f'configuring python in {builddir}...') | 154 | | - cmd = [ | 155 | | - os.path.join(srcdir, 'configure'), | 156 | | - *shlex.split(get_config_var(SRCDIR, 'CONFIG_ARGS') or ''), | 157 | | - ] | | 116 | + config_args = shlex.split(sysconfig.get_config_var('CONFIG_ARGS') or '') | | 117 | + cmd = [os.path.join(srcdir, 'configure'), *config_args] | 158 | 118 | ensure_opt(cmd, 'cache-file', os.path.join(outdir, 'python-config.cache')) | 159 | 119 | prefix = os.path.join(outdir, 'python-installation') | 160 | 120 | ensure_opt(cmd, 'prefix', prefix) | |
|