diff options
author | Caroline Tice <cmtice@google.com> | 2016-01-13 09:48:29 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-01-22 19:30:55 +0000 |
commit | 88272d479f2761cc1906fea564c73033f77a6270 (patch) | |
tree | 4da9c114d64522f59494a908cf9a85b09c994d32 /file_lock_machine.py | |
parent | 439f2b77c86987362f53bd4f6e39896aa6d77f66 (diff) | |
download | toolchain-utils-88272d479f2761cc1906fea564c73033f77a6270.tar.gz |
Fix cros lint errors.
Also move deprecated scripts to the 'deprecated' directory.
BUG=chromiumos:570464
TEST=tested scripts to make sure they still work.
Change-Id: I3442a86d898104591233a0849ea0bafb52ecf1f7
Reviewed-on: https://chrome-internal-review.googlesource.com/244221
Commit-Ready: Caroline Tice <cmtice@google.com>
Tested-by: Caroline Tice <cmtice@google.com>
Reviewed-by: Yunlian Jiang <yunlian@google.com>
Diffstat (limited to 'file_lock_machine.py')
-rwxr-xr-x | file_lock_machine.py | 132 |
1 files changed, 77 insertions, 55 deletions
diff --git a/file_lock_machine.py b/file_lock_machine.py index 5e5c17ca..9b1d3367 100755 --- a/file_lock_machine.py +++ b/file_lock_machine.py @@ -1,22 +1,24 @@ -#!/usr/bin/python +#!/usr/bin/python2 # # Copyright 2010 Google Inc. All Rights Reserved. """Script to lock/unlock machines.""" +from __future__ import print_function + __author__ = 'asharif@google.com (Ahmad Sharif)' +import argparse import datetime import fcntl import getpass import glob import json -import optparse import os import socket import sys import time -from utils import logger +from cros_utils import logger LOCK_SUFFIX = '_check_lock_liveness' @@ -32,7 +34,7 @@ def FileCheckName(name): def OpenLiveCheck(file_name): with FileCreationMask(LOCK_MASK): - fd = open(file_name, 'a+w') + fd = open(file_name, 'a') try: fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: @@ -41,14 +43,16 @@ def OpenLiveCheck(file_name): class FileCreationMask(object): + """Class for the file creation mask.""" def __init__(self, mask): self._mask = mask + self._old_mask = None def __enter__(self): self._old_mask = os.umask(self._mask) - def __exit__(self, type, value, traceback): + def __exit__(self, typ, value, traceback): os.umask(self._old_mask) @@ -89,6 +93,16 @@ class FileLock(object): lock_dir = os.path.dirname(lock_filename) assert os.path.isdir(lock_dir), ("Locks dir: %s doesn't exist!" % lock_dir) self._file = None + self._description = None + + def getDescription(self): + return self._description + + def getFilePath(self): + return self._filepath + + def setDescription(self, desc): + self._description = desc @classmethod def AsString(cls, file_locks): @@ -99,14 +113,16 @@ class FileLock(object): for file_lock in file_locks: elapsed_time = datetime.timedelta( - seconds=int(time.time() - file_lock._description.time)) + seconds=int(time.time() - file_lock.getDescription().time)) elapsed_time = '%s ago' % elapsed_time lock_strings.append( stringify_fmt % - (os.path.basename(file_lock._filepath), file_lock._description.owner, - file_lock._description.exclusive, file_lock._description.counter, - elapsed_time, file_lock._description.reason, - file_lock._description.auto)) + (os.path.basename(file_lock.getFilePath), + file_lock.getDescription().owner, + file_lock.getDescription().exclusive, + file_lock.getDescription().counter, + elapsed_time, file_lock.getDescription().reason, + file_lock.getDescription().auto)) table = '\n'.join(lock_strings) return '\n'.join([header, table]) @@ -161,7 +177,7 @@ class FileLock(object): logger.GetLogger().LogError(ex) return None - def __exit__(self, type, value, traceback): + def __exit__(self, typ, value, traceback): self._file.truncate(0) self._file.write(json.dumps(self._description.__dict__, skipkeys=True)) self._file.close() @@ -171,6 +187,7 @@ class FileLock(object): class Lock(object): + """Lock class""" def __init__(self, lock_file, auto=True): self._to_lock = os.path.basename(lock_file) @@ -245,6 +262,8 @@ class Lock(object): class Machine(object): + """Machine class""" + LOCKS_DIR = '/google/data/rw/users/mo/mobiletc-prebuild/locks' def __init__(self, name, locks_dir=LOCKS_DIR, auto=True): @@ -267,8 +286,9 @@ class Machine(object): locked = self.Lock(exclusive, reason) if locked or not timeout >= 0: break - print 'Lock not acquired for {0}, wait {1} seconds ...'.format(self._name, - sleep) + print('Lock not acquired for {0}, wait {1} seconds ...'.format( + self._name, + sleep)) time.sleep(sleep) timeout -= sleep return locked @@ -280,55 +300,57 @@ class Machine(object): def Main(argv): """The main function.""" - parser = optparse.OptionParser() - parser.add_option('-r', - '--reason', - dest='reason', - default='', - help='The lock reason.') - parser.add_option('-u', - '--unlock', - dest='unlock', - action='store_true', - default=False, - help='Use this to unlock.') - parser.add_option('-l', - '--list_locks', - dest='list_locks', - action='store_true', - default=False, - help='Use this to list locks.') - parser.add_option('-f', - '--ignore_ownership', - dest='ignore_ownership', - action='store_true', - default=False, - help="Use this to force unlock on a lock you don't own.") - parser.add_option('-s', - '--shared', - dest='shared', - action='store_true', - default=False, - help='Use this for a shared (non-exclusive) lock.') - parser.add_option('-d', - '--dir', - dest='locks_dir', - action='store', - default=Machine.LOCKS_DIR, - help='Use this to set different locks_dir') - - options, args = parser.parse_args(argv) + + parser = argparse.ArgumentParser() + parser.add_argument('-r', + '--reason', + dest='reason', + default='', + help='The lock reason.') + parser.add_argument('-u', + '--unlock', + dest='unlock', + action='store_true', + default=False, + help='Use this to unlock.') + parser.add_argument('-l', + '--list_locks', + dest='list_locks', + action='store_true', + default=False, + help='Use this to list locks.') + parser.add_argument('-f', + '--ignore_ownership', + dest='ignore_ownership', + action='store_true', + default=False, + help="Use this to force unlock on a lock you don't own.") + parser.add_argument('-s', + '--shared', + dest='shared', + action='store_true', + default=False, + help='Use this for a shared (non-exclusive) lock.') + parser.add_argument('-d', + '--dir', + dest='locks_dir', + action='store', + default=Machine.LOCKS_DIR, + help='Use this to set different locks_dir') + parser.add_argument('args', nargs='*', help='Machine arg.') + + options = parser.parse_args(argv) options.locks_dir = os.path.abspath(options.locks_dir) exclusive = not options.shared - if not options.list_locks and len(args) != 2: + if not options.list_locks and len(options.args) != 2: logger.GetLogger().LogError( 'Either --list_locks or a machine arg is needed.') return 1 - if len(args) > 1: - machine = Machine(args[1], options.locks_dir, auto=False) + if len(options.args) > 1: + machine = Machine(options.args[1], options.locks_dir, auto=False) else: machine = None @@ -347,4 +369,4 @@ def Main(argv): if __name__ == '__main__': - sys.exit(Main(sys.argv)) + sys.exit(Main(sys.argv[1:])) |