aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <vyanitskiy@sysmocom.de>2022-12-06 04:00:02 +0700
committerVadim Yanitskiy <vyanitskiy@sysmocom.de>2022-12-06 04:04:03 +0700
commite0202abcc13896e2e1ff7d722e66320a154e2f65 (patch)
tree84ffb7fe100621bcb5322da15cb4beebf05708a0
parenta2bd862707eba25233eba3b287266aa893995179 (diff)
libgsmhr/fetch_sources.py: convert to Python 3
* Use https://docs.python.org/3/library/logging.html ** Add more logging, use DEBUG by default * Use https://docs.python.org/3/library/urllib.request.html ** Do not send custom HTTP headers (not needed anymore) * Use https://docs.python.org/3/library/io.html Change-Id: I05d25343b4453550ab07b948cfaa1048b0f58067 Related: OS#3400
-rwxr-xr-xlibgsmhr/fetch_sources.py46
1 files changed, 25 insertions, 21 deletions
diff --git a/libgsmhr/fetch_sources.py b/libgsmhr/fetch_sources.py
index 6fb1b8e..178125c 100755
--- a/libgsmhr/fetch_sources.py
+++ b/libgsmhr/fetch_sources.py
@@ -1,35 +1,30 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
-import urllib2
+import logging as log
+import urllib.request
+import io
import os
import sys
import zipfile
-try:
- import cStringIO as StringIO
-except:
- import StringIO
+URL = "http://www.3gpp.org/ftp/Specs/archive/06_series/06.06/0606-421.zip"
-SRC = "http://www.3gpp.org/ftp/Specs/archive/06_series/06.06/0606-421.zip"
-HDR = {
- "User-Agent": "Mozilla/5.0 (X11; ArchLinux; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0",
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
-}
+def get_zipfile(data: bytes) -> zipfile.ZipFile:
+ return zipfile.ZipFile(io.BytesIO(data), 'r')
-def get_zipfile(data):
- return zipfile.ZipFile(StringIO.StringIO(data))
-
-def get_subfile_data(data, filename):
+def get_subfile_data(data: bytes, filename: str):
+ log.debug('Unpacking \'%s\'', filename)
z = get_zipfile(data)
return z.read(filename)
def process_file(z, e):
- fh = open(os.path.basename(e.filename.lower()), 'w')
- d = z.read(e).replace('\r','')
+ log.debug('Processing file \'%s\'', e.filename.lower())
+ fh = open(os.path.basename(e.filename.lower()), 'wb')
+ d = z.read(e).replace(b'\r', b'')
fh.write(d)
fh.close()
@@ -38,7 +33,7 @@ def main(*args):
# Args
if len(args) != 2:
- print "Usage: %s target_dir" % args[0]
+ print("Usage: %s target_dir" % args[0])
return
tgt = args[1]
@@ -49,9 +44,16 @@ def main(*args):
os.chdir(tgt)
# Get the original data
- req = urllib2.Request(SRC, headers = HDR)
- u = urllib2.urlopen(req)
- d = u.read()
+ log.info('Requesting file: %s', URL)
+ with urllib.request.urlopen(URL) as response:
+ log.debug('Response code: %d', response.code)
+ assert response.code == 200
+
+ for h in ('Last-Modified', 'Content-Type', 'Content-Length'):
+ log.debug('%s: %s', h, response.getheader(h))
+
+ log.info('Downloading %d bytes...', response.length)
+ d = response.read()
# Get DISK.zip
d = get_subfile_data(d, 'DISK.zip')
@@ -67,5 +69,7 @@ def main(*args):
process_file(z, e)
+log.basicConfig(format='[%(levelname)s] %(filename)s:%(lineno)d %(message)s', level=log.DEBUG)
+
if __name__ == '__main__':
main(*sys.argv)