artiq_devtool: actually hold the lock until script termination.

The open channel that ran flock was garbage collected, which caused
the flock command to terminate.
This commit is contained in:
whitequark 2017-11-26 20:54:24 +00:00
parent b5d098c6db
commit e5995794be
1 changed files with 14 additions and 10 deletions

View File

@ -18,8 +18,7 @@ from artiq.tools import verbosity_args, init_logger, logger, SSHClient
def get_argparser(): def get_argparser():
parser = argparse.ArgumentParser(description="ARTIQ core device " parser = argparse.ArgumentParser(description="ARTIQ core device development tool")
"development tool")
verbosity_args(parser) verbosity_args(parser)
@ -34,7 +33,9 @@ def get_argparser():
help="TTY device corresponding to the development board") help="TTY device corresponding to the development board")
parser.add_argument("-l", "--lockfile", metavar="PATH", parser.add_argument("-l", "--lockfile", metavar="PATH",
type=str, default="/run/boards/kc705", type=str, default="/run/boards/kc705",
help="The lockfile to be acquired for the duration of the script") help="The lockfile to be acquired for the duration of the actions")
parser.add_argument("-w", "--wait", action="store_true",
help="Wait for the board to unlock instead of aborting the actions")
parser.add_argument("-t", "--target", metavar="TARGET", parser.add_argument("-t", "--target", metavar="TARGET",
type=str, default="kc705_dds", type=str, default="kc705_dds",
help="Target to build, one of: " help="Target to build, one of: "
@ -71,23 +72,26 @@ def main():
"firmware": firmware, "firmware": firmware,
} }
lock_acquired = False flock_acquired = False
flock_file = None # GC root
def lock(): def lock():
nonlocal lock_acquired nonlocal flock_acquired
nonlocal flock_file
if not lock_acquired: if not flock_acquired:
logger.info("Acquiring device lock") logger.info("Acquiring device lock")
flock = client.spawn_command("flock --verbose --nonblock {} /bin/sleep 86400" flock = client.spawn_command("flock --verbose {block} {lockfile} sleep 86400"
.format(args.lockfile), .format(block="" if args.wait else "--nonblock",
lockfile=args.lockfile),
get_pty=True) get_pty=True)
flock_file = flock.makefile('r') flock_file = flock.makefile('r')
while not lock_acquired: while not flock_acquired:
line = flock_file.readline() line = flock_file.readline()
if not line: if not line:
break break
logger.debug(line.rstrip()) logger.debug(line.rstrip())
if line.startswith("flock: executing"): if line.startswith("flock: executing"):
lock_acquired = True flock_acquired = True
elif line.startswith("flock: failed"): elif line.startswith("flock: failed"):
logger.error("Failed to get lock") logger.error("Failed to get lock")
sys.exit(1) sys.exit(1)