summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Mistewicz <a.mistewicz@samsung.com>2016-07-12 16:26:19 +0200
committerAleksander Mistewicz <a.mistewicz@samsung.com>2016-09-26 14:17:44 +0200
commitc52c8807c2eead1c1acf6b3a49505b7e2dadb1af (patch)
tree3564788f2a64397aff06be2ec5a5e42574c9f106
parent9d6c68a9a92ada927e8b754f8a63f494a548ea5f (diff)
downloadmajor-c52c8807c2eead1c1acf6b3a49505b7e2dadb1af.tar.gz
major-c52c8807c2eead1c1acf6b3a49505b7e2dadb1af.tar.bz2
major-c52c8807c2eead1c1acf6b3a49505b7e2dadb1af.zip
Add tct/resource_locking.py
Change-Id: I8420edfdc606544bbc41bee93cd314d39e8048b1 Signed-off-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
-rwxr-xr-xtct/resource_locking.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/tct/resource_locking.py b/tct/resource_locking.py
new file mode 100755
index 0000000..5213aa7
--- /dev/null
+++ b/tct/resource_locking.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+##
+# @author Aleksander Mistewicz <a.mistewicz@samsung.com>
+
+import os
+import subprocess
+import argparse
+import logging
+
+__version__ = "0.0.1"
+__license__ = "APACHE-2.0"
+__author__ = "Aleksander Mistewicz"
+__author_email__ = "a.mistewicz@samsung.com"
+
+USAGE = "%prog <opts>"
+
+UUID_DIR = "/var/tmp/"
+UUID_PREFIX="uuid-"
+LOCK_SUFFIX=".lock"
+
+class Lockfile(object):
+ @classmethod
+ def lock(self, filename):
+ logging.debug("Lock: %s", filename)
+ return subprocess.call(["lockfile-create", "--retry", "0", "--use-pid", filename])
+
+ @classmethod
+ def unlock(self, filename):
+ logging.debug("Unlock: %s", filename)
+ return subprocess.call(["lockfile-remove", filename])
+
+ @classmethod
+ def check(self, filename):
+ logging.debug("Check: %s", filename)
+ return subprocess.call(["lockfile-check", "--use-pid", filename])
+
+class UUIDlist(object):
+ @classmethod
+ def all(self):
+ return os.listdir(UUID_DIR)
+
+ @classmethod
+ def uuid(self):
+ ret = set()
+ for f in self.all():
+ if f.startswith(UUID_PREFIX) and not f.endswith(LOCK_SUFFIX):
+ ret.add(f)
+ return ret
+
+ @classmethod
+ def target(self, target):
+ ret = set()
+ for f in self.uuid():
+ if f.startswith(UUID_PREFIX + target):
+ ret.add(f)
+ return ret
+
+ @classmethod
+ def not_locked(self, target_list):
+ ret = set()
+ for f in target_list:
+ if Lockfile.check(UUID_DIR + f):
+ ret.add(f)
+ return ret
+
+def parse_arguments():
+ parser = argparse.ArgumentParser(description="Manager of locks on UUID files")
+
+ parser.add_argument("-L", "--log",
+ action="store", dest="loglevel",
+ help="Verbosity level")
+
+ args = parser.parse_args()
+
+ return args
+
+def main():
+ args = parse_arguments()
+ if args.loglevel:
+ numeric_level = getattr(logging, args.loglevel.upper(), None)
+ if not isinstance(numeric_level, int):
+ raise ValueError('Invalid log level: %s' % args.loglevel)
+ logging.basicConfig(format='%(asctime)s %(message)s',level=numeric_level)
+ logging.debug("Begin")
+ logging.debug("End")
+
+if __name__ == '__main__':
+ main()