Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_host(host=None, action='Performing action'):
""" Helper function to get a single host object
"""
host_obj = None
hostdot = host + '.'
message = '{0!s} for host {1!s}'.format(action, host)
try:
host_obj = Host.objects.get(hostname__startswith=hostdot)
except ObjectDoesNotExist:
try:
host_obj = Host.objects.get(hostname__startswith=host)
except ObjectDoesNotExist:
message = 'Host {0!s} does not exist'.format(host)
except MultipleObjectsReturned:
matches = Host.objects.filter(hostname__startswith=host).count()
message = '{0!s} hosts match hostname "{1!s}"'.format(matches, host)
if verbose:
print message
return host_obj
""" Find updates for all hosts, specify host for a single host
"""
updated_hosts = []
hosts = get_hosts(host, 'Finding updates')
ts = datetime.now().replace(microsecond=0)
for host in hosts:
if verbose:
print '\n{0!s}'.format(host)
if host not in updated_hosts:
host.updated_at = ts
host.find_updates()
host.save()
# only include hosts with the same number of packages
filtered_hosts = Host.objects.annotate(
packages_count=Count("packages")).filter(
packages_count=host.packages.count())
# exclude hosts with the current timestamp
filtered_hosts = filtered_hosts.exclude(updated_at=ts)
packages = set(host.packages.all())
repos = set(host.repos.all())
updates = host.updates.all()
phosts = []
for fhost in filtered_hosts:
frepos = set(fhost.repos.all())
rdiff = repos.difference(frepos)
if len(rdiff) != 0:
continue
""" Helper function to get a single host object
"""
host_obj = None
hostdot = host + '.'
message = '{0!s} for host {1!s}'.format(action, host)
try:
host_obj = Host.objects.get(hostname__startswith=hostdot)
except ObjectDoesNotExist:
try:
host_obj = Host.objects.get(hostname__startswith=host)
except ObjectDoesNotExist:
message = 'Host {0!s} does not exist'.format(host)
except MultipleObjectsReturned:
matches = Host.objects.filter(hostname__startswith=host).count()
message = '{0!s} hosts match hostname "{1!s}"'.format(matches, host)
if verbose:
print message
return host_obj