From f47d1c8704c12d92a450ed8d93056f793f9a8160 Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Mon, 26 Sep 2016 14:36:31 +0200 Subject: Add "--lock" to tct/resource_locking.py "target" argument is now mandatory. Change-Id: I6dbcd586da1320ee1ee20f131e8290c4e06fdb13 Signed-off-by: Aleksander Mistewicz --- tct/resource_locking.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tct') diff --git a/tct/resource_locking.py b/tct/resource_locking.py index 5213aa7..71533f0 100755 --- a/tct/resource_locking.py +++ b/tct/resource_locking.py @@ -79,9 +79,33 @@ class UUIDlist(object): ret.add(f) return ret +class UUIDmanager: + def __init__(self, target): + self.target = target + + def lock(self): + # go through the list of not locked uuid files and stop on the first successfully locked + # returns False if none of the uuid files was locked + # otherwise it returns True and prints filename on stdout + for uuid in UUIDlist.not_locked(UUIDlist.target(self.target)): + if Lockfile.lock(UUID_DIR + uuid) == 0: + logging.info("Locked: %s", uuid) + with open(UUID_DIR + uuid + LOCK_SUFFIX, 'w') as f: + f.write(str(os.getppid())) + print uuid[len(UUID_PREFIX):] + return True + return False + def parse_arguments(): parser = argparse.ArgumentParser(description="Manager of locks on UUID files") + parser.add_argument("target", + help="Choose type of a target device to lock/unlock") + + parser.add_argument("-l", "--lock", + action="store_true", default=False, dest="lock", + help="Lock a device") + parser.add_argument("-L", "--log", action="store", dest="loglevel", help="Verbosity level") @@ -98,6 +122,10 @@ def main(): raise ValueError('Invalid log level: %s' % args.loglevel) logging.basicConfig(format='%(asctime)s %(message)s',level=numeric_level) logging.debug("Begin") + uuid_man = UUIDmanager(args.target) + if args.lock: + if not uuid_man.lock(): + logging.warn("File locking unsuccessful!") logging.debug("End") if __name__ == '__main__': -- cgit v1.2.3