def test_should_create_base_dir_if_it_does_not_exist_when_locking(self, mock_open, mock_fcntl, mock_exists, mock_mkdir): mock_fcntl.LOCK_EX = 'LOCK_EX' mock_exists.return_value = False file_handle_mock = Mock() mock_open.return_value = file_handle_mock lock() self.assertEqual(call('shtub'), mock_mkdir.call_args)
def test_should_create_lock(self, mock_open, mock_fcntl): mock_fcntl.LOCK_EX = 'LOCK_EX' file_handle_mock = Mock() mock_open.return_value = file_handle_mock actual_file_handle = lock() self.assertEqual(file_handle_mock, actual_file_handle) self.assertEqual(call('shtub/lock', mode='a'), mock_open.call_args) self.assertEqual( call(file_handle_mock, mock_fcntl.LOCK_EX), mock_fcntl.flock.call_args)
def record_execution(execution): """ loads the list of recent executions from the EXECUTIONS_FILENAME file, appends the given execution to the list, then writes the list back to the file again. To assure only one process is reading and writing the file a file lock is used. """ lock_file_handle = lock() executions = [] if os.path.exists(EXECUTIONS_FILENAME): executions = deserialize_executions(EXECUTIONS_FILENAME) executions.append(execution) serialize_as_dictionaries(EXECUTIONS_FILENAME, executions) logging.info('Recorded %s executions.', len(executions)) unlock(lock_file_handle)
def handle_execution(): """ creates the base directory, initializes the logging and will read in the arguments and input from stdin to create a new execution object. """ if not os.path.exists(BASEDIR): os.mkdir(BASEDIR) global lock_handle lock_handle = lock(SERIALIZATION_LOCK_FILENAME) logging_format = '%(asctime)s %(levelname)5s [%(name)s] process[%(process)d] thread[%(thread)d] - %(message)s' logging.basicConfig(filename=LOG_FILENAME, level=logging.INFO, format=logging_format) command = os.path.basename(sys.argv[0]) arguments = sys.argv[1:] stdin = read_stdin() command_input = CommandInput(command, arguments, stdin) dispatch(command_input)