diff options
author | Aleksander Mistewicz <a.mistewicz@samsung.com> | 2016-09-26 14:36:31 +0200 |
---|---|---|
committer | Aleksander Mistewicz <a.mistewicz@samsung.com> | 2016-10-07 16:49:35 +0200 |
commit | f47d1c8704c12d92a450ed8d93056f793f9a8160 (patch) | |
tree | 45ae3fbe91d27e4107edc25c1249a112d187733c /tct/resource_locking.py | |
parent | c52c8807c2eead1c1acf6b3a49505b7e2dadb1af (diff) | |
download | major-f47d1c8704c12d92a450ed8d93056f793f9a8160.tar.gz major-f47d1c8704c12d92a450ed8d93056f793f9a8160.tar.bz2 major-f47d1c8704c12d92a450ed8d93056f793f9a8160.zip |
Add "--lock" to tct/resource_locking.py
"target" argument is now mandatory.
Change-Id: I6dbcd586da1320ee1ee20f131e8290c4e06fdb13
Signed-off-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
Diffstat (limited to 'tct/resource_locking.py')
-rwxr-xr-x | tct/resource_locking.py | 28 |
1 files changed, 28 insertions, 0 deletions
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__': |