Kaydet (Commit) 1b82dba6 authored tarafından Michael Stahl's avatar Michael Stahl

get-bugzilla-attachments-by-mimetype: port to Python 3 syntax

Change-Id: I928eb1baa7390301036585d84895f44eb4c38d20
üst 5c6e934b
...@@ -34,39 +34,39 @@ def urlopen_retry(url): ...@@ -34,39 +34,39 @@ def urlopen_retry(url):
try: try:
return urllib.urlopen(url) return urllib.urlopen(url)
except IOError as e: except IOError as e:
print "caught IOError: ", e print("caught IOError: " + e)
if maxretries == i: if maxretries == i:
raise raise
print "retrying..." print("retrying...")
def get_from_bug_url_via_xml(url, mimetype, prefix, suffix): def get_from_bug_url_via_xml(url, mimetype, prefix, suffix):
id = url.rsplit('=', 2)[1] id = url.rsplit('=', 2)[1]
print "id is", prefix, id, suffix print("id is " + prefix + id + " " + suffix)
if os.path.isfile(suffix + '/' + prefix + id + '-1.' + suffix): if os.path.isfile(suffix + '/' + prefix + id + '-1.' + suffix):
print "assuming", id, "is up to date" print("assuming " + id + " is up to date")
else: else:
print "parsing", id print("parsing", id)
sock = urlopen_retry(url+"&ctype=xml") sock = urlopen_retry(url+"&ctype=xml")
dom = minidom.parse(sock) dom = minidom.parse(sock)
sock.close() sock.close()
attachmentid=0 attachmentid=0
for attachment in dom.getElementsByTagName('attachment'): for attachment in dom.getElementsByTagName('attachment'):
attachmentid += 1 attachmentid += 1
print " mimetype is", print(" mimetype is")
for node in attachment.childNodes: for node in attachment.childNodes:
if node.nodeName == 'type': if node.nodeName == 'type':
print node.firstChild.nodeValue, print(node.firstChild.nodeValue)
if node.firstChild.nodeValue.lower() != mimetype.lower(): if node.firstChild.nodeValue.lower() != mimetype.lower():
print 'skipping' print('skipping')
break break
elif node.nodeName == 'data': elif node.nodeName == 'data':
# check if attachment is deleted (i.e. https://bugs.kde.org/show_bug.cgi?id=53343&ctype=xml) # check if attachment is deleted (i.e. https://bugs.kde.org/show_bug.cgi?id=53343&ctype=xml)
if not node.firstChild: if not node.firstChild:
print 'deleted attachment, skipping' print('deleted attachment, skipping')
continue continue
download = suffix + '/' +prefix + id + '-' + str(attachmentid) + '.' + suffix download = suffix + '/' +prefix + id + '-' + str(attachmentid) + '.' + suffix
print 'downloading as', download print('downloading as ' + download)
f = open(download, 'w') f = open(download, 'w')
f.write(base64.b64decode(node.firstChild.nodeValue)) f.write(base64.b64decode(node.firstChild.nodeValue))
f.close() f.close()
...@@ -74,11 +74,11 @@ def get_from_bug_url_via_xml(url, mimetype, prefix, suffix): ...@@ -74,11 +74,11 @@ def get_from_bug_url_via_xml(url, mimetype, prefix, suffix):
def get_novell_bug_via_xml(url, mimetype, prefix, suffix): def get_novell_bug_via_xml(url, mimetype, prefix, suffix):
id = url.rsplit('=', 2)[1] id = url.rsplit('=', 2)[1]
print "id is", prefix, id, suffix print("id is " + prefix + id + " " + suffix)
if os.path.isfile(suffix + '/' + prefix + id + '-1.' + suffix): if os.path.isfile(suffix + '/' + prefix + id + '-1.' + suffix):
print "assuming", id, "is up to date" print("assuming " + id + " is up to date")
else: else:
print "parsing", id print("parsing " + id)
sock = urlopen_retry(url+"&ctype=xml") sock = urlopen_retry(url+"&ctype=xml")
dom = minidom.parse(sock) dom = minidom.parse(sock)
sock.close() sock.close()
...@@ -94,18 +94,18 @@ def get_novell_bug_via_xml(url, mimetype, prefix, suffix): ...@@ -94,18 +94,18 @@ def get_novell_bug_via_xml(url, mimetype, prefix, suffix):
realAttachmentId = match.group(1) realAttachmentId = match.group(1)
handle = urlopen_retry(novellattach + realAttachmentId) handle = urlopen_retry(novellattach + realAttachmentId)
if not handle: if not handle:
print "attachment %s is not accessible", realAttachmentId print("attachment %s is not accessible" % realAttachmentId)
continue continue
print " mimetype is", print(" mimetype is")
remoteMime = handle.info().gettype() remoteMime = handle.info().gettype()
print remoteMime, print(remoteMime)
if remoteMime != mimetype: if remoteMime != mimetype:
print "skipping" print("skipping")
continue continue
download = suffix + '/' + prefix + id + '-' + str(attachmentid) + '.' + suffix download = suffix + '/' + prefix + id + '-' + str(attachmentid) + '.' + suffix
print 'downloading as', download print('downloading as ' + download)
f = open(download, 'w') f = open(download, 'w')
f.write(handle.read()) f.write(handle.read())
f.close() f.close()
...@@ -121,14 +121,14 @@ def get_through_rpc_query(rpcurl, showurl, mimetype, prefix, suffix): ...@@ -121,14 +121,14 @@ def get_through_rpc_query(rpcurl, showurl, mimetype, prefix, suffix):
query['value0-0-0']=mimetype query['value0-0-0']=mimetype
result = proxy.Bug.search(query) result = proxy.Bug.search(query)
bugs = result['bugs'] bugs = result['bugs']
print len(bugs), 'bugs to process' print(str(len(bugs)) + ' bugs to process')
for bug in bugs: for bug in bugs:
url = showurl + str(bug['id']) url = showurl + str(bug['id'])
get_from_bug_url_via_xml(url, mimetype, prefix, suffix) get_from_bug_url_via_xml(url, mimetype, prefix, suffix)
except xmlrpclib.Fault, err: except xmlrpclib.Fault as err:
print "A fault occurred" print("A fault occurred")
print "Fault code: %s" % err.faultCode print("Fault code: %s" % err.faultCode)
print err.faultString print(err.faultString)
def get_through_rss_query_url(url, mimetype, prefix, suffix): def get_through_rss_query_url(url, mimetype, prefix, suffix):
try: try:
...@@ -145,12 +145,12 @@ def get_through_rss_query_url(url, mimetype, prefix, suffix): ...@@ -145,12 +145,12 @@ def get_through_rss_query_url(url, mimetype, prefix, suffix):
try: try:
get_bug_function(entry['id'], mimetype, prefix, suffix) get_bug_function(entry['id'], mimetype, prefix, suffix)
except: except:
print entry['id'], "failed:", sys.exc_info()[0] print(entry['id'] + " failed: " + sys.exc_info()[0])
pass pass
def get_through_rss_query(queryurl, mimetype, prefix, suffix): def get_through_rss_query(queryurl, mimetype, prefix, suffix):
url = queryurl + '?query_format=advanced&field0-0-0=attachments.mimetype&type0-0-0=equals&value0-0-0=' + escape(mimetype) + '&ctype=rss' url = queryurl + '?query_format=advanced&field0-0-0=attachments.mimetype&type0-0-0=equals&value0-0-0=' + escape(mimetype) + '&ctype=rss'
print 'url is', url print('url is ' + url)
get_through_rss_query_url(url, mimetype, prefix, suffix) get_through_rss_query_url(url, mimetype, prefix, suffix)
def get_launchpad_bugs(prefix): def get_launchpad_bugs(prefix):
...@@ -168,7 +168,7 @@ def get_launchpad_bugs(prefix): ...@@ -168,7 +168,7 @@ def get_launchpad_bugs(prefix):
for bugtask in libobugs: for bugtask in libobugs:
bug = bugtask.bug bug = bugtask.bug
id = str(bug.id) id = str(bug.id)
print "parsing ", id, "status:", bugtask.status, "title:", bug.title[:50] print("parsing " + id + " status: " + bugtask.status + " title: " + bug.title[:50])
attachmentid = 0 attachmentid = 0
for attachment in bug.attachments: for attachment in bug.attachments:
attachmentid += 1 attachmentid += 1
...@@ -187,10 +187,10 @@ def get_launchpad_bugs(prefix): ...@@ -187,10 +187,10 @@ def get_launchpad_bugs(prefix):
download = suffix + '/' + prefix + id + '-' + str(attachmentid) + '.' + suffix download = suffix + '/' + prefix + id + '-' + str(attachmentid) + '.' + suffix
if os.path.isfile(download): if os.path.isfile(download):
print "assuming", id, "is up to date" print("assuming " + id + " is up to date")
break break
print 'mimetype is', handle.content_type, 'downloading as', download print('mimetype is ' + handle.content_type + ' downloading as ' + download)
f = open(download, "w") f = open(download, "w")
f.write(handle.read()) f.write(handle.read())
...@@ -356,6 +356,6 @@ for (mimetype,extension) in mimetypes.items(): ...@@ -356,6 +356,6 @@ for (mimetype,extension) in mimetypes.items():
try: try:
get_launchpad_bugs("lp") get_launchpad_bugs("lp")
except ImportError: except ImportError:
print "launchpadlib unavailable, skipping Ubuntu tracker" print("launchpadlib unavailable, skipping Ubuntu tracker")
# vim:set shiftwidth=4 softtabstop=4 expandtab: # vim:set shiftwidth=4 softtabstop=4 expandtab:
Markdown is supported
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