From 4855946a5e40747a4ccecf17ba80c8bdbdd8c969 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Sat, 9 Nov 2013 23:30:55 +0100 Subject: get-bugzilla-attachments-by-mimetype: make this run on Python 3 Change-Id: I27cf30c62122ea191c852a1a298a40ef64d35ba9 --- bin/get-bugzilla-attachments-by-mimetype | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'bin/get-bugzilla-attachments-by-mimetype') diff --git a/bin/get-bugzilla-attachments-by-mimetype b/bin/get-bugzilla-attachments-by-mimetype index 100e78eb1994..994e0839811c 100755 --- a/bin/get-bugzilla-attachments-by-mimetype +++ b/bin/get-bugzilla-attachments-by-mimetype @@ -18,13 +18,19 @@ # #where X is the n'th attachment of that type in the bug -import urllib import feedparser import base64 import re import os, os.path import sys -import xmlrpclib +try: + from urllib.request import urlopen +except: + from urllib import urlopen +try: + import xmlrpc.client as xmlrpclib +except: + import xmlrpclib from xml.dom import minidom from xml.sax.saxutils import escape @@ -32,7 +38,7 @@ def urlopen_retry(url): maxretries = 3 for i in range(maxretries + 1): try: - return urllib.urlopen(url) + return urlopen(url) except IOError as e: print("caught IOError: " + e) if maxretries == i: @@ -67,7 +73,7 @@ def get_from_bug_url_via_xml(url, mimetype, prefix, suffix): download = suffix + '/' +prefix + id + '-' + str(attachmentid) + '.' + suffix print('downloading as ' + download) - f = open(download, 'w') + f = open(download, 'wb') f.write(base64.b64decode(node.firstChild.nodeValue)) f.close() break @@ -98,7 +104,11 @@ def get_novell_bug_via_xml(url, mimetype, prefix, suffix): continue print(" mimetype is") - remoteMime = handle.info().gettype() + info = handle.info() + if info.get_content_type: + remoteMime = info.get_content_type() + else: + remoteMime = info.gettype() print(remoteMime) if remoteMime != mimetype: print("skipping") @@ -106,11 +116,15 @@ def get_novell_bug_via_xml(url, mimetype, prefix, suffix): download = suffix + '/' + prefix + id + '-' + str(attachmentid) + '.' + suffix print('downloading as ' + download) - f = open(download, 'w') + f = open(download, 'wb') f.write(handle.read()) f.close() def get_through_rpc_query(rpcurl, showurl, mimetype, prefix, suffix): + try: + os.mkdir(suffix) + except: + pass try: proxy = xmlrpclib.ServerProxy(rpcurl) query = dict() @@ -144,6 +158,8 @@ def get_through_rss_query_url(url, mimetype, prefix, suffix): for entry in d['entries']: try: get_bug_function(entry['id'], mimetype, prefix, suffix) + except KeyboardInterrupt: + raise # Ctrl+C should work except: print(entry['id'] + " failed: " + sys.exc_info()[0]) pass -- cgit