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 b51e05f
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions tasks/__main__zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import os
import sys
import zipfile
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:
Expand Down Expand Up @@ -69,18 +70,34 @@ def _register_distutils_finder(self):
if "distlib" not in self.modules:
return

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

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.loader.get_data(os.path.join(self.path, name)))

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:
rname = resource.name
for name in resource.resources:
child = self.find(f"{rname}/{name}" if rname else name)
if child.is_container:
todo.append(child)
else:
yield child

from distlib.resources import register_finder # noqa: PLC0415

Expand Down Expand Up @@ -113,41 +130,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

0 comments on commit b51e05f

Please sign in to comment.