Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Tobias Langer
/
experiment
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
607d2959
authored
8 years ago
by
Tobias Langer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added helper script to start the experiments.
parent
0512b30f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
0 deletions
+102
-0
runner.py
+102
-0
No files found.
runner.py
0 → 100755
View file @
607d2959
#!/usr/bin/env python3
import
argparse
import
shutil
from
subprocess
import
call
,
Popen
,
PIPE
import
sys
import
os
def
query_yes_no
(
question
,
default
=
None
):
"""
Queries the user for a decision.
"""
if
default
is
None
:
prompt
=
' [y/n]'
elif
default
.
lower
()
==
'yes'
:
prompt
=
' [Y/n]'
elif
default
.
lower
()
==
'no'
:
prompt
=
' [y/N]'
else
:
raise
ValueError
(
'Invalid default answer {}'
.
format
(
default
))
while
True
:
print
(
question
,
prompt
,
end
=
''
)
choice
=
input
()
.
lower
()
if
'yes'
.
find
(
choice
)
==
0
:
return
True
elif
'no'
.
find
(
choice
)
==
0
:
return
False
def
create_dir
(
filename
):
"""
Create the directory denoted by filename, if it doesn't exists. Ask for its
removal if it exists.
"""
try
:
os
.
makedirs
(
filename
)
except
FileExistsError
:
if
not
query_yes_no
(
'Folder exists, remove?'
,
default
=
'yes'
):
return
False
shutil
.
rmtree
(
filename
)
os
.
makedirs
(
filename
)
return
True
class
cd
:
"""Context manager for changing the current working directory"""
def
__init__
(
self
,
newPath
):
self
.
newPath
=
os
.
path
.
expanduser
(
newPath
)
def
__enter__
(
self
):
self
.
savedPath
=
os
.
getcwd
()
os
.
chdir
(
self
.
newPath
)
def
__exit__
(
self
,
etype
,
value
,
traceback
):
os
.
chdir
(
self
.
savedPath
)
def
main
():
parser
=
argparse
.
ArgumentParser
(
'Build and run experiments.'
)
parser
.
add_argument
(
'path'
,
type
=
str
,
default
=
'.'
,
nargs
=
'?'
,
help
=
'Base path of the experiments'
)
args
=
parser
.
parse_args
()
with
cd
(
args
.
path
):
result
=
call
([
'make'
])
if
result
is
not
0
and
result
is
not
2
:
print
(
'Failed building the experiments!'
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
if
not
create_dir
(
'results'
):
sys
.
exit
(
1
)
for
experiment
in
os
.
listdir
():
if
experiment
==
'results'
or
not
os
.
path
.
isdir
(
experiment
):
continue
with
open
(
os
.
path
.
join
(
'results'
,
experiment
+
'.out'
),
'w'
)
as
of
:
with
cd
(
experiment
):
try
:
p
=
Popen
([
'./'
+
experiment
],
stdout
=
of
)
p
.
wait
()
except
Exception
as
e
:
print
(
e
)
print
(
'Failed running experiment: {}'
,
experiment
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
# with cd(experiment):
# try:
# p = Popen(['./' + experiment], stdout=PIPE)
# out, err = p.communicate()
# except Exception as e:
# print(e)
# print('Failed running experiment: {}', experiment,
# file=sys.stderr)
# sys.exit(1)
# with open(os.path.join('results', experiment + '.out'), 'w') as of:
# of.write(str(out))
if
__name__
==
'__main__'
:
main
()
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment