From 16a3fcfabf747820b5d6605ff8d0e155916194a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Gr=C3=B6nlund?= Date: Mon, 18 Nov 2019 09:36:09 +0100 Subject: [PATCH] Handle bytes vs. str error when parsing meta (#683) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In all the cases where meta_exists returns either string data, bytes data or a list, the output needs to be parsed correctly. Signed-off-by: Kristoffer Grönlund --- osc/commandline.py | 2 +- osc/core.py | 39 ++++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index 2b94b142..15645c89 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -7973,7 +7973,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. '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 diff --git a/osc/core.py b/osc/core.py index b1289e48..00f529ac 100644 --- a/osc/core.py +++ b/osc/core.py @@ -3802,6 +3802,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, @@ -3831,19 +3844,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.') @@ -5142,7 +5143,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 @@ -5237,7 +5238,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 @@ -6939,7 +6940,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: @@ -6972,7 +6973,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: @@ -7003,7 +7004,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) @@ -7029,7 +7030,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')