diff options
author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-06-21 09:57:20 +0100 |
---|---|---|
committer | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-06-21 10:06:34 +0100 |
commit | fd891afb3020eceab6d8528dfbc0d844865361d4 (patch) | |
tree | d95d028d7e74d2a30205a6c37dc5772af624e36d /setuptools | |
parent | cdd12f324185d3b4ca07597897e7aec9388cdfa9 (diff) | |
download | python-setuptools-git-fd891afb3020eceab6d8528dfbc0d844865361d4.tar.gz |
Allow dist-info to keep original egg-info directory
During a PEP 517 build, this directory can later be passed to
``build_py`` via the ``existing_egg_info_dir attribute``
(which in turn avoids re-running ``egg_info`` to create a manifest).
Diffstat (limited to 'setuptools')
-rw-r--r-- | setuptools/command/dist_info.py | 40 | ||||
-rw-r--r-- | setuptools/tests/test_dist_info.py | 13 |
2 files changed, 47 insertions, 6 deletions
diff --git a/setuptools/command/dist_info.py b/setuptools/command/dist_info.py index 39a74e1e..7f35a476 100644 --- a/setuptools/command/dist_info.py +++ b/setuptools/command/dist_info.py @@ -5,7 +5,10 @@ As defined in the wheel specification import os import re +import shutil +import sys import warnings +from contextlib import contextmanager from inspect import cleandoc from pathlib import Path @@ -28,9 +31,10 @@ class dist_info(Command): ('tag-date', 'd', "Add date stamp (e.g. 20050528) to version number"), ('tag-build=', 'b', "Specify explicit tag to add to version number"), ('no-date', 'D', "Don't include date stamp [default]"), + ('keep-egg-info', None, "*TRANSITIONAL* will be removed in the future"), ] - boolean_options = ['tag-date'] + boolean_options = ['tag-date', 'keep-egg-info'] negative_opt = {'no-date': 'tag-date'} def initialize_options(self): @@ -40,6 +44,7 @@ class dist_info(Command): self.dist_info_dir = None self.tag_date = None self.tag_build = None + self.keep_egg_info = False def finalize_options(self): if self.egg_base: @@ -72,14 +77,32 @@ class dist_info(Command): self.name = f"{name}-{version}" self.dist_info_dir = os.path.join(self.output_dir, f"{self.name}.dist-info") + @contextmanager + def _maybe_bkp_dir(self, dir_path: str, requires_bkp: bool): + if requires_bkp: + bkp_name = f"__bkp__.{dir_path}.__bkp__" + _rm(bkp_name, ignore_errors=True) + _copy(dir_path, bkp_name, dirs_exist_ok=True, symlinks=True) + try: + yield + finally: + _rm(dir_path, ignore_errors=True) + shutil.move(bkp_name, dir_path) + else: + yield + def run(self): self.output_dir.mkdir(parents=True, exist_ok=True) self.egg_info.run() egg_info_dir = self.egg_info.egg_info + assert os.path.isdir(egg_info_dir), ".egg-info dir should have been created" + log.info("creating '{}'".format(os.path.abspath(self.dist_info_dir))) bdist_wheel = self.get_finalized_command('bdist_wheel') - bdist_wheel.egg2dist(egg_info_dir, self.dist_info_dir) - assert os.path.exists(egg_info_dir) is False + + # TODO: if bdist_wheel if merged into setuptools, just add "keep_egg_info" there + with self._maybe_bkp_dir(egg_info_dir, self.keep_egg_info): + bdist_wheel.egg2dist(egg_info_dir, self.dist_info_dir) def _safe(component: str) -> str: @@ -106,3 +129,14 @@ def _version(version: str) -> str: """ warnings.warn(cleandoc(msg)) return _safe(v).strip("_") + + +def _rm(dir_name, **opts): + if os.path.isdir(dir_name): + shutil.rmtree(dir_name, **opts) + + +def _copy(src, dst, **opts): + if sys.version_info < (3, 8): + opts.pop("dirs_exist_ok", None) + shutil.copytree(src, dst, **opts) diff --git a/setuptools/tests/test_dist_info.py b/setuptools/tests/test_dist_info.py index 5cd1a342..605cb196 100644 --- a/setuptools/tests/test_dist_info.py +++ b/setuptools/tests/test_dist_info.py @@ -112,14 +112,21 @@ class TestDistInfo: dist_info = next(tmp_path.glob("*.dist-info")) assert dist_info.name.startswith("proj-42a") - def test_output_dir(self, tmp_path): + @pytest.mark.parametrize("keep_egg_info", (False, True)) + def test_output_dir(self, tmp_path, keep_egg_info): config = "[metadata]\nname=proj\nversion=42\n" (tmp_path / "setup.cfg").write_text(config, encoding="utf-8") out = (tmp_path / "__out") out.mkdir() - run_command("dist_info", "--output-dir", str(out), cwd=tmp_path) + opts = ["--keep-egg-info"] if keep_egg_info else [] + run_command("dist_info", "--output-dir", out, *opts, cwd=tmp_path) assert len(list(out.glob("*.dist-info"))) == 1 assert len(list(tmp_path.glob("*.dist-info"))) == 0 + expected_egg_info = 1 if keep_egg_info else 0 + assert len(list(out.glob("*.egg-info"))) == expected_egg_info + assert len(list(tmp_path.glob("*.egg-info"))) == 0 + assert len(list(out.glob("__bkp__.*.__bkp__"))) == 0 + assert len(list(tmp_path.glob("__bkp__.*.__bkp__"))) == 0 class TestWheelCompatibility: @@ -184,5 +191,5 @@ class TestWheelCompatibility: def run_command(*cmd, **kwargs): opts = {"stderr": subprocess.STDOUT, "text": True, **kwargs} - cmd = [sys.executable, "-c", "__import__('setuptools').setup()", *cmd] + cmd = [sys.executable, "-c", "__import__('setuptools').setup()", *map(str, cmd)] return subprocess.check_output(cmd, **opts) |