Skip to content

Commit

Permalink
Fix broken Windows zipapp
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Oct 17, 2024
1 parent 228b615 commit c70267c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 63 deletions.
3 changes: 3 additions & 0 deletions docs/changelog/2783.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Upgrade embedded wheels:

* setuptools to ``75.2.0`` from ``75.1.0``
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ lint.ignore = [
"PLR0914", # Too many local variables
"PLR0917", # Too many positional arguments
"PLR6301", # Method could be a function, class method, or static method
"PLW1510", # no need for check for subprocess
"PTH", # no pathlib, <=39 has problems on Windows with absolute/resolve, can revisit once we no longer need 39
"S104", # Possible binding to all interfaces
"S404", # Using subprocess is alright
Expand Down
14 changes: 7 additions & 7 deletions src/virtualenv/seed/wheels/embed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@
},
"3.8": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
"3.9": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
"3.10": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
"3.11": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
"3.12": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
"3.13": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
"3.14": {
"pip": "pip-24.2-py3-none-any.whl",
"setuptools": "setuptools-75.1.0-py3-none-any.whl",
"setuptools": "setuptools-75.2.0-py3-none-any.whl",
"wheel": "wheel-0.44.0-py3-none-any.whl",
},
}
Expand Down
Binary file not shown.
106 changes: 60 additions & 46 deletions tasks/__main__zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
import os
import sys
import zipfile
from functools import cached_property
from importlib.abc import SourceLoader
from importlib.util import spec_from_file_location

ABS_HERE = os.path.abspath(os.path.dirname(__file__))
NEW_IMPORT_SYSTEM = sys.version_info[0] >= 3 # noqa: PLR2004


class VersionPlatformSelect:
def __init__(self) -> None:
self.archive = ABS_HERE
self._zip_file = zipfile.ZipFile(ABS_HERE, "r")
zipapp = ABS_HERE
self.archive = zipapp
self._zip_file = zipfile.ZipFile(zipapp)
self.modules = self._load("modules.json")
self.distributions = self._load("distributions.json")
self.__cache = {}

def _load(self, of_file):
version = ".".join(str(i) for i in sys.version_info[0:2])
per_version = json.loads(self.get_data(of_file).decode("utf-8"))
per_version = json.loads(self.get_data(of_file).decode())
all_platforms = per_version[version] if version in per_version else per_version["3.9"]
content = all_platforms.get("==any", {}) # start will all platforms
not_us = f"!={sys.platform}"
Expand Down Expand Up @@ -65,25 +68,62 @@ def find_distributions(self, context):
def __repr__(self) -> str:
return f"{self.__class__.__name__}(path={ABS_HERE})"

def _register_distutils_finder(self):
def _register_distutils_finder(self): # noqa: C901
if "distlib" not in self.modules:
return

class Resource:
def __init__(self, path: str, name: str, loader: SourceLoader) -> None:
self.path = os.path.join(path, name)
self.name = name
self.loader = loader

@property
def bytes(self) -> bytes:
return self.loader.get_data(self.name)

@property
def is_container(self) -> bool:
return len(self.resources) > 1

@cached_property
def resources(self) -> list[str]:
return [
i.filename
for i in (
(j for j in zip_file.filelist if j.filename.startswith(f"{self.name}/"))
if self.name
else zip_file.filelist
)
]

class DistlibFinder:
def __init__(self, path, loader) -> None:
self.path = path
self.loader = loader

def find(self, name):
class Resource:
def __init__(self, content) -> None:
self.bytes = content

full_path = os.path.join(self.path, name)
return Resource(self.loader.get_data(full_path))
return Resource(self.path, name, self.loader)

def iterator(self, resource_name):
resource = self.find(resource_name)
if resource is not None:
todo = [resource]
while todo:
resource = todo.pop(0)
yield resource
if resource.is_container:
resource_name = resource.name
for name in resource.resources:
child = self.find(f"{resource_name}/{name}" if resource_name else name)
if child.is_container:
todo.append(child)
else:
yield child

from distlib.resources import register_finder # noqa: PLC0415

zip_file = self._zip_file
register_finder(self, lambda module: DistlibFinder(os.path.dirname(module.__file__), self))


Expand Down Expand Up @@ -113,41 +153,15 @@ def locate_file(self, path):
return _VER_DISTRIBUTION_CLASS


if NEW_IMPORT_SYSTEM:
from importlib.abc import SourceLoader
from importlib.util import spec_from_file_location

class VersionedFindLoad(VersionPlatformSelect, SourceLoader):
def find_spec(self, fullname, path, target=None): # noqa: ARG002
zip_path = self.find_mod(fullname)
if zip_path is not None:
return spec_from_file_location(name=fullname, loader=self)
return None

def module_repr(self, module):
raise NotImplementedError

else:
from imp import new_module

class VersionedFindLoad(VersionPlatformSelect):
def find_module(self, fullname, path=None): # noqa: ARG002
return self if self.find_mod(fullname) else None

def load_module(self, fullname):
filename = self.get_filename(fullname)
code = self.get_data(filename)
mod = sys.modules.setdefault(fullname, new_module(fullname))
mod.__file__ = filename
mod.__loader__ = self
is_package = filename.endswith("__init__.py")
if is_package:
mod.__path__ = [os.path.dirname(filename)]
mod.__package__ = fullname
else:
mod.__package__ = fullname.rpartition(".")[0]
exec(code, mod.__dict__) # noqa: S102
return mod
class VersionedFindLoad(VersionPlatformSelect, SourceLoader):
def find_spec(self, fullname, path, target=None): # noqa: ARG002
zip_path = self.find_mod(fullname)
if zip_path is not None:
return spec_from_file_location(name=fullname, loader=self)
return None

def module_repr(self, module):
raise NotImplementedError


def run():
Expand Down
13 changes: 3 additions & 10 deletions tasks/upgrade_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def run(): # noqa: C901

added = collect_package_versions(new_packages)
removed = collect_package_versions(remove_packages)

outcome = (1 if STRICT else 0) if (added or removed) else 0
print(f"Outcome {outcome} added {added} removed {removed}") # noqa: T201
lines = ["Upgrade embedded wheels:", ""]
for key, versions in added.items():
text = f"* {key} to {fmt_version(versions)}"
Expand Down Expand Up @@ -119,15 +119,8 @@ def get_embed_wheel(distribution, for_py_version):
)
dest_target = DEST / "__init__.py"
dest_target.write_text(msg, encoding="utf-8")

subprocess.run(
[sys.executable, "-m", "ruff", "check", str(dest_target), "--fix", "--unsafe-fixes"],
check=False,
)
subprocess.run(
[sys.executable, "-m", "ruff", "format", str(dest_target), "--preview"],
check=False,
)
subprocess.run([sys.executable, "-m", "ruff", "format", str(dest_target), "--preview"])
subprocess.run([sys.executable, "-m", "ruff", "check", str(dest_target), "--fix", "--unsafe-fixes"])

raise SystemExit(outcome)

Expand Down

0 comments on commit c70267c

Please sign in to comment.