Move opener to utils
authormpm@selenic.com
Sat, 27 Aug 2005 14:31:41 -0700
changeset 1090 1bca39b85615
parent 1089 142b5d5ec9cc
child 1091 d62130f99a73
Move opener to utils - move the opener code down to util - add docstring - change commands.py users to simply use file instead
mercurial/commands.py
mercurial/hg.py
mercurial/util.py
--- a/mercurial/commands.py	Sat Aug 27 14:21:25 2005 -0700
+++ b/mercurial/commands.py	Sat Aug 27 14:31:41 2005 -0700
@@ -690,12 +690,12 @@
 
 def debugdata(ui, file_, rev):
     """dump the contents of an data file revision"""
-    r = hg.revlog(hg.opener(""), file_[:-2] + ".i", file_)
+    r = hg.revlog(file, file_[:-2] + ".i", file_)
     ui.write(r.revision(r.lookup(rev)))
 
 def debugindex(ui, file_):
     """dump the contents of an index file"""
-    r = hg.revlog(hg.opener(""), file_, "")
+    r = hg.revlog(file, file_, "")
     ui.write("   rev    offset  length   base linkrev" +
              " nodeid       p1           p2\n")
     for i in range(r.count()):
@@ -706,7 +706,7 @@
 
 def debugindexdot(ui, file_):
     """dump an index DAG as a .dot file"""
-    r = hg.revlog(hg.opener(""), file_, "")
+    r = hg.revlog(file, file_, "")
     ui.write("digraph G {\n")
     for i in range(r.count()):
         e = r.index[i]
--- a/mercurial/hg.py	Sat Aug 27 14:21:25 2005 -0700
+++ b/mercurial/hg.py	Sat Aug 27 14:31:41 2005 -0700
@@ -13,34 +13,6 @@
 from demandload import *
 demandload(globals(), "localrepo httprepo sshrepo")
 
-# used to avoid circular references so destructors work
-def opener(base):
-    p = base
-    def o(path, mode="r"):
-        if p.startswith("http://"):
-            f = os.path.join(p, urllib.quote(path))
-            return httprangereader.httprangereader(f)
-
-        f = os.path.join(p, path)
-
-        mode += "b" # for that other OS
-
-        if mode[0] != "r":
-            try:
-                s = os.stat(f)
-            except OSError:
-                d = os.path.dirname(f)
-                if not os.path.isdir(d):
-                    os.makedirs(d)
-            else:
-                if s.st_nlink > 1:
-                    file(f + ".tmp", "wb").write(file(f, "rb").read())
-                    util.rename(f+".tmp", f)
-
-        return file(f, mode)
-
-    return o
-
 def repository(ui, path=None, create=0):
     if path:
         if path.startswith("http://"):
@@ -52,8 +24,8 @@
                 ui, path.replace("hg://", "http://"))
         if path.startswith("old-http://"):
             return localrepo.localrepository(
-                ui, opener, path.replace("old-http://", "http://"))
+                ui, util.opener, path.replace("old-http://", "http://"))
         if path.startswith("ssh://"):
             return sshrepo.sshrepository(ui, path)
 
-    return localrepo.localrepository(ui, opener, path, create)
+    return localrepo.localrepository(ui, util.opener, path, create)
--- a/mercurial/util.py	Sat Aug 27 14:21:25 2005 -0700
+++ b/mercurial/util.py	Sat Aug 27 14:31:41 2005 -0700
@@ -232,6 +232,41 @@
         else:
             pass
 
+def opener(base):
+    """
+    return a function that opens files relative to base
+
+    this function is used to hide the details of COW semantics and
+    remote file access from higher level code.
+
+    todo: separate remote file access into a separate function
+    """
+    p = base
+    def o(path, mode="r"):
+        if p.startswith("http://"):
+            f = os.path.join(p, urllib.quote(path))
+            return httprangereader.httprangereader(f)
+
+        f = os.path.join(p, path)
+
+        mode += "b" # for that other OS
+
+        if mode[0] != "r":
+            try:
+                s = os.stat(f)
+            except OSError:
+                d = os.path.dirname(f)
+                if not os.path.isdir(d):
+                    os.makedirs(d)
+            else:
+                if s.st_nlink > 1:
+                    file(f + ".tmp", "wb").write(file(f, "rb").read())
+                    rename(f+".tmp", f)
+
+        return file(f, mode)
+
+    return o
+
 def _makelock_file(info, pathname):
     ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
     os.write(ld, info)