Change in gapk[master]: libgsmhr/fetch_sources.py: convert to Python 3
fixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/gapk/+/30482 ) Change subject: libgsmhr/fetch_sources.py: convert to Python 3 ...................................................................... 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 --- M libgsmhr/fetch_sources.py 1 file changed, 26 insertions(+), 22 deletions(-) git pull ssh://gerrit.osmocom.org:29418/gapk refs/changes/82/30482/1 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 @@ # 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 @@ 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 @@ process_file(z, e) +log.basicConfig(format='[%(levelname)s] %(filename)s:%(lineno)d %(message)s', level=log.DEBUG) + if __name__ == '__main__': main(*sys.argv) -- To view, visit https://gerrit.osmocom.org/c/gapk/+/30482 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: gapk Gerrit-Branch: master Gerrit-Change-Id: I05d25343b4453550ab07b948cfaa1048b0f58067 Gerrit-Change-Number: 30482 Gerrit-PatchSet: 1 Gerrit-Owner: fixeria <vyanitskiy@sysmocom.de> Gerrit-MessageType: newchange
participants (1)
-
fixeria