Skip to content

Commit

Permalink
building: simplify add_suffix_to_extensions function
Browse files Browse the repository at this point in the history
Instead of the function taking in a TOC and returning the processed
TOC, have it operate on individual TOC entry tuples.
  • Loading branch information
rokm authored and bwoodsend committed May 26, 2021
1 parent c7a09b6 commit 9e59228
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
13 changes: 8 additions & 5 deletions PyInstaller/building/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

from PyInstaller import HOMEPATH, PLATFORM
from PyInstaller.archive.writers import ZlibArchiveWriter, CArchiveWriter
from PyInstaller.building.utils import _check_guts_toc, add_suffix_to_extensions, \
from PyInstaller.building.utils import _check_guts_toc, \
add_suffix_to_extension, \
checkCache, strip_paths_in_code, get_code_object, \
_make_clean_directory
from PyInstaller.compat import is_win, is_darwin, is_linux, is_cygwin, \
Expand Down Expand Up @@ -225,10 +226,11 @@ def assemble(self):
seenInms = {}
seenFnms = {}
seenFnms_typ = {}
toc = add_suffix_to_extensions(self.toc)
# 'inm' - relative filename inside a CArchive
# 'fnm' - absolute filename as it is on the file system.
for inm, fnm, typ in toc:
for inm, fnm, typ in self.toc:
# Adjust name for extensions, if applicable
inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
# Ensure filename 'fnm' is not None or empty string. Otherwise
# it will fail in case of 'typ' being type OPTION.
if fnm and not os.path.isfile(fnm) and is_path_to_egg(fnm):
Expand Down Expand Up @@ -756,8 +758,9 @@ def _check_guts(self, data, last_build):
def assemble(self):
_make_clean_directory(self.name)
logger.info("Building COLLECT %s", self.tocbasename)
toc = add_suffix_to_extensions(self.toc)
for inm, fnm, typ in toc:
for inm, fnm, typ in self.toc:
# Adjust name for extensions, if applicable
inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
if not os.path.exists(fnm) or not os.path.isfile(fnm) and is_path_to_egg(fnm):
# file is contained within python egg, it is added with the egg
continue
Expand Down
7 changes: 4 additions & 3 deletions PyInstaller/building/osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from PyInstaller.building.api import EXE, COLLECT
from PyInstaller.building.datastruct import Target, TOC, logger
from PyInstaller.building.utils import _check_path_overlap, _rmtree, \
add_suffix_to_extensions, checkCache
add_suffix_to_extension, checkCache


class BUNDLE(Target):
Expand Down Expand Up @@ -164,8 +164,9 @@ def assemble(self):
plistlib.dump(info_plist_dict, plist_fh)

links = []
toc = add_suffix_to_extensions(self.toc)
for inm, fnm, typ in toc:
for inm, fnm, typ in self.toc:
# Adjust name for extensions, if applicable
inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
# Copy files from cache. This ensures that are used files with relative
# paths to dynamic library dependencies (@executable_path)
base_path = inm.split('/', 1)[0]
Expand Down
67 changes: 32 additions & 35 deletions PyInstaller/building/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,40 @@ def _check_guts_toc(attr, old, toc, last_build, pyc=0):

#---

def add_suffix_to_extensions(toc):
def add_suffix_to_extension(inm, fnm, typ):
"""
Returns a new TOC with proper library suffix for EXTENSION items.
Take a TOC entry (inm, fnm, typ) and adjust the inm for EXTENSION
or DEPENDENCY to include the full library suffix.
"""
# TODO: Fix this recursive import
from PyInstaller.building.datastruct import TOC
new_toc = TOC()
for inm, fnm, typ in toc:
if typ == 'EXTENSION':
if fnm.endswith(inm):
# If inm completely fits into end of the fnm, it has
# already been processed
new_toc.append((inm, fnm, typ))
continue
# Change the dotted name into a relative path. This places C
# extensions in the Python-standard location.
inm = inm.replace('.', os.sep)
# In some rare cases extension might already contain a suffix.
# Skip it in this case.
if os.path.splitext(inm)[1] not in EXTENSION_SUFFIXES:
# Determine the base name of the file.
base_name = os.path.basename(inm)
assert '.' not in base_name
# Use this file's existing extension. For extensions such as
# ``libzmq.cp36-win_amd64.pyd``, we can't use
# ``os.path.splitext``, which would give only the ```.pyd`` part
# of the extension.
inm = inm + os.path.basename(fnm)[len(base_name):]

elif typ == 'DEPENDENCY':
# Use the suffix from the filename.
# TODO Verify what extensions are by DEPENDENCIES.
binext = os.path.splitext(fnm)[1]
if not os.path.splitext(inm)[1] == binext:
inm = inm + binext
new_toc.append((inm, fnm, typ))
return new_toc
if typ == 'EXTENSION':
if fnm.endswith(inm):
# If inm completely fits into end of the fnm, it has
# already been processed
return inm, fnm, typ
# Change the dotted name into a relative path. This places C
# extensions in the Python-standard location.
inm = inm.replace('.', os.sep)
# In some rare cases extension might already contain a suffix.
# Skip it in this case.
if os.path.splitext(inm)[1] not in EXTENSION_SUFFIXES:
# Determine the base name of the file.
base_name = os.path.basename(inm)
assert '.' not in base_name
# Use this file's existing extension. For extensions such as
# ``libzmq.cp36-win_amd64.pyd``, we can't use
# ``os.path.splitext``, which would give only the ```.pyd`` part
# of the extension.
inm = inm + os.path.basename(fnm)[len(base_name):]

elif typ == 'DEPENDENCY':
# Use the suffix from the filename.
# TODO Verify what extensions are by DEPENDENCIES.
binext = os.path.splitext(fnm)[1]
if not os.path.splitext(inm)[1] == binext:
inm = inm + binext

return inm, fnm, typ


def applyRedirects(manifest, redirects):
"""
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_building_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ def test_add_suffix_to_extension():
fnm = str(pathlib.PurePath(case[2]))
typ = case[3]

toc = [(inm1, fnm, typ)]
toc_expected = [(inm2, fnm, typ)]
toc = (inm1, fnm, typ)
toc_expected = (inm2, fnm, typ)

# Ensure that processing a TOC entry produces expected result.
toc2 = utils.add_suffix_to_extensions(toc)
toc2 = utils.add_suffix_to_extension(*toc)
assert toc2 == toc_expected

# Ensure that processing an already-processed TOC entry leaves
# it unchanged (i.e., does not mangle it)
toc3 = utils.add_suffix_to_extensions(toc2)
toc3 = utils.add_suffix_to_extension(*toc2)
assert toc3 == toc2

0 comments on commit 9e59228

Please sign in to comment.