From: Aleksander Mistewicz Date: Tue, 12 Jul 2016 14:26:19 +0000 (+0200) Subject: Add tct/resource_locking.py X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F59%2F79759%2F4;p=tools%2Ftestlab%2Fmajor.git Add tct/resource_locking.py Change-Id: I8420edfdc606544bbc41bee93cd314d39e8048b1 Signed-off-by: Aleksander Mistewicz --- 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 + +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 " + +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()