##// END OF EJS Templates
move opener from util to scmutil
Adrian Buehlmann -
r13970:d1391335 default
parent child Browse files
Show More
@@ -4,12 +4,12 b''
4 # $ undumprevlog < repo.dump
4 # $ undumprevlog < repo.dump
5
5
6 import sys
6 import sys
7 from mercurial import revlog, node, util, transaction
7 from mercurial import revlog, node, scmutil, util, transaction
8
8
9 for fp in (sys.stdin, sys.stdout, sys.stderr):
9 for fp in (sys.stdin, sys.stdout, sys.stderr):
10 util.set_binary(fp)
10 util.set_binary(fp)
11
11
12 opener = util.opener('.', False)
12 opener = scmutil.opener('.', False)
13 tr = transaction.transaction(sys.stderr.write, opener, "undump.journal")
13 tr = transaction.transaction(sys.stderr.write, opener, "undump.journal")
14 while 1:
14 while 1:
15 l = sys.stdin.readline()
15 l = sys.stdin.readline()
@@ -10,7 +10,7 b' import tempfile'
10 import urllib
10 import urllib
11 import urllib2
11 import urllib2
12
12
13 from mercurial import strutil, util, encoding
13 from mercurial import strutil, scmutil, util, encoding
14 from mercurial.i18n import _
14 from mercurial.i18n import _
15
15
16 # Subversion stuff. Works best with very recent Python SVN bindings
16 # Subversion stuff. Works best with very recent Python SVN bindings
@@ -998,8 +998,8 b' class svn_sink(converter_sink, commandli'
998 self.run0('checkout', path, wcpath)
998 self.run0('checkout', path, wcpath)
999
999
1000 self.wc = wcpath
1000 self.wc = wcpath
1001 self.opener = util.opener(self.wc)
1001 self.opener = scmutil.opener(self.wc)
1002 self.wopener = util.opener(self.wc)
1002 self.wopener = scmutil.opener(self.wc)
1003 self.childmap = mapfile(ui, self.join('hg-childmap'))
1003 self.childmap = mapfile(ui, self.join('hg-childmap'))
1004 self.is_exec = util.checkexec(self.wc) and util.is_exec or None
1004 self.is_exec = util.checkexec(self.wc) and util.is_exec or None
1005
1005
@@ -61,7 +61,7 b' pretty fast (at least faster than having'
61
61
62 from mercurial.i18n import _
62 from mercurial.i18n import _
63 from mercurial.node import short, nullid
63 from mercurial.node import short, nullid
64 from mercurial import cmdutil, util, commands, encoding
64 from mercurial import cmdutil, scmutil, util, commands, encoding
65 import os, shlex, shutil, tempfile, re
65 import os, shlex, shutil, tempfile, re
66
66
67 def snapshot(ui, repo, files, node, tmproot):
67 def snapshot(ui, repo, files, node, tmproot):
@@ -81,7 +81,7 b' def snapshot(ui, repo, files, node, tmpr'
81 else:
81 else:
82 ui.note(_('making snapshot of %d files from working directory\n') %
82 ui.note(_('making snapshot of %d files from working directory\n') %
83 (len(files)))
83 (len(files)))
84 wopener = util.opener(base)
84 wopener = scmutil.opener(base)
85 fns_and_mtime = []
85 fns_and_mtime = []
86 ctx = repo[node]
86 ctx = repo[node]
87 for fn in files:
87 for fn in files:
@@ -45,7 +45,7 b' create other, independent patch queues w'
45 from mercurial.i18n import _
45 from mercurial.i18n import _
46 from mercurial.node import bin, hex, short, nullid, nullrev
46 from mercurial.node import bin, hex, short, nullid, nullrev
47 from mercurial.lock import release
47 from mercurial.lock import release
48 from mercurial import commands, cmdutil, hg, patch, util
48 from mercurial import commands, cmdutil, hg, patch, scmutil, util
49 from mercurial import repair, extensions, url, error
49 from mercurial import repair, extensions, url, error
50 import os, sys, re, errno, shutil
50 import os, sys, re, errno, shutil
51
51
@@ -259,7 +259,7 b' class queue(object):'
259 except IOError:
259 except IOError:
260 curpath = os.path.join(path, 'patches')
260 curpath = os.path.join(path, 'patches')
261 self.path = patchdir or curpath
261 self.path = patchdir or curpath
262 self.opener = util.opener(self.path)
262 self.opener = scmutil.opener(self.path)
263 self.ui = ui
263 self.ui = ui
264 self.applied_dirty = 0
264 self.applied_dirty = 0
265 self.series_dirty = 0
265 self.series_dirty = 0
@@ -16,7 +16,7 b' map from a changeset hash to its hash in'
16 from mercurial.i18n import _
16 from mercurial.i18n import _
17 import os, tempfile
17 import os, tempfile
18 from mercurial import bundlerepo, cmdutil, hg, merge, match
18 from mercurial import bundlerepo, cmdutil, hg, merge, match
19 from mercurial import patch, revlog, util, error
19 from mercurial import patch, revlog, scmutil, util, error
20 from mercurial import revset, templatekw
20 from mercurial import revset, templatekw
21
21
22 class transplantentry(object):
22 class transplantentry(object):
@@ -31,7 +31,7 b' class transplants(object):'
31 self.opener = opener
31 self.opener = opener
32
32
33 if not opener:
33 if not opener:
34 self.opener = util.opener(self.path)
34 self.opener = scmutil.opener(self.path)
35 self.transplants = {}
35 self.transplants = {}
36 self.dirty = False
36 self.dirty = False
37 self.read()
37 self.read()
@@ -74,7 +74,7 b' class transplanter(object):'
74 def __init__(self, ui, repo):
74 def __init__(self, ui, repo):
75 self.ui = ui
75 self.ui = ui
76 self.path = repo.join('transplant')
76 self.path = repo.join('transplant')
77 self.opener = util.opener(self.path)
77 self.opener = scmutil.opener(self.path)
78 self.transplants = transplants(self.path, 'transplants',
78 self.transplants = transplants(self.path, 'transplants',
79 opener=self.opener)
79 opener=self.opener)
80
80
@@ -8,7 +8,7 b''
8 from i18n import _
8 from i18n import _
9 from node import hex
9 from node import hex
10 import cmdutil
10 import cmdutil
11 import util, encoding
11 import scmutil, util, encoding
12 import cStringIO, os, tarfile, time, zipfile
12 import cStringIO, os, tarfile, time, zipfile
13 import zlib, gzip
13 import zlib, gzip
14
14
@@ -187,7 +187,7 b' class fileit(object):'
187
187
188 def __init__(self, name, mtime):
188 def __init__(self, name, mtime):
189 self.basedir = name
189 self.basedir = name
190 self.opener = util.opener(self.basedir)
190 self.opener = scmutil.opener(self.basedir)
191
191
192 def addfile(self, name, mode, islink, data):
192 def addfile(self, name, mode, islink, data):
193 if islink:
193 if islink:
@@ -9,7 +9,7 b' from node import hex, bin, nullid, nullr'
9 from lock import release
9 from lock import release
10 from i18n import _, gettext
10 from i18n import _, gettext
11 import os, re, sys, difflib, time, tempfile
11 import os, re, sys, difflib, time, tempfile
12 import hg, util, revlog, extensions, copies, error, bookmarks
12 import hg, scmutil, util, revlog, extensions, copies, error, bookmarks
13 import patch, help, mdiff, url, encoding, templatekw, discovery
13 import patch, help, mdiff, url, encoding, templatekw, discovery
14 import archival, changegroup, cmdutil, sshserver, hbisect, hgweb, hgweb.server
14 import archival, changegroup, cmdutil, sshserver, hbisect, hgweb, hgweb.server
15 import merge as mergemod
15 import merge as mergemod
@@ -975,7 +975,7 b' def debugancestor(ui, repo, *args):'
975 """find the ancestor revision of two revisions in a given index"""
975 """find the ancestor revision of two revisions in a given index"""
976 if len(args) == 3:
976 if len(args) == 3:
977 index, rev1, rev2 = args
977 index, rev1, rev2 = args
978 r = revlog.revlog(util.opener(os.getcwd(), audit=False), index)
978 r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), index)
979 lookup = r.lookup
979 lookup = r.lookup
980 elif len(args) == 2:
980 elif len(args) == 2:
981 if not repo:
981 if not repo:
@@ -1393,7 +1393,7 b' def debugdag(ui, repo, file_=None, *revs'
1393 spaces = opts.get('spaces')
1393 spaces = opts.get('spaces')
1394 dots = opts.get('dots')
1394 dots = opts.get('dots')
1395 if file_:
1395 if file_:
1396 rlog = revlog.revlog(util.opener(os.getcwd(), audit=False), file_)
1396 rlog = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), file_)
1397 revs = set((int(r) for r in revs))
1397 revs = set((int(r) for r in revs))
1398 def events():
1398 def events():
1399 for r in rlog:
1399 for r in rlog:
@@ -1443,7 +1443,8 b' def debugdata(ui, repo, file_, rev):'
1443 if len(filelog):
1443 if len(filelog):
1444 r = filelog
1444 r = filelog
1445 if not r:
1445 if not r:
1446 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_[:-2] + ".i")
1446 r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False),
1447 file_[:-2] + ".i")
1447 try:
1448 try:
1448 ui.write(r.revision(r.lookup(rev)))
1449 ui.write(r.revision(r.lookup(rev)))
1449 except KeyError:
1450 except KeyError:
@@ -1482,7 +1483,7 b' def debugindex(ui, repo, file_, **opts):'
1482 raise util.Abort(_("unknown format %d") % format)
1483 raise util.Abort(_("unknown format %d") % format)
1483
1484
1484 if not r:
1485 if not r:
1485 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_)
1486 r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), file_)
1486
1487
1487 if format == 0:
1488 if format == 0:
1488 ui.write(" rev offset length base linkrev"
1489 ui.write(" rev offset length base linkrev"
@@ -1515,7 +1516,7 b' def debugindexdot(ui, repo, file_):'
1515 if len(filelog):
1516 if len(filelog):
1516 r = filelog
1517 r = filelog
1517 if not r:
1518 if not r:
1518 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_)
1519 r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), file_)
1519 ui.write("digraph G {\n")
1520 ui.write("digraph G {\n")
1520 for i in r:
1521 for i in r:
1521 node = r.node(i)
1522 node = r.node(i)
@@ -10,7 +10,7 b' from i18n import _'
10 import repo, changegroup, subrepo, discovery, pushkey
10 import repo, changegroup, subrepo, discovery, pushkey
11 import changelog, dirstate, filelog, manifest, context, bookmarks
11 import changelog, dirstate, filelog, manifest, context, bookmarks
12 import lock, transaction, store, encoding
12 import lock, transaction, store, encoding
13 import util, extensions, hook, error
13 import scmutil, util, extensions, hook, error
14 import match as matchmod
14 import match as matchmod
15 import merge as mergemod
15 import merge as mergemod
16 import tags as tagsmod
16 import tags as tagsmod
@@ -32,8 +32,8 b' class localrepository(repo.repository):'
32 self.path = os.path.join(self.root, ".hg")
32 self.path = os.path.join(self.root, ".hg")
33 self.origroot = path
33 self.origroot = path
34 self.auditor = util.path_auditor(self.root, self._checknested)
34 self.auditor = util.path_auditor(self.root, self._checknested)
35 self.opener = util.opener(self.path)
35 self.opener = scmutil.opener(self.path)
36 self.wopener = util.opener(self.root)
36 self.wopener = scmutil.opener(self.root)
37 self.baseui = baseui
37 self.baseui = baseui
38 self.ui = baseui.copy()
38 self.ui = baseui.copy()
39
39
@@ -90,7 +90,7 b' class localrepository(repo.repository):'
90 if inst.errno != errno.ENOENT:
90 if inst.errno != errno.ENOENT:
91 raise
91 raise
92
92
93 self.store = store.store(requirements, self.sharedpath, util.opener)
93 self.store = store.store(requirements, self.sharedpath, scmutil.opener)
94 self.spath = self.store.path
94 self.spath = self.store.path
95 self.sopener = self.store.opener
95 self.sopener = self.store.opener
96 self.sjoin = self.store.join
96 self.sjoin = self.store.join
@@ -11,7 +11,7 b' import tempfile, zlib'
11
11
12 from i18n import _
12 from i18n import _
13 from node import hex, nullid, short
13 from node import hex, nullid, short
14 import base85, mdiff, util, diffhelpers, copies, encoding
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding
15
15
16 gitre = re.compile('diff --git a/(.*) b/(.*)')
16 gitre = re.compile('diff --git a/(.*) b/(.*)')
17
17
@@ -1111,7 +1111,7 b' def _applydiff(ui, fp, patcher, copyfn, '
1111 err = 0
1111 err = 0
1112 current_file = None
1112 current_file = None
1113 cwd = os.getcwd()
1113 cwd = os.getcwd()
1114 opener = util.opener(cwd)
1114 opener = scmutil.opener(cwd)
1115
1115
1116 for state, values in iterhunks(ui, fp):
1116 for state, values in iterhunks(ui, fp):
1117 if state == 'hunk':
1117 if state == 'hunk':
@@ -7,7 +7,7 b''
7
7
8 from i18n import _
8 from i18n import _
9 import util, error
9 import util, error
10 import os
10 import os, errno
11
11
12 def checkportable(ui, f):
12 def checkportable(ui, f):
13 '''Check if filename f is portable and warn or abort depending on config'''
13 '''Check if filename f is portable and warn or abort depending on config'''
@@ -25,3 +25,98 b' def checkportable(ui, f):'
25 elif bval is None and lval != 'ignore':
25 elif bval is None and lval != 'ignore':
26 raise error.ConfigError(
26 raise error.ConfigError(
27 _("ui.portablefilenames value is invalid ('%s')") % val)
27 _("ui.portablefilenames value is invalid ('%s')") % val)
28
29 class opener(object):
30 '''Open files relative to a base directory
31
32 This class is used to hide the details of COW semantics and
33 remote file access from higher level code.
34 '''
35 def __init__(self, base, audit=True):
36 self.base = base
37 if audit:
38 self.auditor = util.path_auditor(base)
39 else:
40 self.auditor = util.always
41 self.createmode = None
42 self._trustnlink = None
43
44 @util.propertycache
45 def _can_symlink(self):
46 return util.checklink(self.base)
47
48 def _fixfilemode(self, name):
49 if self.createmode is None:
50 return
51 os.chmod(name, self.createmode & 0666)
52
53 def __call__(self, path, mode="r", text=False, atomictemp=False):
54 r = util.checkosfilename(path)
55 if r:
56 raise Abort("%s: %r" % (r, path))
57 self.auditor(path)
58 f = os.path.join(self.base, path)
59
60 if not text and "b" not in mode:
61 mode += "b" # for that other OS
62
63 nlink = -1
64 dirname, basename = os.path.split(f)
65 # If basename is empty, then the path is malformed because it points
66 # to a directory. Let the posixfile() call below raise IOError.
67 if basename and mode not in ('r', 'rb'):
68 if atomictemp:
69 if not os.path.isdir(dirname):
70 util.makedirs(dirname, self.createmode)
71 return util.atomictempfile(f, mode, self.createmode)
72 try:
73 if 'w' in mode:
74 util.unlink(f)
75 nlink = 0
76 else:
77 # nlinks() may behave differently for files on Windows
78 # shares if the file is open.
79 fd = util.posixfile(f)
80 nlink = util.nlinks(f)
81 if nlink < 1:
82 nlink = 2 # force mktempcopy (issue1922)
83 fd.close()
84 except (OSError, IOError), e:
85 if e.errno != errno.ENOENT:
86 raise
87 nlink = 0
88 if not os.path.isdir(dirname):
89 util.makedirs(dirname, self.createmode)
90 if nlink > 0:
91 if self._trustnlink is None:
92 self._trustnlink = nlink > 1 or util.checknlink(f)
93 if nlink > 1 or not self._trustnlink:
94 util.rename(util.mktempcopy(f), f)
95 fp = util.posixfile(f, mode)
96 if nlink == 0:
97 self._fixfilemode(f)
98 return fp
99
100 def symlink(self, src, dst):
101 self.auditor(dst)
102 linkname = os.path.join(self.base, dst)
103 try:
104 os.unlink(linkname)
105 except OSError:
106 pass
107
108 dirname = os.path.dirname(linkname)
109 if not os.path.exists(dirname):
110 util.makedirs(dirname, self.createmode)
111
112 if self._can_symlink:
113 try:
114 os.symlink(src, linkname)
115 except OSError, err:
116 raise OSError(err.errno, _('could not symlink to %r: %s') %
117 (src, err.strerror), linkname)
118 else:
119 f = self(dst, "w")
120 f.write(src)
121 f.close()
122 self._fixfilemode(dst)
@@ -18,7 +18,7 b''
18 # s: "i hate that."
18 # s: "i hate that."
19
19
20 from i18n import _
20 from i18n import _
21 import util, mdiff
21 import scmutil, util, mdiff
22 import sys, os
22 import sys, os
23
23
24 class CantReprocessAndShowBase(Exception):
24 class CantReprocessAndShowBase(Exception):
@@ -429,7 +429,7 b' def simplemerge(ui, local, base, other, '
429
429
430 local = os.path.realpath(local)
430 local = os.path.realpath(local)
431 if not opts.get('print'):
431 if not opts.get('print'):
432 opener = util.opener(os.path.dirname(local))
432 opener = scmutil.opener(os.path.dirname(local))
433 out = opener(os.path.basename(local), "w", atomictemp=True)
433 out = opener(os.path.basename(local), "w", atomictemp=True)
434 else:
434 else:
435 out = sys.stdout
435 out = sys.stdout
@@ -895,101 +895,6 b' def makedirs(name, mode=None):'
895 makedirs(parent, mode)
895 makedirs(parent, mode)
896 makedirs(name, mode)
896 makedirs(name, mode)
897
897
898 class opener(object):
899 """Open files relative to a base directory
900
901 This class is used to hide the details of COW semantics and
902 remote file access from higher level code.
903 """
904 def __init__(self, base, audit=True):
905 self.base = base
906 if audit:
907 self.auditor = path_auditor(base)
908 else:
909 self.auditor = always
910 self.createmode = None
911 self._trustnlink = None
912
913 @propertycache
914 def _can_symlink(self):
915 return checklink(self.base)
916
917 def _fixfilemode(self, name):
918 if self.createmode is None:
919 return
920 os.chmod(name, self.createmode & 0666)
921
922 def __call__(self, path, mode="r", text=False, atomictemp=False):
923 r = checkosfilename(path)
924 if r:
925 raise Abort("%s: %r" % (r, path))
926 self.auditor(path)
927 f = os.path.join(self.base, path)
928
929 if not text and "b" not in mode:
930 mode += "b" # for that other OS
931
932 nlink = -1
933 dirname, basename = os.path.split(f)
934 # If basename is empty, then the path is malformed because it points
935 # to a directory. Let the posixfile() call below raise IOError.
936 if basename and mode not in ('r', 'rb'):
937 if atomictemp:
938 if not os.path.isdir(dirname):
939 makedirs(dirname, self.createmode)
940 return atomictempfile(f, mode, self.createmode)
941 try:
942 if 'w' in mode:
943 unlink(f)
944 nlink = 0
945 else:
946 # nlinks() may behave differently for files on Windows
947 # shares if the file is open.
948 fd = posixfile(f)
949 nlink = nlinks(f)
950 if nlink < 1:
951 nlink = 2 # force mktempcopy (issue1922)
952 fd.close()
953 except (OSError, IOError), e:
954 if e.errno != errno.ENOENT:
955 raise
956 nlink = 0
957 if not os.path.isdir(dirname):
958 makedirs(dirname, self.createmode)
959 if nlink > 0:
960 if self._trustnlink is None:
961 self._trustnlink = nlink > 1 or checknlink(f)
962 if nlink > 1 or not self._trustnlink:
963 rename(mktempcopy(f), f)
964 fp = posixfile(f, mode)
965 if nlink == 0:
966 self._fixfilemode(f)
967 return fp
968
969 def symlink(self, src, dst):
970 self.auditor(dst)
971 linkname = os.path.join(self.base, dst)
972 try:
973 os.unlink(linkname)
974 except OSError:
975 pass
976
977 dirname = os.path.dirname(linkname)
978 if not os.path.exists(dirname):
979 makedirs(dirname, self.createmode)
980
981 if self._can_symlink:
982 try:
983 os.symlink(src, linkname)
984 except OSError, err:
985 raise OSError(err.errno, _('could not symlink to %r: %s') %
986 (src, err.strerror), linkname)
987 else:
988 f = self(dst, "w")
989 f.write(src)
990 f.close()
991 self._fixfilemode(dst)
992
993 class chunkbuffer(object):
898 class chunkbuffer(object):
994 """Allow arbitrary sized chunks of data to be efficiently read from an
899 """Allow arbitrary sized chunks of data to be efficiently read from an
995 iterator over chunks of arbitrary size."""
900 iterator over chunks of arbitrary size."""
@@ -26,7 +26,7 b' We approximate that by reducing the read'
26 summary: change foo
26 summary: change foo
27
27
28 $ cat >> test.py << EOF
28 $ cat >> test.py << EOF
29 > from mercurial import changelog, util
29 > from mercurial import changelog, scmutil
30 > from mercurial.node import *
30 > from mercurial.node import *
31 >
31 >
32 > class singlebyteread(object):
32 > class singlebyteread(object):
@@ -42,7 +42,7 b' We approximate that by reducing the read'
42 > return getattr(self.real, key)
42 > return getattr(self.real, key)
43 >
43 >
44 > def opener(*args):
44 > def opener(*args):
45 > o = util.opener(*args)
45 > o = scmutil.opener(*args)
46 > def wrapper(*a):
46 > def wrapper(*a):
47 > f = o(*a)
47 > f = o(*a)
48 > return singlebyteread(f)
48 > return singlebyteread(f)
General Comments 0
You need to be logged in to leave comments. Login now