Created
August 6, 2015 05:13
-
-
Save smuthali/8e09bd5f4e8fe18332cb to your computer and use it in GitHub Desktop.
test script to validate lslock implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import sys | |
import os.path | |
import fcntl | |
import argparse | |
import logging | |
# Set logging options | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def setLock(dir_path, lock_file_name): | |
""" | |
:param dir_path: | |
:param lock_file_name: | |
:return: | |
""" | |
assert isinstance(lock_file_name, object) | |
rawpath = "%s/%s" %(dir_path, lock_file_name) | |
construct_path = os.path.normpath(rawpath) | |
locked_file = open(construct_path, "w+") | |
fcntl.lockf(locked_file, fcntl.LOCK_EX) | |
return locked_file | |
def main(): | |
""" | |
:return: list | |
""" | |
parser = argparse.ArgumentParser(prog='./lslock-test.py', usage='%(prog)s [options]') | |
parser.add_argument('--dir_path', '-d', required=True, type=str, nargs=1, help="absolute directory path;" | |
" example: /home/iamnuts") | |
parser.add_argument('--flock', '-f', required=True, type=str, nargs=1, help="lock file name;" | |
" example: foo.LOCK or foo.lck or" | |
" foo.lock") | |
total = len(sys.argv) | |
cmdargs = str(sys.argv) | |
logger.debug("total args:%s" % total) | |
logger.debug("total cmd args:%s" % cmdargs) | |
if len(sys.argv) != 5: | |
parser.print_help() | |
sys.exit(1) | |
args = vars(parser.parse_args()) | |
logger.debug("args=%s" % args) | |
# pop the args | |
global path, fname | |
path = args.pop('dir_path') | |
fname = args.pop('flock') | |
directory_path = str(path).strip('[]').strip("''") | |
lock_name = str(fname).strip('[]').strip("''") | |
logger.debug("Directory path: %s" % directory_path) | |
logger.debug("file name: %s" % lock_name) | |
# Run your business logic here | |
set_file_lock = setLock(directory_path, lock_name) | |
assert isinstance(set_file_lock, object) | |
if not set_file_lock: | |
logger.error("Failed to create the lock file") | |
logger.debug("Should not be hitting this condition") | |
sys.exit (-1) | |
logger.info("lock file name: %s" % lock_name) | |
count = 0 | |
while count < 60: | |
logger.info("I believe Songs Of Faith And Devotion (SOFAD) is the best album ever by Depeche Mode") | |
time.sleep(60) | |
count += 1 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment