Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions ats/cwd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Thread-safe helpers for process current-working-directory changes."""
from contextlib import contextmanager
import os
import threading


_cwd_lock = threading.RLock()


@contextmanager
def chdir(path):
"""Temporarily change process cwd while holding the global cwd lock.

Args:
path (str): Directory to make the process current-working-directory
while the context is active.

Yields:
None. The previous current-working-directory is restored when the
context exits.
"""
with _cwd_lock:
here = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(here)
29 changes: 13 additions & 16 deletions ats/machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess, sys, os, time, shlex
from ats.atsut import RUNNING, TIMEDOUT, PASSED, FAILED, LSFERROR, \
SKIPPED, HALTED, AtsError
from ats.cwd import chdir
from ats.log import log, terminal
from shutil import copytree, ignore_patterns

Expand Down Expand Up @@ -291,14 +292,12 @@ def testEnded(self, test, status):
verbose = configuration.options.debug

if not (globalPostrunScript == "unset"):
here = os.getcwd()
os.chdir(test.directory)
if os.path.exists(globalPostrunScript):
self._executePreOrPostRunScript(globalPostrunScript, test, verbose, globalPostrunScript_outname)
else:
log("ATS ERROR: globalPostrunScript %s not found" % (globalPostrunScript), echo=True)
sys.exit(-1)
os.chdir(here)
with chdir(test.directory):
if os.path.exists(globalPostrunScript):
self._executePreOrPostRunScript(globalPostrunScript, test, verbose, globalPostrunScript_outname)
else:
log("ATS ERROR: globalPostrunScript %s not found" % (globalPostrunScript), echo=True)
sys.exit(-1)

self.numberTestsRunning -= 1
if MachineCore.debugClass or MachineCore.canRunNow_debugClass:
Expand Down Expand Up @@ -557,14 +556,12 @@ def _launch(self, test):


if not (globalPrerunScript == "unset"):
here = os.getcwd()
os.chdir(test.directory)
if os.path.exists(globalPrerunScript):
self._executePreOrPostRunScript(globalPrerunScript, test, verbose, globalPrerunScript_outname)
else:
log("ATS ERROR: globalPrerunScript %s not found" % (globalPrerunScript), echo=True)
sys.exit(-1)
os.chdir(here)
with chdir(test.directory):
if os.path.exists(globalPrerunScript):
self._executePreOrPostRunScript(globalPrerunScript, test, verbose, globalPrerunScript_outname)
else:
log("ATS ERROR: globalPrerunScript %s not found" % (globalPrerunScript), echo=True)
sys.exit(-1)

try:
Eadd = test.options.get('env', None)
Expand Down
Loading
Loading