Skip to content

Commit

Permalink
Remove cgi dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mdujava committed Dec 31, 2023
1 parent 275411e commit 7a488f8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Client/src/bkr/client/commands/cmd_update_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@

from __future__ import print_function

import cgi
import sys
from xml.dom.minidom import parseString

from requests.exceptions import HTTPError

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):
Expand Down Expand Up @@ -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' +
Expand Down
4 changes: 2 additions & 2 deletions Client/src/bkr/client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__ = (
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Common/bkr/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

12 changes: 12 additions & 0 deletions Common/bkr/common/helpers_six.py
Original file line number Diff line number Diff line change
@@ -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]
18 changes: 18 additions & 0 deletions Common/bkr/common/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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(''), '')

0 comments on commit 7a488f8

Please sign in to comment.