本文整理汇总了Python中goodcrypto.utils.log_file.LogFile.flush方法的典型用法代码示例。如果您正苦于以下问题:Python LogFile.flush方法的具体用法?Python LogFile.flush怎么用?Python LogFile.flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类goodcrypto.utils.log_file.LogFile
的用法示例。
在下文中一共展示了LogFile.flush方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: execute_gpg_command
# 需要导入模块: from goodcrypto.utils.log_file import LogFile [as 别名]
# 或者: from goodcrypto.utils.log_file.LogFile import flush [as 别名]
def execute_gpg_command(home_dir, initial_args, passphrase=None, data=None):
'''
Issue a GPG command in its own worker so there are no concurrency challenges.
'''
log = LogFile(filename='goodcrypto.oce.gpg_exec_queue.log')
gpg_exec = None
try:
if initial_args is not None:
new_args = []
for arg in initial_args:
new_args.append(arg)
initial_args = new_args
log.write_and_flush('gpg exec: {}'.format(initial_args))
auto_check_trustdb = gpg_constants.CHECK_TRUSTDB in initial_args
# different higher levels may try to generate the same key
# so only allow one key to be generated
if gpg_constants.GEN_KEY in initial_args:
command_ready = need_private_key(home_dir, data)
if not command_ready:
result_code = gpg_constants.GOOD_RESULT
gpg_output = gpg_constants.KEY_EXISTS
gpg_error = None
log.write_and_flush('{}'.format(gpg_output))
# if deleting a key, get the fingerprint because gpg
# only allows deletion in batch mode with the fingerprint
elif gpg_constants.DELETE_KEYS in initial_args:
fingerprint = prep_to_delete_key(home_dir, initial_args)
if fingerprint is not None:
initial_args = [gpg_constants.DELETE_KEYS, fingerprint]
log.write_and_flush('ready to delete key: {}'.format(fingerprint))
command_ready = True
else:
command_ready = True
if command_ready:
gpg_exec = GPGExec(home_dir, auto_check_trustdb=auto_check_trustdb)
result_code, gpg_output, gpg_error = gpg_exec.execute(
initial_args, passphrase, data)
log.write_and_flush('result code: {}'.format(result_code))
except JobTimeoutException as job_exception:
log.write_and_flush('gpg exec {}'.format(str(job_exception)))
result_code = gpg_constants.TIMED_OUT_RESULT
gpg_error = str(job_exception)
gpg_output = None
log.write_and_flush('EXCEPTION - see syr.exception.log for details')
record_exception()
except Exception as exception:
result_code = gpg_constants.ERROR_RESULT
gpg_output = None
gpg_error = str(exception)
log.write_and_flush('EXCEPTION - see syr.exception.log for details')
record_exception()
if gpg_exec is not None and gpg_exec.gpg_home is not None:
gpg_exec.clear_gpg_lock_files()
gpg_exec.clear_gpg_tmp_files()
log.flush()
return result_code, gpg_output, gpg_error
示例2: GPGPlugin
# 需要导入模块: from goodcrypto.utils.log_file import LogFile [as 别名]
# 或者: from goodcrypto.utils.log_file.LogFile import flush [as 别名]
#.........这里部分代码省略.........
gpg_error = None
if gpg_constants.ENCRYPT_DATA in initial_args:
command = gpg_constants.ENCRYPT_DATA
elif gpg_constants.DECRYPT_DATA in initial_args:
command = gpg_constants.DECRYPT_DATA
else:
command = initial_args[0]
# syr.lock.locked() is only a per-process lock
# syr.lock has a system wide lock, but it is not well tested
with locked():
try:
self.log_message('--- started gpg command: {} ---'.format(command))
result_code = gpg_constants.ERROR_RESULT
gpg_output = None
gpg_error = None
result_code, gpg_output, gpg_error = self.activate_queue(
command, initial_args, passphrase, data, wait_for_results)
except JobTimeoutException as job_exception:
self.log_message('gpg command {}'.format(str(job_exception)))
result_code = gpg_constants.TIMED_OUT_RESULT
except Exception:
self.log_message('EXCEPTION - see syr.exception.log for details')
record_exception()
result_code = gpg_constants.ERROR_RESULT
gpg_output = None
gpg_error = str(exception)
self.handle_unexpected_exception(exception)
finally:
self.log_message('gpg command result_code: {}'.format(result_code))
self.log_message('--- finished gpg command: {} ---'.format(command))
self.log.flush()
return result_code, gpg_output, gpg_error
def activate_queue(self, command, initial_args, passphrase, data, wait_for_results):
''' Run the command and wait for the results if appropriate. '''
def wait_until_queued(job_count):
''' Wait until the job is queued or timeout. '''
secs = 0
while (not self.job.is_queued and
not self.job.is_started and
not self.job.is_finished ):
sleep(1)
secs += 1
self.log_message('seconds until {} job was queued: {}'.format(self.job.get_id(), secs))
def wait_for_job(command):
''' Wait until the job finishes or fails.
Gpg (1.x) is not thread safe. We use a queue yo make sure it
only has one instance at a time. But we sometimes need to wait
here in the client for an operation to complete. So we
simulate a remote procedure call.
'''
result_code = gpg_constants.ERROR_RESULT
gpg_output = gpg_error = None
try:
# wait for the job
with elapsed_time() as job_time:
while self.job.result is None:
示例3: GPGExec
# 需要导入模块: from goodcrypto.utils.log_file import LogFile [as 别名]
# 或者: from goodcrypto.utils.log_file.LogFile import flush [as 别名]
#.........这里部分代码省略.........
data = data.decode()
stdin_file.write(data)
data_length = len(data)
self.log_message('data length: {}'.format(data_length))
if data_length > gpg_constants.LARGE_DATA_CHUNK:
timeout = int(
(data_length / gpg_constants.LARGE_DATA_CHUNK) * gpg_constants.TIMEOUT_PER_CHUNK) * 1000 # in ms
self.log_message('timeout in ms: {}'.format(data_length))
if GPGExec.DEBUGGING: self.log_message("data: {}".format(data))
stdin = stdin_file.getvalue()
stdin_file.close()
if GPGExec.DEBUGGING:
self.log_message("gpg args:")
for arg in args:
self.log_message(' {}'.format(arg))
result_ok = self.run_gpg(args, stdin, timeout=timeout)
self.log_message("gpg command result_ok: {}".format(result_ok))
except Exception as exception:
result_ok = False
self.result_code = gpg_constants.ERROR_RESULT
self.gpg_error = str(exception)
self.log_message('result code: {}'.format(self.result_code))
if self.gpg_error and len(self.gpg_error.strip()) > 0:
self.log_message("gpg error: {}".format(self.gpg_error))
self.log_message('EXCEPTION - see syr.exception.log for details')
record_exception()
self.log.flush()
return self.result_code, self.gpg_output, self.gpg_error
def run_gpg(self, args, stdin, timeout=None):
''' Run the GPG command. '''
try:
self.gpg_output = self.gpg_error = None
if gpg_constants.ENCRYPT_DATA in args:
command = gpg_constants.ENCRYPT_DATA
elif gpg_constants.DECRYPT_DATA in args:
command = gpg_constants.DECRYPT_DATA
elif gpg_constants.SEARCH_KEYSERVER in args:
command = gpg_constants.SEARCH_KEYSERVER
elif gpg_constants.RETRIEVE_KEYS in args:
command = gpg_constants.RETRIEVE_KEYS
else:
command = args[0]
self.log_message('--- started executing: {} ---'.format(command))
with elapsed_time() as gpg_time:
if timeout is None:
if stdin and len(stdin) > 0:
gpg_results = self.gpg(*args, _in=stdin, _ok_code=[0,2])
else:
gpg_results = self.gpg(*args, _ok_code=[0,2])
else:
if stdin and len(stdin) > 0:
gpg_results = self.gpg(*args, _in=stdin, _ok_code=[0,2], _timeout=timeout)
else:
gpg_results = self.gpg(*args, _ok_code=[0,2], _timeout=timeout)