From e7c6e5d28f0f165a88df9dd7fb39eae3bd2e86b9 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Tue, 12 Jun 2018 10:04:14 +0000 Subject: [PATCH 37/39] Python 3 preparation: miscellaneous Python scripts not named *.py. --- frontends/roundup.cgi | 20 ++++++++++---------- scripts/add-issue | 5 +++-- scripts/roundup-reminder | 33 +++++++++++++++++---------------- scripts/spam-remover | 21 +++++++++++---------- scripts/weekly-report | 23 ++++++++++++----------- tools/base64 | 3 ++- 6 files changed, 55 insertions(+), 50 deletions(-) diff --git a/frontends/roundup.cgi b/frontends/roundup.cgi index afa922c..7054ba0 100755 --- a/frontends/roundup.cgi +++ b/frontends/roundup.cgi @@ -17,6 +17,7 @@ # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # python version check +from __future__ import print_function from roundup import version_check from roundup.i18n import _ import sys, time @@ -73,11 +74,11 @@ try: import traceback, StringIO, cgi from roundup.cgi import cgitb except: - print "Content-Type: text/plain\n" - print _("Failed to import cgitb!\n\n") + print("Content-Type: text/plain\n") + print(_("Failed to import cgitb!\n\n")) s = StringIO.StringIO() traceback.print_exc(None, s) - print s.getvalue() + print(s.getvalue()) # @@ -89,7 +90,7 @@ def checkconfig(): # see if there's an environment var. ROUNDUP_INSTANCE_HOMES is the # old name for it. - if os.environ.has_key('ROUNDUP_INSTANCE_HOMES'): + if 'ROUNDUP_INSTANCE_HOMES' in os.environ: homes = os.environ.get('ROUNDUP_INSTANCE_HOMES') else: homes = os.environ.get('TRACKER_HOMES', '') @@ -146,7 +147,7 @@ def main(out, err): tracker = path[1] os.environ['TRACKER_NAME'] = tracker os.environ['PATH_INFO'] = string.join(path[2:], '/') - if TRACKER_HOMES.has_key(tracker): + if tracker in TRACKER_HOMES: # redirect if we need a trailing '/' if len(path) == 2: request.send_response(301) @@ -182,19 +183,18 @@ def main(out, err): out.write('Not found: %s'%client.path) else: - import urllib + from roundup.anypy import urllib_ request.send_response(200) request.send_header('Content-Type', 'text/html') request.end_headers() w = request.write w(_('Roundup trackers index\n')) w(_('

Roundup trackers index

    \n')) - homes = TRACKER_HOMES.keys() - homes.sort() + homes = sorted(TRACKER_HOMES.keys()) for tracker in homes: w(_('
  1. %(tracker_name)s\n')%{ 'tracker_url': os.environ['SCRIPT_NAME']+'/'+ - urllib.quote(tracker), + urllib_.quote(tracker), 'tracker_name': cgi.escape(tracker)}) w(_('
')) @@ -223,7 +223,7 @@ except: ts = time.ctime() out.write('''

%s: An error occurred. Please check the server log for more information.

'''%ts) - print >> sys.stderr, 'EXCEPTION AT', ts + print('EXCEPTION AT', ts, file=sys.stderr) traceback.print_exc(0, sys.stderr) sys.stdout.flush() diff --git a/scripts/add-issue b/scripts/add-issue index 7e1d730..2ab37ad 100755 --- a/scripts/add-issue +++ b/scripts/add-issue @@ -8,6 +8,7 @@ created as the current user (%s) if they exist as a Roundup user, or "admin" otherwise. ''' +from __future__ import print_function import sys, os, pwd from roundup import instance, mailgw, date @@ -15,8 +16,8 @@ from roundup import instance, mailgw, date # open the instance username = pwd.getpwuid(os.getuid())[0] if len(sys.argv) < 3: - print "Error: Not enough arguments" - print __doc__.strip()%(sys.argv[0], username) + print("Error: Not enough arguments") + print(__doc__.strip()%(sys.argv[0], username)) sys.exit(1) tracker_home = sys.argv[1] issue_priority = sys.argv[2] diff --git a/scripts/roundup-reminder b/scripts/roundup-reminder index 9caa5dc..c155c9c 100755 --- a/scripts/roundup-reminder +++ b/scripts/roundup-reminder @@ -27,13 +27,14 @@ TODO: introduce some structure ;) TODO: possibly make this more general and configurable... ''' +from __future__ import print_function import sys, cStringIO, MimeWriter, smtplib from roundup import instance, date from roundup.mailer import SMTPConnection # open the instance if len(sys.argv) != 2: - print 'You need to specify an instance home dir' + print('You need to specify an instance home dir') instance_home = sys.argv[1] instance = instance.open(instance_home) db = instance.open('admin') @@ -94,32 +95,32 @@ for user_id in db.user.list(): body = part.startbody('text/plain') # do the plain text bit - print >>body, 'Created ID Activity Title' - print >>body, '='*75 + print('Created ID Activity Title', file=body) + print('='*75, file=body) # '2 months 213 immediate cc_daemon barfage old_priority = None for priority_order, activity_date, creation_date, issue_id in l: priority = db.issue.get(issue_id, 'priority') if (priority != old_priority): old_priority = priority - print >>body, ' ', db.priority.get(priority,'name') + print(' ', db.priority.get(priority,'name'), file=body) # pretty creation creation = (creation_date - date.Date('.')).pretty() activity = (activity_date - date.Date('.')).pretty() title = db.issue.get(issue_id, 'title') if len(title) > 42: title = title[:38] + ' ...' - print >>body, '%-11s %-4s %-9s %-42s'%(creation, issue_id, - activity, title) + print('%-11s %-4s %-9s %-42s'%(creation, issue_id, + activity, title), file=body) # some help to finish off - print >>body, ''' + print(''' To view or respond to any of the issues listed above, visit the URL %s and click on "My Issues". Do NOT respond to this message. -'''%db.config.TRACKER_WEB +'''%db.config.TRACKER_WEB, file=body) # now the HTML one @@ -132,27 +133,27 @@ and click on "My Issues". Do NOT respond to this message. 'month': ' bgcolor="#ffffcd"', 'whenever': ' bgcolor="#ffffff"', } - print >>body, ''' + print('''
-''' +''', file=body) old_priority = None for priority_order, activity_date, creation_date, issue_id in l: priority = db.issue.get(issue_id,'priority') if (priority != old_priority): old_priority = priority - print >>body, ''%db.priority.get(priority,'name') + print(''%db.priority.get(priority,'name'), file=body) creation = (creation_date - date.Date('.')).pretty() title = db.issue.get(issue_id, 'title') issue_id = '%s'%(db.config.TRACKER_WEB, issue_id, issue_id) activity = (activity_date - date.Date('.')).pretty() - print >>body, ''' - '''%(creation, issue_id, activity, title) - print >>body, '
Created ID Activity Title
->->->%s
->->->%s
%s%s%s%s
' + print('''%s%s%s + %s'''%(creation, issue_id, activity, title), file=body) + print('', file=body) - print >>body, '''

To view or respond to any of the issues listed + print('''

To view or respond to any of the issues listed above, simply click on the issue ID. Do not respond to - this message.

''' + this message.

''', file=body) # finish of the multipart writer.lastpart() diff --git a/scripts/spam-remover b/scripts/spam-remover index cdbd761..3d7795d 100755 --- a/scripts/spam-remover +++ b/scripts/spam-remover @@ -22,6 +22,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from __future__ import print_function _doc = ''' %prog [options] Remove file attachment spam from a tracker: @@ -89,7 +90,7 @@ def main(): opt, args = cmd.parse_args() # open the instance if len(args): - print >> sys.stderr, "This command doesn't take arguments" + print("This command doesn't take arguments", file=sys.stderr) cmd.show_help() tracker = instance.open(opt.instance) db = tracker.open('admin') @@ -110,21 +111,21 @@ def main(): clsname, id = hyperdb.splitDesignator(d) cls = db.getclass(clsname) issuefiles = dict.fromkeys(cls.get (id, 'files')) - for fid in issuefiles.keys(): + for fid in list(issuefiles.keys()): f = db.file.getnode(fid) if fid in files_to_remove or f.creator in users: files_to_remove[fid] = True files_found[fid] = True if not opt.quiet: - print "deleting file %s from issue" % f + print("deleting file %s from issue" % f) del issuefiles[fid] if opt.doit: - cls.set(id, files=issuefiles.keys()) + cls.set(id, files=list(issuefiles.keys())) journal = oldjournal = db.getjournal(clsname, id) # do this twice, we may have file-removals *before* file # additions for files to delete and may discover mid-journal # that there are new files to remove - for x in xrange(2): + for x in range(2): newjournal = [] for j in journal: if j[3] == 'set' and 'files' in j[4]: @@ -138,8 +139,8 @@ def main(): files_found.update(f) files_to_remove.update(f) del changes['+'] - # change dict in-place, don't use iteritems - for k, v in changes.items(): + # change dict in-place + for k, v in list(changes.items()): new_f = [] for f in v: if f in files_to_remove: @@ -154,7 +155,7 @@ def main(): if not opt.quiet: msg.append ("Old journal entry: %s" % str(j)) if changes: - j[4]['files'] = tuple(changes.iteritems()) + j[4]['files'] = tuple(changes.items()) else: del j[4]['files'] if j[4]: @@ -165,7 +166,7 @@ def main(): msg.append ("deleted") if len(msg) == 2 and msg[0][4:] != msg[1][4:]: for m in msg: - print m + print(m) else: newjournal.append(j) journal = newjournal @@ -176,7 +177,7 @@ def main(): db.file.set(f, content=' ') db.commit() else: - print "Database not changed" + print("Database not changed") if __name__ == '__main__': diff --git a/scripts/weekly-report b/scripts/weekly-report index d1c36af..a391bf8 100755 --- a/scripts/weekly-report +++ b/scripts/weekly-report @@ -6,12 +6,13 @@ # This script is free software, you may redistribute it # and/or modify under the same terms as Python. +from __future__ import print_function import sys, math from roundup import instance, date # open the instance if len(sys.argv) != 2: - print 'You need to specify an instance home dir' + print('You need to specify an instance home dir') instance_home = sys.argv[1] instance = instance.open(instance_home) db = instance.open('admin') @@ -29,7 +30,7 @@ for issue_id in db.issue.filter(None, {'activity': '-1w;'}): if ts < old: continue if action == 'create': created.append(issue_id) - elif action == 'set' and data.has_key('messages'): + elif action == 'set' and 'messages' in data: num += 1 summary.setdefault(db.issue.get(issue_id, 'status'), []).append(issue_id) messages.append((num, issue_id)) @@ -38,21 +39,21 @@ for issue_id in db.issue.filter(None, {'activity': '-1w;'}): #for k,v in summary.items(): # print k, len(v) -print '\nCREATED:' -print '\n'.join(['%s: %s'%(id, db.issue.get(id, 'title')) - for id in created]) +print('\nCREATED:') +print('\n'.join(['%s: %s'%(id, db.issue.get(id, 'title')) + for id in created])) -print '\nRESOLVED:' +print('\nRESOLVED:') resolved_id = db.status.lookup('resolved') -print '\n'.join(['%s: %s'%(id, db.issue.get(id, 'title')) - for id in summary.get(resolved_id, [])]) +print('\n'.join(['%s: %s'%(id, db.issue.get(id, 'title')) + for id in summary.get(resolved_id, [])])) -print '\nTOP TEN MOST DISCUSSED:' +print('\nTOP TEN MOST DISCUSSED:') messages.sort() messages.reverse() nmax = messages[0][0] fmt = '%%%dd - %%s: %%s'%(int(math.log(nmax, 10)) + 1) -print '\n'.join([fmt%(num, id, db.issue.get(id, 'title')) - for num, id in messages[:10]]) +print('\n'.join([fmt%(num, id, db.issue.get(id, 'title')) + for num, id in messages[:10]])) # vim: set filetype=python ts=4 sw=4 et si diff --git a/tools/base64 b/tools/base64 index 22e4bfb..df7e3d5 100755 --- a/tools/base64 +++ b/tools/base64 @@ -1,3 +1,4 @@ #!/usr/bin/env python +from __future__ import print_function import zlib, base64, sys -print base64.encodestring(zlib.compress(sys.stdin.read())) +print(base64.encodestring(zlib.compress(sys.stdin.read()))) -- 2.7.4