Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix windows test failures related to temp file creation by ODK Validate #541

Merged
merged 1 commit into from
Jul 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions pyxform/validators/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import signal
import subprocess
import tempfile
import threading
import time
from contextlib import closing
Expand Down Expand Up @@ -56,10 +57,11 @@ def _kill_process_after_a_timeout(pid):
# use SIGKILL if hard to kill...
return

# Workarounds for pyinstaller
# https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess
startup_info = None
env = None
if os.name == "nt":
# Workarounds for pyinstaller
# https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess
# disable command window when run from pyinstaller
startup_info = subprocess.STARTUPINFO()
# Less fancy version of bitwise-or-assignment (x |= y) shown in ref url.
Expand All @@ -68,7 +70,19 @@ def _kill_process_after_a_timeout(pid):
else:
startup_info.dwFlags = 0

p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, startupinfo=startup_info)
# Workaround for Java sometimes not being able to use the temp directory.
# https://docs.oracle.com/javase/8/docs/api/java/io/File.html
# CreateTempFile refers to "java.io.tmpdir" which refers to env vars.
env = {
k: v if v is not None else tempfile.gettempdir()
for k, v in {
k: os.environ.get(k) for k in ("TEMP", "TMP", "TMPDIR")
}.items()
}

p = Popen(
command, env=env, stdin=PIPE, stdout=PIPE, stderr=PIPE, startupinfo=startup_info
)
watchdog = threading.Timer(timeout, _kill_process_after_a_timeout, args=(p.pid,))
watchdog.start()
(stdout, stderr) = p.communicate()
Expand Down