Skip to content

Commit

Permalink
Merge branch 'handle-string-conversion' of https://github.com/krig/osc
Browse files Browse the repository at this point in the history
Add core.parse_meta_to_string helper to work around the insane
implementation of core.meta_exists. Since core.meta_exists may return
a list of bytes, a str, a list of str etc., we ultimately convert the
data to str before passing it ET.fromstring(...).

In case of bytes, the explicit decoding is OK because it is assumed to
be a valid utf-8 encoding (the data represents an xml).

Note: at the moment core.parse_meta_to_string is also called even if it
is not necessary (it is only necessary if the "create" parameter of a
corresponding core.meta_exists call is True).

Note 2: this is just a temporary workaround and, eventually, we will make
the implementation of core.meta_exists more reasonable. When doing so,
we will also remove "public" function core.parse_meta_to_string again.
(Yes, this breaks API but the core.meta_exists change will also break the
API in some sense - so that's OK.)
  • Loading branch information
marcus-h committed Feb 7, 2020
2 parents fb9e64c + 16a3fcf commit 32859d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7921,7 +7921,7 @@ def do_importsrcpkg(self, subcmd, opts, srpm):
'name': pac,
'user': user}), apiurl=apiurl)
if data:
data = ET.fromstring(''.join(data))
data = ET.fromstring(parse_meta_to_string(data))
data.find('title').text = ''.join(title)
data.find('description').text = ''.join(descr)
data.find('url').text = url
Expand Down
39 changes: 20 additions & 19 deletions osc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3808,6 +3808,19 @@ def make_meta_url(metatype, path_args=None, apiurl=None, force=False, remove_lin

return makeurl(apiurl, [path], query)

def parse_meta_to_string(data):
"""
Converts the output of meta_exists into a string value
"""
# data can be a bytes object, a list with strings, a list with bytes, just a string.
# So we need the following even if it is ugly.
if sys.version_info >= (3, 0):
if isinstance(data, bytes):
data = decode_it(data)
elif isinstance(data, list):
data = decode_list(data)
return ''.join(data)


def edit_meta(metatype,
path_args=None,
Expand Down Expand Up @@ -3837,19 +3850,7 @@ def edit_meta(metatype,
if metatype == 'pkg':
# check if the package is a link to a different project
project, package = path_args
# data can be a bytes object, a list with strings, a list with bytes, just a string.
# So we need the following even if it is ugly.
if sys.version_info >= (3, 0):
if isinstance(data, bytes):
data = decode_it(data)
orgprj = ET.fromstring(''.join(data)).get('project')
elif isinstance(data, list):
decode_data = decode_list(data)
orgprj = ET.fromstring(''.join(decode_data)).get('project')
else:
orgprj = ET.fromstring(''.join(data)).get('project')
else:
orgprj = ET.fromstring(''.join(data)).get('project')
orgprj = ET.fromstring(parse_meta_to_string(data)).get('project')

if orgprj is not None and unquote(project) != orgprj:
print('The package is linked from a different project.')
Expand Down Expand Up @@ -5134,7 +5135,7 @@ def link_pac(src_project, src_package, dst_project, dst_package, force, rev='',
path_args=(quote_plus(dst_project), quote_plus(dst_package)),
template_args=None,
create_new=False, apiurl=apiurl)
root = ET.fromstring(b''.join(dst_meta))
root = ET.fromstring(parse_meta_to_string(dst_meta))
if root.get('project') != dst_project:
# The source comes from a different project via a project link, we need to create this instance
meta_change = True
Expand Down Expand Up @@ -5229,7 +5230,7 @@ def aggregate_pac(src_project, src_package, dst_project, dst_package, repo_map =
path_args=(quote_plus(dst_project), quote_plus(dst_package)),
template_args=None,
create_new=False, apiurl=apiurl)
root = ET.fromstring(b''.join(dst_meta))
root = ET.fromstring(parse_meta_to_string(dst_meta))
if root.get('project') != dst_project:
# The source comes from a different project via a project link, we need to create this instance
meta_change = True
Expand Down Expand Up @@ -6947,7 +6948,7 @@ def addPerson(apiurl, prj, pac, user, role="maintainer"):
create_new=False)

if data and get_user_meta(apiurl, user) != None:
root = ET.fromstring(b''.join(data))
root = ET.fromstring(parse_meta_to_string(data))
found = False
for person in root.getiterator('person'):
if person.get('userid') == user and person.get('role') == role:
Expand Down Expand Up @@ -6980,7 +6981,7 @@ def delPerson(apiurl, prj, pac, user, role="maintainer"):
template_args=None,
create_new=False)
if data and get_user_meta(apiurl, user) != None:
root = ET.fromstring(''.join(data))
root = ET.fromstring(parse_meta_to_string(data))
found = False
for person in root.getiterator('person'):
if person.get('userid') == user and person.get('role') == role:
Expand Down Expand Up @@ -7011,7 +7012,7 @@ def setBugowner(apiurl, prj, pac, user=None, group=None):
group=user.replace('group:', '')
user=None
if data:
root = ET.fromstring(b''.join(data))
root = ET.fromstring(parse_meta_to_string(data))
for group_element in root.getiterator('group'):
if group_element.get('role') == "bugowner":
root.remove(group_element)
Expand All @@ -7037,7 +7038,7 @@ def setDevelProject(apiurl, prj, pac, dprj, dpkg=None):
create_new=False)

if data and show_project_meta(apiurl, dprj) != None:
root = ET.fromstring(''.join(data))
root = ET.fromstring(parse_meta_to_string(data))
if not root.find('devel') != None:
ET.SubElement(root, 'devel')
elem = root.find('devel')
Expand Down

0 comments on commit 32859d6

Please sign in to comment.