Skip to content

Commit

Permalink
Merge pull request #541 from lindsay-stevens/pyxform-tmperror
Browse files Browse the repository at this point in the history
fix windows test failures related to temp file creation by ODK Validate
  • Loading branch information
lognaturel authored Jul 6, 2021
2 parents 71f4157 + 76a4539 commit cf7ba39
Showing 1 changed file with 17 additions and 3 deletions.
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

0 comments on commit cf7ba39

Please sign in to comment.