diff options
| author | 2018-03-14 02:56:04 +0100 | |
|---|---|---|
| committer | 2018-03-14 20:23:00 -0400 | |
| commit | 23ac25c0b388b5ffebf66154b12a3950b89b977a (patch) | |
| tree | 27ae214e393e6be2efc225d02ffc93135aaa2eb5 /minidinstall | |
| parent | 9883708468224628f9e0e577162fb5345fe20eb4 (diff) | |
Port to Python 3
Diffstat (limited to 'minidinstall')
| -rw-r--r-- | minidinstall/ChangeFile.py | 8 | ||||
| -rw-r--r-- | minidinstall/Dnotify.py | 16 | ||||
| -rwxr-xr-x | minidinstall/DpkgControl.py | 29 | ||||
| -rw-r--r-- | minidinstall/DpkgDatalist.py | 10 | ||||
| -rw-r--r-- | minidinstall/GPGSigVerifier.py | 4 | ||||
| -rw-r--r-- | minidinstall/OrderedDict.py | 12 | ||||
| -rwxr-xr-x | minidinstall/SafeWriteFile.py | 10 | ||||
| -rwxr-xr-x | minidinstall/SignedFile.py | 18 | ||||
| -rw-r--r-- | minidinstall/mail.py | 2 | ||||
| -rw-r--r-- | minidinstall/misc.py | 6 | ||||
| -rw-r--r-- | minidinstall/tweet.py | 10 |
11 files changed, 60 insertions, 65 deletions
diff --git a/minidinstall/ChangeFile.py b/minidinstall/ChangeFile.py index 702069e..3b0cf48 100644 --- a/minidinstall/ChangeFile.py +++ b/minidinstall/ChangeFile.py @@ -19,7 +19,7 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import os, re, sys, string, stat -import threading, Queue +import threading, queue import logging from minidinstall import DpkgControl, SignedFile from minidinstall import misc @@ -28,7 +28,7 @@ class ChangeFileException(Exception): def __init__(self, value): self._value = value def __str__(self): - return `self._value` + return repr(self._value) class ChangeFile(DpkgControl.DpkgParagraph): md5_re = r'^(?P<md5>[0-9a-f]{32})[ \t]+(?P<size>\d+)[ \t]+(?P<section>[-/a-zA-Z0-9]+)[ \t]+(?P<priority>[-a-zA-Z0-9]+)[ \t]+(?P<file>[0-9a-zA-Z][-+:.,=~0-9a-zA-Z_]+)$' @@ -84,7 +84,7 @@ class ChangeFile(DpkgControl.DpkgParagraph): def verify(self, sourcedir): """ verify size and hash values from changes file """ checksum = self._get_checksum_from_changes() - for hash in checksum.keys(): + for hash in list(checksum.keys()): for (hashsum, size, filename) in checksum[hash]: self._verify_file_integrity(os.path.join(sourcedir, filename), int(size), hash, hashsum) @@ -97,7 +97,7 @@ class ChangeFile(DpkgControl.DpkgParagraph): if not stat.S_ISREG(statbuf[stat.ST_MODE]): raise ChangeFileException("%s is not a regular file" % (filename,)) size = statbuf[stat.ST_SIZE] - except OSError, e: + except OSError as e: raise ChangeFileException("Can't stat %s: %s" % (filename,e.strerror)) if size != expected_size: raise ChangeFileException("File size for %s does not match that specified in .dsc" % (filename,)) diff --git a/minidinstall/Dnotify.py b/minidinstall/Dnotify.py index e31080c..18606e1 100644 --- a/minidinstall/Dnotify.py +++ b/minidinstall/Dnotify.py @@ -18,7 +18,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -import os, re, sys, string, stat, threading, Queue, time +import os, re, sys, string, stat, threading, queue, time import logging from minidinstall import misc @@ -26,7 +26,7 @@ class DnotifyException(Exception): def __init__(self, value): self._value = value def __str__(self): - return `self._value` + return repr(self._value) class DirectoryNotifierFactory: def create(self, dirs, use_dnotify=1, poll_time=30, logger=None, cancel_event=None): @@ -101,7 +101,7 @@ class MtimeDirectoryNotifier(DirectoryNotifier): if timeout_time and time.time() > timeout_time: return None self._logger.debug('Polling...') - for dir in self._dirmap.keys(): + for dir in list(self._dirmap.keys()): oldtime = self._dirmap[dir] mtime = os.stat(os.path.join(self._cwd, dir))[stat.ST_MTIME] if oldtime < mtime: @@ -120,7 +120,7 @@ class MtimeDirectoryNotifier(DirectoryNotifier): class DnotifyDirectoryNotifier(DirectoryNotifier): def __init__(self, dirs, logger): DirectoryNotifier.__init__(self, dirs, logger) - self._queue = Queue.Queue() + self._queue = queue.Queue() dnotify = DnotifyThread(self._queue, self._dirs, self._logger) dnotify.start() @@ -134,12 +134,12 @@ class DnotifyDirectoryNotifier(DirectoryNotifier): if dir is None: # We shouldn't have to do this; no one else is reading # from the queue. But we do it just to be safe. - for key in set.keys(): + for key in list(set.keys()): self._queue.put(key) return None set[dir] = 1 i -= 1 - for key in set.keys(): + for key in list(set.keys()): self._queue.put(key) i = self._queue.qsize() self._logger.debug('Queue size (after duplicate filter): %d', (i,)) @@ -149,10 +149,10 @@ class DnotifyDirectoryNotifier(DirectoryNotifier): if timeout is None: return self._queue.get() timeout_time = time.time() + timeout - while 1: + while True: try: self._queue.get(0) - except Queue.Empty: + except queue.Empty: if time.time() > timeout_time: return None else: diff --git a/minidinstall/DpkgControl.py b/minidinstall/DpkgControl.py index 4bda8c5..be08155 100755 --- a/minidinstall/DpkgControl.py +++ b/minidinstall/DpkgControl.py @@ -34,9 +34,8 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import re, string -from DpkgDatalist import * +from .DpkgDatalist import * from minidinstall.SignedFile import * -from types import ListType class DpkgParagraph(DpkgOrderedDatalist): caseSensitive = 0 @@ -48,7 +47,7 @@ class DpkgParagraph(DpkgOrderedDatalist): "Paragraph data from a file object." key = None value = None - while 1: + while True: line = f.readline() if not line: return @@ -60,32 +59,32 @@ class DpkgParagraph(DpkgOrderedDatalist): return line = line[ :-1 ] if line[ 0 ] != ' ': - key, value = string.split( line, ":", 1 ) + key, value = line.split( ":", 1 ) if value: value = value[ 1: ] if not self.caseSensitive: - newkey = string.lower( key ) - if not self.trueFieldCasing.has_key( key ): + newkey = key.lower() + if key not in self.trueFieldCasing: self.trueFieldCasing[ newkey ] = key key = newkey else: - if isinstance( value, ListType ): + if isinstance( value, list ): value.append( line[ 1: ] ) else: value = [ value, line[ 1: ] ] self[ key ] = value def _storeField( self, f, value, lead = " " ): - if isinstance( value, ListType ): - value = string.join( map( lambda v, lead = lead: v and ( lead + v ) or v, value ), "\n" ) + if isinstance( value, list ): + value = "\n".join(list(map( lambda v, lead = lead: v and ( lead + v ) or v, value ))) else: if value: value = lead + value f.write( "%s\n" % ( value ) ) def _store( self, f ): "Write our paragraph data to a file object" - for key in self.keys(): + for key in list(self.keys()): value = self[ key ] - if self.trueFieldCasing.has_key( key ): + if key in self.trueFieldCasing: key = self.trueFieldCasing[ key ] f.write( "%s:" % key ) self._storeField( f, value ) @@ -105,7 +104,7 @@ class DpkgControl(DpkgOrderedDatalist): return p def load( self, f ): - while 1: + while True: p = self._load_one( f ) if not p: break self[ p[ self.key ] ] = p @@ -113,7 +112,7 @@ class DpkgControl(DpkgOrderedDatalist): def _store( self, f ): "Write our control data to a file object" - for key in self.keys(): + for key in list(self.keys()): self[ key ]._store( f ) f.write( "\n" ) @@ -138,8 +137,8 @@ if __name__ == "__main__": import sys types = { 'p' : DpkgParagraph, 'c' : DpkgControl, 's' : DpkgSourceControl } type = sys.argv[ 1 ] - if not types.has_key( type ): - print "Unknown type `%s'!" % type + if type not in types: + print( "Unknown type `%s'!" % type ) sys.exit( 1 ) file = open( sys.argv[ 2 ], "r" ) data = types[ type ]() diff --git a/minidinstall/DpkgDatalist.py b/minidinstall/DpkgDatalist.py index 0c11612..68f9940 100644 --- a/minidinstall/DpkgDatalist.py +++ b/minidinstall/DpkgDatalist.py @@ -21,10 +21,9 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import os, sys -from UserDict import UserDict -from OrderedDict import OrderedDict +from collections import UserDict +from collections import OrderedDict from minidinstall.SafeWriteFile import SafeWriteFile -from types import StringType class DpkgDatalistException(Exception): UNKNOWN = 0 @@ -55,15 +54,14 @@ class _DpkgDatalist: self._store(sys.stdout) return - # Write to a temporary file first - if type(fn) == StringType: + if isinstance(fn, str): vf=SafeWriteFile(fn+".new", fn, "w") else: vf=fn try: self._store(vf) finally: - if type(fn) == StringType: + if isinstance(fn, str): vf.close() diff --git a/minidinstall/GPGSigVerifier.py b/minidinstall/GPGSigVerifier.py index a8fb46c..2e0dee5 100644 --- a/minidinstall/GPGSigVerifier.py +++ b/minidinstall/GPGSigVerifier.py @@ -25,14 +25,14 @@ class GPGSigVerifierException(Exception): def __init__(self, value): self._value = value def __str__(self): - return `self._value` + return repr(self._value) class GPGSigVerificationFailure(Exception): def __init__(self, value, output): self._value = value self._output = output def __str__(self): - return `self._value` + return repr(self._value) def getOutput(self): return self._output diff --git a/minidinstall/OrderedDict.py b/minidinstall/OrderedDict.py index fa3f276..7c842b0 100644 --- a/minidinstall/OrderedDict.py +++ b/minidinstall/OrderedDict.py @@ -20,7 +20,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -from UserDict import UserDict +from collections import UserDict class OrderedDict(UserDict): __order=[] @@ -41,12 +41,12 @@ class OrderedDict(UserDict): return UserDict.__cmp__(self, dict) def __setitem__(self, key, value): - if not self.has_key(key): + if key not in self: self.__order.append(key) UserDict.__setitem__(self, key, value) def __delitem__(self, key): - if self.has_key(key): + if key in self: del self.__order[self.__order.index(key)] UserDict.__delitem__(self, key) @@ -64,13 +64,13 @@ class OrderedDict(UserDict): return self.__order def items(self): - return map(lambda x, self=self: (x, self.__getitem__(x)), self.__order) + return list(map(lambda x, self=self: (x, self.__getitem__(x)), self.__order)) def values(self): - return map(lambda x, self=self: self.__getitem__(x), self.__order) + return list(map(lambda x, self=self: self.__getitem__(x), self.__order)) def update(self, dict): - for k, v in dict.items(): + for k, v in list(dict.items()): self.__setitem__(k, v) # vim:ts=4:sw=4:et: diff --git a/minidinstall/SafeWriteFile.py b/minidinstall/SafeWriteFile.py index 1777d36..591c4f0 100755 --- a/minidinstall/SafeWriteFile.py +++ b/minidinstall/SafeWriteFile.py @@ -21,9 +21,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -from types import StringType from shutil import copy2 -from string import find from os import rename class ObjectNotAllowed(Exception): @@ -37,14 +35,14 @@ class InvalidMode(Exception): class SafeWriteFile: def __init__(self, newname, realname, mode="w", bufsize=-1): - if type(newname)!=StringType: + if not isinstance(newname, str): raise ObjectNotAllowed(newname) - if type(realname)!=StringType: + if not isinstance(realname, str): raise ObjectNotAllowed(realname) - if find(mode, "r")>=0: + if "r" in mode: raise InvalidMode(mode) - if find(mode, "a")>=0 or find(mode, "+") >= 0: + if "a" in mode or "+" in mode: copy2(realname, newname) self.fobj=open(newname, mode, bufsize) self.newname=newname diff --git a/minidinstall/SignedFile.py b/minidinstall/SignedFile.py index 71181c3..efc4730 100755 --- a/minidinstall/SignedFile.py +++ b/minidinstall/SignedFile.py @@ -33,7 +33,7 @@ class SignedFile: line = stream.readline() if (line == "-----BEGIN PGP SIGNED MESSAGE-----\n"): self._signed = 1 - while (1): + while True: line = stream.readline() if (len(line) == 0 or line == '\n'): break @@ -55,18 +55,18 @@ class SignedFile: self._signature = [] self._signatureversion = self._stream.readline() self._stream.readline() # skip blank line - while 1: + while True: line = self._stream.readline() if len(line) == 0 or line == "-----END PGP SIGNATURE-----\n": break self._signature.append(line) - self._signature = string.join + self._signature = ''.join(self._signature) return '' return line def readlines(self): ret = [] - while 1: + while True: line = self.readline() if (line != ''): ret.append(line) @@ -89,19 +89,19 @@ class SignedFile: if __name__=="__main__": import sys if len(sys.argv) == 0: - print "Need one file as an argument" + print("Need one file as an argument") sys.exit(1) filename = sys.argv[1] f=SignedFile(open(filename)) if f.getSigned(): - print "**** SIGNED ****" + print("**** SIGNED ****") else: - print "**** NOT SIGNED ****" + print("**** NOT SIGNED ****") lines=f.readlines() - print lines + print(lines) if not f.getSigned(): assert(len(lines) == len(actuallines)) else: - print "Signature: %s" % (f.getSignature()) + print("Signature: %s" % (f.getSignature())) # vim:ts=4:sw=4:et: diff --git a/minidinstall/mail.py b/minidinstall/mail.py index 30103a3..50df462 100644 --- a/minidinstall/mail.py +++ b/minidinstall/mail.py @@ -43,5 +43,5 @@ def send(smtp_server, smtp_from, smtp_to, body, subject="mini-dinstall mail noti smtp = smtplib.SMTP(smtp_server) smtp.sendmail(smtp_from, [smtp_to], mime_body.as_string()) logger.info("Mail sent to %s (%s)" % (smtp_to, subject)) - except Exception, e: + except Exception as e: logger.exception("Error sending mail to %s ('%s') via %s: %s: %s", smtp_to, subject, smtp_server, type(e), e.args) diff --git a/minidinstall/misc.py b/minidinstall/misc.py index 94fe291..372c450 100644 --- a/minidinstall/misc.py +++ b/minidinstall/misc.py @@ -2,7 +2,7 @@ # misc tools for mini-dinstall -# Copyright © 2004 Thomas Viehmann <tv@beamnet.de> +# Copyright © 2004 Thomas Viehmann <tv@beamnet.de> # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -28,7 +28,7 @@ def dup2(fd,fd2): try: os.dup2(fd,fd2) success = 1 - except OSError, e: + except OSError as e: if (e.errno != errno.EBUSY) or (tries >= 3): raise # wait 0-2 seconds befor next try @@ -55,7 +55,7 @@ def get_file_sum(self, type, filename): elif type == 'sha256': sum = hashlib.sha256() self._logger.debug("Generate %s (python-internal) for %s" % (type, filename)) - f = open(filename) + f = open(filename,'rb') buf = f.read(8192) while buf != '': sum.update(buf) diff --git a/minidinstall/tweet.py b/minidinstall/tweet.py index 548918b..7106085 100644 --- a/minidinstall/tweet.py +++ b/minidinstall/tweet.py @@ -20,7 +20,7 @@ import logging -import urllib2 +import urllib.request import base64 def send(tweet_body, tweet_server, tweet_user, tweet_password): @@ -40,19 +40,19 @@ def send(tweet_body, tweet_server, tweet_user, tweet_password): if not tweet_user or not tweet_password: logger.exception("Missing username or password for twitting") - auth_handler = urllib2.HTTPBasicAuthHandler() + auth_handler = urllib.request.HTTPBasicAuthHandler() auth_handler.add_password(realm=auth_realm, uri=post_url, user=tweet_user, passwd=tweet_password) - m_http_opener = urllib2.build_opener(auth_handler) + m_http_opener = urllib.request.build_opener(auth_handler) - req = urllib2.Request(post_url) + req = urllib.request.Request(post_url) req.add_data("status=%s" % tweet_body) handle = None try: handle = m_http_opener.open(req) a = handle.read() logger.info("Tweet sent to %s (%s)" % (tweet_server, tweet_user)) - except Exception, e: + except Exception as e: logger.exception("Error sending tweet to %s ('%s') via %s: %s: %s", tweet_server, tweet_body, tweet_user, type(e), e.args) |
