Skip to content

Commit

Permalink
Don't abuse os.chdir so much (not thread safe)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo J. A. M. Carneiro committed Jan 18, 2009
1 parent ae5749a commit 8e70ea9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 62 deletions.
40 changes: 13 additions & 27 deletions regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,53 +64,39 @@ def run_test(self, verbose, generate, testName, arguments=[], pyscript=None, ref
os.mkdir(refTestDirName)

if pyscript is None:
Options.options.cwd_launch = refTestDirName
tmpl = "%s"
for arg in arguments:
tmpl = tmpl + " " + arg
wutils.run_program(testName, tmpl)
wutils.run_program(testName, tmpl, cwd=refTestDirName)
else:
argv = [self.env['PYTHON'], os.path.join(Options.cwd_launch, *os.path.split(pyscript))] + arguments
before = os.getcwd()
os.chdir(refTestDirName)
try:
wutils.run_argv(argv)
finally:
os.chdir(before)
wutils.run_argv(argv, cwd=refTestDirName)
print "Remember to commit " + refTestDirName
return 0
else:
if not os.path.exists(refTestDirName):
print "Cannot locate reference traces in " + refTestDirName
return 1


if refTestName is None:
traceDirName = testName + ".ref"
else:
traceDirName = refTestName
traceDirName = os.path.join ('traces', traceDirName)
traceDirName = os.path.join('regression', 'traces', traceDirName)

try:
shutil.rmtree(traceDirName)
except OSError:
pass
os.mkdir(traceDirName)

#os.system("./waf --cwd regression/traces --run " +
# testName + " > /dev/null 2>&1")

if pyscript is None:
Options.options.cwd_launch = traceDirName
wutils.run_program(testName, command_template=wutils.get_command_template(*arguments))
wutils.run_program(testName,
command_template=wutils.get_command_template(*arguments),
cwd=traceDirName)
else:
argv = [self.env['PYTHON'], os.path.join('..', '..', '..', *os.path.split(pyscript))] + arguments
before = os.getcwd()
os.chdir(traceDirName)
try:
wutils.run_argv(argv)
finally:
os.chdir(before)
wutils.run_argv(argv, cwd=traceDirName)

if verbose:
#diffCmd = "diff traces " + refTestDirName + " | head"
Expand Down Expand Up @@ -157,7 +143,7 @@ def run_regression(reference_traces):
"""

testdir = "tests"
testdir = os.path.join("regression", "tests")
if not os.path.exists(testdir):
print "Tests directory does not exist"
sys.exit(3)
Expand Down Expand Up @@ -193,7 +179,7 @@ def run_regression(reference_traces):
except NotImplementedError:
print "SKIP " + test

return len(bad) > 0
return (len(bad) > 0)


def _run_regression_test(test):
Expand All @@ -202,15 +188,15 @@ def _run_regression_test(test):
Arguments:
test -- the name of the test
"""

if os.path.exists("traces"):
files = os.listdir("traces")
traces_dir = os.path.join("regression", "traces")
if os.path.exists(traces_dir):
files = os.listdir(traces_dir)
for file in files:
if file == '.' or file == '..':
continue
shutil.rmtree(os.path.join("traces", file), ignore_errors=True)
else:
os.mkdir("traces")
os.mkdir(traces_dir)

mod = __import__(test, globals(), locals(), [])
return mod.run(verbose=(Options.options.verbose > 0),
Expand Down
11 changes: 2 additions & 9 deletions wscript
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ def configure(conf):
variant_name = Options.options.build_profile

if Options.options.regression_traces is not None:
variant_env['REGRESSION_TRACES'] = os.path.join("..", Options.options.regression_traces)
else:
variant_env['REGRESSION_TRACES'] = None
variant_env['REGRESSION_TRACES'] = os.path.abspath(Options.options.regression_traces)

if Options.options.enable_gcov:
variant_name += '-gcov'
Expand Down Expand Up @@ -441,16 +439,11 @@ def shutdown():
if not env['DIFF']:
raise Utils.WafError("Cannot run regression tests: the 'diff' program is not installed.")

_dir = os.getcwd()
os.chdir("regression")
regression_traces = env['REGRESSION_TRACES']
if not regression_traces:
raise Utils.WafError("Cannot run regression tests: reference traces directory not given"
" (--with-regression-traces configure option)")
try:
retval = regression.run_regression(regression_traces)
finally:
os.chdir(_dir)
retval = regression.run_regression(regression_traces)
if retval:
sys.exit(retval)

Expand Down
41 changes: 15 additions & 26 deletions wutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ def get_proc_env(os_env=None):

return proc_env

def run_argv(argv, os_env=None):
def run_argv(argv, os_env=None, cwd=None):
proc_env = get_proc_env(os_env)
#env = Build.bld.env
retval = subprocess.Popen(argv, env=proc_env).wait()
retval = subprocess.Popen(argv, env=proc_env, cwd=cwd).wait()
if retval:
raise Utils.WafError("Command %s exited with code %i" % (argv, retval))
return retval
Expand Down Expand Up @@ -167,42 +167,31 @@ def get_run_program(program_string, command_template=None):
execvec = shlex.split(command_template % (program_node.abspath(env),))
return program_name, execvec

def run_program(program_string, command_template=None):
def run_program(program_string, command_template=None, cwd=None):
"""
if command_template is not None, then program_string == program
name and argv is given by command_template with %s replaced by the
full path to the program. Else, program_string is interpreted as
a shell command with first name being the program name.
"""
dummy_program_name, execvec = get_run_program(program_string, command_template)
former_cwd = os.getcwd()
if (Options.options.cwd_launch):
os.chdir(Options.options.cwd_launch)
else:
os.chdir(Options.cwd_launch)
try:
retval = run_argv(execvec)
finally:
os.chdir(former_cwd)

return retval
if cwd is None:
if (Options.options.cwd_launch):
cwd = Options.options.cwd_launch
else:
cwd = Options.cwd_launch
return run_argv(execvec, cwd=cwd)



def run_python_program(program_string):
env = Build.bld.env
execvec = shlex.split(program_string)

former_cwd = os.getcwd()
if (Options.options.cwd_launch):
os.chdir(Options.options.cwd_launch)
else:
os.chdir(Options.cwd_launch)
try:
retval = run_argv([env['PYTHON']] + execvec)
finally:
os.chdir(former_cwd)

return retval
if cwd is None:
if (Options.options.cwd_launch):
cwd = Options.options.cwd_launch
else:
cwd = Options.cwd_launch
return run_argv([env['PYTHON']] + execvec, cwd=cwd)


0 comments on commit 8e70ea9

Please sign in to comment.