Skip to content

Commit

Permalink
main: early exit on non-existent script files
Browse files Browse the repository at this point in the history
When trying to run PyInstaller against non-existent script file(s),
exit with error message immediately, before writing the spec file
and trying to build it.

This prevents us from overwriting an existing (and customized) .spec
file if user makes a typo in the .spec file's suffix when trying to
build it, for example, `pyinstaller program.cpes`. It also prevents
creation of a .spec file when `pyinstaller program.py` is accidentally
ran from a directory that does not contain the script (for example,
due to failing to change the directory prior to running the command).

This applies only to the `pyinstaller` command (and the equivalent
`python -m PyInstaller` invocation); the `pyi-makespec` command is
still allowed to be called with non-existent script file(s) that
might be added later, before the actual build attempt with the
generated .spec file.
  • Loading branch information
rokm committed Feb 3, 2024
1 parent 9c9b83a commit 40119f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions PyInstaller/__main__.py
Expand Up @@ -197,6 +197,15 @@ def run(pyi_args: list | None = None, pyi_config: dict | None = None):
)
spec_file = args.filenames[0]
else:
# Ensure that the given script files exist, before trying to generate the .spec file.
# This prevents us from overwriting an existing (and customized) .spec file if user makes a typo in the
# .spec file's suffix when trying to build it, for example, `pyinstaller program.cpes` (see #8276).
# It also prevents creation of a .spec file when `pyinstaller program.py` is accidentally ran from a
# directory that does not contain the script (for example, due to failing to change the directory prior
# to running the command).
for filename in args.filenames:
if not os.path.isfile(filename):
raise SystemExit(f"Script file {filename!r} does not exist.")
spec_file = run_makespec(**vars(args))

sys.argv = [spec_file, *spec_args]
Expand Down
5 changes: 5 additions & 0 deletions news/8279.bugfix.rst
@@ -0,0 +1,5 @@
When trying to run ``pyinstaller`` (or equivalent ``python -m PyInstaller``)
against non-existing script file(s), exit immediately - without trying
to write the .spec file and building it. This prevents us from overwriting
an existing (and customized) .spec file if user makes a typo in the .spec
file's suffix when trying to build it, for example, ``pyinstaller program.cpes``.

0 comments on commit 40119f9

Please sign in to comment.