2017-01-30 09:24:43 +08:00
|
|
|
#!/usr/bin/env python3
|
2019-01-10 12:58:11 +08:00
|
|
|
"""
|
|
|
|
Client to send commands to :mod:`artiq_master` and display results locally.
|
|
|
|
|
|
|
|
The client can perform actions such as accessing/setting datasets,
|
|
|
|
scanning devices, scheduling experiments, and looking for experiments/devices.
|
|
|
|
"""
|
2014-10-05 16:25:31 +08:00
|
|
|
|
|
|
|
import argparse
|
2015-10-20 18:11:50 +08:00
|
|
|
import logging
|
2014-12-10 19:11:13 +08:00
|
|
|
import time
|
2014-12-28 18:56:26 +08:00
|
|
|
import asyncio
|
2015-01-02 14:46:58 +08:00
|
|
|
import sys
|
2016-02-22 01:25:48 +08:00
|
|
|
import os
|
2015-01-02 14:46:58 +08:00
|
|
|
from operator import itemgetter
|
2015-01-23 00:23:00 +08:00
|
|
|
from dateutil.parser import parse as parse_date
|
2014-10-23 18:48:03 +08:00
|
|
|
|
2014-12-10 13:04:18 +08:00
|
|
|
from prettytable import PrettyTable
|
|
|
|
|
2015-01-17 19:38:20 +08:00
|
|
|
from artiq.protocols.pc_rpc import Client
|
|
|
|
from artiq.protocols.sync_struct import Subscriber
|
2016-09-05 15:37:35 +08:00
|
|
|
from artiq.protocols.broadcast import Receiver
|
2015-01-17 19:38:20 +08:00
|
|
|
from artiq.protocols import pyon
|
2019-01-12 09:47:47 +08:00
|
|
|
from artiq.tools import short_format, add_common_args, parse_arguments
|
2015-01-17 19:38:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
def clear_screen():
|
2016-02-22 01:25:48 +08:00
|
|
|
if os.name == "nt":
|
|
|
|
os.system("cls")
|
|
|
|
else:
|
|
|
|
sys.stdout.write("\x1b[2J\x1b[H")
|
2014-10-05 16:25:31 +08:00
|
|
|
|
|
|
|
|
2015-01-23 00:52:13 +08:00
|
|
|
def get_argparser():
|
2014-12-29 12:48:14 +08:00
|
|
|
parser = argparse.ArgumentParser(description="ARTIQ CLI client")
|
2014-10-23 19:07:36 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"-s", "--server", default="::1",
|
|
|
|
help="hostname or IP of the master to connect to")
|
|
|
|
parser.add_argument(
|
2014-12-28 18:56:26 +08:00
|
|
|
"--port", default=None, type=int,
|
2014-10-23 19:07:36 +08:00
|
|
|
help="TCP port to use to connect to the master")
|
2014-12-08 19:22:02 +08:00
|
|
|
|
|
|
|
subparsers = parser.add_subparsers(dest="action")
|
2014-12-10 13:04:18 +08:00
|
|
|
subparsers.required = True
|
2014-12-08 19:22:02 +08:00
|
|
|
|
2014-12-10 13:04:18 +08:00
|
|
|
parser_add = subparsers.add_parser("submit", help="submit an experiment")
|
2015-05-17 16:11:00 +08:00
|
|
|
parser_add.add_argument("-p", "--pipeline", default="main", type=str,
|
|
|
|
help="pipeline to run the experiment in "
|
|
|
|
"(default: %(default)s)")
|
2015-05-24 01:09:22 +08:00
|
|
|
parser_add.add_argument("-P", "--priority", default=0, type=int,
|
|
|
|
help="priority (higher value means sooner "
|
|
|
|
"scheduling, default: %(default)s)")
|
2015-05-28 17:20:58 +08:00
|
|
|
parser_add.add_argument("-t", "--timed", default=None, type=str,
|
|
|
|
help="set a due date for the experiment")
|
2019-01-10 12:58:11 +08:00
|
|
|
parser_add.add_argument("-f", "--flush", default=False,
|
|
|
|
action="store_true",
|
2015-05-28 17:20:58 +08:00
|
|
|
help="flush the pipeline before preparing "
|
|
|
|
"the experiment")
|
2015-08-07 15:51:56 +08:00
|
|
|
parser_add.add_argument("-R", "--repository", default=False,
|
|
|
|
action="store_true",
|
|
|
|
help="use the experiment repository")
|
|
|
|
parser_add.add_argument("-r", "--revision", default=None,
|
|
|
|
help="use a specific repository revision "
|
|
|
|
"(defaults to head, ignored without -R)")
|
2015-07-15 17:08:12 +08:00
|
|
|
parser_add.add_argument("-c", "--class-name", default=None,
|
|
|
|
help="name of the class to run")
|
2019-01-12 09:47:47 +08:00
|
|
|
add_common_args(parser)
|
2016-04-22 16:33:44 +08:00
|
|
|
parser_add.add_argument("file", metavar="FILE",
|
2015-03-08 22:43:04 +08:00
|
|
|
help="file containing the experiment to run")
|
2016-04-22 16:33:44 +08:00
|
|
|
parser_add.add_argument("arguments", metavar="ARGUMENTS", nargs="*",
|
2015-01-07 21:37:07 +08:00
|
|
|
help="run arguments")
|
2014-12-08 19:22:02 +08:00
|
|
|
|
2015-05-17 16:11:00 +08:00
|
|
|
parser_delete = subparsers.add_parser("delete",
|
|
|
|
help="delete an experiment "
|
|
|
|
"from the schedule")
|
2015-10-06 13:50:00 +08:00
|
|
|
parser_delete.add_argument("-g", action="store_true",
|
|
|
|
help="request graceful termination")
|
2016-04-22 16:33:44 +08:00
|
|
|
parser_delete.add_argument("rid", metavar="RID", type=int,
|
2015-05-17 16:11:00 +08:00
|
|
|
help="run identifier (RID)")
|
2014-12-10 13:04:18 +08:00
|
|
|
|
2015-10-12 17:18:23 +08:00
|
|
|
parser_set_dataset = subparsers.add_parser(
|
|
|
|
"set-dataset", help="add or modify a dataset")
|
2016-04-22 16:33:44 +08:00
|
|
|
parser_set_dataset.add_argument("name", metavar="NAME",
|
|
|
|
help="name of the dataset")
|
|
|
|
parser_set_dataset.add_argument("value", metavar="VALUE",
|
2015-10-12 17:18:23 +08:00
|
|
|
help="value in PYON format")
|
2019-01-10 12:58:11 +08:00
|
|
|
|
|
|
|
persist_group = parser_set_dataset.add_mutually_exclusive_group()
|
|
|
|
persist_group.add_argument("-p", "--persist", action="store_true",
|
|
|
|
help="make the dataset persistent")
|
|
|
|
persist_group.add_argument("-n", "--no-persist", action="store_true",
|
|
|
|
help="make the dataset non-persistent")
|
2015-10-12 17:18:23 +08:00
|
|
|
|
|
|
|
parser_del_dataset = subparsers.add_parser(
|
|
|
|
"del-dataset", help="delete a dataset")
|
|
|
|
parser_del_dataset.add_argument("name", help="name of the dataset")
|
2015-01-02 14:46:58 +08:00
|
|
|
|
|
|
|
parser_show = subparsers.add_parser(
|
2015-10-12 17:18:23 +08:00
|
|
|
"show", help="show schedule, log, devices or datasets")
|
2015-01-02 14:46:58 +08:00
|
|
|
parser_show.add_argument(
|
2016-04-22 16:33:44 +08:00
|
|
|
"what", metavar="WHAT",
|
2019-01-10 12:58:11 +08:00
|
|
|
choices=["schedule", "log", "ccb", "devices", "datasets"],
|
|
|
|
help="select object to show: %(choices)s")
|
2014-12-10 13:04:18 +08:00
|
|
|
|
2015-10-04 17:38:07 +08:00
|
|
|
subparsers.add_parser(
|
2015-10-12 17:18:23 +08:00
|
|
|
"scan-devices", help="trigger a device database (re)scan")
|
2015-10-04 17:38:07 +08:00
|
|
|
|
|
|
|
parser_scan_repos = subparsers.add_parser(
|
|
|
|
"scan-repository", help="trigger a repository (re)scan")
|
2016-04-04 22:17:39 +08:00
|
|
|
parser_scan_repos.add_argument("--async", action="store_true",
|
|
|
|
help="trigger scan and return immediately")
|
2016-04-22 16:33:44 +08:00
|
|
|
parser_scan_repos.add_argument("revision", metavar="REVISION",
|
|
|
|
default=None, nargs="?",
|
2015-10-04 17:38:07 +08:00
|
|
|
help="use a specific repository revision "
|
|
|
|
"(defaults to head)")
|
2015-07-18 00:55:48 +08:00
|
|
|
|
2015-12-08 19:24:04 +08:00
|
|
|
parser_ls = subparsers.add_parser(
|
|
|
|
"ls", help="list a directory on the master")
|
2015-12-09 19:13:57 +08:00
|
|
|
parser_ls.add_argument("directory", default="", nargs="?")
|
2015-12-08 19:24:04 +08:00
|
|
|
|
2015-01-23 00:52:13 +08:00
|
|
|
return parser
|
2014-10-05 16:25:31 +08:00
|
|
|
|
|
|
|
|
2014-12-10 13:04:18 +08:00
|
|
|
def _action_submit(remote, args):
|
2015-01-07 21:37:07 +08:00
|
|
|
try:
|
2019-01-10 12:58:11 +08:00
|
|
|
arguments = parse_arguments(args.arguments)
|
|
|
|
except Exception as err:
|
|
|
|
raise ValueError("Failed to parse run arguments") from err
|
2015-01-07 21:37:07 +08:00
|
|
|
|
2015-05-17 16:11:00 +08:00
|
|
|
expid = {
|
2015-10-20 18:11:50 +08:00
|
|
|
"log_level": logging.WARNING + args.quiet*10 - args.verbose*10,
|
2014-12-10 13:04:18 +08:00
|
|
|
"file": args.file,
|
2015-07-15 17:08:12 +08:00
|
|
|
"class_name": args.class_name,
|
2015-01-30 20:36:54 +08:00
|
|
|
"arguments": arguments,
|
2014-12-10 13:04:18 +08:00
|
|
|
}
|
2015-08-07 15:51:56 +08:00
|
|
|
if args.repository:
|
|
|
|
expid["repo_rev"] = args.revision
|
2015-01-23 00:23:00 +08:00
|
|
|
if args.timed is None:
|
2015-05-17 16:11:00 +08:00
|
|
|
due_date = None
|
2014-12-10 13:04:18 +08:00
|
|
|
else:
|
2015-05-17 16:11:00 +08:00
|
|
|
due_date = time.mktime(parse_date(args.timed).timetuple())
|
2015-05-28 17:20:58 +08:00
|
|
|
rid = remote.submit(args.pipeline, expid,
|
|
|
|
args.priority, due_date, args.flush)
|
2015-05-17 16:11:00 +08:00
|
|
|
print("RID: {}".format(rid))
|
2014-12-10 13:04:18 +08:00
|
|
|
|
|
|
|
|
2015-05-17 16:11:00 +08:00
|
|
|
def _action_delete(remote, args):
|
2015-10-06 13:50:00 +08:00
|
|
|
if args.g:
|
|
|
|
remote.request_termination(args.rid)
|
|
|
|
else:
|
|
|
|
remote.delete(args.rid)
|
2014-12-10 13:04:18 +08:00
|
|
|
|
|
|
|
|
2015-10-12 17:18:23 +08:00
|
|
|
def _action_set_dataset(remote, args):
|
2016-07-03 12:19:46 +08:00
|
|
|
persist = None
|
|
|
|
if args.persist:
|
|
|
|
persist = True
|
|
|
|
if args.no_persist:
|
|
|
|
persist = False
|
|
|
|
remote.set(args.name, pyon.decode(args.value), persist)
|
2015-01-02 14:46:58 +08:00
|
|
|
|
|
|
|
|
2015-10-12 17:18:23 +08:00
|
|
|
def _action_del_dataset(remote, args):
|
2015-01-12 18:51:23 +08:00
|
|
|
remote.delete(args.name)
|
2015-01-02 14:46:58 +08:00
|
|
|
|
|
|
|
|
2015-10-12 17:18:23 +08:00
|
|
|
def _action_scan_devices(remote, args):
|
2015-10-04 17:38:07 +08:00
|
|
|
remote.scan()
|
|
|
|
|
|
|
|
|
2015-07-18 00:55:48 +08:00
|
|
|
def _action_scan_repository(remote, args):
|
2016-04-04 22:17:39 +08:00
|
|
|
if args.async:
|
|
|
|
remote.scan_repository_async(args.revision)
|
|
|
|
else:
|
|
|
|
remote.scan_repository(args.revision)
|
2015-07-18 00:55:48 +08:00
|
|
|
|
|
|
|
|
2015-12-08 19:24:04 +08:00
|
|
|
def _action_ls(remote, args):
|
|
|
|
contents = remote.list_directory(args.directory)
|
2015-12-09 19:13:57 +08:00
|
|
|
for name in sorted(contents, key=lambda x: (x[-1] not in "\\/", x)):
|
|
|
|
print(name)
|
2015-12-08 19:24:04 +08:00
|
|
|
|
|
|
|
|
2015-05-17 16:11:00 +08:00
|
|
|
def _show_schedule(schedule):
|
2014-12-28 18:56:26 +08:00
|
|
|
clear_screen()
|
2015-05-17 16:11:00 +08:00
|
|
|
if schedule:
|
2019-01-10 12:58:11 +08:00
|
|
|
sorted_schedule = sorted(schedule.items(),
|
|
|
|
key=lambda x: (-x[1]["priority"],
|
|
|
|
x[1]["due_date"] or 0,
|
|
|
|
x[0]))
|
2015-05-24 01:09:22 +08:00
|
|
|
table = PrettyTable(["RID", "Pipeline", " Status ", "Prio",
|
2015-08-07 15:51:56 +08:00
|
|
|
"Due date", "Revision", "File", "Class name"])
|
2019-01-10 12:58:11 +08:00
|
|
|
for rid, v in sorted_schedule:
|
2015-05-24 01:09:22 +08:00
|
|
|
row = [rid, v["pipeline"], v["status"], v["priority"]]
|
2015-05-17 16:11:00 +08:00
|
|
|
if v["due_date"] is None:
|
2015-03-09 17:51:32 +08:00
|
|
|
row.append("")
|
|
|
|
else:
|
2015-05-17 16:11:00 +08:00
|
|
|
row.append(time.strftime("%m/%d %H:%M:%S",
|
|
|
|
time.localtime(v["due_date"])))
|
2015-08-07 15:51:56 +08:00
|
|
|
expid = v["expid"]
|
|
|
|
if "repo_rev" in expid:
|
|
|
|
row.append(expid["repo_rev"])
|
|
|
|
else:
|
|
|
|
row.append("Outside repo.")
|
|
|
|
row.append(expid["file"])
|
|
|
|
if expid["class_name"] is None:
|
2015-03-09 17:51:32 +08:00
|
|
|
row.append("")
|
|
|
|
else:
|
2015-08-07 15:51:56 +08:00
|
|
|
row.append(expid["class_name"])
|
2014-12-10 19:11:13 +08:00
|
|
|
table.add_row(row)
|
|
|
|
print(table)
|
|
|
|
else:
|
2015-05-17 16:11:00 +08:00
|
|
|
print("Schedule is empty")
|
2014-12-10 13:04:18 +08:00
|
|
|
|
|
|
|
|
2015-01-02 14:46:58 +08:00
|
|
|
def _show_devices(devices):
|
|
|
|
clear_screen()
|
|
|
|
table = PrettyTable(["Name", "Description"])
|
|
|
|
table.align["Description"] = "l"
|
|
|
|
for k, v in sorted(devices.items(), key=itemgetter(0)):
|
|
|
|
table.add_row([k, pyon.encode(v, True)])
|
|
|
|
print(table)
|
|
|
|
|
|
|
|
|
2015-10-12 17:18:23 +08:00
|
|
|
def _show_datasets(datasets):
|
2015-01-02 14:46:58 +08:00
|
|
|
clear_screen()
|
2015-10-12 17:18:23 +08:00
|
|
|
table = PrettyTable(["Dataset", "Persistent", "Value"])
|
|
|
|
for k, (persist, value) in sorted(datasets.items(), key=itemgetter(0)):
|
2015-10-12 18:10:58 +08:00
|
|
|
table.add_row([k, "Y" if persist else "N", short_format(value)])
|
2015-01-02 14:46:58 +08:00
|
|
|
print(table)
|
|
|
|
|
|
|
|
|
2014-12-28 18:56:26 +08:00
|
|
|
def _run_subscriber(host, port, subscriber):
|
|
|
|
loop = asyncio.get_event_loop()
|
2014-10-23 18:48:03 +08:00
|
|
|
try:
|
2014-12-28 18:56:26 +08:00
|
|
|
loop.run_until_complete(subscriber.connect(host, port))
|
|
|
|
try:
|
|
|
|
loop.run_until_complete(asyncio.wait_for(subscriber.receive_task,
|
|
|
|
None))
|
|
|
|
print("Connection to master lost")
|
|
|
|
finally:
|
|
|
|
loop.run_until_complete(subscriber.close())
|
2014-10-23 18:48:03 +08:00
|
|
|
finally:
|
2014-12-28 18:56:26 +08:00
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
2015-01-02 14:46:58 +08:00
|
|
|
def _show_dict(args, notifier_name, display_fun):
|
|
|
|
d = dict()
|
2019-01-10 12:58:11 +08:00
|
|
|
|
2015-01-02 14:46:58 +08:00
|
|
|
def init_d(x):
|
|
|
|
d.clear()
|
|
|
|
d.update(x)
|
|
|
|
return d
|
|
|
|
subscriber = Subscriber(notifier_name, init_d,
|
2015-01-14 22:21:59 +08:00
|
|
|
lambda mod: display_fun(d))
|
2016-09-05 15:37:35 +08:00
|
|
|
port = 3250 if args.port is None else args.port
|
|
|
|
_run_subscriber(args.server, port, subscriber)
|
2015-01-02 14:46:58 +08:00
|
|
|
|
|
|
|
|
2015-10-14 01:06:57 +08:00
|
|
|
def _print_log_record(record):
|
|
|
|
level, source, t, message = record
|
|
|
|
t = time.strftime("%m/%d %H:%M:%S", time.localtime(t))
|
|
|
|
print(level, source, t, message)
|
|
|
|
|
|
|
|
|
2015-08-08 16:25:55 +08:00
|
|
|
def _show_log(args):
|
2016-09-05 15:37:35 +08:00
|
|
|
subscriber = Receiver("log", [_print_log_record])
|
|
|
|
port = 1067 if args.port is None else args.port
|
|
|
|
_run_subscriber(args.server, port, subscriber)
|
2015-08-08 16:25:55 +08:00
|
|
|
|
|
|
|
|
2016-09-05 19:07:35 +08:00
|
|
|
def _show_ccb(args):
|
|
|
|
subscriber = Receiver("ccb", [
|
|
|
|
lambda d: print(d["service"],
|
|
|
|
"args:", d["args"],
|
|
|
|
"kwargs:", d["kwargs"])
|
|
|
|
])
|
|
|
|
port = 1067 if args.port is None else args.port
|
|
|
|
_run_subscriber(args.server, port, subscriber)
|
|
|
|
|
|
|
|
|
2014-12-28 18:56:26 +08:00
|
|
|
def main():
|
2015-01-23 00:52:13 +08:00
|
|
|
args = get_argparser().parse_args()
|
2015-01-02 14:46:58 +08:00
|
|
|
action = args.action.replace("-", "_")
|
|
|
|
if action == "show":
|
2015-05-17 16:11:00 +08:00
|
|
|
if args.what == "schedule":
|
|
|
|
_show_dict(args, "schedule", _show_schedule)
|
2015-08-08 16:25:55 +08:00
|
|
|
elif args.what == "log":
|
|
|
|
_show_log(args)
|
2016-09-05 19:07:35 +08:00
|
|
|
elif args.what == "ccb":
|
|
|
|
_show_ccb(args)
|
2015-01-02 14:46:58 +08:00
|
|
|
elif args.what == "devices":
|
|
|
|
_show_dict(args, "devices", _show_devices)
|
2015-10-12 17:18:23 +08:00
|
|
|
elif args.what == "datasets":
|
|
|
|
_show_dict(args, "datasets", _show_datasets)
|
2015-01-02 14:46:58 +08:00
|
|
|
else:
|
2019-01-10 12:58:11 +08:00
|
|
|
raise ValueError
|
2014-12-28 18:56:26 +08:00
|
|
|
else:
|
2015-01-13 03:55:50 +08:00
|
|
|
port = 3251 if args.port is None else args.port
|
2015-01-12 18:51:23 +08:00
|
|
|
target_name = {
|
|
|
|
"submit": "master_schedule",
|
2015-05-17 16:11:00 +08:00
|
|
|
"delete": "master_schedule",
|
2015-10-12 17:18:23 +08:00
|
|
|
"set_dataset": "master_dataset_db",
|
|
|
|
"del_dataset": "master_dataset_db",
|
|
|
|
"scan_devices": "master_device_db",
|
2015-12-08 19:24:04 +08:00
|
|
|
"scan_repository": "master_experiment_db",
|
|
|
|
"ls": "master_experiment_db"
|
2015-01-12 18:51:23 +08:00
|
|
|
}[action]
|
2015-01-02 14:46:58 +08:00
|
|
|
remote = Client(args.server, port, target_name)
|
2014-12-28 18:56:26 +08:00
|
|
|
try:
|
2015-01-02 14:46:58 +08:00
|
|
|
globals()["_action_" + action](remote, args)
|
2014-12-28 18:56:26 +08:00
|
|
|
finally:
|
|
|
|
remote.close_rpc()
|
2014-10-05 16:25:31 +08:00
|
|
|
|
2019-01-10 12:58:11 +08:00
|
|
|
|
2014-10-05 16:25:31 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|