From cb0b0bf5d5fe51653a80df0212bd4b60b2d1b8ee Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Tue, 15 Jul 2025 14:31:28 +0200 Subject: [PATCH] Move remaining imports in osc.babysitter into try-except block --- osc/babysitter.py | 156 ++++++++++++++++++++++++---------------------- 1 file changed, 83 insertions(+), 73 deletions(-) diff --git a/osc/babysitter.py b/osc/babysitter.py index 059adb1b..4ec44760 100644 --- a/osc/babysitter.py +++ b/osc/babysitter.py @@ -10,10 +10,6 @@ import signal import ssl import sys import traceback -from http.client import HTTPException, BadStatusLine -from urllib.error import URLError, HTTPError - -import urllib3.exceptions from . import commandline from . import oscerr @@ -23,23 +19,6 @@ from .util.cpio import CpioError from .util.helper import decode_it from .util.packagequery import PackageError -try: - # import as RPMError because the class "error" is too generic - # pylint: disable=E0611 - from rpm import error as RPMError -except: - # if rpm-python isn't installed (we might be on a debian system): - class RPMError(Exception): - pass - - -try: - from keyring.errors import KeyringLocked -except ImportError: - # python-keyring is not installed - class KeyringLocked(Exception): - pass - try: from argcomplete import ArgcompleteException @@ -63,6 +42,8 @@ for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': def run(prg, argv=None): + ignore_exceptions = (ArgcompleteException, SystemExit) + try: try: # we haven't parsed options yet, that's why we rely on argv directly @@ -72,7 +53,7 @@ def run(prg, argv=None): # here we actually run the program prg.main(argv) return 0 - except (ArgcompleteException, SystemExit): + except ignore_exceptions: # no need to handle traceback or post_mortem options -> don't load osc.conf to improve osc responsivness raise except: @@ -108,49 +89,6 @@ def run(prg, argv=None): print(e, file=sys.stderr) except oscerr.NoWorkingCopy as e: print(e, file=sys.stderr) - except HTTPError as e: - from . import _private - - print('Server returned an error:', e, file=sys.stderr) - if hasattr(e, 'osc_msg'): - print(e.osc_msg, file=sys.stderr) - - try: - body = e.read() - except AttributeError: - body = '' - - output.print_msg(e.hdrs, print_to="debug") - output.print_msg(body, print_to="debug") - - if e.code in [400, 403, 404, 500]: - if b'' in body: - msg = body.split(b'')[1] - msg = msg.split(b'')[0] - msg = _private.api.xml_unescape(msg) - print(decode_it(msg), file=sys.stderr) - if e.code >= 500 and e.code <= 599: - print(f'\nRequest: {e.filename}') - print('Headers:') - for h, v in e.hdrs.items(): - if h != 'Set-Cookie': - print(f"{h}: {v}") - - except BadStatusLine as e: - print('Server returned an invalid response:', e, file=sys.stderr) - print(e.line, file=sys.stderr) - except HTTPException as e: - print(e, file=sys.stderr) - except URLError as e: - msg = 'Failed to reach a server' - if hasattr(e, '_osc_host_port'): - msg += f' ({e._osc_host_port})' - msg += ':\n' - print(msg, e.reason, file=sys.stderr) - except ssl.SSLError as e: - if 'tlsv1' in str(e): - print('The python on this system or the server does not support TLSv1.2', file=sys.stderr) - print("SSL Error:", e, file=sys.stderr) except OSError as e: # ignore broken pipe if e.errno != errno.EPIPE: @@ -187,20 +125,92 @@ def run(prg, argv=None): print(str(e), file=sys.stderr) except PackageError as e: print(f'{e.fname}:', e.msg, file=sys.stderr) - except RPMError as e: - print(e, file=sys.stderr) - except KeyringLocked as e: - print(e, file=sys.stderr) except oscerr.CertVerificationError as e: print(e, file=sys.stderr) - except urllib3.exceptions.MaxRetryError as e: - print(e.reason, file=sys.stderr) - except urllib3.exceptions.ProtocolError as e: - print(e.args[0], file=sys.stderr) except CpioError as e: print(e, file=sys.stderr) except oscerr.OscBaseError as e: print('*** Error:', e, file=sys.stderr) + except ignore_exceptions: + raise + except: + # handling exceptions thrown outside osc + # we're doing this to postpone loading all 3rd party modules any time osc is executed + + from http.client import HTTPException, BadStatusLine + from urllib.error import URLError, HTTPError + import urllib3.exceptions + + try: + # import as RPMError because the class "error" is too generic + # pylint: disable=E0611 + from rpm import error as RPMError + except: + # if rpm-python isn't installed (we might be on a debian system): + class RPMError(Exception): + pass + + try: + from keyring.errors import KeyringLocked + except ImportError: + # python-keyring is not installed + class KeyringLocked(Exception): + pass + + try: + raise + except RPMError as e: + print(e, file=sys.stderr) + except KeyringLocked as e: + print(e, file=sys.stderr) + except HTTPError as e: + from . import _private + + print('Server returned an error:', e, file=sys.stderr) + if hasattr(e, 'osc_msg'): + print(e.osc_msg, file=sys.stderr) + + try: + body = e.read() + except AttributeError: + body = '' + + output.print_msg(e.hdrs, print_to="debug") + output.print_msg(body, print_to="debug") + + if e.code in [400, 403, 404, 500]: + if b'' in body: + msg = body.split(b'')[1] + msg = msg.split(b'')[0] + msg = _private.api.xml_unescape(msg) + print(decode_it(msg), file=sys.stderr) + if e.code >= 500 and e.code <= 599: + print(f'\nRequest: {e.filename}') + print('Headers:') + for h, v in e.hdrs.items(): + if h != 'Set-Cookie': + print(f"{h}: {v}") + except BadStatusLine as e: + print('Server returned an invalid response:', e, file=sys.stderr) + print(e.line, file=sys.stderr) + except HTTPException as e: + print(e, file=sys.stderr) + except URLError as e: + msg = 'Failed to reach a server' + if hasattr(e, '_osc_host_port'): + msg += f' ({e._osc_host_port})' + msg += ':\n' + print(msg, e.reason, file=sys.stderr) + except ssl.SSLError as e: + if 'tlsv1' in str(e): + print('The python on this system or the server does not support TLSv1.2', file=sys.stderr) + print("SSL Error:", e, file=sys.stderr) + except urllib3.exceptions.MaxRetryError as e: + print(e.reason, file=sys.stderr) + except urllib3.exceptions.ProtocolError as e: + print(e.args[0], file=sys.stderr) + except: + raise from . import core as osc_core