Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Commit

Permalink
More Python3 fixes
Browse files Browse the repository at this point in the history
- use dict, list, set literals and comprehensions where sensible
- don't build lists from generators superfluously
- don't use dict.keys(), rather the dict var itself where an iterable is
  expected
- sorted() returns a list already
- remove Python 2 compat
- use f-strings, except for logging, don't use % operator there, for
  late interpolation
- remove u-strings from comments, too

Signed-off-by: Nils Philippsen <[email protected]>
  • Loading branch information
nphilipp committed Jul 1, 2020
1 parent 4fa717e commit 16d23f5
Show file tree
Hide file tree
Showing 27 changed files with 506 additions and 497 deletions.
26 changes: 13 additions & 13 deletions fedmsg.d/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@
#
# Authors: Ralph Bean <[email protected]>
#
config = dict(
config = {
# Set this to dev if you're hacking on fedmsg or an app.
# Set to stg or prod if running in the Fedora Infrastructure
environment="dev",
'environment': "dev",

# Default is 0
high_water_mark=0,
io_threads=1,
'high_water_mark': 0,
'io_threads': 1,

## For the fedmsg-hub and fedmsg-relay. ##

# We almost always want the fedmsg-hub to be sending messages with zmq as
# opposed to amqp or stomp.
zmq_enabled=True,
'zmq_enabled': True,

# When subscribing to messages, we want to allow splats ('*') so we tell
# the hub to not be strict when comparing messages topics to subscription
# topics.
zmq_strict=False,
'zmq_strict': False,

# Number of seconds to sleep after initializing waiting for sockets to sync
post_init_sleep=0.5,
'post_init_sleep': 0.5,

# Wait a whole second to kill all the last io threads for messages to
# exit our outgoing queue (if we have any). This is in milliseconds.
zmq_linger=1000,
'zmq_linger': 1000,

# See the following
# - http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
# - http://api.zeromq.org/3-2:zmq-setsockopt
zmq_tcp_keepalive=1,
zmq_tcp_keepalive_cnt=3,
zmq_tcp_keepalive_idle=60,
zmq_tcp_keepalive_intvl=5,
)
'zmq_tcp_keepalive': 1,
'zmq_tcp_keepalive_cnt': 3,
'zmq_tcp_keepalive_idle': 60,
'zmq_tcp_keepalive_intvl': 5,
}
36 changes: 18 additions & 18 deletions fedmsg.d/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@
# See the following for constraints on this format http://bit.ly/Xn1WDn
bare_format = "[%(asctime)s][%(name)10s %(levelname)7s] %(message)s"

config = dict(
logging=dict(
version=1,
formatters=dict(
bare={
config = {
'logging': {
'version': 1,
'formatters': {
'bare': {
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": bare_format
},
),
handlers=dict(
console={
},
'handlers': {
'console': {
"class": "logging.StreamHandler",
"formatter": "bare",
"level": "DEBUG",
"stream": "ext://sys.stdout",
}
),
loggers=dict(
fedmsg={
},
'loggers': {
'fedmsg': {
"level": "DEBUG",
"propagate": False,
"handlers": ["console"],
},
moksha={
'moksha': {
"level": "DEBUG",
"propagate": False,
"handlers": ["console"],
},
pkgdb2client={
'pkgdb2client': {
"level": "INFO",
"propagate": False,
"handlers": ["console"],
},
modules={
'modules': {
"level": "DEBUG",
"propagate": False,
"handlers": ["console"],
},
pdcupdater={
'pdcupdater': {
"level": "DEBUG",
"propagate": False,
"handlers": ["console"],
},
),
),
)
},
},
}
10 changes: 5 additions & 5 deletions fedmsg.d/pdcupdater-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@
'pdcupdater.ContainerRPMInclusionDepChainHandler.container_build_user': 'containerbuild',

# Augment the base fedmsg logging config to also handle pdcupdater loggers.
'logging': dict(
loggers=dict(
pdcupdater={
'logging': {
'loggers': {
'pdcupdater': {
"level": "DEBUG",
"propagate": False,
"handlers": ["console"],
},
)
)
},
},
}
33 changes: 16 additions & 17 deletions fedmsg.d/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@
# Authors: Ralph Bean <[email protected]>
#
import os
import socket

SEP = os.path.sep
here = os.getcwd()

config = dict(
sign_messages=False,
validate_signatures=False,
config = {
'sign_messages': False,
'validate_signatures': False,

# Use these implementations to sign and validate messages
crypto_backend='x509',
crypto_validate_backends=['x509'],
'crypto_backend': 'x509',
'crypto_validate_backends': ['x509'],

ssldir="/etc/pki/fedmsg",
crl_location="https://fedoraproject.org/fedmsg/crl.pem",
crl_cache="/var/run/fedmsg/crl.pem",
crl_cache_expiry=10,
'ssldir': "/etc/pki/fedmsg",
'crl_location': "https://fedoraproject.org/fedmsg/crl.pem",
'crl_cache': "/var/run/fedmsg/crl.pem",
'crl_cache_expiry': 10,

ca_cert_location="https://fedoraproject.org/fedmsg/ca.crt",
ca_cert_cache="/var/run/fedmsg/ca.crt",
ca_cert_cache_expiry=0, # Never expires
'ca_cert_location': "https://fedoraproject.org/fedmsg/ca.crt",
'ca_cert_cache': "/var/run/fedmsg/ca.crt",
'ca_cert_cache_expiry': 0, # Never expires

certnames={
'certnames': {
# In prod/stg, map hostname to the name of the cert in ssldir.
# Unfortunately, we can't use socket.getfqdn()
#"app01.stg": "app01.stg.phx2.fedoraproject.org",
Expand All @@ -49,7 +48,7 @@
# A mapping of fully qualified topics to a list of cert names for which
# a valid signature is to be considered authorized. Messages on topics not
# listed here are considered automatically authorized.
routing_policy={
'routing_policy': {
# Only allow announcements from production if they're signed by a
# certain certificate.
"org.fedoraproject.prod.announce.announcement": [
Expand All @@ -62,5 +61,5 @@
# When this is False, only messages that have a topic in the routing_policy
# but whose cert names aren't in the associated list are dropped; messages
# whose topics do not appear in the routing_policy are not dropped.
routing_nitpicky=False,
)
'routing_nitpicky': False,
}
57 changes: 29 additions & 28 deletions pdcupdater/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import logging
import logging.config
import sys
Expand Down Expand Up @@ -36,11 +34,11 @@ def retry():

def _initialize_basics(pdc):
""" Gotta have these before we can really do anything... """
arches = [dict(name=name) for name in ["armhfp", "i386", "x86_64"]]
arches = [{'name': name} for name in ["armhfp", "i386", "x86_64"]]
pdc_arches = list(pdc.get_paged(pdc['arches']._))
for arch in arches:
if arch not in pdc_arches:
log.info("Creating arch %r." % arch['name'])
log.info("Creating arch %r.", arch['name'])
pdc['arches']._(arch)


Expand All @@ -52,8 +50,8 @@ def initialize():
_initialize_basics(pdc)
handlers = pdcupdater.handlers.load_handlers(config)
for handler in handlers:
log.info("Calling .initialize() on %r" % handler)
pdc.set_comment("Initialized via %r" % handler)
log.info("Calling .initialize() on %r", handler)
pdc.set_comment(f"Initialized via {handler!r}")
try:
handler.initialize(pdc)
except beanbag.bbexcept.BeanBagException as e:
Expand All @@ -70,7 +68,7 @@ def audit():
results = {}
for handler in handlers:
name = type(handler).__name__
log.info('Performing audit for %s' % name)
log.info('Performing audit for %s', name)
results[name] = handler.audit(pdc)

verbose = False
Expand All @@ -79,7 +77,7 @@ def audit():

def _print_audit_report(results, verbose):
fail = False
for key, values in list(results.items()):
for key, values in results.items():
present, absent = values
fail = fail or present or absent

Expand All @@ -88,30 +86,33 @@ def _print_audit_report(results, verbose):
else:
print("WARNING - audit script detected something is wrong.")

print("\nSummary")
print("=======\n")
print()
print("Summary")
print("=======")
print()

for key, values in list(results.items()):
for key, values in results.items():
present, absent = values
if not present and not absent:
print(( "- [x]", key))
print(f"- [x] {key}")
else:
print(("- [!]", key))
print((" ", len(present), "extra entries in PDC unaccounted for"))
print((" ", len(absent), "entries absent from PDC"))
print(f"- [!] {key}")
print(f" {len(present)} extra entries in PDC unaccounted for")
print(f" {len(absent)} entries absent from PDC")

print("\nDetails")
print()
print("Details")
print("=======")

limit = 100
for key, values in list(results.items()):
for key, values in results.items():
present, absent = values
if not present and not absent:
continue

print()
print(key)
print(("-" * len(key)))
print("-" * len(key))
print()

if not present:
Expand All @@ -121,16 +122,16 @@ def _print_audit_report(results, verbose):
print()
if verbose or len(present) < limit:
for value in present:
print(("-", value))
print(f"- {value}")
if isinstance(present, dict):
print((" ", present[value]))
print(f" {present[value]}")
else:
present = list(present)
for value in present[:limit]:
print(("-", value))
print(f"- {value}")
if isinstance(present, dict):
print((" ", present[value]))
print(("- (plus %i more... truncated.)" % (len(present) - limit)))
print(f" {present[value]}")
print(f"- (plus {len(present) - limit} more... truncated.)")
print()

if not absent:
Expand All @@ -140,16 +141,16 @@ def _print_audit_report(results, verbose):
print()
if verbose or len(absent) < limit:
for value in absent:
print("-", value)
print(f"- {value}")
if isinstance(absent, dict):
print(" ", absent[value])
print(f" {absent[value]}")
else:
absent = list(absent)
for value in absent[:limit]:
print("-", value)
print(f"- {value}")
if isinstance(absent, dict):
print(" ", absent[value])
print("- (plus %i more... truncated.)" % (len(absent) - limit))
print(f" {absent[value]}")
print(f"- (plus {len(absent) - limit} more... truncated.)")

if not fail:
return 0
Expand Down
2 changes: 1 addition & 1 deletion pdcupdater/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def consume(self, envelope):
if 'message-id' in msg['headers']:
msg['msg_id'] = msg['headers']['message-id']

self.log.debug("Received %r, %r" % (msg['msg_id'], topic))
self.log.debug("Received %r, %r", msg['msg_id'], topic)

pdc = pdc_client.PDCClient(**self.pdc_config)
pdcupdater.utils.handle_message(pdc, self.handlers, msg)
3 changes: 2 additions & 1 deletion pdcupdater/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def construct_topics(self, config):
for topic in self.topic_suffixes
]

@abc.abstractproperty
@property
@abc.abstractmethod
def topic_suffixes(self):
pass

Expand Down
Loading

0 comments on commit 16d23f5

Please sign in to comment.