Skip to content

Commit

Permalink
makespec: Remove *variable path* substitution.
Browse files Browse the repository at this point in the history
Remove the automatic substitution of PyInstaller.HOME_PATH (most likely
the ``site-packages`` directory) in relative CLI path parameters to into
a relocatable ``os.path.join(HOME_PATH, ...)``. I can't think of any
good reason why anyone would even notice this feature exists whereas
it's more likely that a PyInstaller test would unwittingly run into it.
  • Loading branch information
bwoodsend committed Nov 26, 2023
1 parent 48969d9 commit b825ef6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 79 deletions.
45 changes: 1 addition & 44 deletions PyInstaller/building/makespec.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ def make_path_spec_relative(filename, spec_dir):
return filename


# Support for trying to avoid hard-coded paths in the .spec files. Eg, all files rooted in the Installer directory tree
# will be written using "HOMEPATH", thus allowing this spec file to be used with any Installer installation. Same thing
# could be done for other paths too.
path_conversions = ((HOMEPATH, "HOMEPATH"),)


class SourceDestAction(argparse.Action):
"""
A command line option which takes multiple source:dest pairs.
Expand Down Expand Up @@ -81,27 +75,6 @@ def __call__(self, parser, namespace, value, option_string=None):
getattr(namespace, self.dest).append((portable_filepath(src), portable_filepath(dest)))


def make_variable_path(filename, conversions=path_conversions):
if not os.path.isabs(filename):
# os.path.commonpath can not compare relative and absolute paths, and if filename is not absolute, none of the
# paths in conversions will match anyway.
return None, filename
for (from_path, to_name) in conversions:
assert os.path.abspath(from_path) == from_path, ("path '%s' should already be absolute" % from_path)
try:
common_path = os.path.commonpath([filename, from_path])
except ValueError:
# Per https://docs.python.org/3/library/os.path.html#os.path.commonpath, this raises ValueError in several
# cases which prevent computing a common path.
common_path = None
if common_path == from_path:
rest = filename[len(from_path):]
if rest.startswith(('\\', '/')):
rest = rest[1:]
return to_name, rest
return None, filename


def removed_key_option(x):
from PyInstaller.exceptions import RemovedCipherFeatureError
raise RemovedCipherFeatureError("Please remove your --key=xxx argument.")
Expand Down Expand Up @@ -132,21 +105,6 @@ def __call__(self, *args, **kwargs):
raise RemovedWinSideBySideSupportError("Please remove your --win-no-prefer-redirects argument.")


# An object used in place of a "path string", which knows how to repr() itself using variable names instead of
# hard-coded paths.
class Path:
def __init__(self, *parts):
self.path = os.path.join(*parts)
self.variable_prefix = self.filename_suffix = None

def __repr__(self):
if self.filename_suffix is None:
self.variable_prefix, self.filename_suffix = make_variable_path(self.path)
if self.variable_prefix is None:
return repr(portable_filepath(self.path))
return "os.path.join(%s, %r)" % (self.variable_prefix, portable_filepath(self.filename_suffix))


# An object used to construct extra preamble for the spec file, in order to accommodate extra collect_*() calls from the
# command-line
class Preamble:
Expand Down Expand Up @@ -759,8 +717,7 @@ def main(

# If script paths are relative, make them relative to the directory containing .spec file.
scripts = [make_path_spec_relative(x, specpath) for x in scripts]
# With absolute paths replace prefix with variable HOMEPATH.
scripts = list(map(Path, scripts))
scripts = list(map(portable_filepath, scripts))

# Translate the default of ``debug=None`` to an empty list.
if debug is None:
Expand Down
2 changes: 2 additions & 0 deletions news/8116.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`ValueError` build error on Windows if :option:`--specpath` is set to
a different drive.
38 changes: 3 additions & 35 deletions tests/unit/test_makespec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,6 @@
from PyInstaller.building import makespec


def test_make_variable_path():
p = os.path.join(makespec.HOMEPATH, "aaa", "bbb", "ccc")
assert (makespec.make_variable_path(p) == ("HOMEPATH", os.path.join("aaa", "bbb", "ccc")))


def test_make_variable_path_regression():
p = os.path.join(makespec.HOMEPATH + "aaa", "bbb", "ccc")
assert makespec.make_variable_path(p) == (None, p)


def test_Path_constructor():
p = makespec.Path("aaa", "bbb", "ccc")
assert p.path == os.path.join("aaa", "bbb", "ccc")


def test_Path_repr():
p = makespec.Path(makespec.HOMEPATH, "aaa", "bbb", "ccc")
assert p.path == os.path.join(makespec.HOMEPATH, "aaa", "bbb", "ccc")
assert (repr(p) == "os.path.join(HOMEPATH,%r)" % os.path.join("aaa", "bbb", "ccc"))


def test_Path_repr_relative():
p = makespec.Path("aaa", "bbb", "ccc.py")
assert p.path == os.path.join("aaa", "bbb", "ccc.py")
assert repr(p) == "%r" % os.path.join("aaa", "bbb", "ccc.py")


def test_Path_regression():
p = makespec.Path(makespec.HOMEPATH + "-aaa", "bbb", "ccc")
assert p.path == os.path.join(makespec.HOMEPATH + "-aaa", "bbb", "ccc")
assert (repr(p) == repr(os.path.join(makespec.HOMEPATH + "-aaa", "bbb", "ccc")))


def test_add_data(capsys):
"""
Test CLI parsing of --add-data and --add-binary.
Expand All @@ -59,9 +26,10 @@ def test_add_data(capsys):

assert parser.parse_args([]).datas == []
assert parser.parse_args(["--add-data", "/foo/bar:."]).datas == [("/foo/bar", ".")]
assert parser.parse_args([r"--add-data=C:\foo\bar:baz"]).datas == [(r"C:\foo\bar", "baz")]
if os.name == "nt":
assert parser.parse_args([r"--add-data=C:\foo\bar:baz"]).datas == [(r"C:/foo/bar", "baz")]
assert parser.parse_args([r"--add-data=c:/foo/bar:baz"]).datas == [(r"c:/foo/bar", "baz")]
assert parser.parse_args([r"--add-data=/foo/:bar"]).datas == [("/foo/", "bar")]
assert parser.parse_args([r"--add-data=/foo/:bar"]).datas == [("/foo", "bar")]

for args in [["--add-data", "foo/bar"], ["--add-data", "C:/foo/bar"]]:
with pytest.raises(SystemExit):
Expand Down

0 comments on commit b825ef6

Please sign in to comment.