From 7a488f8f38c2778ab19c5bef41b03aff46565fa9 Mon Sep 17 00:00:00 2001 From: Matej Dujava Date: Fri, 29 Dec 2023 20:59:34 +0100 Subject: [PATCH] Remove cgi dependency Closes: #194 --- .../client/commands/cmd_update_inventory.py | 4 ++-- Client/src/bkr/client/main.py | 4 ++-- Common/bkr/common/helpers.py | 1 - Common/bkr/common/helpers_six.py | 12 ++++++++++++ Common/bkr/common/test_helpers.py | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 Common/bkr/common/helpers_six.py create mode 100644 Common/bkr/common/test_helpers.py diff --git a/Client/src/bkr/client/commands/cmd_update_inventory.py b/Client/src/bkr/client/commands/cmd_update_inventory.py index b1d8b7805..156d23b34 100644 --- a/Client/src/bkr/client/commands/cmd_update_inventory.py +++ b/Client/src/bkr/client/commands/cmd_update_inventory.py @@ -87,7 +87,6 @@ from __future__ import print_function -import cgi import sys from xml.dom.minidom import parseString @@ -95,6 +94,7 @@ from bkr.client import BeakerCommand from bkr.client.task_watcher import watch_tasks +from bkr.common.helpers_six import parse_content_type class Update_Inventory(BeakerCommand): @@ -152,7 +152,7 @@ def run(self, *args, **kwargs): res.raise_for_status() except HTTPError as e: sys.stderr.write('HTTP error: %s, %s\n' % (fqdn, e)) - content_type, _ = cgi.parse_header(e.response.headers.get( + content_type = parse_content_type(e.response.headers.get( 'Content-Type', '')) if content_type == 'text/plain': sys.stderr.write('\t' + diff --git a/Client/src/bkr/client/main.py b/Client/src/bkr/client/main.py index faca452d4..bfb68cb19 100755 --- a/Client/src/bkr/client/main.py +++ b/Client/src/bkr/client/main.py @@ -5,7 +5,6 @@ # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. -import cgi import errno import logging import signal @@ -22,6 +21,7 @@ from bkr.client.command import ClientCommandContainer from bkr.client.command import CommandOptionParser from bkr.common import __version__ +from bkr.common.helpers_six import parse_content_type from bkr.log import log_to_stream __all__ = ( @@ -129,7 +129,7 @@ def main(): except maybe_http_error as e: warn_on_version_mismatch(e.response) sys.stderr.write('HTTP error: %s\n' % e) - content_type, _ = cgi.parse_header(e.response.headers.get('Content-Type', '')) + content_type = parse_content_type(e.response.headers.get('Content-Type', '')) if content_type == 'text/plain': sys.stderr.write(e.response.content.decode('utf-8').rstrip('\n') + '\n') return 1 diff --git a/Common/bkr/common/helpers.py b/Common/bkr/common/helpers.py index f6c5c707a..d701ad8bf 100644 --- a/Common/bkr/common/helpers.py +++ b/Common/bkr/common/helpers.py @@ -277,4 +277,3 @@ def total_seconds(td): represented by the given timedelta. """ return (float(td.microseconds) + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 - diff --git a/Common/bkr/common/helpers_six.py b/Common/bkr/common/helpers_six.py new file mode 100644 index 000000000..68e6f7134 --- /dev/null +++ b/Common/bkr/common/helpers_six.py @@ -0,0 +1,12 @@ + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +def parse_content_type(value): + """ + Return just content type, without options + """ + groups = value.split(';', 1) + return groups[0] diff --git a/Common/bkr/common/test_helpers.py b/Common/bkr/common/test_helpers.py new file mode 100644 index 000000000..956fdaa81 --- /dev/null +++ b/Common/bkr/common/test_helpers.py @@ -0,0 +1,18 @@ +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +import unittest + +from bkr.common.helpers_six import parse_content_type + + +class ParsingContentType(unittest.TestCase): + + def test_ok(self): + self.assertEqual(parse_content_type('type/subtype; charset=utf-8'), 'type/subtype') + self.assertEqual(parse_content_type('type/subtype'), 'type/subtype') + + def test_empty(self): + self.assertEqual(parse_content_type(''), '')