Initial port to python3
[htsworkflow.git] / htsworkflow / automation / copier.py
index 1dc719d96b0c0bdfc851e0ef3e56e162b00e77bc..25fc6ee5afdab7da089409fb8280739b2cc6e048 100644 (file)
@@ -1,4 +1,4 @@
-import ConfigParser
+import configparser
 import copy
 import logging
 import logging.handlers
@@ -9,7 +9,7 @@ import subprocess
 import sys
 import time
 import traceback
-import urlparse
+import urllib.parse
 
 from benderjab import rpc
 
@@ -45,7 +45,7 @@ class rsync(object):
         # We made sure source ends in a / earlier
         cur_list = [ source+subdir for subdir in self.list_filter(stdout)]
         entries.extend(cur_list)
-    LOGGER.debug(u"Found the following: %s" % (unicode(entries)))
+    LOGGER.debug("Found the following: %s" % (str(entries)))
     return entries
 
   def list_filter(self, lines):
@@ -54,7 +54,7 @@ class rsync(object):
     """
     dirs_to_copy = []
     direntries = [ x[0:42].split() + [x[43:-1]] for x in lines ]
-    LOGGER.debug(u'direntries: %s' % (unicode(direntries),))
+    LOGGER.debug('direntries: %s' % (str(direntries),))
     for permissions, size, filedate, filetime, filename in direntries:
       if permissions[0] == 'd':
         # hey its a directory, the first step to being something we want to
@@ -122,7 +122,7 @@ class rsync(object):
 
       return path roots that have finished.
       """
-      for dir_key, proc_value in self.processes.items():
+      for dir_key, proc_value in list(self.processes.items()):
           retcode = proc_value.poll()
           if retcode is None:
               # process hasn't finished yet
@@ -145,7 +145,7 @@ class rsync(object):
       """
       Return list of current run folder names
       """
-      return self.processes.keys()
+      return list(self.processes.keys())
 
 class CopierBot(rpc.XmlRpcBot):
     def __init__(self, section=None, configfile=None):
@@ -200,7 +200,7 @@ class CopierBot(rpc.XmlRpcBot):
                                    require_resource=True)
         except bot.JIDMissingResource:
             msg = 'need a full jabber ID + resource for xml-rpc destinations'
-            print >>sys.stderr, msg
+            print(msg, file=sys.stderr)
             raise bot.JIDMissingResource(msg)
 
     def run(self):
@@ -215,7 +215,7 @@ class CopierBot(rpc.XmlRpcBot):
         start our copy
         """
         # Note, args comes in over the network, so don't trust it.
-        LOGGER.debug("Arguments to startCopy %s" % (unicode(args),))
+        LOGGER.debug("Arguments to startCopy %s" % (str(args),))
         copy_urls = []
         for a in args:
             clean_url = self.validate_url(a)
@@ -265,7 +265,7 @@ class CopierBot(rpc.XmlRpcBot):
         """
         self.rsync.poll()
         for p in self.pending:
-            if p not in self.rsync.keys():
+            if p not in list(self.rsync.keys()):
                 self.reportSequencingFinished(p)
                 self.pending.remove(p)
 
@@ -273,29 +273,29 @@ class CopierBot(rpc.XmlRpcBot):
         """
         Parse xmpp chat messages
         """
-        help = u"I can [copy], or report current [status]"
-        if re.match(u"help", msg):
+        help = "I can [copy], or report current [status]"
+        if re.match("help", msg):
             reply = help
         elif re.match("copy", msg):
             started = self.startCopy()
-            reply = u"started copying " + ", ".join(started)
-        elif re.match(u"status", msg):
-            msg = [u"Currently %d rsync processes are running." % (len(self.rsync))]
-            for d in self.rsync.keys():
-              msg.append(u"  " + d)
+            reply = "started copying " + ", ".join(started)
+        elif re.match("status", msg):
+            msg = ["Currently %d rsync processes are running." % (len(self.rsync))]
+            for d in list(self.rsync.keys()):
+              msg.append("  " + d)
             reply = os.linesep.join(msg)
         else:
-            reply = u"I didn't understand '%s'" % (unicode(msg))
+            reply = "I didn't understand '%s'" % (str(msg))
         return reply
 
     def validate_url(self, url):
-        user_url = urlparse.urlsplit(url)
+        user_url = urllib.parse.urlsplit(url)
         user_scheme = user_url[0]
         user_netloc = user_url[1]
         user_path = user_url[2]
 
         for source in self.sources:
-            source_url = urlparse.urlsplit(source)
+            source_url = urllib.parse.urlsplit(source)
             source_scheme = source_url[0]
             source_netloc = source_url[1]
             source_path = source_url[2]