Skip to content

Commit

Permalink
feat: update syntax to support Python 2 and 3
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Styk <mart.styk@gmail.com>
  • Loading branch information
StykMartin committed Feb 8, 2025
1 parent ba8a30f commit 6f9d19e
Show file tree
Hide file tree
Showing 59 changed files with 712 additions and 629 deletions.
54 changes: 0 additions & 54 deletions Server/bkr/server/BasicAuthTransport.py

This file was deleted.

47 changes: 24 additions & 23 deletions Server/bkr/server/CSV_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from turbogears import expose, widgets
from sqlalchemy.exc import InvalidRequestError
from bkr.server import identity
from bkr.server.util import ensure_str
from bkr.server.xmlrpccontroller import RPCRoot
from tempfile import NamedTemporaryFile
from cherrypy.lib.cptools import serve_file
Expand All @@ -22,6 +23,7 @@
SystemAccessPolicy, SystemPermission, SystemPool)
from bkr.server.widgets import HorizontalForm, RadioButtonList
from kid import XML
import six

import csv
import datetime
Expand All @@ -31,7 +33,7 @@
def smart_bool(s):
if s is True or s is False:
return s
t = unicode(s).strip().lower()
t = six.text_type(s).strip().lower()
if t == 'true':
return True
if t == 'false':
Expand All @@ -46,9 +48,9 @@ def __init__(self, f, **kw):
self.reader = csv.DictReader(f, **kw)

def next(self):
row = self.reader.next()
row = next(self.reader)
d = {}
for k, v in row.iteritems():
for k, v in six.iteritems(row):

if isinstance(k, str):
k = k.decode('utf8')
Expand Down Expand Up @@ -203,8 +205,8 @@ def action_import(self, csv_file, *args, **kw):
log.append('Too many fields on line %s (expecting %s)'
% (reader.line_num, len(reader.fieldnames)))
continue
if any(value is missing for value in data.itervalues()):
missing_fields = [field for field, value in data.iteritems()
if any(value is missing for value in six.itervalues(data)):
missing_fields = [field for field, value in six.iteritems(data)
if value is missing]
log.append('Missing fields on line %s: %s' % (reader.line_num,
', '.join(missing_fields)))
Expand All @@ -215,14 +217,14 @@ def action_import(self, csv_file, *args, **kw):
try:
with session.begin_nested():
self._import_row(data, log)
except Exception, e:
except Exception as e:
# log and continue processing more rows
log.append('Error importing line %s: %s' % (reader.line_num, e))

if is_empty:
log.append('Empty CSV file supplied')

except csv.Error, e:
except csv.Error as e:
session.rollback()
log.append('Error parsing CSV file: %s' % e)

Expand Down Expand Up @@ -258,8 +260,7 @@ def _to_csv(cls, file):
for item in cls.query():
for data in cls.to_datastruct(item):
data['csv_type'] = cls.csv_type
# XXX remove encoding in Python 3...
writer.writerow(dict((k, unicode(v).encode('utf8')) for k, v in data.iteritems()))
writer.writerow(dict((k, ensure_str(six.text_type(v))) for k, v in six.iteritems(data)))

@classmethod
def from_csv(cls, system, data, log):
Expand Down Expand Up @@ -288,7 +289,7 @@ def _from_csv(cls,system,data,csv_type,log):
newdata = smart_bool(data[key])
else:
newdata = None
if unicode(newdata) != unicode(current_data):
if six.text_type(newdata) != six.text_type(current_data):
system.record_activity(user=identity.current.user, service=u'CSV',
action=u'Changed', field=key,
old=u'%s' % current_data, new=u'%s' % newdata)
Expand All @@ -300,7 +301,7 @@ def to_datastruct(self):
val = getattr(self, csv_key, None)
if val is None:
val = ''
datastruct[csv_key] = unicode(val)
datastruct[csv_key] = six.text_type(val)
yield datastruct

class CSV_System(CSV):
Expand Down Expand Up @@ -331,7 +332,7 @@ def _from_csv(cls,system,data,csv_type,log):
else:
newdata = None
current_data = getattr(system, key, None)
if unicode(newdata) != unicode(current_data):
if six.text_type(newdata) != six.text_type(current_data):
setattr(system,key,newdata)
system.record_activity(user=identity.current.user,
service=u'CSV', action=u'Changed', field=key,
Expand Down Expand Up @@ -508,7 +509,7 @@ def _from_csv(cls,system,data,csv_type,log):
else:
newdata = None
current_data = getattr(csv_object, key, None)
if unicode(newdata) != unicode(current_data):
if six.text_type(newdata) != six.text_type(current_data):
system.record_activity(user=identity.current.user, service=u'CSV',
action=u'Changed', field=key, old=u'***', new=u'***')
setattr(csv_object,key,newdata)
Expand Down Expand Up @@ -607,8 +608,8 @@ def _from_csv(cls,system,data,csv_type,log):

if data['update'] and data['family']:
try:
osversion = OSVersion.by_name(OSMajor.by_name(unicode(data['family'])),
unicode(data['update']))
osversion = OSVersion.by_name(OSMajor.by_name(six.text_type(data['family'])),
six.text_type(data['update']))
except InvalidRequestError:
log.append("%s: Invalid Family %s Update %s" % (system.fqdn,
data['family'],
Expand Down Expand Up @@ -674,7 +675,7 @@ class CSV_Install(CSV):
@classmethod
def query(cls):
for system in System.all(identity.current.user):
for install in system.provisions.itervalues():
for install in six.itervalues(system.provisions):
yield CSV_Install(install)

@classmethod
Expand Down Expand Up @@ -713,27 +714,27 @@ def _from_csv(cls,system,data,csv_type,log):
log.append("%s: Error! You must specify Family along with Update" % system.fqdn)
return False
try:
update = OSVersion.by_name(family, unicode(update))
update = OSVersion.by_name(family, six.text_type(update))
except InvalidRequestError:
log.append("%s: Error! Invalid update %s" % (system.fqdn,
data['update']))
return False

#Import Update specific
if update and family:
if system.provisions.has_key(arch):
if arch in system.provisions:
system_arch = system.provisions[arch]
else:
system_arch = Provision(arch=arch)
system.provisions[arch] = system_arch

if system_arch.provision_families.has_key(family):
if family in system_arch.provision_families:
system_provfam = system_arch.provision_families[family]
else:
system_provfam = ProvisionFamily(osmajor=family)
system_arch.provision_families[family] = system_provfam

if system_provfam.provision_family_updates.has_key(update):
if update in system_provfam.provision_family_updates:
prov = system_provfam.provision_family_updates[update]
else:
prov = ProvisionFamilyUpdate(osversion=update)
Expand All @@ -742,13 +743,13 @@ def _from_csv(cls,system,data,csv_type,log):

#Import Family specific
if family and not update:
if system.provisions.has_key(arch):
if arch in system.provisions:
system_arch = system.provisions[arch]
else:
system_arch = Provision(arch=arch)
system.provisions[arch] = system_arch

if system_arch.provision_families.has_key(family):
if family in system_arch.provision_families:
prov = system_arch.provision_families[family]
else:
prov = ProvisionFamily(osmajor=family)
Expand All @@ -757,7 +758,7 @@ def _from_csv(cls,system,data,csv_type,log):

#Import Arch specific
if not family and not update:
if system.provisions.has_key(arch):
if arch in system.provisions:
prov = system.provisions[arch]
else:
prov = Provision(arch=arch)
Expand Down
2 changes: 1 addition & 1 deletion Server/bkr/server/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def save(self, **kw):

try:
item.set(kw['value'], valid_from, identity.current.user)
except Exception, msg:
except Exception as msg:
flash(_(u"Failed to save setting: %s" % msg))
raise redirect("/configuration/edit?id=%d" % item.id)

Expand Down
2 changes: 1 addition & 1 deletion Server/bkr/server/controller_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def result_columns(cls,values_checked = None):
if values_checked is not None:
vals_to_set = values_checked
response.simple_cookie['column_values'] = ','.join(values_checked)
elif request.simple_cookie.has_key('column_values'):
elif 'column_values' in request.simple_cookie:
text = request.simple_cookie['column_values'].value
vals_to_set = text.split(',')
else:
Expand Down
7 changes: 5 additions & 2 deletions Server/bkr/server/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
LabInfoForm, myPaginateDataGrid
from bkr.server.xmlrpccontroller import RPCRoot

import six


log = logging.getLogger("bkr.server.controllers")

# This ridiculous hack gets us an HTML5 doctype in our Kid template output.
Expand Down Expand Up @@ -514,7 +517,7 @@ def _systems(self, systems, title, *args, **kw):
try:
systems = XmlHost.from_string('<and>%s</and>' % kw['xmlsearch']).apply_filter(
systems)
except ValueError, e:
except ValueError as e:
raise PlainTextHTTPException(status=400, message=str(e))

if kw.get("systemsearch"):
Expand Down Expand Up @@ -657,7 +660,7 @@ def _view_system_as_html(self, fqdn=None, **kw):
attrs = dict()
options = {}
options['readonly'] = readonly
options['reprovision_distro_tree_id'] = [(dt.id, unicode(dt)) for dt in
options['reprovision_distro_tree_id'] = [(dt.id, six.text_type(dt)) for dt in
system.distro_trees().order_by(Distro.name,
DistroTree.variant,
DistroTree.arch_id)]
Expand Down
15 changes: 9 additions & 6 deletions Server/bkr/server/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from bkr.server.bexceptions import DatabaseLookupError

from bkr.server.model import (OSMajor, OSVersion, Distro, DistroTree,
DistroTag, DistroActivity)
DistroTag, DistroActivity)

import six


__all__ = ['Distros']

Expand Down Expand Up @@ -284,8 +287,8 @@ def filter(self, filter):
distros = distros[:limit]
return [{'distro_id': distro.id,
'distro_name': distro.name,
'distro_version': unicode(distro.osversion),
'distro_tags': [unicode(tag) for tag in distro.tags],
'distro_version': six.text_type(distro.osversion),
'distro_tags': [six.text_type(tag) for tag in distro.tags],
} for distro in distros]

@cherrypy.expose
Expand All @@ -301,7 +304,7 @@ def edit_version(self, name, version):
'RedHatEnterpriseLinuxServer5.6' or 'Fedora14'
:type version: string
"""
distros = Distro.query.filter(Distro.name.like(unicode(name)))
distros = Distro.query.filter(Distro.name.like(six.text_type(name)))
edited = []

os_major = version.split('.')[0]
Expand All @@ -324,8 +327,8 @@ def edit_version(self, name, version):
edited.append('%s' % distro.name)
distro.activity.append(DistroActivity(user=identity.current.user,
service=u'XMLRPC', field_name=u'osversion', action=u'Changed',
old_value=unicode(distro.osversion),
new_value=unicode(osversion)))
old_value=six.text_type(distro.osversion),
new_value=six.text_type(osversion)))
distro.osversion = osversion
return edited

Expand Down
22 changes: 12 additions & 10 deletions Server/bkr/server/distrotrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import urlparse
import cherrypy
import os
from kid import Element
Expand All @@ -16,7 +15,10 @@
LabController, LabControllerDistroTree, DistroTreeActivity, \
Arch, DistroTag
from bkr.server.widgets import TaskSearchForm, myPaginateDataGrid, SearchBar, \
DistroTreeInstallOptionsWidget, DeleteLinkWidgetForm
DistroTreeInstallOptionsWidget, DeleteLinkWidgetForm

import six
from six.moves import urllib
from bkr.server.helpers import make_link
from bkr.server.controller_utilities import Utility, restrict_http_method
from bkr.server.xmlrpccontroller import RPCRoot
Expand Down Expand Up @@ -142,7 +144,7 @@ def yum_config(self, distro_tree_id, *args, **kwargs):
baseurl=%s
enabled=1
gpgcheck=0
''' % (repo.repo_id, repo.repo_id, urlparse.urljoin(base, repo.path)))
''' % (repo.repo_id, repo.repo_id, urllib.parse.urljoin(base, repo.path)))
return '\n'.join(sections)

@expose()
Expand Down Expand Up @@ -229,14 +231,14 @@ def add_distro_urls(distro_tree, lab_controller, urls):
"""
unrecognized_scheme = ''
new_urls_by_scheme = dict(
(urlparse.urlparse(url, scheme=unrecognized_scheme).scheme, url) for url in urls)
(urllib.parse.urlparse(url, scheme=unrecognized_scheme).scheme, url) for url in urls)

if unrecognized_scheme in new_urls_by_scheme:
raise ValueError('URL %s is not absolute' % new_urls_by_scheme[unrecognized_scheme])

for lca in distro_tree.lab_controller_assocs:
if lca.lab_controller == lab_controller:
scheme = urlparse.urlparse(lca.url).scheme
scheme = urllib.parse.urlparse(lca.url).scheme
new_url = new_urls_by_scheme.pop(scheme, None)
# replace old url if it's the same scheme
if new_url is not None and lca.url != new_url:
Expand Down Expand Up @@ -348,12 +350,12 @@ def filter(self, filter):
return [{'distro_tree_id': dt.id,
'distro_id': dt.distro.id,
'distro_name': dt.distro.name,
'distro_osversion': unicode(dt.distro.osversion),
'distro_osmajor' : unicode(dt.distro.osversion.osmajor),
'distro_tags': [unicode(tag) for tag in dt.distro.tags],
'arch': unicode(dt.arch),
'distro_osversion': six.text_type(dt.distro.osversion),
'distro_osmajor' : six.text_type(dt.distro.osversion.osmajor),
'distro_tags': [six.text_type(tag) for tag in dt.distro.tags],
'arch': six.text_type(dt.arch),
'variant': dt.variant,
'images' : [(unicode(image.image_type), image.path) for image in dt.images],
'images' : [(six.text_type(image.image_type), image.path) for image in dt.images],
'kernel_options': dt.kernel_options or u'',
'kernel_options_post': dt.kernel_options_post or u'',
'ks_meta': dt.ks_meta or u'',
Expand Down
Loading

0 comments on commit 6f9d19e

Please sign in to comment.