Run 2to3 over Python sources.
[cumulus.git] / python / cumulus / cmd_util.py
index 2e163cf..0827cb0 100644 (file)
@@ -36,7 +36,7 @@ def check_version(format):
 # environment variable.
 def get_passphrase():
     ENV_KEY = 'LBS_GPG_PASSPHRASE'
-    if not os.environ.has_key(ENV_KEY):
+    if ENV_KEY not in os.environ:
         os.environ[ENV_KEY] = getpass.getpass()
 
 def cmd_prune_db(args):
@@ -64,8 +64,8 @@ def cmd_clean(args, clean_threshold=7.0):
     # Expire segments which are poorly-utilized.
     for s in db.get_segment_cleaning_list():
         if s.cleaning_benefit > clean_threshold:
-            print "Cleaning segment %d (benefit %.2f)" % (s.id,
-                                                          s.cleaning_benefit)
+            print("Cleaning segment %d (benefit %.2f)" % (s.id,
+                                                          s.cleaning_benefit))
             db.mark_segment_expired(s)
         else:
             break
@@ -77,7 +77,7 @@ def cmd_list_snapshots(args):
         Syntax: $0 --data=DATADIR list-snapshots
     """
     store = cumulus.CumulusStore(options.store)
-    for s in sorted(store.list_snapshots()): print s
+    for s in sorted(store.list_snapshots()): print(s)
 
 def cmd_list_snapshot_sizes(args):
     """ List size of data needed for each snapshot.
@@ -104,7 +104,7 @@ def cmd_list_snapshot_sizes(args):
             remcount += 1
         size += added - removed
         previous = segments
-        print "%s: %.3f +%.3f -%.3f (+%d/-%d segments)" % (s, size / 1024.0**2, added / 1024.0**2, removed / 1024.0**2, addcount, remcount)
+        print("%s: %.3f +%.3f -%.3f (+%d/-%d segments)" % (s, size / 1024.0**2, added / 1024.0**2, removed / 1024.0**2, addcount, remcount))
 
 def cmd_garbage_collect(args):
     """ Search for any files which are not needed by any current
@@ -120,7 +120,7 @@ def cmd_garbage_collect(args):
         referenced.add(s)
         referenced.update(d['Segments'].split())
 
-    print referenced
+    print(referenced)
 
     to_delete = []
     to_preserve = []
@@ -131,12 +131,12 @@ def cmd_garbage_collect(args):
             else:
                 to_delete.append(path)
 
-    print to_preserve
-    print to_delete
+    print(to_preserve)
+    print(to_delete)
 
     raw_backend = backend.raw_backend
     for f in to_delete:
-        print "Delete:", f
+        print("Delete:", f)
         if not options.dry_run:
             raw_backend.delete(f)
 cmd_gc = cmd_garbage_collect
@@ -149,8 +149,8 @@ def cmd_read_snapshots(snapshots):
     for s in snapshots:
         d = cumulus.parse_full(store.load_snapshot(s))
         check_version(d['Format'])
-        print d
-        print d['Segments'].split()
+        print(d)
+        print(d['Segments'].split())
     store.cleanup()
 
 def cmd_read_metadata(args):
@@ -179,14 +179,14 @@ def cmd_verify_snapshots(snapshots):
     store = cumulus.CumulusStore(options.store)
     for s in snapshots:
         cumulus.accessed_segments.clear()
-        print "#### Snapshot", s
+        print("#### Snapshot", s)
         d = cumulus.parse_full(store.load_snapshot(s))
         check_version(d['Format'])
-        print "## Root:", d['Root']
+        print("## Root:", d['Root'])
         metadata = cumulus.iterate_metadata(store, d['Root'])
         for m in metadata:
             if m.fields['type'] not in ('-', 'f'): continue
-            print "%s [%d bytes]" % (m.fields['name'], int(m.fields['size']))
+            print("%s [%d bytes]" % (m.fields['name'], int(m.fields['size'])))
             verifier = cumulus.ChecksumVerifier(m.fields['checksum'])
             size = 0
             for block in m.data():
@@ -203,11 +203,11 @@ def cmd_verify_snapshots(snapshots):
         # doesn't contain duplicates.
         listed_segments = set(d['Segments'].split())
         if cumulus.accessed_segments - listed_segments:
-            print "Error: Some segments not listed in descriptor!"
-            print sorted(list(cumulus.accessed_segments - listed_segments))
+            print("Error: Some segments not listed in descriptor!")
+            print(sorted(list(cumulus.accessed_segments - listed_segments)))
         if listed_segments - cumulus.accessed_segments :
-            print "Warning: Extra unused segments listed in descriptor!"
-            print sorted(list(listed_segments - cumulus.accessed_segments))
+            print("Warning: Extra unused segments listed in descriptor!")
+            print(sorted(list(listed_segments - cumulus.accessed_segments)))
     store.cleanup()
 
 def cmd_restore_snapshot(args):
@@ -232,7 +232,7 @@ def cmd_restore_snapshot(args):
         return False
 
     def warn(m, msg):
-        print "Warning: %s: %s" % (m.items.name, msg)
+        print("Warning: %s: %s" % (m.items.name, msg))
 
     # Phase 1: Read the complete metadata log and create directory structure.
     metadata_items = []
@@ -262,16 +262,16 @@ def cmd_restore_snapshot(args):
 
         try:
             if not os.path.isdir(path):
-                print "mkdir:", path
+                print("mkdir:", path)
                 os.makedirs(path)
-        except Exception, e:
+        except Exception as e:
             warn(m, "Error creating directory structure: %s" % (e,))
             continue
 
     # Phase 2: Restore files, ordered by how data is stored in segments.
     def restore_file(pathname, m):
         assert m.items.type in ('-', 'f')
-        print "extract:", pathname
+        print("extract:", pathname)
         destpath = os.path.join(destdir, pathname)
 
         file = open(destpath, 'wb')
@@ -290,13 +290,13 @@ def cmd_restore_snapshot(args):
 
     while metadata_segments:
         (segment, items) = metadata_segments.popitem()
-        print "+ Segment", segment
+        print("+ Segment", segment)
         for pathname in sorted(items):
             if pathname in metadata_paths:
                 restore_file(pathname, metadata_paths[pathname])
                 del metadata_paths[pathname]
 
-    print "+ Remaining files"
+    print("+ Remaining files")
     while metadata_paths:
         (pathname, m) = metadata_paths.popitem()
         restore_file(pathname, m)
@@ -304,7 +304,7 @@ def cmd_restore_snapshot(args):
     # Phase 3: Restore special files (symlinks, devices).
     # Phase 4: Restore directory permissions and modification times.
     for (pathname, m) in reversed(metadata_items):
-        print "permissions:", pathname
+        print("permissions:", pathname)
         destpath = os.path.join(destdir, pathname)
         (path, filename) = os.path.split(destpath)
 
@@ -326,9 +326,9 @@ def cmd_restore_snapshot(args):
                 os.mkfifo(destpath)
             elif m.items.type in ('c', 'b'):
                 if m.items.type == 'c':
-                    mode = 0600 | stat.S_IFCHR
+                    mode = 0o600 | stat.S_IFCHR
                 else:
-                    mode = 0600 | stat.S_IFBLK
+                    mode = 0o600 | stat.S_IFBLK
                 os.mknod(destpath, mode, os.makedev(*m.items.device))
             elif m.items.type == 's':
                 pass        # TODO: Implement
@@ -336,7 +336,7 @@ def cmd_restore_snapshot(args):
                 warn(m, "Unknown type code: " + m.items.type)
                 continue
 
-        except Exception, e:
+        except Exception as e:
             warn(m, "Error restoring: %s" % (e,))
             continue
 
@@ -344,7 +344,7 @@ def cmd_restore_snapshot(args):
             uid = m.items.user[0]
             gid = m.items.group[0]
             os.lchown(destpath, uid, gid)
-        except Exception, e:
+        except Exception as e:
             warn(m, "Error restoring file ownership: %s" % (e,))
 
         if m.items.type == 'l':
@@ -352,12 +352,12 @@ def cmd_restore_snapshot(args):
 
         try:
             os.chmod(destpath, m.items.mode)
-        except Exception, e:
+        except Exception as e:
             warn(m, "Error restoring file permissions: %s" % (e,))
 
         try:
             os.utime(destpath, (time.time(), m.items.mtime))
-        except Exception, e:
+        except Exception as e:
             warn(m, "Error restoring file timestamps: %s" % (e,))
 
     store.cleanup()
@@ -365,7 +365,7 @@ def cmd_restore_snapshot(args):
 def main(argv):
     usage = ["%prog [option]... command [arg]...", "", "Commands:"]
     cmd = method = None
-    for cmd, method in globals().iteritems():
+    for cmd, method in globals().items():
         if cmd.startswith ('cmd_'):
             usage.append(cmd[4:].replace('_', '-') + ':' + method.__doc__)
     parser = OptionParser(usage="\n".join(usage))
@@ -391,6 +391,6 @@ def main(argv):
     if method:
         method (args)
     else:
-        print "Unknown command:", cmd
+        print("Unknown command:", cmd)
         parser.print_usage()
         sys.exit(1)