Skip to content

Commit

Permalink
add optional process_creation_flags argument to _install_update_win() (
Browse files Browse the repository at this point in the history
…#90)

* add optional process_creation_flags arg to _install_update_win()

* add rudimentary test for process_creation_flags arg
  • Loading branch information
dennisvang authored Nov 8, 2023
1 parent 340d7e5 commit 35462d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/tufup/utils/platform_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def _install_update_win(
batch_template_extra_kwargs: Optional[dict] = None,
log_file_name: Optional[str] = None,
robocopy_options_override: Optional[List[str]] = None,
process_creation_flags = None,
):
"""
Create a batch script that moves files from src to dst, then run the
Expand Down Expand Up @@ -159,7 +160,14 @@ def _install_update_win(
to be overridden completely. It accepts a list of option strings. This
will cause the purge arguments to be ignored as well.
The `process_creation_flags` option allows users to override creation flags for
the subprocess call that runs the batch script. For example, one could specify
`subprocess.CREATE_NO_WINDOW` to prevent a window from opening. See [2] and [3]
for details.
[1]: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
[2]: https://docs.python.org/3/library/subprocess.html#windows-constants
[3]: https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
"""
if batch_template_extra_kwargs is None:
batch_template_extra_kwargs = dict()
Expand Down Expand Up @@ -206,8 +214,14 @@ def _install_update_win(
if as_admin:
run_bat_as_admin(file_path=script_path)
else:
# by default we create a new console with window, but user can override this
# using the process_creation_flags argument
if process_creation_flags is None:
process_creation_flags = subprocess.CREATE_NEW_CONSOLE
else:
logger.debug('using custom process creation flags')
# we use Popen() instead of run(), because the latter blocks execution
subprocess.Popen([script_path], creationflags=subprocess.CREATE_NEW_CONSOLE)
subprocess.Popen([script_path], creationflags=process_creation_flags)
logger.debug('exiting')
# exit current process
sys.exit(0)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_utils_platform_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

DUMMY_APP_CONTENT = f"""
import subprocess
import sys
sys.path.append('{(BASE_DIR.parent / 'src').as_posix()}')
from tufup.utils.platform_specific import install_update
Expand Down Expand Up @@ -176,6 +177,22 @@ def test_install_update_log_file(self):
log_file_content = log_file_path.read_text()
self.assertTrue(log_file_content)

@unittest.skipIf(
condition=not ON_WINDOWS, reason='process_creation_flags is for windows only'
)
def test_install_update_process_creation_flags(self):
# the log file is only used to verify that the batch file has run successfully
log_file_name = 'install.log'
extra_kwargs_strings = [
'process_creation_flags=subprocess.CREATE_NO_WINDOW',
f'log_file_name="{log_file_name}"',
]
# run the dummy app in a separate process
self.run_dummy_app(extra_kwargs_strings=extra_kwargs_strings)
# a log file should exist
log_file_path = self.dst_dir / log_file_name
self.assertTrue(log_file_path.read_text())

@unittest.skipIf(
condition=not ON_WINDOWS, reason='windows batch files are windows only'
)
Expand Down

0 comments on commit 35462d9

Please sign in to comment.