flake8: apply E3 rules

This commit is contained in:
Yuming Zhu 2020-02-26 02:31:31 +08:00
parent 05340b146b
commit 0f727a2ab4
32 changed files with 534 additions and 70 deletions

View file

@ -91,12 +91,14 @@ except ImportError:
PROFILE_MODULES = {} # {module_name: module_instance}
def _(args):
"""Stub function for translation"""
return args # pragma: no cover
## Constants ##
RPM_HEADER_MAGIC = six.b('\x8e\xad\xe8')
RPM_TAG_HEADERSIGNATURES = 62
RPM_TAG_FILEDIGESTALGO = 5011
@ -130,6 +132,7 @@ for h in (
# BEGIN kojikamid dup #
class Enum(dict):
"""A simple class to track our enumerated constants
@ -178,6 +181,7 @@ class Enum(dict):
# END kojikamid dup #
API_VERSION = 1
TASK_STATES = Enum((
@ -290,10 +294,12 @@ DEFAULT_AUTH_TIMEOUT = 60
# Exceptions
PythonImportError = ImportError # will be masked by koji's one
class GenericError(Exception):
"""Base class for our custom exceptions"""
faultCode = 1000
fromFault = False
def __str__(self):
try:
return str(self.args[0]['args'][0])
@ -304,97 +310,120 @@ class GenericError(Exception):
return str(self.__dict__)
# END kojikamid dup #
class LockError(GenericError):
"""Raised when there is a lock conflict"""
faultCode = 1001
class AuthError(GenericError):
"""Raised when there is an error in authentication"""
faultCode = 1002
class TagError(GenericError):
"""Raised when a tagging operation fails"""
faultCode = 1003
class ActionNotAllowed(GenericError):
"""Raised when the session does not have permission to take some action"""
faultCode = 1004
# BEGIN kojikamid dup #
class BuildError(GenericError):
"""Raised when a build fails"""
faultCode = 1005
# END kojikamid dup #
class AuthLockError(AuthError):
"""Raised when a lock prevents authentication"""
faultCode = 1006
class AuthExpired(AuthError):
"""Raised when a session has expired"""
faultCode = 1007
class SequenceError(AuthError):
"""Raised when requests are received out of sequence"""
faultCode = 1008
class RetryError(AuthError):
"""Raised when a request is received twice and cannot be rerun"""
faultCode = 1009
class PreBuildError(BuildError):
"""Raised when a build fails during pre-checks"""
faultCode = 1010
class PostBuildError(BuildError):
"""Raised when a build fails during post-checks"""
faultCode = 1011
class BuildrootError(BuildError):
"""Raised when there is an error with the buildroot"""
faultCode = 1012
class FunctionDeprecated(GenericError):
"""Raised by a deprecated function"""
faultCode = 1013
class ServerOffline(GenericError):
"""Raised when the server is offline"""
faultCode = 1014
class LiveCDError(GenericError):
"""Raised when LiveCD Image creation fails"""
faultCode = 1015
class PluginError(GenericError):
"""Raised when there is an error with a plugin"""
faultCode = 1016
class CallbackError(PluginError):
"""Raised when there is an error executing a callback"""
faultCode = 1017
class ApplianceError(GenericError):
"""Raised when Appliance Image creation fails"""
faultCode = 1018
class ParameterError(GenericError):
"""Raised when an rpc call receives incorrect arguments"""
faultCode = 1019
class ImportError(GenericError):
"""Raised when an import fails"""
faultCode = 1020
class ConfigurationError(GenericError):
"""Raised when load of koji configuration fails"""
faultCode = 1021
class LiveMediaError(GenericError):
"""Raised when LiveMedia Image creation fails"""
faultCode = 1022
class MultiCallInProgress(object):
"""
Placeholder class to be returned by method calls when in the process of
@ -418,6 +447,7 @@ def convertFault(fault):
# otherwise...
return fault
def listFaults():
"""Return a list of faults
@ -442,6 +472,7 @@ def listFaults():
# functions for encoding/decoding optional arguments
def encode_args(*args, **opts):
"""The function encodes optional arguments as regular arguments.
@ -453,6 +484,7 @@ def encode_args(*args, **opts):
args = args + (opts,)
return args
def decode_args(*args):
"""Decodes optional arguments from a flat argument list
@ -468,6 +500,7 @@ def decode_args(*args):
args = args[:-1]
return args, opts
def decode_args2(args, names, strict=True):
"An alternate form of decode_args, returns a dictionary"
args, opts = decode_args(*args)
@ -477,6 +510,7 @@ def decode_args2(args, names, strict=True):
ret.update(opts)
return ret
def decode_int(n):
"""If n is not an integer, attempt to convert it"""
if isinstance(n, six.integer_types):
@ -486,6 +520,7 @@ def decode_int(n):
# commonly used functions
def safe_xmlrpc_loads(s):
"""Load xmlrpc data from a string, but catch faults"""
try:
@ -530,6 +565,7 @@ def ensuredir(directory):
# END kojikamid dup #
def daemonize():
"""Detach and run in background"""
pid = os.fork()
@ -553,6 +589,7 @@ def daemonize():
os.close(fd1)
os.close(fd2)
def multibyte(data):
"""Convert a list of bytes to an integer (network byte order)"""
sum = 0
@ -561,6 +598,7 @@ def multibyte(data):
sum += data[i] << (8 * (n - i - 1))
return sum
def find_rpm_sighdr(path):
"""Finds the offset and length of the signature header."""
# see Maximum RPM Appendix A: Format of the RPM File
@ -570,6 +608,7 @@ def find_rpm_sighdr(path):
sigsize = rpm_hdr_size(path, sig_start)
return (sig_start, sigsize)
def rpm_hdr_size(f, ofs=None):
"""Returns the length (in bytes) of the rpm header
@ -776,6 +815,7 @@ def rip_rpm_sighdr(src):
fo.close()
return sighdr
def rip_rpm_hdr(src):
"""Rip the main header out of an rpm"""
(start, size) = find_rpm_sighdr(src)
@ -787,6 +827,7 @@ def rip_rpm_hdr(src):
fo.close()
return hdr
def _ord(s):
# in python2 it is char/str, while in py3 it is already int/bytes
if isinstance(s, int):
@ -794,6 +835,7 @@ def _ord(s):
else:
return ord(s)
def __parse_packet_header(pgp_packet):
"""Parse pgp_packet header, return tag type and the rest of pgp_packet"""
byte0 = _ord(pgp_packet[0])
@ -828,6 +870,7 @@ def __parse_packet_header(pgp_packet):
raise ValueError('Invalid OpenPGP packet length')
return (tag, pgp_packet[offset:])
def __subpacket_key_ids(subs):
"""Parse v4 signature subpackets and return a list of issuer key IDs"""
res = []
@ -847,6 +890,7 @@ def __subpacket_key_ids(subs):
subs = subs[off + length:]
return res
def get_sigpacket_key_id(sigpacket):
"""Return ID of the key used to create sigpacket as a hexadecimal string"""
(tag, sigpacket) = __parse_packet_header(sigpacket)
@ -870,6 +914,7 @@ def get_sigpacket_key_id(sigpacket):
'Unknown PGP signature packet version %s' % _ord(sigpacket[0]))
return hex_string(key_id)
def get_sighdr_key(sighdr):
"""Parse the sighdr and return the sigkey"""
rh = RawHeader(sighdr)
@ -881,6 +926,7 @@ def get_sighdr_key(sighdr):
else:
return get_sigpacket_key_id(sig)
def splice_rpm_sighdr(sighdr, src, dst=None, bufsize=8192):
"""Write a copy of an rpm with signature header spliced in"""
(start, size) = find_rpm_sighdr(src)
@ -901,6 +947,7 @@ def splice_rpm_sighdr(sighdr, src, dst=None, bufsize=8192):
dst_fo.close()
return dst
def get_rpm_header(f, ts=None):
"""Return the rpm header."""
if rpm is None:
@ -998,6 +1045,7 @@ def get_header_fields(X, fields, src_arch=False):
ret[f] = get_header_field(hdr, f, src_arch=src_arch)
return ret
def parse_NVR(nvr):
"""split N-V-R into dictionary of data"""
ret = {}
@ -1018,6 +1066,7 @@ def parse_NVR(nvr):
ret['name'] = ret['name'][epochIndex + 1:]
return ret
def parse_NVRA(nvra):
"""split N-V-R.A.rpm into dictionary of data
@ -1061,6 +1110,7 @@ def check_NVR(nvr, strict=False):
else:
return False
def _check_NVR(nvr):
if isinstance(nvr, six.string_types):
nvr = parse_NVR(nvr)
@ -1107,6 +1157,7 @@ def is_debuginfo(name):
return (name.endswith('-debuginfo') or name.endswith('-debugsource') or
'-debuginfo-' in name)
def canonArch(arch):
"""Given an arch, return the "canonical" arch"""
# XXX - this could stand to be smarter, and we should probably
@ -1132,6 +1183,7 @@ def canonArch(arch):
else:
return arch
def parse_arches(arches, to_list=False, strict=False, allow_none=False):
"""Normalize user input for a list of arches.
@ -1195,8 +1247,10 @@ class POMHandler(xml.sax.handler.ContentHandler):
self.tag_content = None
self.values.clear()
ENTITY_RE = re.compile(r'&[A-Za-z0-9]+;')
def parse_pom(path=None, contents=None):
"""
Parse the Maven .pom file return a map containing information
@ -1236,6 +1290,7 @@ def parse_pom(path=None, contents=None):
raise GenericError('could not extract %s from POM: %s' % (field, (path or '<contents>')))
return values
def pom_to_maven_info(pominfo):
"""
Convert the output of parsing a POM into a format compatible
@ -1250,6 +1305,7 @@ def pom_to_maven_info(pominfo):
'version': pominfo['version']}
return maveninfo
def maven_info_to_nvr(maveninfo):
"""
Convert the maveninfo to NVR-compatible format.
@ -1264,6 +1320,7 @@ def maven_info_to_nvr(maveninfo):
nvr['package_name'] = nvr['name']
return nvr
def mavenLabel(maveninfo):
"""
Return a user-friendly label for the given maveninfo. maveninfo is
@ -1271,6 +1328,7 @@ def mavenLabel(maveninfo):
"""
return '%(group_id)s-%(artifact_id)s-%(version)s' % maveninfo
def hex_string(s):
"""Converts a string to a string of hex digits"""
return ''.join(['%02x' % _ord(x) for x in s])
@ -1339,6 +1397,7 @@ This is a meta-package that requires a defined group of packages
""")
return ''.join(data)
def generate_comps(groups, expand_groups=False):
"""Generate comps content from groups data"""
def boolean_text(x):
@ -1635,11 +1694,14 @@ name=build
return ''.join(parts)
def get_sequence_value(cursor, sequence):
cursor.execute("""SELECT nextval(%(sequence)s)""", locals())
return cursor.fetchone()[0]
# From Python Cookbook 2nd Edition, Recipe 8.6
def format_exc_plus():
""" Format the usual traceback information, followed by a listing of
all the local variables in each frame.
@ -1669,6 +1731,7 @@ def format_exc_plus():
rv += "<ERROR WHILE PRINTING VALUE>\n"
return rv
def openRemoteFile(relpath, topurl=None, topdir=None, tempdir=None):
"""Open a file on the main server (read-only)
@ -2085,6 +2148,7 @@ class PathInfo(object):
"""Return the output directory for the task with the given id"""
return self.work(volume=volume) + '/' + self.taskrelpath(task_id)
pathinfo = PathInfo()
@ -3171,6 +3235,7 @@ class DBHandler(logging.Handler):
A handler class which writes logging records, appropriately formatted,
to a database.
"""
def __init__(self, cnx, table, mapping=None):
"""
Initialize the handler.
@ -3221,6 +3286,7 @@ class DBHandler(logging.Handler):
except:
self.handleError(record)
def formatTime(value):
"""Format a timestamp so it looks nicer"""
if not value:
@ -3237,6 +3303,7 @@ def formatTime(value):
else:
return value
def formatTimeLong(value):
"""Format a timestamp to a more human-reable format, i.e.:
Sat, 07 Sep 2002 00:00:01 GMT
@ -3248,6 +3315,7 @@ def formatTimeLong(value):
localtime = time.mktime(time.strptime(formatTime(value), '%Y-%m-%d %H:%M:%S'))
return time.strftime('%a, %d %b %Y %H:%M:%S %Z', time.localtime(localtime))
def buildLabel(buildInfo, showEpoch=False):
"""Format buildInfo (dict) into a descriptive label."""
epoch = buildInfo.get('epoch')
@ -3262,6 +3330,7 @@ def buildLabel(buildInfo, showEpoch=False):
buildInfo.get('version'),
buildInfo.get('release'))
def _module_info(url):
module_info = ''
if '?' in url:
@ -3280,12 +3349,14 @@ def _module_info(url):
else:
return '%s:%s' % (repo_info, rev_info)
def taskLabel(taskInfo):
try:
return _taskLabel(taskInfo)
except Exception:
return "malformed task"
def _taskLabel(taskInfo):
"""Format taskInfo (dict) into a descriptive label."""
method = taskInfo['method']
@ -3405,10 +3476,13 @@ def _taskLabel(taskInfo):
else:
return '%s (%s)' % (method, arch)
CONTROL_CHARS = [chr(i) for i in range(32)]
NONPRINTABLE_CHARS = ''.join([c for c in CONTROL_CHARS if c not in '\r\n\t'])
if six.PY3:
NONPRINTABLE_CHARS_TABLE = dict.fromkeys(map(ord, NONPRINTABLE_CHARS), None)
def removeNonprintable(value):
# expects raw-encoded string, not unicode
if six.PY2:
@ -3506,12 +3580,14 @@ def add_file_logger(logger, fn):
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(name)s: %(message)s'))
logging.getLogger(logger).addHandler(handler)
def add_stderr_logger(logger):
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] {%(process)d} %(name)s:%(lineno)d %(message)s'))
handler.setLevel(logging.DEBUG)
logging.getLogger(logger).addHandler(handler)
def add_sys_logger(logger):
# For remote logging;
# address = ('host.example.com', logging.handlers.SysLogHandler.SYSLOG_UDP_PORT)
@ -3522,6 +3598,7 @@ def add_sys_logger(logger):
handler.setLevel(logging.INFO)
logging.getLogger(logger).addHandler(handler)
def add_mail_logger(logger, addr):
"""Adding e-mail logger
@ -3542,5 +3619,6 @@ def add_mail_logger(logger, addr):
handler.setLevel(logging.ERROR)
logging.getLogger(logger).addHandler(handler)
def remove_log_handler(logger, handler):
logging.getLogger(logger).removeHandler(handler)