convert: if getting a file from Perforce fails try to get it one more time
authorEugene Baranov <eug.baranov@gmail.com>
Wed, 08 Jul 2015 18:11:40 +0100
changeset 25775 220d9ae6a9a8
parent 25774 4f8c20fe66f0
child 25776 809c3e4a9e31
convert: if getting a file from Perforce fails try to get it one more time When converting a particularly large Perforce changelist (especially with some big files), it is very likely to run into an intermittent network issue (e.g. WSAECONNRESET or WSAETIMEDOUT) getting one of the files, which will result in the entire changelist converting being aborted. Which can be quite unfortunate since you might have waited hours getting all other files. To mitigate this let's attempt to get the file one more time, escalating original exception if that attempt fails.
hgext/convert/p4.py
--- a/hgext/convert/p4.py	Mon Jul 13 23:34:12 2015 +0900
+++ b/hgext/convert/p4.py	Wed Jul 08 18:11:40 2015 +0100
@@ -196,38 +196,54 @@
     def getfile(self, name, rev):
         cmd = 'p4 -G print %s' \
             % util.shellquote("%s#%s" % (self.depotname[name], rev))
-        stdout = util.popen(cmd, mode='rb')
 
-        mode = None
-        contents = ""
-        keywords = None
+        lasterror = None
+        while True:
+            stdout = util.popen(cmd, mode='rb')
+
+            mode = None
+            contents = ""
+            keywords = None
 
-        for d in loaditer(stdout):
-            code = d["code"]
-            data = d.get("data")
+            for d in loaditer(stdout):
+                code = d["code"]
+                data = d.get("data")
 
-            if code == "error":
-                raise IOError(d["generic"], data)
+                if code == "error":
+                    # if this is the first time error happened
+                    # re-attempt getting the file
+                    if not lasterror:
+                        lasterror = IOError(d["generic"], data)
+                        # this will exit inner-most for-loop
+                        break
+                    else:
+                        raise lasterror
 
-            elif code == "stat":
-                action = d.get("action")
-                if action in ["purge", "delete", "move/delete"]:
-                    return None, None
-                p4type = self.re_type.match(d["type"])
-                if p4type:
-                    mode = ""
-                    flags = (p4type.group(1) or "") + (p4type.group(3) or "")
-                    if "x" in flags:
-                        mode = "x"
-                    if p4type.group(2) == "symlink":
-                        mode = "l"
-                    if "ko" in flags:
-                        keywords = self.re_keywords_old
-                    elif "k" in flags:
-                        keywords = self.re_keywords
+                elif code == "stat":
+                    action = d.get("action")
+                    if action in ["purge", "delete", "move/delete"]:
+                        return None, None
+                    p4type = self.re_type.match(d["type"])
+                    if p4type:
+                        mode = ""
+                        flags = ((p4type.group(1) or "")
+                               + (p4type.group(3) or ""))
+                        if "x" in flags:
+                            mode = "x"
+                        if p4type.group(2) == "symlink":
+                            mode = "l"
+                        if "ko" in flags:
+                            keywords = self.re_keywords_old
+                        elif "k" in flags:
+                            keywords = self.re_keywords
 
-            elif code == "text" or code == "binary":
-                contents += data
+                elif code == "text" or code == "binary":
+                    contents += data
+
+                lasterror = None
+
+            if not lasterror:
+                break
 
         if mode is None:
             return None, None