Commit a8419362 authored by Jelle van der Waa's avatar Jelle van der Waa 🚧
Browse files

mirrors: Update mirrorcheck to Python 3

urllib2 has been renamed to urllib and the module layout is changed,
update the code to reflect these changes in Python 3
parent 86f5b0ad
......@@ -11,7 +11,7 @@
from collections import deque
from datetime import datetime, timedelta
from httplib import HTTPException
from http.client import HTTPException
import logging
import os
from pytz import utc
......@@ -24,8 +24,8 @@
import tempfile
from threading import Thread
import types
from Queue import Queue, Empty
import urllib2
from queue import Queue, Empty
import urllib
from django.core.management.base import BaseCommand
from django.db import transaction
......@@ -119,17 +119,17 @@ def check_mirror_url(mirror_url, location, timeout):
logger.info("checking URL %s", url)
log = MirrorLog(url=mirror_url, check_time=now(), location=location)
headers = {'User-Agent': 'archweb/1.0'}
req = urllib2.Request(url, None, headers)
req = urllib.request.Request(url, None, headers)
start = time.time()
try:
result = urllib2.urlopen(req, timeout=timeout)
result = urllib.request.urlopen(req, timeout=timeout)
data = result.read()
result.close()
end = time.time()
parse_lastsync(log, data)
log.duration = end - start
logger.debug("success: %s, %.2f", url, log.duration)
except urllib2.HTTPError as e:
except urllib.error.HTTPError as e:
if e.code == 404:
# we have a duration, just not a success
end = time.time()
......@@ -137,7 +137,7 @@ def check_mirror_url(mirror_url, location, timeout):
log.is_success = False
log.error = str(e)
logger.debug("failed: %s, %s", url, log.error)
except urllib2.URLError as e:
except urllib.error.URLError as e:
log.is_success = False
log.error = e.reason
if isinstance(e.reason, types.StringTypes) and \
......
......@@ -21,8 +21,8 @@ def setUp(self):
def tearDown(self):
self.mirror_url.delete()
@mock.patch('urllib2.Request')
@mock.patch('urllib2.urlopen')
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_invalid(self, urlopen, Request):
urlopen.return_value.read.return_value = 'data'
Request.get_host.return_value = 'archlinux.org'
......@@ -33,8 +33,8 @@ def test_invalid(self, urlopen, Request):
self.assertNotEqual(mirrorlog.error, '')
self.assertEqual(mirrorlog.is_success, False)
@mock.patch('urllib2.Request')
@mock.patch('urllib2.urlopen')
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_valid(self, urlopen, Request):
urlopen.return_value.read.return_value = str(int(time.time()))
Request.get_host.return_value = 'archlinux.org'
......@@ -45,8 +45,8 @@ def test_valid(self, urlopen, Request):
self.assertEqual(mirrorlog.error, '')
self.assertEqual(mirrorlog.is_success, True)
@mock.patch('urllib2.Request')
@mock.patch('urllib2.urlopen')
@mock.patch('urllib.request.Request')
@mock.patch('urllib.request.urlopen')
def test_valid(self, urlopen, Request):
urlopen.return_value.read.return_value = str(int(time.time()))
Request.get_host.return_value = 'archlinux.org'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment