2014-12-03 18:20:30 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
from inspect import isclass
|
|
|
|
from operator import itemgetter
|
|
|
|
|
2014-12-08 16:11:31 +08:00
|
|
|
from artiq.management.file_import import file_import
|
2015-01-12 18:51:23 +08:00
|
|
|
from artiq.language.db import *
|
2014-12-03 18:20:30 +08:00
|
|
|
from artiq.management import pyon
|
2015-01-12 18:51:23 +08:00
|
|
|
from artiq.management.db import *
|
2014-12-03 18:20:30 +08:00
|
|
|
|
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
class ELFRunner(AutoDB):
|
|
|
|
class DBKeys:
|
|
|
|
comm = Device()
|
|
|
|
implicit_core = False
|
2014-12-03 18:20:30 +08:00
|
|
|
|
2015-01-07 19:21:17 +08:00
|
|
|
def run(self, filename):
|
2014-12-03 18:20:30 +08:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
binary = f.read()
|
|
|
|
comm.load(binary)
|
2015-01-07 19:21:17 +08:00
|
|
|
comm.run("run")
|
2014-12-03 18:20:30 +08:00
|
|
|
comm.serve(dict(), dict())
|
|
|
|
|
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
class SimpleParamLogger:
|
|
|
|
def set(self, timestamp, name, value):
|
|
|
|
print("Parameter change: {} -> {}".format(name, value))
|
|
|
|
|
|
|
|
|
2014-12-03 18:20:30 +08:00
|
|
|
def _get_args():
|
2015-01-12 18:51:23 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Local experiment running tool")
|
2014-12-03 18:20:30 +08:00
|
|
|
|
|
|
|
parser.add_argument("-d", "--ddb", default="ddb.pyon",
|
|
|
|
help="device database file")
|
|
|
|
parser.add_argument("-p", "--pdb", default="pdb.pyon",
|
|
|
|
help="parameter database file")
|
|
|
|
|
|
|
|
parser.add_argument("-e", "--elf", default=False, action="store_true",
|
|
|
|
help="run ELF binary")
|
|
|
|
parser.add_argument("-u", "--unit", default=None,
|
|
|
|
help="unit to run")
|
2014-12-08 16:11:31 +08:00
|
|
|
parser.add_argument("file",
|
|
|
|
help="file containing the unit to run")
|
2015-01-07 19:21:17 +08:00
|
|
|
parser.add_argument("arguments", nargs="*",
|
|
|
|
help="run arguments")
|
2014-12-03 18:20:30 +08:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2015-01-07 19:21:17 +08:00
|
|
|
def _parse_arguments(arguments):
|
|
|
|
d = {}
|
|
|
|
for argument in arguments:
|
|
|
|
name, value = argument.split("=")
|
|
|
|
d[name] = pyon.decode(value)
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2014-12-03 18:20:30 +08:00
|
|
|
def main():
|
2015-01-07 19:21:17 +08:00
|
|
|
args = _get_args()
|
2014-12-03 18:20:30 +08:00
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
ddb = FlatFileDB(args.ddb)
|
|
|
|
pdb = FlatFileDB(args.pdb)
|
|
|
|
pdb.hooks.append(SimpleParamLogger())
|
|
|
|
rdb = ResultDB()
|
|
|
|
dbh = DBHub(ddb, pdb, rdb)
|
2014-12-03 18:20:30 +08:00
|
|
|
try:
|
|
|
|
if args.elf:
|
2015-01-07 19:21:17 +08:00
|
|
|
if args.arguments:
|
|
|
|
print("Run arguments are not supported in ELF mode")
|
|
|
|
sys.exit(1)
|
|
|
|
unit_inst = ELFRunner(dps)
|
|
|
|
unit_inst.run(args.file)
|
2014-12-03 18:20:30 +08:00
|
|
|
else:
|
2014-12-08 16:11:31 +08:00
|
|
|
module = file_import(args.file)
|
2014-12-03 18:20:30 +08:00
|
|
|
if args.unit is None:
|
|
|
|
units = [(k, v) for k, v in module.__dict__.items()
|
2014-12-08 19:22:02 +08:00
|
|
|
if k[0] != "_"
|
|
|
|
and isclass(v)
|
2015-01-12 18:51:23 +08:00
|
|
|
and issubclass(v, AutoDB)
|
|
|
|
and v is not AutoDB]
|
2014-12-03 18:20:30 +08:00
|
|
|
l = len(units)
|
|
|
|
if l == 0:
|
|
|
|
print("No units found in module")
|
|
|
|
sys.exit(1)
|
|
|
|
elif l > 1:
|
|
|
|
print("More than one unit found in module:")
|
|
|
|
for k, v in sorted(units, key=itemgetter(0)):
|
|
|
|
print(" " + k)
|
|
|
|
print("Use -u to specify which unit to use.")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
unit = units[0][1]
|
|
|
|
else:
|
|
|
|
unit = getattr(module, args.unit)
|
2015-01-07 19:21:17 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
arguments = _parse_arguments(args.arguments)
|
|
|
|
except:
|
|
|
|
print("Failed to parse run arguments")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
unit_inst = unit(dbh, **arguments)
|
2015-01-10 15:41:35 +08:00
|
|
|
unit_inst.run()
|
2015-01-07 19:21:17 +08:00
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
if rdb.data:
|
|
|
|
print("Results:")
|
|
|
|
for k, v in rdb.data.items():
|
|
|
|
print("{}: {}".format(k, v))
|
2014-12-03 18:20:30 +08:00
|
|
|
finally:
|
2015-01-12 18:51:23 +08:00
|
|
|
dbh.close()
|
2014-12-03 18:20:30 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|