Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1a1b4e

Browse files
committed
Enable debugging for urllib
As of today, using -d or --debug does not offer debugging for the requests made using urllib. This is needed for debugging connection issues, in particular when there are redirects involved. We need this monkey patching because between Python 3.5.2 [1] and 3.11.X, Python stopped respecting http.client.HTTPConnection.debuglevel, and the fix [2] seems got only available starting with Python 3.12 When we start using only 3.12 and later, we can rollback this and just change the code to set http.client.HTTPConnection.debuglevel to 1, when debugging is enabled [1] https://stackoverflow.com/a/74416575 [2] python/cpython#99353 BEFORE: $ rm -rf test/; python3 -m obs_maven.core -d ../spacewalk/java/buildconf/ivy/obs-maven-config.yaml test -a antlr4-runtime DEBUG:root:Reading configuration INFO:root:Processing artifact antlr4-runtime DEBUG:root:Parsing https://download.opensuse.org/repositories/systemsmanagement:/Uyuni:/Master/openSUSE_Leap_15.6/repodata/repomd.xml DEBUG:root:Loading RPMs from cache file: .obs-to-maven-cache/Uyuni/63cc3f113c0efce8106bf832c89795ff5111983a9e4bd5cea7742462e48c6221-primary.xml.gz.data DEBUG:root:package mtime: 1775727845, [] INFO:root:Downloading /tmp/obsmvn-rwdr_yt4/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm DEBUG:root:Getting binary from: https://download.opensuse.org/repositories/systemsmanagement:/Uyuni:/Master/openSUSE_Leap_15.6/noarch/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm DEBUG:root:not linked: /usr/share/doc/packages/antlr4-java /usr/share/doc/packages/antlr4-java/CHANGES.txt /usr/share/doc/packages/antlr4-java/README.md /usr/share/java/antlr4 /usr/share/java/antlr4/antlr4-runtime.jar /usr/share/licenses/antlr4-java /usr/share/licenses/antlr4-java/LICENSE.txt /usr/share/maven-metadata/antlr4-antlr4-runtime.xml /usr/share/maven-poms/antlr4 /usr/share/maven-poms/antlr4/antlr4-master.pom /usr/share/maven-poms/antlr4/antlr4-runtime.pom DEBUG:root:full pattern: ^/usr/.*/antlr4-runtime[^/]*\.jar$ INFO:root:extracting /usr/share/java/antlr4/antlr4-runtime.jar to /tmp/obsmvn-rwdr_yt4/antlr4-runtime.jar INFO:root:deploying /tmp/obsmvn-rwdr_yt4/antlr4-runtime.jar to test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.jar DEBUG:root:Setting mtime 1775727845 on test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.jar DEBUG:root:Computing test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.jar.sha1 DEBUG:root:Computing test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.pom.sha1 AFTER: $ rm -rf test/; python3 -m obs_maven.core -d ../spacewalk/java/buildconf/ivy/obs-maven-config.yaml test -a antlr4-runtime DEBUG:root:Reading configuration INFO:root:Processing artifact antlr4-runtime DEBUG:root:Parsing https://download.opensuse.org/repositories/systemsmanagement:/Uyuni:/Master/openSUSE_Leap_15.6/repodata/repomd.xml DEBUG:root:HTTPS Request: GET /repositories/systemsmanagement:/Uyuni:/Master/openSUSE_Leap_15.6/repodata/repomd.xml HTTP/1.1 DEBUG:root:HTTPS Response: 200 OK DEBUG:root:Loading RPMs from cache file: .obs-to-maven-cache/Uyuni/63cc3f113c0efce8106bf832c89795ff5111983a9e4bd5cea7742462e48c6221-primary.xml.gz.data DEBUG:root:package mtime: 1775727845, [] INFO:root:Downloading /tmp/obsmvn-f2s6lyau/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm DEBUG:root:Getting binary from: https://download.opensuse.org/repositories/systemsmanagement:/Uyuni:/Master/openSUSE_Leap_15.6/noarch/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm DEBUG:root:HTTPS Request: GET /repositories/systemsmanagement:/Uyuni:/Master/openSUSE_Leap_15.6/noarch/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm HTTP/1.1 DEBUG:root:HTTPS Response: 302 Found DEBUG:root:HTTPS Redirect to: https://ftp.gwdg.de/pub/opensuse/repositories/systemsmanagement%3A/Uyuni%3A/Master/openSUSE_Leap_15.6/noarch/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm DEBUG:root:HTTPS Request: GET /pub/opensuse/repositories/systemsmanagement%3A/Uyuni%3A/Master/openSUSE_Leap_15.6/noarch/antlr4-java-4.13.0-241000.1.12.uyuni5.noarch.rpm HTTP/1.1 DEBUG:root:HTTPS Response: 200 OK DEBUG:root:not linked: /usr/share/doc/packages/antlr4-java /usr/share/doc/packages/antlr4-java/CHANGES.txt /usr/share/doc/packages/antlr4-java/README.md /usr/share/java/antlr4 /usr/share/java/antlr4/antlr4-runtime.jar /usr/share/licenses/antlr4-java /usr/share/licenses/antlr4-java/LICENSE.txt /usr/share/maven-metadata/antlr4-antlr4-runtime.xml /usr/share/maven-poms/antlr4 /usr/share/maven-poms/antlr4/antlr4-master.pom /usr/share/maven-poms/antlr4/antlr4-runtime.pom DEBUG:root:full pattern: ^/usr/.*/antlr4-runtime[^/]*\.jar$ INFO:root:extracting /usr/share/java/antlr4/antlr4-runtime.jar to /tmp/obsmvn-f2s6lyau/antlr4-runtime.jar INFO:root:deploying /tmp/obsmvn-f2s6lyau/antlr4-runtime.jar to test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.jar DEBUG:root:Setting mtime 1775727845 on test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.jar DEBUG:root:Computing test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.jar.sha1 DEBUG:root:Computing test/suse/antlr4-runtime/4.13.0/antlr4-runtime-4.13.0.pom.sha1
1 parent 39e05b3 commit d1a1b4e

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

obs_maven/core.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
# You should have received a copy of the GNU General Public License
1717

1818
import argparse
19+
import http.client
1920
import logging
2021
import os
2122
import os.path
2223
import shutil
2324
import sys
2425
import tempfile
26+
import urllib.request
2527
import yaml
2628
import xml.etree.ElementTree as ET
2729

@@ -32,6 +34,56 @@
3234
logging.basicConfig(level=logging.INFO)
3335

3436

37+
class _DebugConnectionMixin:
38+
"""Mixin class that adds debug logging to HTTP connections"""
39+
_protocol_name = "HTTP"
40+
41+
def send(self, data):
42+
if isinstance(data, bytes):
43+
# Log the first line (request line) for readability
44+
first_line = data.split(b'\r\n')[0].decode('ascii', errors='replace')
45+
logging.debug(f"{self._protocol_name} Request: {first_line}")
46+
return super().send(data)
47+
48+
def getresponse(self, *args, **kwargs):
49+
response = super().getresponse(*args, **kwargs)
50+
logging.debug(f"{self._protocol_name} Response: {response.status} {response.reason}")
51+
if 300 <= response.status < 400:
52+
location = response.getheader('Location', 'N/A')
53+
logging.debug(f"{self._protocol_name} Redirect to: {location}")
54+
return response
55+
56+
57+
class _DebugHTTPConnection(_DebugConnectionMixin, http.client.HTTPConnection):
58+
"""HTTPConnection subclass that logs debug info through Python logging"""
59+
_protocol_name = "HTTP"
60+
61+
def __init__(self, *args, **kwargs):
62+
super().__init__(*args, **kwargs)
63+
self.set_debuglevel(1)
64+
65+
66+
class _DebugHTTPSConnection(_DebugConnectionMixin, http.client.HTTPSConnection):
67+
"""HTTPSConnection subclass that logs debug info through Python logging"""
68+
_protocol_name = "HTTPS"
69+
70+
def __init__(self, *args, **kwargs):
71+
super().__init__(*args, **kwargs)
72+
self.set_debuglevel(1)
73+
74+
75+
class HTTPDebugHandler(urllib.request.HTTPHandler):
76+
"""Custom HTTP handler that enables debug output for urllib requests"""
77+
def http_open(self, req):
78+
return self.do_open(_DebugHTTPConnection, req)
79+
80+
81+
class HTTPSDebugHandler(urllib.request.HTTPSHandler):
82+
"""Custom HTTPS handler that enables debug output for urllib requests"""
83+
def https_open(self, req):
84+
return self.do_open(_DebugHTTPSConnection, req)
85+
86+
3587
class Configuration:
3688
def __init__(self, config_path, repo, cache_path, allowed_artifacts):
3789
data = {}
@@ -107,6 +159,10 @@ def main():
107159
args = parser.parse_args()
108160

109161
logging.getLogger().setLevel(args.loglevel)
162+
if args.loglevel == logging.DEBUG:
163+
# Enable HTTP debugging by installing custom handlers
164+
opener = urllib.request.build_opener(HTTPDebugHandler(), HTTPSDebugHandler())
165+
urllib.request.install_opener(opener)
110166
logging.debug("Reading configuration")
111167
config = Configuration(args.config, args.out, args.cache, args.allowed_artifacts)
112168
tmp = tempfile.mkdtemp(prefix="obsmvn-")

0 commit comments

Comments
 (0)