[Lsb-messages] /var/www/bzr/lsb/devel/appbat r1002: a bit more pythonic

Mats Wichmann mats at linuxfoundation.org
Fri Dec 30 00:02:51 UTC 2016


------------------------------------------------------------
revno: 1002
committer: Mats Wichmann <mats at linuxfoundation.org>
branch nick: appbat
timestamp: Thu 2016-12-29 17:02:51 -0700
message:
  a bit more pythonic
modified:
  extras/entitycheck.py
-------------- next part --------------
=== modified file 'extras/entitycheck.py'
--- a/extras/entitycheck.py	2016-12-29 21:03:19 +0000
+++ b/extras/entitycheck.py	2016-12-30 00:02:51 +0000
@@ -86,10 +86,11 @@
             self.name, self.file = match
 
     def __repr__(self):
-        return 'entity({}, {})'.format(self.name, self.file)
+        return "entity('{}', '{}')".format(self.name, self.file)
 
     def __str__(self):
-        return 'Entity: {} -> {}'.format(self.name, self.file)
+        '''entity class "string" is "\tfilename", as that's what some things want to print'''
+        return '\t{}'.format(self.file)
 
 
     BLOCKSIZE = 1024*1024
@@ -336,35 +337,27 @@
         else:
             found.append(item)
             if check_sums or generate_sums: item.domd5()
-    return (found, missing)
+    return found, missing
 
 def check_extra(path, collection):
     """Check for files in a path that are not described by entities.
     Creates an entity instance for each and returns a list (this
     is to be able to use a common print routine, only the names matter)
     """
-    notfound = []
-    paths = {}
-    for item in collection:
-        paths[item.file] = item
-    for file in os.listdir(path):
-        if file not in paths.keys():
-            notfound.append(entity((None, file)))
+    paths = dict((item.file, item) for item in collection)
+    # if we require Python >= 2.7, can use dict comprehension:
+    #paths = {item.file:item for item in collection}
+    notfound = [entity((None, file)) for file in os.listdir(path) if file not in paths]
     return notfound
 
 def check_checksums(collection, checksums):
     """Check checksums on entities in collection against 'checksums' dictionary.
     Returns a tuple (entities with bad checksums, missing checksums)
     """
-    badsums = []
-    nosums = []
-    for entity in collection:
-        if checksums.has_key(entity.file):
-            if entity.md5sum != checksums[entity.file]:
-               badsums.append(entity)
-        else:
-            nosums.append(entity)
-    return (badsums, nosums)
+    badsums = [entity for entity in collection if entity.file in checksums 
+               and entity.md5sum != checksums[entity.file]]
+    nosums = [entity for entity in collection if entity.file not in checksums]
+    return badsums, nosums
 
 def dump_coll(collection, msg):
     """Print a collection: use msg and a count to print a header,
@@ -373,7 +366,7 @@
     if collection:
         print msg, len(collection)
         for item in collection:
-            print "\t", item.file
+            print item
 
 def report(fnd_pkg, fnd_pat, miss_pkg, miss_pat, extras):
     """Generate package/patch report.
@@ -445,7 +438,8 @@
 def writemd5(collection):
     """Generate a new checksum file from checksums saved in entities. """
     sums = open(epaths['md5sum_file'], 'w')
-    if noisy: print "writing checksums to {md5sum_file}".format(**epaths)
+    if noisy: 
+        print "writing checksums to {md5sum_file}".format(**epaths)
     for entity in collection:
         sums.write("%s  %s\n" % (entity.md5sum, entity.file))
     sums.close()



More information about the lsb-messages mailing list