##// END OF EJS Templates
py3: provide bytes stdin/out/err through util module...
Yuya Nishihara -
r30472:277f4fe6 default
parent child Browse files
Show More
@@ -1,222 +1,231 b''
1 # pycompat.py - portability shim for python 3
1 # pycompat.py - portability shim for python 3
2 #
2 #
3 # This software may be used and distributed according to the terms of the
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
4 # GNU General Public License version 2 or any later version.
5
5
6 """Mercurial portability shim for python 3.
6 """Mercurial portability shim for python 3.
7
7
8 This contains aliases to hide python version-specific details from the core.
8 This contains aliases to hide python version-specific details from the core.
9 """
9 """
10
10
11 from __future__ import absolute_import
11 from __future__ import absolute_import
12
12
13 import os
13 import os
14 import sys
14 import sys
15
15
16 ispy3 = (sys.version_info[0] >= 3)
16 ispy3 = (sys.version_info[0] >= 3)
17
17
18 if not ispy3:
18 if not ispy3:
19 import cPickle as pickle
19 import cPickle as pickle
20 import cStringIO as io
20 import cStringIO as io
21 import httplib
21 import httplib
22 import Queue as _queue
22 import Queue as _queue
23 import SocketServer as socketserver
23 import SocketServer as socketserver
24 import urlparse
24 import urlparse
25 urlunquote = urlparse.unquote
25 urlunquote = urlparse.unquote
26 import xmlrpclib
26 import xmlrpclib
27 else:
27 else:
28 import http.client as httplib
28 import http.client as httplib
29 import io
29 import io
30 import pickle
30 import pickle
31 import queue as _queue
31 import queue as _queue
32 import socketserver
32 import socketserver
33 import urllib.parse as urlparse
33 import urllib.parse as urlparse
34 urlunquote = urlparse.unquote_to_bytes
34 urlunquote = urlparse.unquote_to_bytes
35 import xmlrpc.client as xmlrpclib
35 import xmlrpc.client as xmlrpclib
36
36
37 if ispy3:
37 if ispy3:
38 import builtins
38 import builtins
39 import functools
39 import functools
40 fsencode = os.fsencode
40 fsencode = os.fsencode
41 fsdecode = os.fsdecode
41 fsdecode = os.fsdecode
42 # A bytes version of os.name.
42 # A bytes version of os.name.
43 osname = os.name.encode('ascii')
43 osname = os.name.encode('ascii')
44 ospathsep = os.pathsep.encode('ascii')
44 ospathsep = os.pathsep.encode('ascii')
45 ossep = os.sep.encode('ascii')
45 ossep = os.sep.encode('ascii')
46
46
47 # TODO: .buffer might not exist if std streams were replaced; we'll need
48 # a silly wrapper to make a bytes stream backed by a unicode one.
49 stdin = sys.stdin.buffer
50 stdout = sys.stdout.buffer
51 stderr = sys.stderr.buffer
52
47 # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix,
53 # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix,
48 # we can use os.fsencode() to get back bytes argv.
54 # we can use os.fsencode() to get back bytes argv.
49 #
55 #
50 # https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55
56 # https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55
51 #
57 #
52 # TODO: On Windows, the native argv is wchar_t, so we'll need a different
58 # TODO: On Windows, the native argv is wchar_t, so we'll need a different
53 # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
59 # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
54 sysargv = list(map(os.fsencode, sys.argv))
60 sysargv = list(map(os.fsencode, sys.argv))
55
61
56 def sysstr(s):
62 def sysstr(s):
57 """Return a keyword str to be passed to Python functions such as
63 """Return a keyword str to be passed to Python functions such as
58 getattr() and str.encode()
64 getattr() and str.encode()
59
65
60 This never raises UnicodeDecodeError. Non-ascii characters are
66 This never raises UnicodeDecodeError. Non-ascii characters are
61 considered invalid and mapped to arbitrary but unique code points
67 considered invalid and mapped to arbitrary but unique code points
62 such that 'sysstr(a) != sysstr(b)' for all 'a != b'.
68 such that 'sysstr(a) != sysstr(b)' for all 'a != b'.
63 """
69 """
64 if isinstance(s, builtins.str):
70 if isinstance(s, builtins.str):
65 return s
71 return s
66 return s.decode(u'latin-1')
72 return s.decode(u'latin-1')
67
73
68 def _wrapattrfunc(f):
74 def _wrapattrfunc(f):
69 @functools.wraps(f)
75 @functools.wraps(f)
70 def w(object, name, *args):
76 def w(object, name, *args):
71 return f(object, sysstr(name), *args)
77 return f(object, sysstr(name), *args)
72 return w
78 return w
73
79
74 # these wrappers are automagically imported by hgloader
80 # these wrappers are automagically imported by hgloader
75 delattr = _wrapattrfunc(builtins.delattr)
81 delattr = _wrapattrfunc(builtins.delattr)
76 getattr = _wrapattrfunc(builtins.getattr)
82 getattr = _wrapattrfunc(builtins.getattr)
77 hasattr = _wrapattrfunc(builtins.hasattr)
83 hasattr = _wrapattrfunc(builtins.hasattr)
78 setattr = _wrapattrfunc(builtins.setattr)
84 setattr = _wrapattrfunc(builtins.setattr)
79 xrange = builtins.range
85 xrange = builtins.range
80
86
81 else:
87 else:
82 def sysstr(s):
88 def sysstr(s):
83 return s
89 return s
84
90
85 # Partial backport from os.py in Python 3, which only accepts bytes.
91 # Partial backport from os.py in Python 3, which only accepts bytes.
86 # In Python 2, our paths should only ever be bytes, a unicode path
92 # In Python 2, our paths should only ever be bytes, a unicode path
87 # indicates a bug.
93 # indicates a bug.
88 def fsencode(filename):
94 def fsencode(filename):
89 if isinstance(filename, str):
95 if isinstance(filename, str):
90 return filename
96 return filename
91 else:
97 else:
92 raise TypeError(
98 raise TypeError(
93 "expect str, not %s" % type(filename).__name__)
99 "expect str, not %s" % type(filename).__name__)
94
100
95 # In Python 2, fsdecode() has a very chance to receive bytes. So it's
101 # In Python 2, fsdecode() has a very chance to receive bytes. So it's
96 # better not to touch Python 2 part as it's already working fine.
102 # better not to touch Python 2 part as it's already working fine.
97 def fsdecode(filename):
103 def fsdecode(filename):
98 return filename
104 return filename
99
105
100 osname = os.name
106 osname = os.name
101 ospathsep = os.pathsep
107 ospathsep = os.pathsep
102 ossep = os.sep
108 ossep = os.sep
109 stdin = sys.stdin
110 stdout = sys.stdout
111 stderr = sys.stderr
103 sysargv = sys.argv
112 sysargv = sys.argv
104
113
105 stringio = io.StringIO
114 stringio = io.StringIO
106 empty = _queue.Empty
115 empty = _queue.Empty
107 queue = _queue.Queue
116 queue = _queue.Queue
108
117
109 class _pycompatstub(object):
118 class _pycompatstub(object):
110 def __init__(self):
119 def __init__(self):
111 self._aliases = {}
120 self._aliases = {}
112
121
113 def _registeraliases(self, origin, items):
122 def _registeraliases(self, origin, items):
114 """Add items that will be populated at the first access"""
123 """Add items that will be populated at the first access"""
115 items = map(sysstr, items)
124 items = map(sysstr, items)
116 self._aliases.update(
125 self._aliases.update(
117 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item))
126 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item))
118 for item in items)
127 for item in items)
119
128
120 def __getattr__(self, name):
129 def __getattr__(self, name):
121 try:
130 try:
122 origin, item = self._aliases[name]
131 origin, item = self._aliases[name]
123 except KeyError:
132 except KeyError:
124 raise AttributeError(name)
133 raise AttributeError(name)
125 self.__dict__[name] = obj = getattr(origin, item)
134 self.__dict__[name] = obj = getattr(origin, item)
126 return obj
135 return obj
127
136
128 httpserver = _pycompatstub()
137 httpserver = _pycompatstub()
129 urlreq = _pycompatstub()
138 urlreq = _pycompatstub()
130 urlerr = _pycompatstub()
139 urlerr = _pycompatstub()
131 if not ispy3:
140 if not ispy3:
132 import BaseHTTPServer
141 import BaseHTTPServer
133 import CGIHTTPServer
142 import CGIHTTPServer
134 import SimpleHTTPServer
143 import SimpleHTTPServer
135 import urllib2
144 import urllib2
136 import urllib
145 import urllib
137 urlreq._registeraliases(urllib, (
146 urlreq._registeraliases(urllib, (
138 "addclosehook",
147 "addclosehook",
139 "addinfourl",
148 "addinfourl",
140 "ftpwrapper",
149 "ftpwrapper",
141 "pathname2url",
150 "pathname2url",
142 "quote",
151 "quote",
143 "splitattr",
152 "splitattr",
144 "splitpasswd",
153 "splitpasswd",
145 "splitport",
154 "splitport",
146 "splituser",
155 "splituser",
147 "unquote",
156 "unquote",
148 "url2pathname",
157 "url2pathname",
149 "urlencode",
158 "urlencode",
150 ))
159 ))
151 urlreq._registeraliases(urllib2, (
160 urlreq._registeraliases(urllib2, (
152 "AbstractHTTPHandler",
161 "AbstractHTTPHandler",
153 "BaseHandler",
162 "BaseHandler",
154 "build_opener",
163 "build_opener",
155 "FileHandler",
164 "FileHandler",
156 "FTPHandler",
165 "FTPHandler",
157 "HTTPBasicAuthHandler",
166 "HTTPBasicAuthHandler",
158 "HTTPDigestAuthHandler",
167 "HTTPDigestAuthHandler",
159 "HTTPHandler",
168 "HTTPHandler",
160 "HTTPPasswordMgrWithDefaultRealm",
169 "HTTPPasswordMgrWithDefaultRealm",
161 "HTTPSHandler",
170 "HTTPSHandler",
162 "install_opener",
171 "install_opener",
163 "ProxyHandler",
172 "ProxyHandler",
164 "Request",
173 "Request",
165 "urlopen",
174 "urlopen",
166 ))
175 ))
167 urlerr._registeraliases(urllib2, (
176 urlerr._registeraliases(urllib2, (
168 "HTTPError",
177 "HTTPError",
169 "URLError",
178 "URLError",
170 ))
179 ))
171 httpserver._registeraliases(BaseHTTPServer, (
180 httpserver._registeraliases(BaseHTTPServer, (
172 "HTTPServer",
181 "HTTPServer",
173 "BaseHTTPRequestHandler",
182 "BaseHTTPRequestHandler",
174 ))
183 ))
175 httpserver._registeraliases(SimpleHTTPServer, (
184 httpserver._registeraliases(SimpleHTTPServer, (
176 "SimpleHTTPRequestHandler",
185 "SimpleHTTPRequestHandler",
177 ))
186 ))
178 httpserver._registeraliases(CGIHTTPServer, (
187 httpserver._registeraliases(CGIHTTPServer, (
179 "CGIHTTPRequestHandler",
188 "CGIHTTPRequestHandler",
180 ))
189 ))
181
190
182 else:
191 else:
183 import urllib.request
192 import urllib.request
184 urlreq._registeraliases(urllib.request, (
193 urlreq._registeraliases(urllib.request, (
185 "AbstractHTTPHandler",
194 "AbstractHTTPHandler",
186 "addclosehook",
195 "addclosehook",
187 "addinfourl",
196 "addinfourl",
188 "BaseHandler",
197 "BaseHandler",
189 "build_opener",
198 "build_opener",
190 "FileHandler",
199 "FileHandler",
191 "FTPHandler",
200 "FTPHandler",
192 "ftpwrapper",
201 "ftpwrapper",
193 "HTTPHandler",
202 "HTTPHandler",
194 "HTTPSHandler",
203 "HTTPSHandler",
195 "install_opener",
204 "install_opener",
196 "pathname2url",
205 "pathname2url",
197 "HTTPBasicAuthHandler",
206 "HTTPBasicAuthHandler",
198 "HTTPDigestAuthHandler",
207 "HTTPDigestAuthHandler",
199 "HTTPPasswordMgrWithDefaultRealm",
208 "HTTPPasswordMgrWithDefaultRealm",
200 "ProxyHandler",
209 "ProxyHandler",
201 "quote",
210 "quote",
202 "Request",
211 "Request",
203 "splitattr",
212 "splitattr",
204 "splitpasswd",
213 "splitpasswd",
205 "splitport",
214 "splitport",
206 "splituser",
215 "splituser",
207 "unquote",
216 "unquote",
208 "url2pathname",
217 "url2pathname",
209 "urlopen",
218 "urlopen",
210 ))
219 ))
211 import urllib.error
220 import urllib.error
212 urlerr._registeraliases(urllib.error, (
221 urlerr._registeraliases(urllib.error, (
213 "HTTPError",
222 "HTTPError",
214 "URLError",
223 "URLError",
215 ))
224 ))
216 import http.server
225 import http.server
217 httpserver._registeraliases(http.server, (
226 httpserver._registeraliases(http.server, (
218 "HTTPServer",
227 "HTTPServer",
219 "BaseHTTPRequestHandler",
228 "BaseHTTPRequestHandler",
220 "SimpleHTTPRequestHandler",
229 "SimpleHTTPRequestHandler",
221 "CGIHTTPRequestHandler",
230 "CGIHTTPRequestHandler",
222 ))
231 ))
@@ -1,3238 +1,3242 b''
1 # util.py - Mercurial utility functions and platform specific implementations
1 # util.py - Mercurial utility functions and platform specific implementations
2 #
2 #
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 """Mercurial utility functions and platform specific implementations.
10 """Mercurial utility functions and platform specific implementations.
11
11
12 This contains helper routines that are independent of the SCM core and
12 This contains helper routines that are independent of the SCM core and
13 hide platform-specific details from the core.
13 hide platform-specific details from the core.
14 """
14 """
15
15
16 from __future__ import absolute_import
16 from __future__ import absolute_import
17
17
18 import bz2
18 import bz2
19 import calendar
19 import calendar
20 import collections
20 import collections
21 import datetime
21 import datetime
22 import errno
22 import errno
23 import gc
23 import gc
24 import hashlib
24 import hashlib
25 import imp
25 import imp
26 import os
26 import os
27 import platform as pyplatform
27 import platform as pyplatform
28 import re as remod
28 import re as remod
29 import shutil
29 import shutil
30 import signal
30 import signal
31 import socket
31 import socket
32 import stat
32 import stat
33 import string
33 import string
34 import subprocess
34 import subprocess
35 import sys
35 import sys
36 import tempfile
36 import tempfile
37 import textwrap
37 import textwrap
38 import time
38 import time
39 import traceback
39 import traceback
40 import zlib
40 import zlib
41
41
42 from . import (
42 from . import (
43 encoding,
43 encoding,
44 error,
44 error,
45 i18n,
45 i18n,
46 osutil,
46 osutil,
47 parsers,
47 parsers,
48 pycompat,
48 pycompat,
49 )
49 )
50
50
51 empty = pycompat.empty
51 empty = pycompat.empty
52 httplib = pycompat.httplib
52 httplib = pycompat.httplib
53 httpserver = pycompat.httpserver
53 httpserver = pycompat.httpserver
54 pickle = pycompat.pickle
54 pickle = pycompat.pickle
55 queue = pycompat.queue
55 queue = pycompat.queue
56 socketserver = pycompat.socketserver
56 socketserver = pycompat.socketserver
57 stderr = pycompat.stderr
58 stdin = pycompat.stdin
59 stdout = pycompat.stdout
57 stringio = pycompat.stringio
60 stringio = pycompat.stringio
58 urlerr = pycompat.urlerr
61 urlerr = pycompat.urlerr
59 urlparse = pycompat.urlparse
62 urlparse = pycompat.urlparse
60 urlreq = pycompat.urlreq
63 urlreq = pycompat.urlreq
61 xmlrpclib = pycompat.xmlrpclib
64 xmlrpclib = pycompat.xmlrpclib
62
65
63 if os.name == 'nt':
66 if os.name == 'nt':
64 from . import windows as platform
67 from . import windows as platform
68 stdout = platform.winstdout(pycompat.stdout)
65 else:
69 else:
66 from . import posix as platform
70 from . import posix as platform
67
71
68 _ = i18n._
72 _ = i18n._
69
73
70 bindunixsocket = platform.bindunixsocket
74 bindunixsocket = platform.bindunixsocket
71 cachestat = platform.cachestat
75 cachestat = platform.cachestat
72 checkexec = platform.checkexec
76 checkexec = platform.checkexec
73 checklink = platform.checklink
77 checklink = platform.checklink
74 copymode = platform.copymode
78 copymode = platform.copymode
75 executablepath = platform.executablepath
79 executablepath = platform.executablepath
76 expandglobs = platform.expandglobs
80 expandglobs = platform.expandglobs
77 explainexit = platform.explainexit
81 explainexit = platform.explainexit
78 findexe = platform.findexe
82 findexe = platform.findexe
79 gethgcmd = platform.gethgcmd
83 gethgcmd = platform.gethgcmd
80 getuser = platform.getuser
84 getuser = platform.getuser
81 getpid = os.getpid
85 getpid = os.getpid
82 groupmembers = platform.groupmembers
86 groupmembers = platform.groupmembers
83 groupname = platform.groupname
87 groupname = platform.groupname
84 hidewindow = platform.hidewindow
88 hidewindow = platform.hidewindow
85 isexec = platform.isexec
89 isexec = platform.isexec
86 isowner = platform.isowner
90 isowner = platform.isowner
87 localpath = platform.localpath
91 localpath = platform.localpath
88 lookupreg = platform.lookupreg
92 lookupreg = platform.lookupreg
89 makedir = platform.makedir
93 makedir = platform.makedir
90 nlinks = platform.nlinks
94 nlinks = platform.nlinks
91 normpath = platform.normpath
95 normpath = platform.normpath
92 normcase = platform.normcase
96 normcase = platform.normcase
93 normcasespec = platform.normcasespec
97 normcasespec = platform.normcasespec
94 normcasefallback = platform.normcasefallback
98 normcasefallback = platform.normcasefallback
95 openhardlinks = platform.openhardlinks
99 openhardlinks = platform.openhardlinks
96 oslink = platform.oslink
100 oslink = platform.oslink
97 parsepatchoutput = platform.parsepatchoutput
101 parsepatchoutput = platform.parsepatchoutput
98 pconvert = platform.pconvert
102 pconvert = platform.pconvert
99 poll = platform.poll
103 poll = platform.poll
100 popen = platform.popen
104 popen = platform.popen
101 posixfile = platform.posixfile
105 posixfile = platform.posixfile
102 quotecommand = platform.quotecommand
106 quotecommand = platform.quotecommand
103 readpipe = platform.readpipe
107 readpipe = platform.readpipe
104 rename = platform.rename
108 rename = platform.rename
105 removedirs = platform.removedirs
109 removedirs = platform.removedirs
106 samedevice = platform.samedevice
110 samedevice = platform.samedevice
107 samefile = platform.samefile
111 samefile = platform.samefile
108 samestat = platform.samestat
112 samestat = platform.samestat
109 setbinary = platform.setbinary
113 setbinary = platform.setbinary
110 setflags = platform.setflags
114 setflags = platform.setflags
111 setsignalhandler = platform.setsignalhandler
115 setsignalhandler = platform.setsignalhandler
112 shellquote = platform.shellquote
116 shellquote = platform.shellquote
113 spawndetached = platform.spawndetached
117 spawndetached = platform.spawndetached
114 split = platform.split
118 split = platform.split
115 sshargs = platform.sshargs
119 sshargs = platform.sshargs
116 statfiles = getattr(osutil, 'statfiles', platform.statfiles)
120 statfiles = getattr(osutil, 'statfiles', platform.statfiles)
117 statisexec = platform.statisexec
121 statisexec = platform.statisexec
118 statislink = platform.statislink
122 statislink = platform.statislink
119 testpid = platform.testpid
123 testpid = platform.testpid
120 umask = platform.umask
124 umask = platform.umask
121 unlink = platform.unlink
125 unlink = platform.unlink
122 unlinkpath = platform.unlinkpath
126 unlinkpath = platform.unlinkpath
123 username = platform.username
127 username = platform.username
124
128
125 # Python compatibility
129 # Python compatibility
126
130
127 _notset = object()
131 _notset = object()
128
132
129 # disable Python's problematic floating point timestamps (issue4836)
133 # disable Python's problematic floating point timestamps (issue4836)
130 # (Python hypocritically says you shouldn't change this behavior in
134 # (Python hypocritically says you shouldn't change this behavior in
131 # libraries, and sure enough Mercurial is not a library.)
135 # libraries, and sure enough Mercurial is not a library.)
132 os.stat_float_times(False)
136 os.stat_float_times(False)
133
137
134 def safehasattr(thing, attr):
138 def safehasattr(thing, attr):
135 return getattr(thing, attr, _notset) is not _notset
139 return getattr(thing, attr, _notset) is not _notset
136
140
137 DIGESTS = {
141 DIGESTS = {
138 'md5': hashlib.md5,
142 'md5': hashlib.md5,
139 'sha1': hashlib.sha1,
143 'sha1': hashlib.sha1,
140 'sha512': hashlib.sha512,
144 'sha512': hashlib.sha512,
141 }
145 }
142 # List of digest types from strongest to weakest
146 # List of digest types from strongest to weakest
143 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
147 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
144
148
145 for k in DIGESTS_BY_STRENGTH:
149 for k in DIGESTS_BY_STRENGTH:
146 assert k in DIGESTS
150 assert k in DIGESTS
147
151
148 class digester(object):
152 class digester(object):
149 """helper to compute digests.
153 """helper to compute digests.
150
154
151 This helper can be used to compute one or more digests given their name.
155 This helper can be used to compute one or more digests given their name.
152
156
153 >>> d = digester(['md5', 'sha1'])
157 >>> d = digester(['md5', 'sha1'])
154 >>> d.update('foo')
158 >>> d.update('foo')
155 >>> [k for k in sorted(d)]
159 >>> [k for k in sorted(d)]
156 ['md5', 'sha1']
160 ['md5', 'sha1']
157 >>> d['md5']
161 >>> d['md5']
158 'acbd18db4cc2f85cedef654fccc4a4d8'
162 'acbd18db4cc2f85cedef654fccc4a4d8'
159 >>> d['sha1']
163 >>> d['sha1']
160 '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
164 '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
161 >>> digester.preferred(['md5', 'sha1'])
165 >>> digester.preferred(['md5', 'sha1'])
162 'sha1'
166 'sha1'
163 """
167 """
164
168
165 def __init__(self, digests, s=''):
169 def __init__(self, digests, s=''):
166 self._hashes = {}
170 self._hashes = {}
167 for k in digests:
171 for k in digests:
168 if k not in DIGESTS:
172 if k not in DIGESTS:
169 raise Abort(_('unknown digest type: %s') % k)
173 raise Abort(_('unknown digest type: %s') % k)
170 self._hashes[k] = DIGESTS[k]()
174 self._hashes[k] = DIGESTS[k]()
171 if s:
175 if s:
172 self.update(s)
176 self.update(s)
173
177
174 def update(self, data):
178 def update(self, data):
175 for h in self._hashes.values():
179 for h in self._hashes.values():
176 h.update(data)
180 h.update(data)
177
181
178 def __getitem__(self, key):
182 def __getitem__(self, key):
179 if key not in DIGESTS:
183 if key not in DIGESTS:
180 raise Abort(_('unknown digest type: %s') % k)
184 raise Abort(_('unknown digest type: %s') % k)
181 return self._hashes[key].hexdigest()
185 return self._hashes[key].hexdigest()
182
186
183 def __iter__(self):
187 def __iter__(self):
184 return iter(self._hashes)
188 return iter(self._hashes)
185
189
186 @staticmethod
190 @staticmethod
187 def preferred(supported):
191 def preferred(supported):
188 """returns the strongest digest type in both supported and DIGESTS."""
192 """returns the strongest digest type in both supported and DIGESTS."""
189
193
190 for k in DIGESTS_BY_STRENGTH:
194 for k in DIGESTS_BY_STRENGTH:
191 if k in supported:
195 if k in supported:
192 return k
196 return k
193 return None
197 return None
194
198
195 class digestchecker(object):
199 class digestchecker(object):
196 """file handle wrapper that additionally checks content against a given
200 """file handle wrapper that additionally checks content against a given
197 size and digests.
201 size and digests.
198
202
199 d = digestchecker(fh, size, {'md5': '...'})
203 d = digestchecker(fh, size, {'md5': '...'})
200
204
201 When multiple digests are given, all of them are validated.
205 When multiple digests are given, all of them are validated.
202 """
206 """
203
207
204 def __init__(self, fh, size, digests):
208 def __init__(self, fh, size, digests):
205 self._fh = fh
209 self._fh = fh
206 self._size = size
210 self._size = size
207 self._got = 0
211 self._got = 0
208 self._digests = dict(digests)
212 self._digests = dict(digests)
209 self._digester = digester(self._digests.keys())
213 self._digester = digester(self._digests.keys())
210
214
211 def read(self, length=-1):
215 def read(self, length=-1):
212 content = self._fh.read(length)
216 content = self._fh.read(length)
213 self._digester.update(content)
217 self._digester.update(content)
214 self._got += len(content)
218 self._got += len(content)
215 return content
219 return content
216
220
217 def validate(self):
221 def validate(self):
218 if self._size != self._got:
222 if self._size != self._got:
219 raise Abort(_('size mismatch: expected %d, got %d') %
223 raise Abort(_('size mismatch: expected %d, got %d') %
220 (self._size, self._got))
224 (self._size, self._got))
221 for k, v in self._digests.items():
225 for k, v in self._digests.items():
222 if v != self._digester[k]:
226 if v != self._digester[k]:
223 # i18n: first parameter is a digest name
227 # i18n: first parameter is a digest name
224 raise Abort(_('%s mismatch: expected %s, got %s') %
228 raise Abort(_('%s mismatch: expected %s, got %s') %
225 (k, v, self._digester[k]))
229 (k, v, self._digester[k]))
226
230
227 try:
231 try:
228 buffer = buffer
232 buffer = buffer
229 except NameError:
233 except NameError:
230 if not pycompat.ispy3:
234 if not pycompat.ispy3:
231 def buffer(sliceable, offset=0):
235 def buffer(sliceable, offset=0):
232 return sliceable[offset:]
236 return sliceable[offset:]
233 else:
237 else:
234 def buffer(sliceable, offset=0):
238 def buffer(sliceable, offset=0):
235 return memoryview(sliceable)[offset:]
239 return memoryview(sliceable)[offset:]
236
240
237 closefds = os.name == 'posix'
241 closefds = os.name == 'posix'
238
242
239 _chunksize = 4096
243 _chunksize = 4096
240
244
241 class bufferedinputpipe(object):
245 class bufferedinputpipe(object):
242 """a manually buffered input pipe
246 """a manually buffered input pipe
243
247
244 Python will not let us use buffered IO and lazy reading with 'polling' at
248 Python will not let us use buffered IO and lazy reading with 'polling' at
245 the same time. We cannot probe the buffer state and select will not detect
249 the same time. We cannot probe the buffer state and select will not detect
246 that data are ready to read if they are already buffered.
250 that data are ready to read if they are already buffered.
247
251
248 This class let us work around that by implementing its own buffering
252 This class let us work around that by implementing its own buffering
249 (allowing efficient readline) while offering a way to know if the buffer is
253 (allowing efficient readline) while offering a way to know if the buffer is
250 empty from the output (allowing collaboration of the buffer with polling).
254 empty from the output (allowing collaboration of the buffer with polling).
251
255
252 This class lives in the 'util' module because it makes use of the 'os'
256 This class lives in the 'util' module because it makes use of the 'os'
253 module from the python stdlib.
257 module from the python stdlib.
254 """
258 """
255
259
256 def __init__(self, input):
260 def __init__(self, input):
257 self._input = input
261 self._input = input
258 self._buffer = []
262 self._buffer = []
259 self._eof = False
263 self._eof = False
260 self._lenbuf = 0
264 self._lenbuf = 0
261
265
262 @property
266 @property
263 def hasbuffer(self):
267 def hasbuffer(self):
264 """True is any data is currently buffered
268 """True is any data is currently buffered
265
269
266 This will be used externally a pre-step for polling IO. If there is
270 This will be used externally a pre-step for polling IO. If there is
267 already data then no polling should be set in place."""
271 already data then no polling should be set in place."""
268 return bool(self._buffer)
272 return bool(self._buffer)
269
273
270 @property
274 @property
271 def closed(self):
275 def closed(self):
272 return self._input.closed
276 return self._input.closed
273
277
274 def fileno(self):
278 def fileno(self):
275 return self._input.fileno()
279 return self._input.fileno()
276
280
277 def close(self):
281 def close(self):
278 return self._input.close()
282 return self._input.close()
279
283
280 def read(self, size):
284 def read(self, size):
281 while (not self._eof) and (self._lenbuf < size):
285 while (not self._eof) and (self._lenbuf < size):
282 self._fillbuffer()
286 self._fillbuffer()
283 return self._frombuffer(size)
287 return self._frombuffer(size)
284
288
285 def readline(self, *args, **kwargs):
289 def readline(self, *args, **kwargs):
286 if 1 < len(self._buffer):
290 if 1 < len(self._buffer):
287 # this should not happen because both read and readline end with a
291 # this should not happen because both read and readline end with a
288 # _frombuffer call that collapse it.
292 # _frombuffer call that collapse it.
289 self._buffer = [''.join(self._buffer)]
293 self._buffer = [''.join(self._buffer)]
290 self._lenbuf = len(self._buffer[0])
294 self._lenbuf = len(self._buffer[0])
291 lfi = -1
295 lfi = -1
292 if self._buffer:
296 if self._buffer:
293 lfi = self._buffer[-1].find('\n')
297 lfi = self._buffer[-1].find('\n')
294 while (not self._eof) and lfi < 0:
298 while (not self._eof) and lfi < 0:
295 self._fillbuffer()
299 self._fillbuffer()
296 if self._buffer:
300 if self._buffer:
297 lfi = self._buffer[-1].find('\n')
301 lfi = self._buffer[-1].find('\n')
298 size = lfi + 1
302 size = lfi + 1
299 if lfi < 0: # end of file
303 if lfi < 0: # end of file
300 size = self._lenbuf
304 size = self._lenbuf
301 elif 1 < len(self._buffer):
305 elif 1 < len(self._buffer):
302 # we need to take previous chunks into account
306 # we need to take previous chunks into account
303 size += self._lenbuf - len(self._buffer[-1])
307 size += self._lenbuf - len(self._buffer[-1])
304 return self._frombuffer(size)
308 return self._frombuffer(size)
305
309
306 def _frombuffer(self, size):
310 def _frombuffer(self, size):
307 """return at most 'size' data from the buffer
311 """return at most 'size' data from the buffer
308
312
309 The data are removed from the buffer."""
313 The data are removed from the buffer."""
310 if size == 0 or not self._buffer:
314 if size == 0 or not self._buffer:
311 return ''
315 return ''
312 buf = self._buffer[0]
316 buf = self._buffer[0]
313 if 1 < len(self._buffer):
317 if 1 < len(self._buffer):
314 buf = ''.join(self._buffer)
318 buf = ''.join(self._buffer)
315
319
316 data = buf[:size]
320 data = buf[:size]
317 buf = buf[len(data):]
321 buf = buf[len(data):]
318 if buf:
322 if buf:
319 self._buffer = [buf]
323 self._buffer = [buf]
320 self._lenbuf = len(buf)
324 self._lenbuf = len(buf)
321 else:
325 else:
322 self._buffer = []
326 self._buffer = []
323 self._lenbuf = 0
327 self._lenbuf = 0
324 return data
328 return data
325
329
326 def _fillbuffer(self):
330 def _fillbuffer(self):
327 """read data to the buffer"""
331 """read data to the buffer"""
328 data = os.read(self._input.fileno(), _chunksize)
332 data = os.read(self._input.fileno(), _chunksize)
329 if not data:
333 if not data:
330 self._eof = True
334 self._eof = True
331 else:
335 else:
332 self._lenbuf += len(data)
336 self._lenbuf += len(data)
333 self._buffer.append(data)
337 self._buffer.append(data)
334
338
335 def popen2(cmd, env=None, newlines=False):
339 def popen2(cmd, env=None, newlines=False):
336 # Setting bufsize to -1 lets the system decide the buffer size.
340 # Setting bufsize to -1 lets the system decide the buffer size.
337 # The default for bufsize is 0, meaning unbuffered. This leads to
341 # The default for bufsize is 0, meaning unbuffered. This leads to
338 # poor performance on Mac OS X: http://bugs.python.org/issue4194
342 # poor performance on Mac OS X: http://bugs.python.org/issue4194
339 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
343 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
340 close_fds=closefds,
344 close_fds=closefds,
341 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
345 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
342 universal_newlines=newlines,
346 universal_newlines=newlines,
343 env=env)
347 env=env)
344 return p.stdin, p.stdout
348 return p.stdin, p.stdout
345
349
346 def popen3(cmd, env=None, newlines=False):
350 def popen3(cmd, env=None, newlines=False):
347 stdin, stdout, stderr, p = popen4(cmd, env, newlines)
351 stdin, stdout, stderr, p = popen4(cmd, env, newlines)
348 return stdin, stdout, stderr
352 return stdin, stdout, stderr
349
353
350 def popen4(cmd, env=None, newlines=False, bufsize=-1):
354 def popen4(cmd, env=None, newlines=False, bufsize=-1):
351 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
355 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
352 close_fds=closefds,
356 close_fds=closefds,
353 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
357 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
354 stderr=subprocess.PIPE,
358 stderr=subprocess.PIPE,
355 universal_newlines=newlines,
359 universal_newlines=newlines,
356 env=env)
360 env=env)
357 return p.stdin, p.stdout, p.stderr, p
361 return p.stdin, p.stdout, p.stderr, p
358
362
359 def version():
363 def version():
360 """Return version information if available."""
364 """Return version information if available."""
361 try:
365 try:
362 from . import __version__
366 from . import __version__
363 return __version__.version
367 return __version__.version
364 except ImportError:
368 except ImportError:
365 return 'unknown'
369 return 'unknown'
366
370
367 def versiontuple(v=None, n=4):
371 def versiontuple(v=None, n=4):
368 """Parses a Mercurial version string into an N-tuple.
372 """Parses a Mercurial version string into an N-tuple.
369
373
370 The version string to be parsed is specified with the ``v`` argument.
374 The version string to be parsed is specified with the ``v`` argument.
371 If it isn't defined, the current Mercurial version string will be parsed.
375 If it isn't defined, the current Mercurial version string will be parsed.
372
376
373 ``n`` can be 2, 3, or 4. Here is how some version strings map to
377 ``n`` can be 2, 3, or 4. Here is how some version strings map to
374 returned values:
378 returned values:
375
379
376 >>> v = '3.6.1+190-df9b73d2d444'
380 >>> v = '3.6.1+190-df9b73d2d444'
377 >>> versiontuple(v, 2)
381 >>> versiontuple(v, 2)
378 (3, 6)
382 (3, 6)
379 >>> versiontuple(v, 3)
383 >>> versiontuple(v, 3)
380 (3, 6, 1)
384 (3, 6, 1)
381 >>> versiontuple(v, 4)
385 >>> versiontuple(v, 4)
382 (3, 6, 1, '190-df9b73d2d444')
386 (3, 6, 1, '190-df9b73d2d444')
383
387
384 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
388 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
385 (3, 6, 1, '190-df9b73d2d444+20151118')
389 (3, 6, 1, '190-df9b73d2d444+20151118')
386
390
387 >>> v = '3.6'
391 >>> v = '3.6'
388 >>> versiontuple(v, 2)
392 >>> versiontuple(v, 2)
389 (3, 6)
393 (3, 6)
390 >>> versiontuple(v, 3)
394 >>> versiontuple(v, 3)
391 (3, 6, None)
395 (3, 6, None)
392 >>> versiontuple(v, 4)
396 >>> versiontuple(v, 4)
393 (3, 6, None, None)
397 (3, 6, None, None)
394
398
395 >>> v = '3.9-rc'
399 >>> v = '3.9-rc'
396 >>> versiontuple(v, 2)
400 >>> versiontuple(v, 2)
397 (3, 9)
401 (3, 9)
398 >>> versiontuple(v, 3)
402 >>> versiontuple(v, 3)
399 (3, 9, None)
403 (3, 9, None)
400 >>> versiontuple(v, 4)
404 >>> versiontuple(v, 4)
401 (3, 9, None, 'rc')
405 (3, 9, None, 'rc')
402
406
403 >>> v = '3.9-rc+2-02a8fea4289b'
407 >>> v = '3.9-rc+2-02a8fea4289b'
404 >>> versiontuple(v, 2)
408 >>> versiontuple(v, 2)
405 (3, 9)
409 (3, 9)
406 >>> versiontuple(v, 3)
410 >>> versiontuple(v, 3)
407 (3, 9, None)
411 (3, 9, None)
408 >>> versiontuple(v, 4)
412 >>> versiontuple(v, 4)
409 (3, 9, None, 'rc+2-02a8fea4289b')
413 (3, 9, None, 'rc+2-02a8fea4289b')
410 """
414 """
411 if not v:
415 if not v:
412 v = version()
416 v = version()
413 parts = remod.split('[\+-]', v, 1)
417 parts = remod.split('[\+-]', v, 1)
414 if len(parts) == 1:
418 if len(parts) == 1:
415 vparts, extra = parts[0], None
419 vparts, extra = parts[0], None
416 else:
420 else:
417 vparts, extra = parts
421 vparts, extra = parts
418
422
419 vints = []
423 vints = []
420 for i in vparts.split('.'):
424 for i in vparts.split('.'):
421 try:
425 try:
422 vints.append(int(i))
426 vints.append(int(i))
423 except ValueError:
427 except ValueError:
424 break
428 break
425 # (3, 6) -> (3, 6, None)
429 # (3, 6) -> (3, 6, None)
426 while len(vints) < 3:
430 while len(vints) < 3:
427 vints.append(None)
431 vints.append(None)
428
432
429 if n == 2:
433 if n == 2:
430 return (vints[0], vints[1])
434 return (vints[0], vints[1])
431 if n == 3:
435 if n == 3:
432 return (vints[0], vints[1], vints[2])
436 return (vints[0], vints[1], vints[2])
433 if n == 4:
437 if n == 4:
434 return (vints[0], vints[1], vints[2], extra)
438 return (vints[0], vints[1], vints[2], extra)
435
439
436 # used by parsedate
440 # used by parsedate
437 defaultdateformats = (
441 defaultdateformats = (
438 '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601
442 '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601
439 '%Y-%m-%dT%H:%M', # without seconds
443 '%Y-%m-%dT%H:%M', # without seconds
440 '%Y-%m-%dT%H%M%S', # another awful but legal variant without :
444 '%Y-%m-%dT%H%M%S', # another awful but legal variant without :
441 '%Y-%m-%dT%H%M', # without seconds
445 '%Y-%m-%dT%H%M', # without seconds
442 '%Y-%m-%d %H:%M:%S', # our common legal variant
446 '%Y-%m-%d %H:%M:%S', # our common legal variant
443 '%Y-%m-%d %H:%M', # without seconds
447 '%Y-%m-%d %H:%M', # without seconds
444 '%Y-%m-%d %H%M%S', # without :
448 '%Y-%m-%d %H%M%S', # without :
445 '%Y-%m-%d %H%M', # without seconds
449 '%Y-%m-%d %H%M', # without seconds
446 '%Y-%m-%d %I:%M:%S%p',
450 '%Y-%m-%d %I:%M:%S%p',
447 '%Y-%m-%d %H:%M',
451 '%Y-%m-%d %H:%M',
448 '%Y-%m-%d %I:%M%p',
452 '%Y-%m-%d %I:%M%p',
449 '%Y-%m-%d',
453 '%Y-%m-%d',
450 '%m-%d',
454 '%m-%d',
451 '%m/%d',
455 '%m/%d',
452 '%m/%d/%y',
456 '%m/%d/%y',
453 '%m/%d/%Y',
457 '%m/%d/%Y',
454 '%a %b %d %H:%M:%S %Y',
458 '%a %b %d %H:%M:%S %Y',
455 '%a %b %d %I:%M:%S%p %Y',
459 '%a %b %d %I:%M:%S%p %Y',
456 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
460 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
457 '%b %d %H:%M:%S %Y',
461 '%b %d %H:%M:%S %Y',
458 '%b %d %I:%M:%S%p %Y',
462 '%b %d %I:%M:%S%p %Y',
459 '%b %d %H:%M:%S',
463 '%b %d %H:%M:%S',
460 '%b %d %I:%M:%S%p',
464 '%b %d %I:%M:%S%p',
461 '%b %d %H:%M',
465 '%b %d %H:%M',
462 '%b %d %I:%M%p',
466 '%b %d %I:%M%p',
463 '%b %d %Y',
467 '%b %d %Y',
464 '%b %d',
468 '%b %d',
465 '%H:%M:%S',
469 '%H:%M:%S',
466 '%I:%M:%S%p',
470 '%I:%M:%S%p',
467 '%H:%M',
471 '%H:%M',
468 '%I:%M%p',
472 '%I:%M%p',
469 )
473 )
470
474
471 extendeddateformats = defaultdateformats + (
475 extendeddateformats = defaultdateformats + (
472 "%Y",
476 "%Y",
473 "%Y-%m",
477 "%Y-%m",
474 "%b",
478 "%b",
475 "%b %Y",
479 "%b %Y",
476 )
480 )
477
481
478 def cachefunc(func):
482 def cachefunc(func):
479 '''cache the result of function calls'''
483 '''cache the result of function calls'''
480 # XXX doesn't handle keywords args
484 # XXX doesn't handle keywords args
481 if func.__code__.co_argcount == 0:
485 if func.__code__.co_argcount == 0:
482 cache = []
486 cache = []
483 def f():
487 def f():
484 if len(cache) == 0:
488 if len(cache) == 0:
485 cache.append(func())
489 cache.append(func())
486 return cache[0]
490 return cache[0]
487 return f
491 return f
488 cache = {}
492 cache = {}
489 if func.__code__.co_argcount == 1:
493 if func.__code__.co_argcount == 1:
490 # we gain a small amount of time because
494 # we gain a small amount of time because
491 # we don't need to pack/unpack the list
495 # we don't need to pack/unpack the list
492 def f(arg):
496 def f(arg):
493 if arg not in cache:
497 if arg not in cache:
494 cache[arg] = func(arg)
498 cache[arg] = func(arg)
495 return cache[arg]
499 return cache[arg]
496 else:
500 else:
497 def f(*args):
501 def f(*args):
498 if args not in cache:
502 if args not in cache:
499 cache[args] = func(*args)
503 cache[args] = func(*args)
500 return cache[args]
504 return cache[args]
501
505
502 return f
506 return f
503
507
504 class sortdict(dict):
508 class sortdict(dict):
505 '''a simple sorted dictionary'''
509 '''a simple sorted dictionary'''
506 def __init__(self, data=None):
510 def __init__(self, data=None):
507 self._list = []
511 self._list = []
508 if data:
512 if data:
509 self.update(data)
513 self.update(data)
510 def copy(self):
514 def copy(self):
511 return sortdict(self)
515 return sortdict(self)
512 def __setitem__(self, key, val):
516 def __setitem__(self, key, val):
513 if key in self:
517 if key in self:
514 self._list.remove(key)
518 self._list.remove(key)
515 self._list.append(key)
519 self._list.append(key)
516 dict.__setitem__(self, key, val)
520 dict.__setitem__(self, key, val)
517 def __iter__(self):
521 def __iter__(self):
518 return self._list.__iter__()
522 return self._list.__iter__()
519 def update(self, src):
523 def update(self, src):
520 if isinstance(src, dict):
524 if isinstance(src, dict):
521 src = src.iteritems()
525 src = src.iteritems()
522 for k, v in src:
526 for k, v in src:
523 self[k] = v
527 self[k] = v
524 def clear(self):
528 def clear(self):
525 dict.clear(self)
529 dict.clear(self)
526 self._list = []
530 self._list = []
527 def items(self):
531 def items(self):
528 return [(k, self[k]) for k in self._list]
532 return [(k, self[k]) for k in self._list]
529 def __delitem__(self, key):
533 def __delitem__(self, key):
530 dict.__delitem__(self, key)
534 dict.__delitem__(self, key)
531 self._list.remove(key)
535 self._list.remove(key)
532 def pop(self, key, *args, **kwargs):
536 def pop(self, key, *args, **kwargs):
533 dict.pop(self, key, *args, **kwargs)
537 dict.pop(self, key, *args, **kwargs)
534 try:
538 try:
535 self._list.remove(key)
539 self._list.remove(key)
536 except ValueError:
540 except ValueError:
537 pass
541 pass
538 def keys(self):
542 def keys(self):
539 return self._list
543 return self._list
540 def iterkeys(self):
544 def iterkeys(self):
541 return self._list.__iter__()
545 return self._list.__iter__()
542 def iteritems(self):
546 def iteritems(self):
543 for k in self._list:
547 for k in self._list:
544 yield k, self[k]
548 yield k, self[k]
545 def insert(self, index, key, val):
549 def insert(self, index, key, val):
546 self._list.insert(index, key)
550 self._list.insert(index, key)
547 dict.__setitem__(self, key, val)
551 dict.__setitem__(self, key, val)
548 def __repr__(self):
552 def __repr__(self):
549 if not self:
553 if not self:
550 return '%s()' % self.__class__.__name__
554 return '%s()' % self.__class__.__name__
551 return '%s(%r)' % (self.__class__.__name__, self.items())
555 return '%s(%r)' % (self.__class__.__name__, self.items())
552
556
553 class _lrucachenode(object):
557 class _lrucachenode(object):
554 """A node in a doubly linked list.
558 """A node in a doubly linked list.
555
559
556 Holds a reference to nodes on either side as well as a key-value
560 Holds a reference to nodes on either side as well as a key-value
557 pair for the dictionary entry.
561 pair for the dictionary entry.
558 """
562 """
559 __slots__ = (u'next', u'prev', u'key', u'value')
563 __slots__ = (u'next', u'prev', u'key', u'value')
560
564
561 def __init__(self):
565 def __init__(self):
562 self.next = None
566 self.next = None
563 self.prev = None
567 self.prev = None
564
568
565 self.key = _notset
569 self.key = _notset
566 self.value = None
570 self.value = None
567
571
568 def markempty(self):
572 def markempty(self):
569 """Mark the node as emptied."""
573 """Mark the node as emptied."""
570 self.key = _notset
574 self.key = _notset
571
575
572 class lrucachedict(object):
576 class lrucachedict(object):
573 """Dict that caches most recent accesses and sets.
577 """Dict that caches most recent accesses and sets.
574
578
575 The dict consists of an actual backing dict - indexed by original
579 The dict consists of an actual backing dict - indexed by original
576 key - and a doubly linked circular list defining the order of entries in
580 key - and a doubly linked circular list defining the order of entries in
577 the cache.
581 the cache.
578
582
579 The head node is the newest entry in the cache. If the cache is full,
583 The head node is the newest entry in the cache. If the cache is full,
580 we recycle head.prev and make it the new head. Cache accesses result in
584 we recycle head.prev and make it the new head. Cache accesses result in
581 the node being moved to before the existing head and being marked as the
585 the node being moved to before the existing head and being marked as the
582 new head node.
586 new head node.
583 """
587 """
584 def __init__(self, max):
588 def __init__(self, max):
585 self._cache = {}
589 self._cache = {}
586
590
587 self._head = head = _lrucachenode()
591 self._head = head = _lrucachenode()
588 head.prev = head
592 head.prev = head
589 head.next = head
593 head.next = head
590 self._size = 1
594 self._size = 1
591 self._capacity = max
595 self._capacity = max
592
596
593 def __len__(self):
597 def __len__(self):
594 return len(self._cache)
598 return len(self._cache)
595
599
596 def __contains__(self, k):
600 def __contains__(self, k):
597 return k in self._cache
601 return k in self._cache
598
602
599 def __iter__(self):
603 def __iter__(self):
600 # We don't have to iterate in cache order, but why not.
604 # We don't have to iterate in cache order, but why not.
601 n = self._head
605 n = self._head
602 for i in range(len(self._cache)):
606 for i in range(len(self._cache)):
603 yield n.key
607 yield n.key
604 n = n.next
608 n = n.next
605
609
606 def __getitem__(self, k):
610 def __getitem__(self, k):
607 node = self._cache[k]
611 node = self._cache[k]
608 self._movetohead(node)
612 self._movetohead(node)
609 return node.value
613 return node.value
610
614
611 def __setitem__(self, k, v):
615 def __setitem__(self, k, v):
612 node = self._cache.get(k)
616 node = self._cache.get(k)
613 # Replace existing value and mark as newest.
617 # Replace existing value and mark as newest.
614 if node is not None:
618 if node is not None:
615 node.value = v
619 node.value = v
616 self._movetohead(node)
620 self._movetohead(node)
617 return
621 return
618
622
619 if self._size < self._capacity:
623 if self._size < self._capacity:
620 node = self._addcapacity()
624 node = self._addcapacity()
621 else:
625 else:
622 # Grab the last/oldest item.
626 # Grab the last/oldest item.
623 node = self._head.prev
627 node = self._head.prev
624
628
625 # At capacity. Kill the old entry.
629 # At capacity. Kill the old entry.
626 if node.key is not _notset:
630 if node.key is not _notset:
627 del self._cache[node.key]
631 del self._cache[node.key]
628
632
629 node.key = k
633 node.key = k
630 node.value = v
634 node.value = v
631 self._cache[k] = node
635 self._cache[k] = node
632 # And mark it as newest entry. No need to adjust order since it
636 # And mark it as newest entry. No need to adjust order since it
633 # is already self._head.prev.
637 # is already self._head.prev.
634 self._head = node
638 self._head = node
635
639
636 def __delitem__(self, k):
640 def __delitem__(self, k):
637 node = self._cache.pop(k)
641 node = self._cache.pop(k)
638 node.markempty()
642 node.markempty()
639
643
640 # Temporarily mark as newest item before re-adjusting head to make
644 # Temporarily mark as newest item before re-adjusting head to make
641 # this node the oldest item.
645 # this node the oldest item.
642 self._movetohead(node)
646 self._movetohead(node)
643 self._head = node.next
647 self._head = node.next
644
648
645 # Additional dict methods.
649 # Additional dict methods.
646
650
647 def get(self, k, default=None):
651 def get(self, k, default=None):
648 try:
652 try:
649 return self._cache[k].value
653 return self._cache[k].value
650 except KeyError:
654 except KeyError:
651 return default
655 return default
652
656
653 def clear(self):
657 def clear(self):
654 n = self._head
658 n = self._head
655 while n.key is not _notset:
659 while n.key is not _notset:
656 n.markempty()
660 n.markempty()
657 n = n.next
661 n = n.next
658
662
659 self._cache.clear()
663 self._cache.clear()
660
664
661 def copy(self):
665 def copy(self):
662 result = lrucachedict(self._capacity)
666 result = lrucachedict(self._capacity)
663 n = self._head.prev
667 n = self._head.prev
664 # Iterate in oldest-to-newest order, so the copy has the right ordering
668 # Iterate in oldest-to-newest order, so the copy has the right ordering
665 for i in range(len(self._cache)):
669 for i in range(len(self._cache)):
666 result[n.key] = n.value
670 result[n.key] = n.value
667 n = n.prev
671 n = n.prev
668 return result
672 return result
669
673
670 def _movetohead(self, node):
674 def _movetohead(self, node):
671 """Mark a node as the newest, making it the new head.
675 """Mark a node as the newest, making it the new head.
672
676
673 When a node is accessed, it becomes the freshest entry in the LRU
677 When a node is accessed, it becomes the freshest entry in the LRU
674 list, which is denoted by self._head.
678 list, which is denoted by self._head.
675
679
676 Visually, let's make ``N`` the new head node (* denotes head):
680 Visually, let's make ``N`` the new head node (* denotes head):
677
681
678 previous/oldest <-> head <-> next/next newest
682 previous/oldest <-> head <-> next/next newest
679
683
680 ----<->--- A* ---<->-----
684 ----<->--- A* ---<->-----
681 | |
685 | |
682 E <-> D <-> N <-> C <-> B
686 E <-> D <-> N <-> C <-> B
683
687
684 To:
688 To:
685
689
686 ----<->--- N* ---<->-----
690 ----<->--- N* ---<->-----
687 | |
691 | |
688 E <-> D <-> C <-> B <-> A
692 E <-> D <-> C <-> B <-> A
689
693
690 This requires the following moves:
694 This requires the following moves:
691
695
692 C.next = D (node.prev.next = node.next)
696 C.next = D (node.prev.next = node.next)
693 D.prev = C (node.next.prev = node.prev)
697 D.prev = C (node.next.prev = node.prev)
694 E.next = N (head.prev.next = node)
698 E.next = N (head.prev.next = node)
695 N.prev = E (node.prev = head.prev)
699 N.prev = E (node.prev = head.prev)
696 N.next = A (node.next = head)
700 N.next = A (node.next = head)
697 A.prev = N (head.prev = node)
701 A.prev = N (head.prev = node)
698 """
702 """
699 head = self._head
703 head = self._head
700 # C.next = D
704 # C.next = D
701 node.prev.next = node.next
705 node.prev.next = node.next
702 # D.prev = C
706 # D.prev = C
703 node.next.prev = node.prev
707 node.next.prev = node.prev
704 # N.prev = E
708 # N.prev = E
705 node.prev = head.prev
709 node.prev = head.prev
706 # N.next = A
710 # N.next = A
707 # It is tempting to do just "head" here, however if node is
711 # It is tempting to do just "head" here, however if node is
708 # adjacent to head, this will do bad things.
712 # adjacent to head, this will do bad things.
709 node.next = head.prev.next
713 node.next = head.prev.next
710 # E.next = N
714 # E.next = N
711 node.next.prev = node
715 node.next.prev = node
712 # A.prev = N
716 # A.prev = N
713 node.prev.next = node
717 node.prev.next = node
714
718
715 self._head = node
719 self._head = node
716
720
717 def _addcapacity(self):
721 def _addcapacity(self):
718 """Add a node to the circular linked list.
722 """Add a node to the circular linked list.
719
723
720 The new node is inserted before the head node.
724 The new node is inserted before the head node.
721 """
725 """
722 head = self._head
726 head = self._head
723 node = _lrucachenode()
727 node = _lrucachenode()
724 head.prev.next = node
728 head.prev.next = node
725 node.prev = head.prev
729 node.prev = head.prev
726 node.next = head
730 node.next = head
727 head.prev = node
731 head.prev = node
728 self._size += 1
732 self._size += 1
729 return node
733 return node
730
734
731 def lrucachefunc(func):
735 def lrucachefunc(func):
732 '''cache most recent results of function calls'''
736 '''cache most recent results of function calls'''
733 cache = {}
737 cache = {}
734 order = collections.deque()
738 order = collections.deque()
735 if func.__code__.co_argcount == 1:
739 if func.__code__.co_argcount == 1:
736 def f(arg):
740 def f(arg):
737 if arg not in cache:
741 if arg not in cache:
738 if len(cache) > 20:
742 if len(cache) > 20:
739 del cache[order.popleft()]
743 del cache[order.popleft()]
740 cache[arg] = func(arg)
744 cache[arg] = func(arg)
741 else:
745 else:
742 order.remove(arg)
746 order.remove(arg)
743 order.append(arg)
747 order.append(arg)
744 return cache[arg]
748 return cache[arg]
745 else:
749 else:
746 def f(*args):
750 def f(*args):
747 if args not in cache:
751 if args not in cache:
748 if len(cache) > 20:
752 if len(cache) > 20:
749 del cache[order.popleft()]
753 del cache[order.popleft()]
750 cache[args] = func(*args)
754 cache[args] = func(*args)
751 else:
755 else:
752 order.remove(args)
756 order.remove(args)
753 order.append(args)
757 order.append(args)
754 return cache[args]
758 return cache[args]
755
759
756 return f
760 return f
757
761
758 class propertycache(object):
762 class propertycache(object):
759 def __init__(self, func):
763 def __init__(self, func):
760 self.func = func
764 self.func = func
761 self.name = func.__name__
765 self.name = func.__name__
762 def __get__(self, obj, type=None):
766 def __get__(self, obj, type=None):
763 result = self.func(obj)
767 result = self.func(obj)
764 self.cachevalue(obj, result)
768 self.cachevalue(obj, result)
765 return result
769 return result
766
770
767 def cachevalue(self, obj, value):
771 def cachevalue(self, obj, value):
768 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
772 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
769 obj.__dict__[self.name] = value
773 obj.__dict__[self.name] = value
770
774
771 def pipefilter(s, cmd):
775 def pipefilter(s, cmd):
772 '''filter string S through command CMD, returning its output'''
776 '''filter string S through command CMD, returning its output'''
773 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
777 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
774 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
778 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
775 pout, perr = p.communicate(s)
779 pout, perr = p.communicate(s)
776 return pout
780 return pout
777
781
778 def tempfilter(s, cmd):
782 def tempfilter(s, cmd):
779 '''filter string S through a pair of temporary files with CMD.
783 '''filter string S through a pair of temporary files with CMD.
780 CMD is used as a template to create the real command to be run,
784 CMD is used as a template to create the real command to be run,
781 with the strings INFILE and OUTFILE replaced by the real names of
785 with the strings INFILE and OUTFILE replaced by the real names of
782 the temporary files generated.'''
786 the temporary files generated.'''
783 inname, outname = None, None
787 inname, outname = None, None
784 try:
788 try:
785 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
789 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
786 fp = os.fdopen(infd, 'wb')
790 fp = os.fdopen(infd, 'wb')
787 fp.write(s)
791 fp.write(s)
788 fp.close()
792 fp.close()
789 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
793 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
790 os.close(outfd)
794 os.close(outfd)
791 cmd = cmd.replace('INFILE', inname)
795 cmd = cmd.replace('INFILE', inname)
792 cmd = cmd.replace('OUTFILE', outname)
796 cmd = cmd.replace('OUTFILE', outname)
793 code = os.system(cmd)
797 code = os.system(cmd)
794 if sys.platform == 'OpenVMS' and code & 1:
798 if sys.platform == 'OpenVMS' and code & 1:
795 code = 0
799 code = 0
796 if code:
800 if code:
797 raise Abort(_("command '%s' failed: %s") %
801 raise Abort(_("command '%s' failed: %s") %
798 (cmd, explainexit(code)))
802 (cmd, explainexit(code)))
799 return readfile(outname)
803 return readfile(outname)
800 finally:
804 finally:
801 try:
805 try:
802 if inname:
806 if inname:
803 os.unlink(inname)
807 os.unlink(inname)
804 except OSError:
808 except OSError:
805 pass
809 pass
806 try:
810 try:
807 if outname:
811 if outname:
808 os.unlink(outname)
812 os.unlink(outname)
809 except OSError:
813 except OSError:
810 pass
814 pass
811
815
812 filtertable = {
816 filtertable = {
813 'tempfile:': tempfilter,
817 'tempfile:': tempfilter,
814 'pipe:': pipefilter,
818 'pipe:': pipefilter,
815 }
819 }
816
820
817 def filter(s, cmd):
821 def filter(s, cmd):
818 "filter a string through a command that transforms its input to its output"
822 "filter a string through a command that transforms its input to its output"
819 for name, fn in filtertable.iteritems():
823 for name, fn in filtertable.iteritems():
820 if cmd.startswith(name):
824 if cmd.startswith(name):
821 return fn(s, cmd[len(name):].lstrip())
825 return fn(s, cmd[len(name):].lstrip())
822 return pipefilter(s, cmd)
826 return pipefilter(s, cmd)
823
827
824 def binary(s):
828 def binary(s):
825 """return true if a string is binary data"""
829 """return true if a string is binary data"""
826 return bool(s and '\0' in s)
830 return bool(s and '\0' in s)
827
831
828 def increasingchunks(source, min=1024, max=65536):
832 def increasingchunks(source, min=1024, max=65536):
829 '''return no less than min bytes per chunk while data remains,
833 '''return no less than min bytes per chunk while data remains,
830 doubling min after each chunk until it reaches max'''
834 doubling min after each chunk until it reaches max'''
831 def log2(x):
835 def log2(x):
832 if not x:
836 if not x:
833 return 0
837 return 0
834 i = 0
838 i = 0
835 while x:
839 while x:
836 x >>= 1
840 x >>= 1
837 i += 1
841 i += 1
838 return i - 1
842 return i - 1
839
843
840 buf = []
844 buf = []
841 blen = 0
845 blen = 0
842 for chunk in source:
846 for chunk in source:
843 buf.append(chunk)
847 buf.append(chunk)
844 blen += len(chunk)
848 blen += len(chunk)
845 if blen >= min:
849 if blen >= min:
846 if min < max:
850 if min < max:
847 min = min << 1
851 min = min << 1
848 nmin = 1 << log2(blen)
852 nmin = 1 << log2(blen)
849 if nmin > min:
853 if nmin > min:
850 min = nmin
854 min = nmin
851 if min > max:
855 if min > max:
852 min = max
856 min = max
853 yield ''.join(buf)
857 yield ''.join(buf)
854 blen = 0
858 blen = 0
855 buf = []
859 buf = []
856 if buf:
860 if buf:
857 yield ''.join(buf)
861 yield ''.join(buf)
858
862
859 Abort = error.Abort
863 Abort = error.Abort
860
864
861 def always(fn):
865 def always(fn):
862 return True
866 return True
863
867
864 def never(fn):
868 def never(fn):
865 return False
869 return False
866
870
867 def nogc(func):
871 def nogc(func):
868 """disable garbage collector
872 """disable garbage collector
869
873
870 Python's garbage collector triggers a GC each time a certain number of
874 Python's garbage collector triggers a GC each time a certain number of
871 container objects (the number being defined by gc.get_threshold()) are
875 container objects (the number being defined by gc.get_threshold()) are
872 allocated even when marked not to be tracked by the collector. Tracking has
876 allocated even when marked not to be tracked by the collector. Tracking has
873 no effect on when GCs are triggered, only on what objects the GC looks
877 no effect on when GCs are triggered, only on what objects the GC looks
874 into. As a workaround, disable GC while building complex (huge)
878 into. As a workaround, disable GC while building complex (huge)
875 containers.
879 containers.
876
880
877 This garbage collector issue have been fixed in 2.7.
881 This garbage collector issue have been fixed in 2.7.
878 """
882 """
879 if sys.version_info >= (2, 7):
883 if sys.version_info >= (2, 7):
880 return func
884 return func
881 def wrapper(*args, **kwargs):
885 def wrapper(*args, **kwargs):
882 gcenabled = gc.isenabled()
886 gcenabled = gc.isenabled()
883 gc.disable()
887 gc.disable()
884 try:
888 try:
885 return func(*args, **kwargs)
889 return func(*args, **kwargs)
886 finally:
890 finally:
887 if gcenabled:
891 if gcenabled:
888 gc.enable()
892 gc.enable()
889 return wrapper
893 return wrapper
890
894
891 def pathto(root, n1, n2):
895 def pathto(root, n1, n2):
892 '''return the relative path from one place to another.
896 '''return the relative path from one place to another.
893 root should use os.sep to separate directories
897 root should use os.sep to separate directories
894 n1 should use os.sep to separate directories
898 n1 should use os.sep to separate directories
895 n2 should use "/" to separate directories
899 n2 should use "/" to separate directories
896 returns an os.sep-separated path.
900 returns an os.sep-separated path.
897
901
898 If n1 is a relative path, it's assumed it's
902 If n1 is a relative path, it's assumed it's
899 relative to root.
903 relative to root.
900 n2 should always be relative to root.
904 n2 should always be relative to root.
901 '''
905 '''
902 if not n1:
906 if not n1:
903 return localpath(n2)
907 return localpath(n2)
904 if os.path.isabs(n1):
908 if os.path.isabs(n1):
905 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
909 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
906 return os.path.join(root, localpath(n2))
910 return os.path.join(root, localpath(n2))
907 n2 = '/'.join((pconvert(root), n2))
911 n2 = '/'.join((pconvert(root), n2))
908 a, b = splitpath(n1), n2.split('/')
912 a, b = splitpath(n1), n2.split('/')
909 a.reverse()
913 a.reverse()
910 b.reverse()
914 b.reverse()
911 while a and b and a[-1] == b[-1]:
915 while a and b and a[-1] == b[-1]:
912 a.pop()
916 a.pop()
913 b.pop()
917 b.pop()
914 b.reverse()
918 b.reverse()
915 return os.sep.join((['..'] * len(a)) + b) or '.'
919 return os.sep.join((['..'] * len(a)) + b) or '.'
916
920
917 def mainfrozen():
921 def mainfrozen():
918 """return True if we are a frozen executable.
922 """return True if we are a frozen executable.
919
923
920 The code supports py2exe (most common, Windows only) and tools/freeze
924 The code supports py2exe (most common, Windows only) and tools/freeze
921 (portable, not much used).
925 (portable, not much used).
922 """
926 """
923 return (safehasattr(sys, "frozen") or # new py2exe
927 return (safehasattr(sys, "frozen") or # new py2exe
924 safehasattr(sys, "importers") or # old py2exe
928 safehasattr(sys, "importers") or # old py2exe
925 imp.is_frozen(u"__main__")) # tools/freeze
929 imp.is_frozen(u"__main__")) # tools/freeze
926
930
927 # the location of data files matching the source code
931 # the location of data files matching the source code
928 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
932 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
929 # executable version (py2exe) doesn't support __file__
933 # executable version (py2exe) doesn't support __file__
930 datapath = os.path.dirname(sys.executable)
934 datapath = os.path.dirname(sys.executable)
931 else:
935 else:
932 datapath = os.path.dirname(__file__)
936 datapath = os.path.dirname(__file__)
933
937
934 if not isinstance(datapath, bytes):
938 if not isinstance(datapath, bytes):
935 datapath = pycompat.fsencode(datapath)
939 datapath = pycompat.fsencode(datapath)
936
940
937 i18n.setdatapath(datapath)
941 i18n.setdatapath(datapath)
938
942
939 _hgexecutable = None
943 _hgexecutable = None
940
944
941 def hgexecutable():
945 def hgexecutable():
942 """return location of the 'hg' executable.
946 """return location of the 'hg' executable.
943
947
944 Defaults to $HG or 'hg' in the search path.
948 Defaults to $HG or 'hg' in the search path.
945 """
949 """
946 if _hgexecutable is None:
950 if _hgexecutable is None:
947 hg = os.environ.get('HG')
951 hg = os.environ.get('HG')
948 mainmod = sys.modules['__main__']
952 mainmod = sys.modules['__main__']
949 if hg:
953 if hg:
950 _sethgexecutable(hg)
954 _sethgexecutable(hg)
951 elif mainfrozen():
955 elif mainfrozen():
952 if getattr(sys, 'frozen', None) == 'macosx_app':
956 if getattr(sys, 'frozen', None) == 'macosx_app':
953 # Env variable set by py2app
957 # Env variable set by py2app
954 _sethgexecutable(os.environ['EXECUTABLEPATH'])
958 _sethgexecutable(os.environ['EXECUTABLEPATH'])
955 else:
959 else:
956 _sethgexecutable(sys.executable)
960 _sethgexecutable(sys.executable)
957 elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
961 elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
958 _sethgexecutable(mainmod.__file__)
962 _sethgexecutable(mainmod.__file__)
959 else:
963 else:
960 exe = findexe('hg') or os.path.basename(sys.argv[0])
964 exe = findexe('hg') or os.path.basename(sys.argv[0])
961 _sethgexecutable(exe)
965 _sethgexecutable(exe)
962 return _hgexecutable
966 return _hgexecutable
963
967
964 def _sethgexecutable(path):
968 def _sethgexecutable(path):
965 """set location of the 'hg' executable"""
969 """set location of the 'hg' executable"""
966 global _hgexecutable
970 global _hgexecutable
967 _hgexecutable = path
971 _hgexecutable = path
968
972
969 def _isstdout(f):
973 def _isstdout(f):
970 fileno = getattr(f, 'fileno', None)
974 fileno = getattr(f, 'fileno', None)
971 return fileno and fileno() == sys.__stdout__.fileno()
975 return fileno and fileno() == sys.__stdout__.fileno()
972
976
973 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
977 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
974 '''enhanced shell command execution.
978 '''enhanced shell command execution.
975 run with environment maybe modified, maybe in different dir.
979 run with environment maybe modified, maybe in different dir.
976
980
977 if command fails and onerr is None, return status, else raise onerr
981 if command fails and onerr is None, return status, else raise onerr
978 object as exception.
982 object as exception.
979
983
980 if out is specified, it is assumed to be a file-like object that has a
984 if out is specified, it is assumed to be a file-like object that has a
981 write() method. stdout and stderr will be redirected to out.'''
985 write() method. stdout and stderr will be redirected to out.'''
982 if environ is None:
986 if environ is None:
983 environ = {}
987 environ = {}
984 try:
988 try:
985 sys.stdout.flush()
989 sys.stdout.flush()
986 except Exception:
990 except Exception:
987 pass
991 pass
988 def py2shell(val):
992 def py2shell(val):
989 'convert python object into string that is useful to shell'
993 'convert python object into string that is useful to shell'
990 if val is None or val is False:
994 if val is None or val is False:
991 return '0'
995 return '0'
992 if val is True:
996 if val is True:
993 return '1'
997 return '1'
994 return str(val)
998 return str(val)
995 origcmd = cmd
999 origcmd = cmd
996 cmd = quotecommand(cmd)
1000 cmd = quotecommand(cmd)
997 if sys.platform == 'plan9' and (sys.version_info[0] == 2
1001 if sys.platform == 'plan9' and (sys.version_info[0] == 2
998 and sys.version_info[1] < 7):
1002 and sys.version_info[1] < 7):
999 # subprocess kludge to work around issues in half-baked Python
1003 # subprocess kludge to work around issues in half-baked Python
1000 # ports, notably bichued/python:
1004 # ports, notably bichued/python:
1001 if not cwd is None:
1005 if not cwd is None:
1002 os.chdir(cwd)
1006 os.chdir(cwd)
1003 rc = os.system(cmd)
1007 rc = os.system(cmd)
1004 else:
1008 else:
1005 env = dict(os.environ)
1009 env = dict(os.environ)
1006 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1010 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1007 env['HG'] = hgexecutable()
1011 env['HG'] = hgexecutable()
1008 if out is None or _isstdout(out):
1012 if out is None or _isstdout(out):
1009 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1013 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1010 env=env, cwd=cwd)
1014 env=env, cwd=cwd)
1011 else:
1015 else:
1012 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1016 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1013 env=env, cwd=cwd, stdout=subprocess.PIPE,
1017 env=env, cwd=cwd, stdout=subprocess.PIPE,
1014 stderr=subprocess.STDOUT)
1018 stderr=subprocess.STDOUT)
1015 for line in iter(proc.stdout.readline, ''):
1019 for line in iter(proc.stdout.readline, ''):
1016 out.write(line)
1020 out.write(line)
1017 proc.wait()
1021 proc.wait()
1018 rc = proc.returncode
1022 rc = proc.returncode
1019 if sys.platform == 'OpenVMS' and rc & 1:
1023 if sys.platform == 'OpenVMS' and rc & 1:
1020 rc = 0
1024 rc = 0
1021 if rc and onerr:
1025 if rc and onerr:
1022 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1026 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1023 explainexit(rc)[0])
1027 explainexit(rc)[0])
1024 if errprefix:
1028 if errprefix:
1025 errmsg = '%s: %s' % (errprefix, errmsg)
1029 errmsg = '%s: %s' % (errprefix, errmsg)
1026 raise onerr(errmsg)
1030 raise onerr(errmsg)
1027 return rc
1031 return rc
1028
1032
1029 def checksignature(func):
1033 def checksignature(func):
1030 '''wrap a function with code to check for calling errors'''
1034 '''wrap a function with code to check for calling errors'''
1031 def check(*args, **kwargs):
1035 def check(*args, **kwargs):
1032 try:
1036 try:
1033 return func(*args, **kwargs)
1037 return func(*args, **kwargs)
1034 except TypeError:
1038 except TypeError:
1035 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1039 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1036 raise error.SignatureError
1040 raise error.SignatureError
1037 raise
1041 raise
1038
1042
1039 return check
1043 return check
1040
1044
1041 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1045 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1042 '''copy a file, preserving mode and optionally other stat info like
1046 '''copy a file, preserving mode and optionally other stat info like
1043 atime/mtime
1047 atime/mtime
1044
1048
1045 checkambig argument is used with filestat, and is useful only if
1049 checkambig argument is used with filestat, and is useful only if
1046 destination file is guarded by any lock (e.g. repo.lock or
1050 destination file is guarded by any lock (e.g. repo.lock or
1047 repo.wlock).
1051 repo.wlock).
1048
1052
1049 copystat and checkambig should be exclusive.
1053 copystat and checkambig should be exclusive.
1050 '''
1054 '''
1051 assert not (copystat and checkambig)
1055 assert not (copystat and checkambig)
1052 oldstat = None
1056 oldstat = None
1053 if os.path.lexists(dest):
1057 if os.path.lexists(dest):
1054 if checkambig:
1058 if checkambig:
1055 oldstat = checkambig and filestat(dest)
1059 oldstat = checkambig and filestat(dest)
1056 unlink(dest)
1060 unlink(dest)
1057 # hardlinks are problematic on CIFS, quietly ignore this flag
1061 # hardlinks are problematic on CIFS, quietly ignore this flag
1058 # until we find a way to work around it cleanly (issue4546)
1062 # until we find a way to work around it cleanly (issue4546)
1059 if False and hardlink:
1063 if False and hardlink:
1060 try:
1064 try:
1061 oslink(src, dest)
1065 oslink(src, dest)
1062 return
1066 return
1063 except (IOError, OSError):
1067 except (IOError, OSError):
1064 pass # fall back to normal copy
1068 pass # fall back to normal copy
1065 if os.path.islink(src):
1069 if os.path.islink(src):
1066 os.symlink(os.readlink(src), dest)
1070 os.symlink(os.readlink(src), dest)
1067 # copytime is ignored for symlinks, but in general copytime isn't needed
1071 # copytime is ignored for symlinks, but in general copytime isn't needed
1068 # for them anyway
1072 # for them anyway
1069 else:
1073 else:
1070 try:
1074 try:
1071 shutil.copyfile(src, dest)
1075 shutil.copyfile(src, dest)
1072 if copystat:
1076 if copystat:
1073 # copystat also copies mode
1077 # copystat also copies mode
1074 shutil.copystat(src, dest)
1078 shutil.copystat(src, dest)
1075 else:
1079 else:
1076 shutil.copymode(src, dest)
1080 shutil.copymode(src, dest)
1077 if oldstat and oldstat.stat:
1081 if oldstat and oldstat.stat:
1078 newstat = filestat(dest)
1082 newstat = filestat(dest)
1079 if newstat.isambig(oldstat):
1083 if newstat.isambig(oldstat):
1080 # stat of copied file is ambiguous to original one
1084 # stat of copied file is ambiguous to original one
1081 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1085 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1082 os.utime(dest, (advanced, advanced))
1086 os.utime(dest, (advanced, advanced))
1083 except shutil.Error as inst:
1087 except shutil.Error as inst:
1084 raise Abort(str(inst))
1088 raise Abort(str(inst))
1085
1089
1086 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
1090 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
1087 """Copy a directory tree using hardlinks if possible."""
1091 """Copy a directory tree using hardlinks if possible."""
1088 num = 0
1092 num = 0
1089
1093
1090 if hardlink is None:
1094 if hardlink is None:
1091 hardlink = (os.stat(src).st_dev ==
1095 hardlink = (os.stat(src).st_dev ==
1092 os.stat(os.path.dirname(dst)).st_dev)
1096 os.stat(os.path.dirname(dst)).st_dev)
1093 if hardlink:
1097 if hardlink:
1094 topic = _('linking')
1098 topic = _('linking')
1095 else:
1099 else:
1096 topic = _('copying')
1100 topic = _('copying')
1097
1101
1098 if os.path.isdir(src):
1102 if os.path.isdir(src):
1099 os.mkdir(dst)
1103 os.mkdir(dst)
1100 for name, kind in osutil.listdir(src):
1104 for name, kind in osutil.listdir(src):
1101 srcname = os.path.join(src, name)
1105 srcname = os.path.join(src, name)
1102 dstname = os.path.join(dst, name)
1106 dstname = os.path.join(dst, name)
1103 def nprog(t, pos):
1107 def nprog(t, pos):
1104 if pos is not None:
1108 if pos is not None:
1105 return progress(t, pos + num)
1109 return progress(t, pos + num)
1106 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
1110 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
1107 num += n
1111 num += n
1108 else:
1112 else:
1109 if hardlink:
1113 if hardlink:
1110 try:
1114 try:
1111 oslink(src, dst)
1115 oslink(src, dst)
1112 except (IOError, OSError):
1116 except (IOError, OSError):
1113 hardlink = False
1117 hardlink = False
1114 shutil.copy(src, dst)
1118 shutil.copy(src, dst)
1115 else:
1119 else:
1116 shutil.copy(src, dst)
1120 shutil.copy(src, dst)
1117 num += 1
1121 num += 1
1118 progress(topic, num)
1122 progress(topic, num)
1119 progress(topic, None)
1123 progress(topic, None)
1120
1124
1121 return hardlink, num
1125 return hardlink, num
1122
1126
1123 _winreservednames = '''con prn aux nul
1127 _winreservednames = '''con prn aux nul
1124 com1 com2 com3 com4 com5 com6 com7 com8 com9
1128 com1 com2 com3 com4 com5 com6 com7 com8 com9
1125 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
1129 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
1126 _winreservedchars = ':*?"<>|'
1130 _winreservedchars = ':*?"<>|'
1127 def checkwinfilename(path):
1131 def checkwinfilename(path):
1128 r'''Check that the base-relative path is a valid filename on Windows.
1132 r'''Check that the base-relative path is a valid filename on Windows.
1129 Returns None if the path is ok, or a UI string describing the problem.
1133 Returns None if the path is ok, or a UI string describing the problem.
1130
1134
1131 >>> checkwinfilename("just/a/normal/path")
1135 >>> checkwinfilename("just/a/normal/path")
1132 >>> checkwinfilename("foo/bar/con.xml")
1136 >>> checkwinfilename("foo/bar/con.xml")
1133 "filename contains 'con', which is reserved on Windows"
1137 "filename contains 'con', which is reserved on Windows"
1134 >>> checkwinfilename("foo/con.xml/bar")
1138 >>> checkwinfilename("foo/con.xml/bar")
1135 "filename contains 'con', which is reserved on Windows"
1139 "filename contains 'con', which is reserved on Windows"
1136 >>> checkwinfilename("foo/bar/xml.con")
1140 >>> checkwinfilename("foo/bar/xml.con")
1137 >>> checkwinfilename("foo/bar/AUX/bla.txt")
1141 >>> checkwinfilename("foo/bar/AUX/bla.txt")
1138 "filename contains 'AUX', which is reserved on Windows"
1142 "filename contains 'AUX', which is reserved on Windows"
1139 >>> checkwinfilename("foo/bar/bla:.txt")
1143 >>> checkwinfilename("foo/bar/bla:.txt")
1140 "filename contains ':', which is reserved on Windows"
1144 "filename contains ':', which is reserved on Windows"
1141 >>> checkwinfilename("foo/bar/b\07la.txt")
1145 >>> checkwinfilename("foo/bar/b\07la.txt")
1142 "filename contains '\\x07', which is invalid on Windows"
1146 "filename contains '\\x07', which is invalid on Windows"
1143 >>> checkwinfilename("foo/bar/bla ")
1147 >>> checkwinfilename("foo/bar/bla ")
1144 "filename ends with ' ', which is not allowed on Windows"
1148 "filename ends with ' ', which is not allowed on Windows"
1145 >>> checkwinfilename("../bar")
1149 >>> checkwinfilename("../bar")
1146 >>> checkwinfilename("foo\\")
1150 >>> checkwinfilename("foo\\")
1147 "filename ends with '\\', which is invalid on Windows"
1151 "filename ends with '\\', which is invalid on Windows"
1148 >>> checkwinfilename("foo\\/bar")
1152 >>> checkwinfilename("foo\\/bar")
1149 "directory name ends with '\\', which is invalid on Windows"
1153 "directory name ends with '\\', which is invalid on Windows"
1150 '''
1154 '''
1151 if path.endswith('\\'):
1155 if path.endswith('\\'):
1152 return _("filename ends with '\\', which is invalid on Windows")
1156 return _("filename ends with '\\', which is invalid on Windows")
1153 if '\\/' in path:
1157 if '\\/' in path:
1154 return _("directory name ends with '\\', which is invalid on Windows")
1158 return _("directory name ends with '\\', which is invalid on Windows")
1155 for n in path.replace('\\', '/').split('/'):
1159 for n in path.replace('\\', '/').split('/'):
1156 if not n:
1160 if not n:
1157 continue
1161 continue
1158 for c in n:
1162 for c in n:
1159 if c in _winreservedchars:
1163 if c in _winreservedchars:
1160 return _("filename contains '%s', which is reserved "
1164 return _("filename contains '%s', which is reserved "
1161 "on Windows") % c
1165 "on Windows") % c
1162 if ord(c) <= 31:
1166 if ord(c) <= 31:
1163 return _("filename contains %r, which is invalid "
1167 return _("filename contains %r, which is invalid "
1164 "on Windows") % c
1168 "on Windows") % c
1165 base = n.split('.')[0]
1169 base = n.split('.')[0]
1166 if base and base.lower() in _winreservednames:
1170 if base and base.lower() in _winreservednames:
1167 return _("filename contains '%s', which is reserved "
1171 return _("filename contains '%s', which is reserved "
1168 "on Windows") % base
1172 "on Windows") % base
1169 t = n[-1]
1173 t = n[-1]
1170 if t in '. ' and n not in '..':
1174 if t in '. ' and n not in '..':
1171 return _("filename ends with '%s', which is not allowed "
1175 return _("filename ends with '%s', which is not allowed "
1172 "on Windows") % t
1176 "on Windows") % t
1173
1177
1174 if os.name == 'nt':
1178 if os.name == 'nt':
1175 checkosfilename = checkwinfilename
1179 checkosfilename = checkwinfilename
1176 else:
1180 else:
1177 checkosfilename = platform.checkosfilename
1181 checkosfilename = platform.checkosfilename
1178
1182
1179 def makelock(info, pathname):
1183 def makelock(info, pathname):
1180 try:
1184 try:
1181 return os.symlink(info, pathname)
1185 return os.symlink(info, pathname)
1182 except OSError as why:
1186 except OSError as why:
1183 if why.errno == errno.EEXIST:
1187 if why.errno == errno.EEXIST:
1184 raise
1188 raise
1185 except AttributeError: # no symlink in os
1189 except AttributeError: # no symlink in os
1186 pass
1190 pass
1187
1191
1188 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
1192 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
1189 os.write(ld, info)
1193 os.write(ld, info)
1190 os.close(ld)
1194 os.close(ld)
1191
1195
1192 def readlock(pathname):
1196 def readlock(pathname):
1193 try:
1197 try:
1194 return os.readlink(pathname)
1198 return os.readlink(pathname)
1195 except OSError as why:
1199 except OSError as why:
1196 if why.errno not in (errno.EINVAL, errno.ENOSYS):
1200 if why.errno not in (errno.EINVAL, errno.ENOSYS):
1197 raise
1201 raise
1198 except AttributeError: # no symlink in os
1202 except AttributeError: # no symlink in os
1199 pass
1203 pass
1200 fp = posixfile(pathname)
1204 fp = posixfile(pathname)
1201 r = fp.read()
1205 r = fp.read()
1202 fp.close()
1206 fp.close()
1203 return r
1207 return r
1204
1208
1205 def fstat(fp):
1209 def fstat(fp):
1206 '''stat file object that may not have fileno method.'''
1210 '''stat file object that may not have fileno method.'''
1207 try:
1211 try:
1208 return os.fstat(fp.fileno())
1212 return os.fstat(fp.fileno())
1209 except AttributeError:
1213 except AttributeError:
1210 return os.stat(fp.name)
1214 return os.stat(fp.name)
1211
1215
1212 # File system features
1216 # File system features
1213
1217
1214 def fscasesensitive(path):
1218 def fscasesensitive(path):
1215 """
1219 """
1216 Return true if the given path is on a case-sensitive filesystem
1220 Return true if the given path is on a case-sensitive filesystem
1217
1221
1218 Requires a path (like /foo/.hg) ending with a foldable final
1222 Requires a path (like /foo/.hg) ending with a foldable final
1219 directory component.
1223 directory component.
1220 """
1224 """
1221 s1 = os.lstat(path)
1225 s1 = os.lstat(path)
1222 d, b = os.path.split(path)
1226 d, b = os.path.split(path)
1223 b2 = b.upper()
1227 b2 = b.upper()
1224 if b == b2:
1228 if b == b2:
1225 b2 = b.lower()
1229 b2 = b.lower()
1226 if b == b2:
1230 if b == b2:
1227 return True # no evidence against case sensitivity
1231 return True # no evidence against case sensitivity
1228 p2 = os.path.join(d, b2)
1232 p2 = os.path.join(d, b2)
1229 try:
1233 try:
1230 s2 = os.lstat(p2)
1234 s2 = os.lstat(p2)
1231 if s2 == s1:
1235 if s2 == s1:
1232 return False
1236 return False
1233 return True
1237 return True
1234 except OSError:
1238 except OSError:
1235 return True
1239 return True
1236
1240
1237 try:
1241 try:
1238 import re2
1242 import re2
1239 _re2 = None
1243 _re2 = None
1240 except ImportError:
1244 except ImportError:
1241 _re2 = False
1245 _re2 = False
1242
1246
1243 class _re(object):
1247 class _re(object):
1244 def _checkre2(self):
1248 def _checkre2(self):
1245 global _re2
1249 global _re2
1246 try:
1250 try:
1247 # check if match works, see issue3964
1251 # check if match works, see issue3964
1248 _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
1252 _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
1249 except ImportError:
1253 except ImportError:
1250 _re2 = False
1254 _re2 = False
1251
1255
1252 def compile(self, pat, flags=0):
1256 def compile(self, pat, flags=0):
1253 '''Compile a regular expression, using re2 if possible
1257 '''Compile a regular expression, using re2 if possible
1254
1258
1255 For best performance, use only re2-compatible regexp features. The
1259 For best performance, use only re2-compatible regexp features. The
1256 only flags from the re module that are re2-compatible are
1260 only flags from the re module that are re2-compatible are
1257 IGNORECASE and MULTILINE.'''
1261 IGNORECASE and MULTILINE.'''
1258 if _re2 is None:
1262 if _re2 is None:
1259 self._checkre2()
1263 self._checkre2()
1260 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
1264 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
1261 if flags & remod.IGNORECASE:
1265 if flags & remod.IGNORECASE:
1262 pat = '(?i)' + pat
1266 pat = '(?i)' + pat
1263 if flags & remod.MULTILINE:
1267 if flags & remod.MULTILINE:
1264 pat = '(?m)' + pat
1268 pat = '(?m)' + pat
1265 try:
1269 try:
1266 return re2.compile(pat)
1270 return re2.compile(pat)
1267 except re2.error:
1271 except re2.error:
1268 pass
1272 pass
1269 return remod.compile(pat, flags)
1273 return remod.compile(pat, flags)
1270
1274
1271 @propertycache
1275 @propertycache
1272 def escape(self):
1276 def escape(self):
1273 '''Return the version of escape corresponding to self.compile.
1277 '''Return the version of escape corresponding to self.compile.
1274
1278
1275 This is imperfect because whether re2 or re is used for a particular
1279 This is imperfect because whether re2 or re is used for a particular
1276 function depends on the flags, etc, but it's the best we can do.
1280 function depends on the flags, etc, but it's the best we can do.
1277 '''
1281 '''
1278 global _re2
1282 global _re2
1279 if _re2 is None:
1283 if _re2 is None:
1280 self._checkre2()
1284 self._checkre2()
1281 if _re2:
1285 if _re2:
1282 return re2.escape
1286 return re2.escape
1283 else:
1287 else:
1284 return remod.escape
1288 return remod.escape
1285
1289
1286 re = _re()
1290 re = _re()
1287
1291
1288 _fspathcache = {}
1292 _fspathcache = {}
1289 def fspath(name, root):
1293 def fspath(name, root):
1290 '''Get name in the case stored in the filesystem
1294 '''Get name in the case stored in the filesystem
1291
1295
1292 The name should be relative to root, and be normcase-ed for efficiency.
1296 The name should be relative to root, and be normcase-ed for efficiency.
1293
1297
1294 Note that this function is unnecessary, and should not be
1298 Note that this function is unnecessary, and should not be
1295 called, for case-sensitive filesystems (simply because it's expensive).
1299 called, for case-sensitive filesystems (simply because it's expensive).
1296
1300
1297 The root should be normcase-ed, too.
1301 The root should be normcase-ed, too.
1298 '''
1302 '''
1299 def _makefspathcacheentry(dir):
1303 def _makefspathcacheentry(dir):
1300 return dict((normcase(n), n) for n in os.listdir(dir))
1304 return dict((normcase(n), n) for n in os.listdir(dir))
1301
1305
1302 seps = os.sep
1306 seps = os.sep
1303 if os.altsep:
1307 if os.altsep:
1304 seps = seps + os.altsep
1308 seps = seps + os.altsep
1305 # Protect backslashes. This gets silly very quickly.
1309 # Protect backslashes. This gets silly very quickly.
1306 seps.replace('\\','\\\\')
1310 seps.replace('\\','\\\\')
1307 pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1311 pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1308 dir = os.path.normpath(root)
1312 dir = os.path.normpath(root)
1309 result = []
1313 result = []
1310 for part, sep in pattern.findall(name):
1314 for part, sep in pattern.findall(name):
1311 if sep:
1315 if sep:
1312 result.append(sep)
1316 result.append(sep)
1313 continue
1317 continue
1314
1318
1315 if dir not in _fspathcache:
1319 if dir not in _fspathcache:
1316 _fspathcache[dir] = _makefspathcacheentry(dir)
1320 _fspathcache[dir] = _makefspathcacheentry(dir)
1317 contents = _fspathcache[dir]
1321 contents = _fspathcache[dir]
1318
1322
1319 found = contents.get(part)
1323 found = contents.get(part)
1320 if not found:
1324 if not found:
1321 # retry "once per directory" per "dirstate.walk" which
1325 # retry "once per directory" per "dirstate.walk" which
1322 # may take place for each patches of "hg qpush", for example
1326 # may take place for each patches of "hg qpush", for example
1323 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
1327 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
1324 found = contents.get(part)
1328 found = contents.get(part)
1325
1329
1326 result.append(found or part)
1330 result.append(found or part)
1327 dir = os.path.join(dir, part)
1331 dir = os.path.join(dir, part)
1328
1332
1329 return ''.join(result)
1333 return ''.join(result)
1330
1334
1331 def checknlink(testfile):
1335 def checknlink(testfile):
1332 '''check whether hardlink count reporting works properly'''
1336 '''check whether hardlink count reporting works properly'''
1333
1337
1334 # testfile may be open, so we need a separate file for checking to
1338 # testfile may be open, so we need a separate file for checking to
1335 # work around issue2543 (or testfile may get lost on Samba shares)
1339 # work around issue2543 (or testfile may get lost on Samba shares)
1336 f1 = testfile + ".hgtmp1"
1340 f1 = testfile + ".hgtmp1"
1337 if os.path.lexists(f1):
1341 if os.path.lexists(f1):
1338 return False
1342 return False
1339 try:
1343 try:
1340 posixfile(f1, 'w').close()
1344 posixfile(f1, 'w').close()
1341 except IOError:
1345 except IOError:
1342 try:
1346 try:
1343 os.unlink(f1)
1347 os.unlink(f1)
1344 except OSError:
1348 except OSError:
1345 pass
1349 pass
1346 return False
1350 return False
1347
1351
1348 f2 = testfile + ".hgtmp2"
1352 f2 = testfile + ".hgtmp2"
1349 fd = None
1353 fd = None
1350 try:
1354 try:
1351 oslink(f1, f2)
1355 oslink(f1, f2)
1352 # nlinks() may behave differently for files on Windows shares if
1356 # nlinks() may behave differently for files on Windows shares if
1353 # the file is open.
1357 # the file is open.
1354 fd = posixfile(f2)
1358 fd = posixfile(f2)
1355 return nlinks(f2) > 1
1359 return nlinks(f2) > 1
1356 except OSError:
1360 except OSError:
1357 return False
1361 return False
1358 finally:
1362 finally:
1359 if fd is not None:
1363 if fd is not None:
1360 fd.close()
1364 fd.close()
1361 for f in (f1, f2):
1365 for f in (f1, f2):
1362 try:
1366 try:
1363 os.unlink(f)
1367 os.unlink(f)
1364 except OSError:
1368 except OSError:
1365 pass
1369 pass
1366
1370
1367 def endswithsep(path):
1371 def endswithsep(path):
1368 '''Check path ends with os.sep or os.altsep.'''
1372 '''Check path ends with os.sep or os.altsep.'''
1369 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
1373 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
1370
1374
1371 def splitpath(path):
1375 def splitpath(path):
1372 '''Split path by os.sep.
1376 '''Split path by os.sep.
1373 Note that this function does not use os.altsep because this is
1377 Note that this function does not use os.altsep because this is
1374 an alternative of simple "xxx.split(os.sep)".
1378 an alternative of simple "xxx.split(os.sep)".
1375 It is recommended to use os.path.normpath() before using this
1379 It is recommended to use os.path.normpath() before using this
1376 function if need.'''
1380 function if need.'''
1377 return path.split(os.sep)
1381 return path.split(os.sep)
1378
1382
1379 def gui():
1383 def gui():
1380 '''Are we running in a GUI?'''
1384 '''Are we running in a GUI?'''
1381 if sys.platform == 'darwin':
1385 if sys.platform == 'darwin':
1382 if 'SSH_CONNECTION' in os.environ:
1386 if 'SSH_CONNECTION' in os.environ:
1383 # handle SSH access to a box where the user is logged in
1387 # handle SSH access to a box where the user is logged in
1384 return False
1388 return False
1385 elif getattr(osutil, 'isgui', None):
1389 elif getattr(osutil, 'isgui', None):
1386 # check if a CoreGraphics session is available
1390 # check if a CoreGraphics session is available
1387 return osutil.isgui()
1391 return osutil.isgui()
1388 else:
1392 else:
1389 # pure build; use a safe default
1393 # pure build; use a safe default
1390 return True
1394 return True
1391 else:
1395 else:
1392 return os.name == "nt" or os.environ.get("DISPLAY")
1396 return os.name == "nt" or os.environ.get("DISPLAY")
1393
1397
1394 def mktempcopy(name, emptyok=False, createmode=None):
1398 def mktempcopy(name, emptyok=False, createmode=None):
1395 """Create a temporary file with the same contents from name
1399 """Create a temporary file with the same contents from name
1396
1400
1397 The permission bits are copied from the original file.
1401 The permission bits are copied from the original file.
1398
1402
1399 If the temporary file is going to be truncated immediately, you
1403 If the temporary file is going to be truncated immediately, you
1400 can use emptyok=True as an optimization.
1404 can use emptyok=True as an optimization.
1401
1405
1402 Returns the name of the temporary file.
1406 Returns the name of the temporary file.
1403 """
1407 """
1404 d, fn = os.path.split(name)
1408 d, fn = os.path.split(name)
1405 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1409 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1406 os.close(fd)
1410 os.close(fd)
1407 # Temporary files are created with mode 0600, which is usually not
1411 # Temporary files are created with mode 0600, which is usually not
1408 # what we want. If the original file already exists, just copy
1412 # what we want. If the original file already exists, just copy
1409 # its mode. Otherwise, manually obey umask.
1413 # its mode. Otherwise, manually obey umask.
1410 copymode(name, temp, createmode)
1414 copymode(name, temp, createmode)
1411 if emptyok:
1415 if emptyok:
1412 return temp
1416 return temp
1413 try:
1417 try:
1414 try:
1418 try:
1415 ifp = posixfile(name, "rb")
1419 ifp = posixfile(name, "rb")
1416 except IOError as inst:
1420 except IOError as inst:
1417 if inst.errno == errno.ENOENT:
1421 if inst.errno == errno.ENOENT:
1418 return temp
1422 return temp
1419 if not getattr(inst, 'filename', None):
1423 if not getattr(inst, 'filename', None):
1420 inst.filename = name
1424 inst.filename = name
1421 raise
1425 raise
1422 ofp = posixfile(temp, "wb")
1426 ofp = posixfile(temp, "wb")
1423 for chunk in filechunkiter(ifp):
1427 for chunk in filechunkiter(ifp):
1424 ofp.write(chunk)
1428 ofp.write(chunk)
1425 ifp.close()
1429 ifp.close()
1426 ofp.close()
1430 ofp.close()
1427 except: # re-raises
1431 except: # re-raises
1428 try: os.unlink(temp)
1432 try: os.unlink(temp)
1429 except OSError: pass
1433 except OSError: pass
1430 raise
1434 raise
1431 return temp
1435 return temp
1432
1436
1433 class filestat(object):
1437 class filestat(object):
1434 """help to exactly detect change of a file
1438 """help to exactly detect change of a file
1435
1439
1436 'stat' attribute is result of 'os.stat()' if specified 'path'
1440 'stat' attribute is result of 'os.stat()' if specified 'path'
1437 exists. Otherwise, it is None. This can avoid preparative
1441 exists. Otherwise, it is None. This can avoid preparative
1438 'exists()' examination on client side of this class.
1442 'exists()' examination on client side of this class.
1439 """
1443 """
1440 def __init__(self, path):
1444 def __init__(self, path):
1441 try:
1445 try:
1442 self.stat = os.stat(path)
1446 self.stat = os.stat(path)
1443 except OSError as err:
1447 except OSError as err:
1444 if err.errno != errno.ENOENT:
1448 if err.errno != errno.ENOENT:
1445 raise
1449 raise
1446 self.stat = None
1450 self.stat = None
1447
1451
1448 __hash__ = object.__hash__
1452 __hash__ = object.__hash__
1449
1453
1450 def __eq__(self, old):
1454 def __eq__(self, old):
1451 try:
1455 try:
1452 # if ambiguity between stat of new and old file is
1456 # if ambiguity between stat of new and old file is
1453 # avoided, comparison of size, ctime and mtime is enough
1457 # avoided, comparison of size, ctime and mtime is enough
1454 # to exactly detect change of a file regardless of platform
1458 # to exactly detect change of a file regardless of platform
1455 return (self.stat.st_size == old.stat.st_size and
1459 return (self.stat.st_size == old.stat.st_size and
1456 self.stat.st_ctime == old.stat.st_ctime and
1460 self.stat.st_ctime == old.stat.st_ctime and
1457 self.stat.st_mtime == old.stat.st_mtime)
1461 self.stat.st_mtime == old.stat.st_mtime)
1458 except AttributeError:
1462 except AttributeError:
1459 return False
1463 return False
1460
1464
1461 def isambig(self, old):
1465 def isambig(self, old):
1462 """Examine whether new (= self) stat is ambiguous against old one
1466 """Examine whether new (= self) stat is ambiguous against old one
1463
1467
1464 "S[N]" below means stat of a file at N-th change:
1468 "S[N]" below means stat of a file at N-th change:
1465
1469
1466 - S[n-1].ctime < S[n].ctime: can detect change of a file
1470 - S[n-1].ctime < S[n].ctime: can detect change of a file
1467 - S[n-1].ctime == S[n].ctime
1471 - S[n-1].ctime == S[n].ctime
1468 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
1472 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
1469 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
1473 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
1470 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
1474 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
1471 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
1475 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
1472
1476
1473 Case (*2) above means that a file was changed twice or more at
1477 Case (*2) above means that a file was changed twice or more at
1474 same time in sec (= S[n-1].ctime), and comparison of timestamp
1478 same time in sec (= S[n-1].ctime), and comparison of timestamp
1475 is ambiguous.
1479 is ambiguous.
1476
1480
1477 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
1481 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
1478 timestamp is ambiguous".
1482 timestamp is ambiguous".
1479
1483
1480 But advancing mtime only in case (*2) doesn't work as
1484 But advancing mtime only in case (*2) doesn't work as
1481 expected, because naturally advanced S[n].mtime in case (*1)
1485 expected, because naturally advanced S[n].mtime in case (*1)
1482 might be equal to manually advanced S[n-1 or earlier].mtime.
1486 might be equal to manually advanced S[n-1 or earlier].mtime.
1483
1487
1484 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
1488 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
1485 treated as ambiguous regardless of mtime, to avoid overlooking
1489 treated as ambiguous regardless of mtime, to avoid overlooking
1486 by confliction between such mtime.
1490 by confliction between such mtime.
1487
1491
1488 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
1492 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
1489 S[n].mtime", even if size of a file isn't changed.
1493 S[n].mtime", even if size of a file isn't changed.
1490 """
1494 """
1491 try:
1495 try:
1492 return (self.stat.st_ctime == old.stat.st_ctime)
1496 return (self.stat.st_ctime == old.stat.st_ctime)
1493 except AttributeError:
1497 except AttributeError:
1494 return False
1498 return False
1495
1499
1496 def avoidambig(self, path, old):
1500 def avoidambig(self, path, old):
1497 """Change file stat of specified path to avoid ambiguity
1501 """Change file stat of specified path to avoid ambiguity
1498
1502
1499 'old' should be previous filestat of 'path'.
1503 'old' should be previous filestat of 'path'.
1500
1504
1501 This skips avoiding ambiguity, if a process doesn't have
1505 This skips avoiding ambiguity, if a process doesn't have
1502 appropriate privileges for 'path'.
1506 appropriate privileges for 'path'.
1503 """
1507 """
1504 advanced = (old.stat.st_mtime + 1) & 0x7fffffff
1508 advanced = (old.stat.st_mtime + 1) & 0x7fffffff
1505 try:
1509 try:
1506 os.utime(path, (advanced, advanced))
1510 os.utime(path, (advanced, advanced))
1507 except OSError as inst:
1511 except OSError as inst:
1508 if inst.errno == errno.EPERM:
1512 if inst.errno == errno.EPERM:
1509 # utime() on the file created by another user causes EPERM,
1513 # utime() on the file created by another user causes EPERM,
1510 # if a process doesn't have appropriate privileges
1514 # if a process doesn't have appropriate privileges
1511 return
1515 return
1512 raise
1516 raise
1513
1517
1514 def __ne__(self, other):
1518 def __ne__(self, other):
1515 return not self == other
1519 return not self == other
1516
1520
1517 class atomictempfile(object):
1521 class atomictempfile(object):
1518 '''writable file object that atomically updates a file
1522 '''writable file object that atomically updates a file
1519
1523
1520 All writes will go to a temporary copy of the original file. Call
1524 All writes will go to a temporary copy of the original file. Call
1521 close() when you are done writing, and atomictempfile will rename
1525 close() when you are done writing, and atomictempfile will rename
1522 the temporary copy to the original name, making the changes
1526 the temporary copy to the original name, making the changes
1523 visible. If the object is destroyed without being closed, all your
1527 visible. If the object is destroyed without being closed, all your
1524 writes are discarded.
1528 writes are discarded.
1525
1529
1526 checkambig argument of constructor is used with filestat, and is
1530 checkambig argument of constructor is used with filestat, and is
1527 useful only if target file is guarded by any lock (e.g. repo.lock
1531 useful only if target file is guarded by any lock (e.g. repo.lock
1528 or repo.wlock).
1532 or repo.wlock).
1529 '''
1533 '''
1530 def __init__(self, name, mode='w+b', createmode=None, checkambig=False):
1534 def __init__(self, name, mode='w+b', createmode=None, checkambig=False):
1531 self.__name = name # permanent name
1535 self.__name = name # permanent name
1532 self._tempname = mktempcopy(name, emptyok=('w' in mode),
1536 self._tempname = mktempcopy(name, emptyok=('w' in mode),
1533 createmode=createmode)
1537 createmode=createmode)
1534 self._fp = posixfile(self._tempname, mode)
1538 self._fp = posixfile(self._tempname, mode)
1535 self._checkambig = checkambig
1539 self._checkambig = checkambig
1536
1540
1537 # delegated methods
1541 # delegated methods
1538 self.read = self._fp.read
1542 self.read = self._fp.read
1539 self.write = self._fp.write
1543 self.write = self._fp.write
1540 self.seek = self._fp.seek
1544 self.seek = self._fp.seek
1541 self.tell = self._fp.tell
1545 self.tell = self._fp.tell
1542 self.fileno = self._fp.fileno
1546 self.fileno = self._fp.fileno
1543
1547
1544 def close(self):
1548 def close(self):
1545 if not self._fp.closed:
1549 if not self._fp.closed:
1546 self._fp.close()
1550 self._fp.close()
1547 filename = localpath(self.__name)
1551 filename = localpath(self.__name)
1548 oldstat = self._checkambig and filestat(filename)
1552 oldstat = self._checkambig and filestat(filename)
1549 if oldstat and oldstat.stat:
1553 if oldstat and oldstat.stat:
1550 rename(self._tempname, filename)
1554 rename(self._tempname, filename)
1551 newstat = filestat(filename)
1555 newstat = filestat(filename)
1552 if newstat.isambig(oldstat):
1556 if newstat.isambig(oldstat):
1553 # stat of changed file is ambiguous to original one
1557 # stat of changed file is ambiguous to original one
1554 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1558 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1555 os.utime(filename, (advanced, advanced))
1559 os.utime(filename, (advanced, advanced))
1556 else:
1560 else:
1557 rename(self._tempname, filename)
1561 rename(self._tempname, filename)
1558
1562
1559 def discard(self):
1563 def discard(self):
1560 if not self._fp.closed:
1564 if not self._fp.closed:
1561 try:
1565 try:
1562 os.unlink(self._tempname)
1566 os.unlink(self._tempname)
1563 except OSError:
1567 except OSError:
1564 pass
1568 pass
1565 self._fp.close()
1569 self._fp.close()
1566
1570
1567 def __del__(self):
1571 def __del__(self):
1568 if safehasattr(self, '_fp'): # constructor actually did something
1572 if safehasattr(self, '_fp'): # constructor actually did something
1569 self.discard()
1573 self.discard()
1570
1574
1571 def __enter__(self):
1575 def __enter__(self):
1572 return self
1576 return self
1573
1577
1574 def __exit__(self, exctype, excvalue, traceback):
1578 def __exit__(self, exctype, excvalue, traceback):
1575 if exctype is not None:
1579 if exctype is not None:
1576 self.discard()
1580 self.discard()
1577 else:
1581 else:
1578 self.close()
1582 self.close()
1579
1583
1580 def makedirs(name, mode=None, notindexed=False):
1584 def makedirs(name, mode=None, notindexed=False):
1581 """recursive directory creation with parent mode inheritance
1585 """recursive directory creation with parent mode inheritance
1582
1586
1583 Newly created directories are marked as "not to be indexed by
1587 Newly created directories are marked as "not to be indexed by
1584 the content indexing service", if ``notindexed`` is specified
1588 the content indexing service", if ``notindexed`` is specified
1585 for "write" mode access.
1589 for "write" mode access.
1586 """
1590 """
1587 try:
1591 try:
1588 makedir(name, notindexed)
1592 makedir(name, notindexed)
1589 except OSError as err:
1593 except OSError as err:
1590 if err.errno == errno.EEXIST:
1594 if err.errno == errno.EEXIST:
1591 return
1595 return
1592 if err.errno != errno.ENOENT or not name:
1596 if err.errno != errno.ENOENT or not name:
1593 raise
1597 raise
1594 parent = os.path.dirname(os.path.abspath(name))
1598 parent = os.path.dirname(os.path.abspath(name))
1595 if parent == name:
1599 if parent == name:
1596 raise
1600 raise
1597 makedirs(parent, mode, notindexed)
1601 makedirs(parent, mode, notindexed)
1598 try:
1602 try:
1599 makedir(name, notindexed)
1603 makedir(name, notindexed)
1600 except OSError as err:
1604 except OSError as err:
1601 # Catch EEXIST to handle races
1605 # Catch EEXIST to handle races
1602 if err.errno == errno.EEXIST:
1606 if err.errno == errno.EEXIST:
1603 return
1607 return
1604 raise
1608 raise
1605 if mode is not None:
1609 if mode is not None:
1606 os.chmod(name, mode)
1610 os.chmod(name, mode)
1607
1611
1608 def readfile(path):
1612 def readfile(path):
1609 with open(path, 'rb') as fp:
1613 with open(path, 'rb') as fp:
1610 return fp.read()
1614 return fp.read()
1611
1615
1612 def writefile(path, text):
1616 def writefile(path, text):
1613 with open(path, 'wb') as fp:
1617 with open(path, 'wb') as fp:
1614 fp.write(text)
1618 fp.write(text)
1615
1619
1616 def appendfile(path, text):
1620 def appendfile(path, text):
1617 with open(path, 'ab') as fp:
1621 with open(path, 'ab') as fp:
1618 fp.write(text)
1622 fp.write(text)
1619
1623
1620 class chunkbuffer(object):
1624 class chunkbuffer(object):
1621 """Allow arbitrary sized chunks of data to be efficiently read from an
1625 """Allow arbitrary sized chunks of data to be efficiently read from an
1622 iterator over chunks of arbitrary size."""
1626 iterator over chunks of arbitrary size."""
1623
1627
1624 def __init__(self, in_iter):
1628 def __init__(self, in_iter):
1625 """in_iter is the iterator that's iterating over the input chunks.
1629 """in_iter is the iterator that's iterating over the input chunks.
1626 targetsize is how big a buffer to try to maintain."""
1630 targetsize is how big a buffer to try to maintain."""
1627 def splitbig(chunks):
1631 def splitbig(chunks):
1628 for chunk in chunks:
1632 for chunk in chunks:
1629 if len(chunk) > 2**20:
1633 if len(chunk) > 2**20:
1630 pos = 0
1634 pos = 0
1631 while pos < len(chunk):
1635 while pos < len(chunk):
1632 end = pos + 2 ** 18
1636 end = pos + 2 ** 18
1633 yield chunk[pos:end]
1637 yield chunk[pos:end]
1634 pos = end
1638 pos = end
1635 else:
1639 else:
1636 yield chunk
1640 yield chunk
1637 self.iter = splitbig(in_iter)
1641 self.iter = splitbig(in_iter)
1638 self._queue = collections.deque()
1642 self._queue = collections.deque()
1639 self._chunkoffset = 0
1643 self._chunkoffset = 0
1640
1644
1641 def read(self, l=None):
1645 def read(self, l=None):
1642 """Read L bytes of data from the iterator of chunks of data.
1646 """Read L bytes of data from the iterator of chunks of data.
1643 Returns less than L bytes if the iterator runs dry.
1647 Returns less than L bytes if the iterator runs dry.
1644
1648
1645 If size parameter is omitted, read everything"""
1649 If size parameter is omitted, read everything"""
1646 if l is None:
1650 if l is None:
1647 return ''.join(self.iter)
1651 return ''.join(self.iter)
1648
1652
1649 left = l
1653 left = l
1650 buf = []
1654 buf = []
1651 queue = self._queue
1655 queue = self._queue
1652 while left > 0:
1656 while left > 0:
1653 # refill the queue
1657 # refill the queue
1654 if not queue:
1658 if not queue:
1655 target = 2**18
1659 target = 2**18
1656 for chunk in self.iter:
1660 for chunk in self.iter:
1657 queue.append(chunk)
1661 queue.append(chunk)
1658 target -= len(chunk)
1662 target -= len(chunk)
1659 if target <= 0:
1663 if target <= 0:
1660 break
1664 break
1661 if not queue:
1665 if not queue:
1662 break
1666 break
1663
1667
1664 # The easy way to do this would be to queue.popleft(), modify the
1668 # The easy way to do this would be to queue.popleft(), modify the
1665 # chunk (if necessary), then queue.appendleft(). However, for cases
1669 # chunk (if necessary), then queue.appendleft(). However, for cases
1666 # where we read partial chunk content, this incurs 2 dequeue
1670 # where we read partial chunk content, this incurs 2 dequeue
1667 # mutations and creates a new str for the remaining chunk in the
1671 # mutations and creates a new str for the remaining chunk in the
1668 # queue. Our code below avoids this overhead.
1672 # queue. Our code below avoids this overhead.
1669
1673
1670 chunk = queue[0]
1674 chunk = queue[0]
1671 chunkl = len(chunk)
1675 chunkl = len(chunk)
1672 offset = self._chunkoffset
1676 offset = self._chunkoffset
1673
1677
1674 # Use full chunk.
1678 # Use full chunk.
1675 if offset == 0 and left >= chunkl:
1679 if offset == 0 and left >= chunkl:
1676 left -= chunkl
1680 left -= chunkl
1677 queue.popleft()
1681 queue.popleft()
1678 buf.append(chunk)
1682 buf.append(chunk)
1679 # self._chunkoffset remains at 0.
1683 # self._chunkoffset remains at 0.
1680 continue
1684 continue
1681
1685
1682 chunkremaining = chunkl - offset
1686 chunkremaining = chunkl - offset
1683
1687
1684 # Use all of unconsumed part of chunk.
1688 # Use all of unconsumed part of chunk.
1685 if left >= chunkremaining:
1689 if left >= chunkremaining:
1686 left -= chunkremaining
1690 left -= chunkremaining
1687 queue.popleft()
1691 queue.popleft()
1688 # offset == 0 is enabled by block above, so this won't merely
1692 # offset == 0 is enabled by block above, so this won't merely
1689 # copy via ``chunk[0:]``.
1693 # copy via ``chunk[0:]``.
1690 buf.append(chunk[offset:])
1694 buf.append(chunk[offset:])
1691 self._chunkoffset = 0
1695 self._chunkoffset = 0
1692
1696
1693 # Partial chunk needed.
1697 # Partial chunk needed.
1694 else:
1698 else:
1695 buf.append(chunk[offset:offset + left])
1699 buf.append(chunk[offset:offset + left])
1696 self._chunkoffset += left
1700 self._chunkoffset += left
1697 left -= chunkremaining
1701 left -= chunkremaining
1698
1702
1699 return ''.join(buf)
1703 return ''.join(buf)
1700
1704
1701 def filechunkiter(f, size=131072, limit=None):
1705 def filechunkiter(f, size=131072, limit=None):
1702 """Create a generator that produces the data in the file size
1706 """Create a generator that produces the data in the file size
1703 (default 131072) bytes at a time, up to optional limit (default is
1707 (default 131072) bytes at a time, up to optional limit (default is
1704 to read all data). Chunks may be less than size bytes if the
1708 to read all data). Chunks may be less than size bytes if the
1705 chunk is the last chunk in the file, or the file is a socket or
1709 chunk is the last chunk in the file, or the file is a socket or
1706 some other type of file that sometimes reads less data than is
1710 some other type of file that sometimes reads less data than is
1707 requested."""
1711 requested."""
1708 assert size >= 0
1712 assert size >= 0
1709 assert limit is None or limit >= 0
1713 assert limit is None or limit >= 0
1710 while True:
1714 while True:
1711 if limit is None:
1715 if limit is None:
1712 nbytes = size
1716 nbytes = size
1713 else:
1717 else:
1714 nbytes = min(limit, size)
1718 nbytes = min(limit, size)
1715 s = nbytes and f.read(nbytes)
1719 s = nbytes and f.read(nbytes)
1716 if not s:
1720 if not s:
1717 break
1721 break
1718 if limit:
1722 if limit:
1719 limit -= len(s)
1723 limit -= len(s)
1720 yield s
1724 yield s
1721
1725
1722 def makedate(timestamp=None):
1726 def makedate(timestamp=None):
1723 '''Return a unix timestamp (or the current time) as a (unixtime,
1727 '''Return a unix timestamp (or the current time) as a (unixtime,
1724 offset) tuple based off the local timezone.'''
1728 offset) tuple based off the local timezone.'''
1725 if timestamp is None:
1729 if timestamp is None:
1726 timestamp = time.time()
1730 timestamp = time.time()
1727 if timestamp < 0:
1731 if timestamp < 0:
1728 hint = _("check your clock")
1732 hint = _("check your clock")
1729 raise Abort(_("negative timestamp: %d") % timestamp, hint=hint)
1733 raise Abort(_("negative timestamp: %d") % timestamp, hint=hint)
1730 delta = (datetime.datetime.utcfromtimestamp(timestamp) -
1734 delta = (datetime.datetime.utcfromtimestamp(timestamp) -
1731 datetime.datetime.fromtimestamp(timestamp))
1735 datetime.datetime.fromtimestamp(timestamp))
1732 tz = delta.days * 86400 + delta.seconds
1736 tz = delta.days * 86400 + delta.seconds
1733 return timestamp, tz
1737 return timestamp, tz
1734
1738
1735 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1739 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1736 """represent a (unixtime, offset) tuple as a localized time.
1740 """represent a (unixtime, offset) tuple as a localized time.
1737 unixtime is seconds since the epoch, and offset is the time zone's
1741 unixtime is seconds since the epoch, and offset is the time zone's
1738 number of seconds away from UTC.
1742 number of seconds away from UTC.
1739
1743
1740 >>> datestr((0, 0))
1744 >>> datestr((0, 0))
1741 'Thu Jan 01 00:00:00 1970 +0000'
1745 'Thu Jan 01 00:00:00 1970 +0000'
1742 >>> datestr((42, 0))
1746 >>> datestr((42, 0))
1743 'Thu Jan 01 00:00:42 1970 +0000'
1747 'Thu Jan 01 00:00:42 1970 +0000'
1744 >>> datestr((-42, 0))
1748 >>> datestr((-42, 0))
1745 'Wed Dec 31 23:59:18 1969 +0000'
1749 'Wed Dec 31 23:59:18 1969 +0000'
1746 >>> datestr((0x7fffffff, 0))
1750 >>> datestr((0x7fffffff, 0))
1747 'Tue Jan 19 03:14:07 2038 +0000'
1751 'Tue Jan 19 03:14:07 2038 +0000'
1748 >>> datestr((-0x80000000, 0))
1752 >>> datestr((-0x80000000, 0))
1749 'Fri Dec 13 20:45:52 1901 +0000'
1753 'Fri Dec 13 20:45:52 1901 +0000'
1750 """
1754 """
1751 t, tz = date or makedate()
1755 t, tz = date or makedate()
1752 if "%1" in format or "%2" in format or "%z" in format:
1756 if "%1" in format or "%2" in format or "%z" in format:
1753 sign = (tz > 0) and "-" or "+"
1757 sign = (tz > 0) and "-" or "+"
1754 minutes = abs(tz) // 60
1758 minutes = abs(tz) // 60
1755 q, r = divmod(minutes, 60)
1759 q, r = divmod(minutes, 60)
1756 format = format.replace("%z", "%1%2")
1760 format = format.replace("%z", "%1%2")
1757 format = format.replace("%1", "%c%02d" % (sign, q))
1761 format = format.replace("%1", "%c%02d" % (sign, q))
1758 format = format.replace("%2", "%02d" % r)
1762 format = format.replace("%2", "%02d" % r)
1759 d = t - tz
1763 d = t - tz
1760 if d > 0x7fffffff:
1764 if d > 0x7fffffff:
1761 d = 0x7fffffff
1765 d = 0x7fffffff
1762 elif d < -0x80000000:
1766 elif d < -0x80000000:
1763 d = -0x80000000
1767 d = -0x80000000
1764 # Never use time.gmtime() and datetime.datetime.fromtimestamp()
1768 # Never use time.gmtime() and datetime.datetime.fromtimestamp()
1765 # because they use the gmtime() system call which is buggy on Windows
1769 # because they use the gmtime() system call which is buggy on Windows
1766 # for negative values.
1770 # for negative values.
1767 t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
1771 t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
1768 s = t.strftime(format)
1772 s = t.strftime(format)
1769 return s
1773 return s
1770
1774
1771 def shortdate(date=None):
1775 def shortdate(date=None):
1772 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1776 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1773 return datestr(date, format='%Y-%m-%d')
1777 return datestr(date, format='%Y-%m-%d')
1774
1778
1775 def parsetimezone(s):
1779 def parsetimezone(s):
1776 """find a trailing timezone, if any, in string, and return a
1780 """find a trailing timezone, if any, in string, and return a
1777 (offset, remainder) pair"""
1781 (offset, remainder) pair"""
1778
1782
1779 if s.endswith("GMT") or s.endswith("UTC"):
1783 if s.endswith("GMT") or s.endswith("UTC"):
1780 return 0, s[:-3].rstrip()
1784 return 0, s[:-3].rstrip()
1781
1785
1782 # Unix-style timezones [+-]hhmm
1786 # Unix-style timezones [+-]hhmm
1783 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1787 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1784 sign = (s[-5] == "+") and 1 or -1
1788 sign = (s[-5] == "+") and 1 or -1
1785 hours = int(s[-4:-2])
1789 hours = int(s[-4:-2])
1786 minutes = int(s[-2:])
1790 minutes = int(s[-2:])
1787 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1791 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1788
1792
1789 # ISO8601 trailing Z
1793 # ISO8601 trailing Z
1790 if s.endswith("Z") and s[-2:-1].isdigit():
1794 if s.endswith("Z") and s[-2:-1].isdigit():
1791 return 0, s[:-1]
1795 return 0, s[:-1]
1792
1796
1793 # ISO8601-style [+-]hh:mm
1797 # ISO8601-style [+-]hh:mm
1794 if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and
1798 if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and
1795 s[-5:-3].isdigit() and s[-2:].isdigit()):
1799 s[-5:-3].isdigit() and s[-2:].isdigit()):
1796 sign = (s[-6] == "+") and 1 or -1
1800 sign = (s[-6] == "+") and 1 or -1
1797 hours = int(s[-5:-3])
1801 hours = int(s[-5:-3])
1798 minutes = int(s[-2:])
1802 minutes = int(s[-2:])
1799 return -sign * (hours * 60 + minutes) * 60, s[:-6]
1803 return -sign * (hours * 60 + minutes) * 60, s[:-6]
1800
1804
1801 return None, s
1805 return None, s
1802
1806
1803 def strdate(string, format, defaults=[]):
1807 def strdate(string, format, defaults=[]):
1804 """parse a localized time string and return a (unixtime, offset) tuple.
1808 """parse a localized time string and return a (unixtime, offset) tuple.
1805 if the string cannot be parsed, ValueError is raised."""
1809 if the string cannot be parsed, ValueError is raised."""
1806 # NOTE: unixtime = localunixtime + offset
1810 # NOTE: unixtime = localunixtime + offset
1807 offset, date = parsetimezone(string)
1811 offset, date = parsetimezone(string)
1808
1812
1809 # add missing elements from defaults
1813 # add missing elements from defaults
1810 usenow = False # default to using biased defaults
1814 usenow = False # default to using biased defaults
1811 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1815 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1812 found = [True for p in part if ("%"+p) in format]
1816 found = [True for p in part if ("%"+p) in format]
1813 if not found:
1817 if not found:
1814 date += "@" + defaults[part][usenow]
1818 date += "@" + defaults[part][usenow]
1815 format += "@%" + part[0]
1819 format += "@%" + part[0]
1816 else:
1820 else:
1817 # We've found a specific time element, less specific time
1821 # We've found a specific time element, less specific time
1818 # elements are relative to today
1822 # elements are relative to today
1819 usenow = True
1823 usenow = True
1820
1824
1821 timetuple = time.strptime(date, format)
1825 timetuple = time.strptime(date, format)
1822 localunixtime = int(calendar.timegm(timetuple))
1826 localunixtime = int(calendar.timegm(timetuple))
1823 if offset is None:
1827 if offset is None:
1824 # local timezone
1828 # local timezone
1825 unixtime = int(time.mktime(timetuple))
1829 unixtime = int(time.mktime(timetuple))
1826 offset = unixtime - localunixtime
1830 offset = unixtime - localunixtime
1827 else:
1831 else:
1828 unixtime = localunixtime + offset
1832 unixtime = localunixtime + offset
1829 return unixtime, offset
1833 return unixtime, offset
1830
1834
1831 def parsedate(date, formats=None, bias=None):
1835 def parsedate(date, formats=None, bias=None):
1832 """parse a localized date/time and return a (unixtime, offset) tuple.
1836 """parse a localized date/time and return a (unixtime, offset) tuple.
1833
1837
1834 The date may be a "unixtime offset" string or in one of the specified
1838 The date may be a "unixtime offset" string or in one of the specified
1835 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1839 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1836
1840
1837 >>> parsedate(' today ') == parsedate(\
1841 >>> parsedate(' today ') == parsedate(\
1838 datetime.date.today().strftime('%b %d'))
1842 datetime.date.today().strftime('%b %d'))
1839 True
1843 True
1840 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1844 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1841 datetime.timedelta(days=1)\
1845 datetime.timedelta(days=1)\
1842 ).strftime('%b %d'))
1846 ).strftime('%b %d'))
1843 True
1847 True
1844 >>> now, tz = makedate()
1848 >>> now, tz = makedate()
1845 >>> strnow, strtz = parsedate('now')
1849 >>> strnow, strtz = parsedate('now')
1846 >>> (strnow - now) < 1
1850 >>> (strnow - now) < 1
1847 True
1851 True
1848 >>> tz == strtz
1852 >>> tz == strtz
1849 True
1853 True
1850 """
1854 """
1851 if bias is None:
1855 if bias is None:
1852 bias = {}
1856 bias = {}
1853 if not date:
1857 if not date:
1854 return 0, 0
1858 return 0, 0
1855 if isinstance(date, tuple) and len(date) == 2:
1859 if isinstance(date, tuple) and len(date) == 2:
1856 return date
1860 return date
1857 if not formats:
1861 if not formats:
1858 formats = defaultdateformats
1862 formats = defaultdateformats
1859 date = date.strip()
1863 date = date.strip()
1860
1864
1861 if date == 'now' or date == _('now'):
1865 if date == 'now' or date == _('now'):
1862 return makedate()
1866 return makedate()
1863 if date == 'today' or date == _('today'):
1867 if date == 'today' or date == _('today'):
1864 date = datetime.date.today().strftime('%b %d')
1868 date = datetime.date.today().strftime('%b %d')
1865 elif date == 'yesterday' or date == _('yesterday'):
1869 elif date == 'yesterday' or date == _('yesterday'):
1866 date = (datetime.date.today() -
1870 date = (datetime.date.today() -
1867 datetime.timedelta(days=1)).strftime('%b %d')
1871 datetime.timedelta(days=1)).strftime('%b %d')
1868
1872
1869 try:
1873 try:
1870 when, offset = map(int, date.split(' '))
1874 when, offset = map(int, date.split(' '))
1871 except ValueError:
1875 except ValueError:
1872 # fill out defaults
1876 # fill out defaults
1873 now = makedate()
1877 now = makedate()
1874 defaults = {}
1878 defaults = {}
1875 for part in ("d", "mb", "yY", "HI", "M", "S"):
1879 for part in ("d", "mb", "yY", "HI", "M", "S"):
1876 # this piece is for rounding the specific end of unknowns
1880 # this piece is for rounding the specific end of unknowns
1877 b = bias.get(part)
1881 b = bias.get(part)
1878 if b is None:
1882 if b is None:
1879 if part[0] in "HMS":
1883 if part[0] in "HMS":
1880 b = "00"
1884 b = "00"
1881 else:
1885 else:
1882 b = "0"
1886 b = "0"
1883
1887
1884 # this piece is for matching the generic end to today's date
1888 # this piece is for matching the generic end to today's date
1885 n = datestr(now, "%" + part[0])
1889 n = datestr(now, "%" + part[0])
1886
1890
1887 defaults[part] = (b, n)
1891 defaults[part] = (b, n)
1888
1892
1889 for format in formats:
1893 for format in formats:
1890 try:
1894 try:
1891 when, offset = strdate(date, format, defaults)
1895 when, offset = strdate(date, format, defaults)
1892 except (ValueError, OverflowError):
1896 except (ValueError, OverflowError):
1893 pass
1897 pass
1894 else:
1898 else:
1895 break
1899 break
1896 else:
1900 else:
1897 raise Abort(_('invalid date: %r') % date)
1901 raise Abort(_('invalid date: %r') % date)
1898 # validate explicit (probably user-specified) date and
1902 # validate explicit (probably user-specified) date and
1899 # time zone offset. values must fit in signed 32 bits for
1903 # time zone offset. values must fit in signed 32 bits for
1900 # current 32-bit linux runtimes. timezones go from UTC-12
1904 # current 32-bit linux runtimes. timezones go from UTC-12
1901 # to UTC+14
1905 # to UTC+14
1902 if when < -0x80000000 or when > 0x7fffffff:
1906 if when < -0x80000000 or when > 0x7fffffff:
1903 raise Abort(_('date exceeds 32 bits: %d') % when)
1907 raise Abort(_('date exceeds 32 bits: %d') % when)
1904 if offset < -50400 or offset > 43200:
1908 if offset < -50400 or offset > 43200:
1905 raise Abort(_('impossible time zone offset: %d') % offset)
1909 raise Abort(_('impossible time zone offset: %d') % offset)
1906 return when, offset
1910 return when, offset
1907
1911
1908 def matchdate(date):
1912 def matchdate(date):
1909 """Return a function that matches a given date match specifier
1913 """Return a function that matches a given date match specifier
1910
1914
1911 Formats include:
1915 Formats include:
1912
1916
1913 '{date}' match a given date to the accuracy provided
1917 '{date}' match a given date to the accuracy provided
1914
1918
1915 '<{date}' on or before a given date
1919 '<{date}' on or before a given date
1916
1920
1917 '>{date}' on or after a given date
1921 '>{date}' on or after a given date
1918
1922
1919 >>> p1 = parsedate("10:29:59")
1923 >>> p1 = parsedate("10:29:59")
1920 >>> p2 = parsedate("10:30:00")
1924 >>> p2 = parsedate("10:30:00")
1921 >>> p3 = parsedate("10:30:59")
1925 >>> p3 = parsedate("10:30:59")
1922 >>> p4 = parsedate("10:31:00")
1926 >>> p4 = parsedate("10:31:00")
1923 >>> p5 = parsedate("Sep 15 10:30:00 1999")
1927 >>> p5 = parsedate("Sep 15 10:30:00 1999")
1924 >>> f = matchdate("10:30")
1928 >>> f = matchdate("10:30")
1925 >>> f(p1[0])
1929 >>> f(p1[0])
1926 False
1930 False
1927 >>> f(p2[0])
1931 >>> f(p2[0])
1928 True
1932 True
1929 >>> f(p3[0])
1933 >>> f(p3[0])
1930 True
1934 True
1931 >>> f(p4[0])
1935 >>> f(p4[0])
1932 False
1936 False
1933 >>> f(p5[0])
1937 >>> f(p5[0])
1934 False
1938 False
1935 """
1939 """
1936
1940
1937 def lower(date):
1941 def lower(date):
1938 d = {'mb': "1", 'd': "1"}
1942 d = {'mb': "1", 'd': "1"}
1939 return parsedate(date, extendeddateformats, d)[0]
1943 return parsedate(date, extendeddateformats, d)[0]
1940
1944
1941 def upper(date):
1945 def upper(date):
1942 d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"}
1946 d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"}
1943 for days in ("31", "30", "29"):
1947 for days in ("31", "30", "29"):
1944 try:
1948 try:
1945 d["d"] = days
1949 d["d"] = days
1946 return parsedate(date, extendeddateformats, d)[0]
1950 return parsedate(date, extendeddateformats, d)[0]
1947 except Abort:
1951 except Abort:
1948 pass
1952 pass
1949 d["d"] = "28"
1953 d["d"] = "28"
1950 return parsedate(date, extendeddateformats, d)[0]
1954 return parsedate(date, extendeddateformats, d)[0]
1951
1955
1952 date = date.strip()
1956 date = date.strip()
1953
1957
1954 if not date:
1958 if not date:
1955 raise Abort(_("dates cannot consist entirely of whitespace"))
1959 raise Abort(_("dates cannot consist entirely of whitespace"))
1956 elif date[0] == "<":
1960 elif date[0] == "<":
1957 if not date[1:]:
1961 if not date[1:]:
1958 raise Abort(_("invalid day spec, use '<DATE'"))
1962 raise Abort(_("invalid day spec, use '<DATE'"))
1959 when = upper(date[1:])
1963 when = upper(date[1:])
1960 return lambda x: x <= when
1964 return lambda x: x <= when
1961 elif date[0] == ">":
1965 elif date[0] == ">":
1962 if not date[1:]:
1966 if not date[1:]:
1963 raise Abort(_("invalid day spec, use '>DATE'"))
1967 raise Abort(_("invalid day spec, use '>DATE'"))
1964 when = lower(date[1:])
1968 when = lower(date[1:])
1965 return lambda x: x >= when
1969 return lambda x: x >= when
1966 elif date[0] == "-":
1970 elif date[0] == "-":
1967 try:
1971 try:
1968 days = int(date[1:])
1972 days = int(date[1:])
1969 except ValueError:
1973 except ValueError:
1970 raise Abort(_("invalid day spec: %s") % date[1:])
1974 raise Abort(_("invalid day spec: %s") % date[1:])
1971 if days < 0:
1975 if days < 0:
1972 raise Abort(_("%s must be nonnegative (see 'hg help dates')")
1976 raise Abort(_("%s must be nonnegative (see 'hg help dates')")
1973 % date[1:])
1977 % date[1:])
1974 when = makedate()[0] - days * 3600 * 24
1978 when = makedate()[0] - days * 3600 * 24
1975 return lambda x: x >= when
1979 return lambda x: x >= when
1976 elif " to " in date:
1980 elif " to " in date:
1977 a, b = date.split(" to ")
1981 a, b = date.split(" to ")
1978 start, stop = lower(a), upper(b)
1982 start, stop = lower(a), upper(b)
1979 return lambda x: x >= start and x <= stop
1983 return lambda x: x >= start and x <= stop
1980 else:
1984 else:
1981 start, stop = lower(date), upper(date)
1985 start, stop = lower(date), upper(date)
1982 return lambda x: x >= start and x <= stop
1986 return lambda x: x >= start and x <= stop
1983
1987
1984 def stringmatcher(pattern):
1988 def stringmatcher(pattern):
1985 """
1989 """
1986 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1990 accepts a string, possibly starting with 're:' or 'literal:' prefix.
1987 returns the matcher name, pattern, and matcher function.
1991 returns the matcher name, pattern, and matcher function.
1988 missing or unknown prefixes are treated as literal matches.
1992 missing or unknown prefixes are treated as literal matches.
1989
1993
1990 helper for tests:
1994 helper for tests:
1991 >>> def test(pattern, *tests):
1995 >>> def test(pattern, *tests):
1992 ... kind, pattern, matcher = stringmatcher(pattern)
1996 ... kind, pattern, matcher = stringmatcher(pattern)
1993 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1997 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
1994
1998
1995 exact matching (no prefix):
1999 exact matching (no prefix):
1996 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
2000 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
1997 ('literal', 'abcdefg', [False, False, True])
2001 ('literal', 'abcdefg', [False, False, True])
1998
2002
1999 regex matching ('re:' prefix)
2003 regex matching ('re:' prefix)
2000 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
2004 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
2001 ('re', 'a.+b', [False, False, True])
2005 ('re', 'a.+b', [False, False, True])
2002
2006
2003 force exact matches ('literal:' prefix)
2007 force exact matches ('literal:' prefix)
2004 >>> test('literal:re:foobar', 'foobar', 're:foobar')
2008 >>> test('literal:re:foobar', 'foobar', 're:foobar')
2005 ('literal', 're:foobar', [False, True])
2009 ('literal', 're:foobar', [False, True])
2006
2010
2007 unknown prefixes are ignored and treated as literals
2011 unknown prefixes are ignored and treated as literals
2008 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
2012 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
2009 ('literal', 'foo:bar', [False, False, True])
2013 ('literal', 'foo:bar', [False, False, True])
2010 """
2014 """
2011 if pattern.startswith('re:'):
2015 if pattern.startswith('re:'):
2012 pattern = pattern[3:]
2016 pattern = pattern[3:]
2013 try:
2017 try:
2014 regex = remod.compile(pattern)
2018 regex = remod.compile(pattern)
2015 except remod.error as e:
2019 except remod.error as e:
2016 raise error.ParseError(_('invalid regular expression: %s')
2020 raise error.ParseError(_('invalid regular expression: %s')
2017 % e)
2021 % e)
2018 return 're', pattern, regex.search
2022 return 're', pattern, regex.search
2019 elif pattern.startswith('literal:'):
2023 elif pattern.startswith('literal:'):
2020 pattern = pattern[8:]
2024 pattern = pattern[8:]
2021 return 'literal', pattern, pattern.__eq__
2025 return 'literal', pattern, pattern.__eq__
2022
2026
2023 def shortuser(user):
2027 def shortuser(user):
2024 """Return a short representation of a user name or email address."""
2028 """Return a short representation of a user name or email address."""
2025 f = user.find('@')
2029 f = user.find('@')
2026 if f >= 0:
2030 if f >= 0:
2027 user = user[:f]
2031 user = user[:f]
2028 f = user.find('<')
2032 f = user.find('<')
2029 if f >= 0:
2033 if f >= 0:
2030 user = user[f + 1:]
2034 user = user[f + 1:]
2031 f = user.find(' ')
2035 f = user.find(' ')
2032 if f >= 0:
2036 if f >= 0:
2033 user = user[:f]
2037 user = user[:f]
2034 f = user.find('.')
2038 f = user.find('.')
2035 if f >= 0:
2039 if f >= 0:
2036 user = user[:f]
2040 user = user[:f]
2037 return user
2041 return user
2038
2042
2039 def emailuser(user):
2043 def emailuser(user):
2040 """Return the user portion of an email address."""
2044 """Return the user portion of an email address."""
2041 f = user.find('@')
2045 f = user.find('@')
2042 if f >= 0:
2046 if f >= 0:
2043 user = user[:f]
2047 user = user[:f]
2044 f = user.find('<')
2048 f = user.find('<')
2045 if f >= 0:
2049 if f >= 0:
2046 user = user[f + 1:]
2050 user = user[f + 1:]
2047 return user
2051 return user
2048
2052
2049 def email(author):
2053 def email(author):
2050 '''get email of author.'''
2054 '''get email of author.'''
2051 r = author.find('>')
2055 r = author.find('>')
2052 if r == -1:
2056 if r == -1:
2053 r = None
2057 r = None
2054 return author[author.find('<') + 1:r]
2058 return author[author.find('<') + 1:r]
2055
2059
2056 def ellipsis(text, maxlength=400):
2060 def ellipsis(text, maxlength=400):
2057 """Trim string to at most maxlength (default: 400) columns in display."""
2061 """Trim string to at most maxlength (default: 400) columns in display."""
2058 return encoding.trim(text, maxlength, ellipsis='...')
2062 return encoding.trim(text, maxlength, ellipsis='...')
2059
2063
2060 def unitcountfn(*unittable):
2064 def unitcountfn(*unittable):
2061 '''return a function that renders a readable count of some quantity'''
2065 '''return a function that renders a readable count of some quantity'''
2062
2066
2063 def go(count):
2067 def go(count):
2064 for multiplier, divisor, format in unittable:
2068 for multiplier, divisor, format in unittable:
2065 if count >= divisor * multiplier:
2069 if count >= divisor * multiplier:
2066 return format % (count / float(divisor))
2070 return format % (count / float(divisor))
2067 return unittable[-1][2] % count
2071 return unittable[-1][2] % count
2068
2072
2069 return go
2073 return go
2070
2074
2071 bytecount = unitcountfn(
2075 bytecount = unitcountfn(
2072 (100, 1 << 30, _('%.0f GB')),
2076 (100, 1 << 30, _('%.0f GB')),
2073 (10, 1 << 30, _('%.1f GB')),
2077 (10, 1 << 30, _('%.1f GB')),
2074 (1, 1 << 30, _('%.2f GB')),
2078 (1, 1 << 30, _('%.2f GB')),
2075 (100, 1 << 20, _('%.0f MB')),
2079 (100, 1 << 20, _('%.0f MB')),
2076 (10, 1 << 20, _('%.1f MB')),
2080 (10, 1 << 20, _('%.1f MB')),
2077 (1, 1 << 20, _('%.2f MB')),
2081 (1, 1 << 20, _('%.2f MB')),
2078 (100, 1 << 10, _('%.0f KB')),
2082 (100, 1 << 10, _('%.0f KB')),
2079 (10, 1 << 10, _('%.1f KB')),
2083 (10, 1 << 10, _('%.1f KB')),
2080 (1, 1 << 10, _('%.2f KB')),
2084 (1, 1 << 10, _('%.2f KB')),
2081 (1, 1, _('%.0f bytes')),
2085 (1, 1, _('%.0f bytes')),
2082 )
2086 )
2083
2087
2084 def uirepr(s):
2088 def uirepr(s):
2085 # Avoid double backslash in Windows path repr()
2089 # Avoid double backslash in Windows path repr()
2086 return repr(s).replace('\\\\', '\\')
2090 return repr(s).replace('\\\\', '\\')
2087
2091
2088 # delay import of textwrap
2092 # delay import of textwrap
2089 def MBTextWrapper(**kwargs):
2093 def MBTextWrapper(**kwargs):
2090 class tw(textwrap.TextWrapper):
2094 class tw(textwrap.TextWrapper):
2091 """
2095 """
2092 Extend TextWrapper for width-awareness.
2096 Extend TextWrapper for width-awareness.
2093
2097
2094 Neither number of 'bytes' in any encoding nor 'characters' is
2098 Neither number of 'bytes' in any encoding nor 'characters' is
2095 appropriate to calculate terminal columns for specified string.
2099 appropriate to calculate terminal columns for specified string.
2096
2100
2097 Original TextWrapper implementation uses built-in 'len()' directly,
2101 Original TextWrapper implementation uses built-in 'len()' directly,
2098 so overriding is needed to use width information of each characters.
2102 so overriding is needed to use width information of each characters.
2099
2103
2100 In addition, characters classified into 'ambiguous' width are
2104 In addition, characters classified into 'ambiguous' width are
2101 treated as wide in East Asian area, but as narrow in other.
2105 treated as wide in East Asian area, but as narrow in other.
2102
2106
2103 This requires use decision to determine width of such characters.
2107 This requires use decision to determine width of such characters.
2104 """
2108 """
2105 def _cutdown(self, ucstr, space_left):
2109 def _cutdown(self, ucstr, space_left):
2106 l = 0
2110 l = 0
2107 colwidth = encoding.ucolwidth
2111 colwidth = encoding.ucolwidth
2108 for i in xrange(len(ucstr)):
2112 for i in xrange(len(ucstr)):
2109 l += colwidth(ucstr[i])
2113 l += colwidth(ucstr[i])
2110 if space_left < l:
2114 if space_left < l:
2111 return (ucstr[:i], ucstr[i:])
2115 return (ucstr[:i], ucstr[i:])
2112 return ucstr, ''
2116 return ucstr, ''
2113
2117
2114 # overriding of base class
2118 # overriding of base class
2115 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
2119 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
2116 space_left = max(width - cur_len, 1)
2120 space_left = max(width - cur_len, 1)
2117
2121
2118 if self.break_long_words:
2122 if self.break_long_words:
2119 cut, res = self._cutdown(reversed_chunks[-1], space_left)
2123 cut, res = self._cutdown(reversed_chunks[-1], space_left)
2120 cur_line.append(cut)
2124 cur_line.append(cut)
2121 reversed_chunks[-1] = res
2125 reversed_chunks[-1] = res
2122 elif not cur_line:
2126 elif not cur_line:
2123 cur_line.append(reversed_chunks.pop())
2127 cur_line.append(reversed_chunks.pop())
2124
2128
2125 # this overriding code is imported from TextWrapper of Python 2.6
2129 # this overriding code is imported from TextWrapper of Python 2.6
2126 # to calculate columns of string by 'encoding.ucolwidth()'
2130 # to calculate columns of string by 'encoding.ucolwidth()'
2127 def _wrap_chunks(self, chunks):
2131 def _wrap_chunks(self, chunks):
2128 colwidth = encoding.ucolwidth
2132 colwidth = encoding.ucolwidth
2129
2133
2130 lines = []
2134 lines = []
2131 if self.width <= 0:
2135 if self.width <= 0:
2132 raise ValueError("invalid width %r (must be > 0)" % self.width)
2136 raise ValueError("invalid width %r (must be > 0)" % self.width)
2133
2137
2134 # Arrange in reverse order so items can be efficiently popped
2138 # Arrange in reverse order so items can be efficiently popped
2135 # from a stack of chucks.
2139 # from a stack of chucks.
2136 chunks.reverse()
2140 chunks.reverse()
2137
2141
2138 while chunks:
2142 while chunks:
2139
2143
2140 # Start the list of chunks that will make up the current line.
2144 # Start the list of chunks that will make up the current line.
2141 # cur_len is just the length of all the chunks in cur_line.
2145 # cur_len is just the length of all the chunks in cur_line.
2142 cur_line = []
2146 cur_line = []
2143 cur_len = 0
2147 cur_len = 0
2144
2148
2145 # Figure out which static string will prefix this line.
2149 # Figure out which static string will prefix this line.
2146 if lines:
2150 if lines:
2147 indent = self.subsequent_indent
2151 indent = self.subsequent_indent
2148 else:
2152 else:
2149 indent = self.initial_indent
2153 indent = self.initial_indent
2150
2154
2151 # Maximum width for this line.
2155 # Maximum width for this line.
2152 width = self.width - len(indent)
2156 width = self.width - len(indent)
2153
2157
2154 # First chunk on line is whitespace -- drop it, unless this
2158 # First chunk on line is whitespace -- drop it, unless this
2155 # is the very beginning of the text (i.e. no lines started yet).
2159 # is the very beginning of the text (i.e. no lines started yet).
2156 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
2160 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
2157 del chunks[-1]
2161 del chunks[-1]
2158
2162
2159 while chunks:
2163 while chunks:
2160 l = colwidth(chunks[-1])
2164 l = colwidth(chunks[-1])
2161
2165
2162 # Can at least squeeze this chunk onto the current line.
2166 # Can at least squeeze this chunk onto the current line.
2163 if cur_len + l <= width:
2167 if cur_len + l <= width:
2164 cur_line.append(chunks.pop())
2168 cur_line.append(chunks.pop())
2165 cur_len += l
2169 cur_len += l
2166
2170
2167 # Nope, this line is full.
2171 # Nope, this line is full.
2168 else:
2172 else:
2169 break
2173 break
2170
2174
2171 # The current line is full, and the next chunk is too big to
2175 # The current line is full, and the next chunk is too big to
2172 # fit on *any* line (not just this one).
2176 # fit on *any* line (not just this one).
2173 if chunks and colwidth(chunks[-1]) > width:
2177 if chunks and colwidth(chunks[-1]) > width:
2174 self._handle_long_word(chunks, cur_line, cur_len, width)
2178 self._handle_long_word(chunks, cur_line, cur_len, width)
2175
2179
2176 # If the last chunk on this line is all whitespace, drop it.
2180 # If the last chunk on this line is all whitespace, drop it.
2177 if (self.drop_whitespace and
2181 if (self.drop_whitespace and
2178 cur_line and cur_line[-1].strip() == ''):
2182 cur_line and cur_line[-1].strip() == ''):
2179 del cur_line[-1]
2183 del cur_line[-1]
2180
2184
2181 # Convert current line back to a string and store it in list
2185 # Convert current line back to a string and store it in list
2182 # of all lines (return value).
2186 # of all lines (return value).
2183 if cur_line:
2187 if cur_line:
2184 lines.append(indent + ''.join(cur_line))
2188 lines.append(indent + ''.join(cur_line))
2185
2189
2186 return lines
2190 return lines
2187
2191
2188 global MBTextWrapper
2192 global MBTextWrapper
2189 MBTextWrapper = tw
2193 MBTextWrapper = tw
2190 return tw(**kwargs)
2194 return tw(**kwargs)
2191
2195
2192 def wrap(line, width, initindent='', hangindent=''):
2196 def wrap(line, width, initindent='', hangindent=''):
2193 maxindent = max(len(hangindent), len(initindent))
2197 maxindent = max(len(hangindent), len(initindent))
2194 if width <= maxindent:
2198 if width <= maxindent:
2195 # adjust for weird terminal size
2199 # adjust for weird terminal size
2196 width = max(78, maxindent + 1)
2200 width = max(78, maxindent + 1)
2197 line = line.decode(encoding.encoding, encoding.encodingmode)
2201 line = line.decode(encoding.encoding, encoding.encodingmode)
2198 initindent = initindent.decode(encoding.encoding, encoding.encodingmode)
2202 initindent = initindent.decode(encoding.encoding, encoding.encodingmode)
2199 hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode)
2203 hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode)
2200 wrapper = MBTextWrapper(width=width,
2204 wrapper = MBTextWrapper(width=width,
2201 initial_indent=initindent,
2205 initial_indent=initindent,
2202 subsequent_indent=hangindent)
2206 subsequent_indent=hangindent)
2203 return wrapper.fill(line).encode(encoding.encoding)
2207 return wrapper.fill(line).encode(encoding.encoding)
2204
2208
2205 if (pyplatform.python_implementation() == 'CPython' and
2209 if (pyplatform.python_implementation() == 'CPython' and
2206 sys.version_info < (3, 0)):
2210 sys.version_info < (3, 0)):
2207 # There is an issue in CPython that some IO methods do not handle EINTR
2211 # There is an issue in CPython that some IO methods do not handle EINTR
2208 # correctly. The following table shows what CPython version (and functions)
2212 # correctly. The following table shows what CPython version (and functions)
2209 # are affected (buggy: has the EINTR bug, okay: otherwise):
2213 # are affected (buggy: has the EINTR bug, okay: otherwise):
2210 #
2214 #
2211 # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0
2215 # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0
2212 # --------------------------------------------------
2216 # --------------------------------------------------
2213 # fp.__iter__ | buggy | buggy | okay
2217 # fp.__iter__ | buggy | buggy | okay
2214 # fp.read* | buggy | okay [1] | okay
2218 # fp.read* | buggy | okay [1] | okay
2215 #
2219 #
2216 # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo.
2220 # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo.
2217 #
2221 #
2218 # Here we workaround the EINTR issue for fileobj.__iter__. Other methods
2222 # Here we workaround the EINTR issue for fileobj.__iter__. Other methods
2219 # like "read*" are ignored for now, as Python < 2.7.4 is a minority.
2223 # like "read*" are ignored for now, as Python < 2.7.4 is a minority.
2220 #
2224 #
2221 # Although we can workaround the EINTR issue for fp.__iter__, it is slower:
2225 # Although we can workaround the EINTR issue for fp.__iter__, it is slower:
2222 # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in
2226 # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in
2223 # CPython 2, because CPython 2 maintains an internal readahead buffer for
2227 # CPython 2, because CPython 2 maintains an internal readahead buffer for
2224 # fp.__iter__ but not other fp.read* methods.
2228 # fp.__iter__ but not other fp.read* methods.
2225 #
2229 #
2226 # On modern systems like Linux, the "read" syscall cannot be interrupted
2230 # On modern systems like Linux, the "read" syscall cannot be interrupted
2227 # when reading "fast" files like on-disk files. So the EINTR issue only
2231 # when reading "fast" files like on-disk files. So the EINTR issue only
2228 # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG)
2232 # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG)
2229 # files approximately as "fast" files and use the fast (unsafe) code path,
2233 # files approximately as "fast" files and use the fast (unsafe) code path,
2230 # to minimize the performance impact.
2234 # to minimize the performance impact.
2231 if sys.version_info >= (2, 7, 4):
2235 if sys.version_info >= (2, 7, 4):
2232 # fp.readline deals with EINTR correctly, use it as a workaround.
2236 # fp.readline deals with EINTR correctly, use it as a workaround.
2233 def _safeiterfile(fp):
2237 def _safeiterfile(fp):
2234 return iter(fp.readline, '')
2238 return iter(fp.readline, '')
2235 else:
2239 else:
2236 # fp.read* are broken too, manually deal with EINTR in a stupid way.
2240 # fp.read* are broken too, manually deal with EINTR in a stupid way.
2237 # note: this may block longer than necessary because of bufsize.
2241 # note: this may block longer than necessary because of bufsize.
2238 def _safeiterfile(fp, bufsize=4096):
2242 def _safeiterfile(fp, bufsize=4096):
2239 fd = fp.fileno()
2243 fd = fp.fileno()
2240 line = ''
2244 line = ''
2241 while True:
2245 while True:
2242 try:
2246 try:
2243 buf = os.read(fd, bufsize)
2247 buf = os.read(fd, bufsize)
2244 except OSError as ex:
2248 except OSError as ex:
2245 # os.read only raises EINTR before any data is read
2249 # os.read only raises EINTR before any data is read
2246 if ex.errno == errno.EINTR:
2250 if ex.errno == errno.EINTR:
2247 continue
2251 continue
2248 else:
2252 else:
2249 raise
2253 raise
2250 line += buf
2254 line += buf
2251 if '\n' in buf:
2255 if '\n' in buf:
2252 splitted = line.splitlines(True)
2256 splitted = line.splitlines(True)
2253 line = ''
2257 line = ''
2254 for l in splitted:
2258 for l in splitted:
2255 if l[-1] == '\n':
2259 if l[-1] == '\n':
2256 yield l
2260 yield l
2257 else:
2261 else:
2258 line = l
2262 line = l
2259 if not buf:
2263 if not buf:
2260 break
2264 break
2261 if line:
2265 if line:
2262 yield line
2266 yield line
2263
2267
2264 def iterfile(fp):
2268 def iterfile(fp):
2265 fastpath = True
2269 fastpath = True
2266 if type(fp) is file:
2270 if type(fp) is file:
2267 fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode)
2271 fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode)
2268 if fastpath:
2272 if fastpath:
2269 return fp
2273 return fp
2270 else:
2274 else:
2271 return _safeiterfile(fp)
2275 return _safeiterfile(fp)
2272 else:
2276 else:
2273 # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed.
2277 # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed.
2274 def iterfile(fp):
2278 def iterfile(fp):
2275 return fp
2279 return fp
2276
2280
2277 def iterlines(iterator):
2281 def iterlines(iterator):
2278 for chunk in iterator:
2282 for chunk in iterator:
2279 for line in chunk.splitlines():
2283 for line in chunk.splitlines():
2280 yield line
2284 yield line
2281
2285
2282 def expandpath(path):
2286 def expandpath(path):
2283 return os.path.expanduser(os.path.expandvars(path))
2287 return os.path.expanduser(os.path.expandvars(path))
2284
2288
2285 def hgcmd():
2289 def hgcmd():
2286 """Return the command used to execute current hg
2290 """Return the command used to execute current hg
2287
2291
2288 This is different from hgexecutable() because on Windows we want
2292 This is different from hgexecutable() because on Windows we want
2289 to avoid things opening new shell windows like batch files, so we
2293 to avoid things opening new shell windows like batch files, so we
2290 get either the python call or current executable.
2294 get either the python call or current executable.
2291 """
2295 """
2292 if mainfrozen():
2296 if mainfrozen():
2293 if getattr(sys, 'frozen', None) == 'macosx_app':
2297 if getattr(sys, 'frozen', None) == 'macosx_app':
2294 # Env variable set by py2app
2298 # Env variable set by py2app
2295 return [os.environ['EXECUTABLEPATH']]
2299 return [os.environ['EXECUTABLEPATH']]
2296 else:
2300 else:
2297 return [sys.executable]
2301 return [sys.executable]
2298 return gethgcmd()
2302 return gethgcmd()
2299
2303
2300 def rundetached(args, condfn):
2304 def rundetached(args, condfn):
2301 """Execute the argument list in a detached process.
2305 """Execute the argument list in a detached process.
2302
2306
2303 condfn is a callable which is called repeatedly and should return
2307 condfn is a callable which is called repeatedly and should return
2304 True once the child process is known to have started successfully.
2308 True once the child process is known to have started successfully.
2305 At this point, the child process PID is returned. If the child
2309 At this point, the child process PID is returned. If the child
2306 process fails to start or finishes before condfn() evaluates to
2310 process fails to start or finishes before condfn() evaluates to
2307 True, return -1.
2311 True, return -1.
2308 """
2312 """
2309 # Windows case is easier because the child process is either
2313 # Windows case is easier because the child process is either
2310 # successfully starting and validating the condition or exiting
2314 # successfully starting and validating the condition or exiting
2311 # on failure. We just poll on its PID. On Unix, if the child
2315 # on failure. We just poll on its PID. On Unix, if the child
2312 # process fails to start, it will be left in a zombie state until
2316 # process fails to start, it will be left in a zombie state until
2313 # the parent wait on it, which we cannot do since we expect a long
2317 # the parent wait on it, which we cannot do since we expect a long
2314 # running process on success. Instead we listen for SIGCHLD telling
2318 # running process on success. Instead we listen for SIGCHLD telling
2315 # us our child process terminated.
2319 # us our child process terminated.
2316 terminated = set()
2320 terminated = set()
2317 def handler(signum, frame):
2321 def handler(signum, frame):
2318 terminated.add(os.wait())
2322 terminated.add(os.wait())
2319 prevhandler = None
2323 prevhandler = None
2320 SIGCHLD = getattr(signal, 'SIGCHLD', None)
2324 SIGCHLD = getattr(signal, 'SIGCHLD', None)
2321 if SIGCHLD is not None:
2325 if SIGCHLD is not None:
2322 prevhandler = signal.signal(SIGCHLD, handler)
2326 prevhandler = signal.signal(SIGCHLD, handler)
2323 try:
2327 try:
2324 pid = spawndetached(args)
2328 pid = spawndetached(args)
2325 while not condfn():
2329 while not condfn():
2326 if ((pid in terminated or not testpid(pid))
2330 if ((pid in terminated or not testpid(pid))
2327 and not condfn()):
2331 and not condfn()):
2328 return -1
2332 return -1
2329 time.sleep(0.1)
2333 time.sleep(0.1)
2330 return pid
2334 return pid
2331 finally:
2335 finally:
2332 if prevhandler is not None:
2336 if prevhandler is not None:
2333 signal.signal(signal.SIGCHLD, prevhandler)
2337 signal.signal(signal.SIGCHLD, prevhandler)
2334
2338
2335 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2339 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2336 """Return the result of interpolating items in the mapping into string s.
2340 """Return the result of interpolating items in the mapping into string s.
2337
2341
2338 prefix is a single character string, or a two character string with
2342 prefix is a single character string, or a two character string with
2339 a backslash as the first character if the prefix needs to be escaped in
2343 a backslash as the first character if the prefix needs to be escaped in
2340 a regular expression.
2344 a regular expression.
2341
2345
2342 fn is an optional function that will be applied to the replacement text
2346 fn is an optional function that will be applied to the replacement text
2343 just before replacement.
2347 just before replacement.
2344
2348
2345 escape_prefix is an optional flag that allows using doubled prefix for
2349 escape_prefix is an optional flag that allows using doubled prefix for
2346 its escaping.
2350 its escaping.
2347 """
2351 """
2348 fn = fn or (lambda s: s)
2352 fn = fn or (lambda s: s)
2349 patterns = '|'.join(mapping.keys())
2353 patterns = '|'.join(mapping.keys())
2350 if escape_prefix:
2354 if escape_prefix:
2351 patterns += '|' + prefix
2355 patterns += '|' + prefix
2352 if len(prefix) > 1:
2356 if len(prefix) > 1:
2353 prefix_char = prefix[1:]
2357 prefix_char = prefix[1:]
2354 else:
2358 else:
2355 prefix_char = prefix
2359 prefix_char = prefix
2356 mapping[prefix_char] = prefix_char
2360 mapping[prefix_char] = prefix_char
2357 r = remod.compile(r'%s(%s)' % (prefix, patterns))
2361 r = remod.compile(r'%s(%s)' % (prefix, patterns))
2358 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2362 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2359
2363
2360 def getport(port):
2364 def getport(port):
2361 """Return the port for a given network service.
2365 """Return the port for a given network service.
2362
2366
2363 If port is an integer, it's returned as is. If it's a string, it's
2367 If port is an integer, it's returned as is. If it's a string, it's
2364 looked up using socket.getservbyname(). If there's no matching
2368 looked up using socket.getservbyname(). If there's no matching
2365 service, error.Abort is raised.
2369 service, error.Abort is raised.
2366 """
2370 """
2367 try:
2371 try:
2368 return int(port)
2372 return int(port)
2369 except ValueError:
2373 except ValueError:
2370 pass
2374 pass
2371
2375
2372 try:
2376 try:
2373 return socket.getservbyname(port)
2377 return socket.getservbyname(port)
2374 except socket.error:
2378 except socket.error:
2375 raise Abort(_("no port number associated with service '%s'") % port)
2379 raise Abort(_("no port number associated with service '%s'") % port)
2376
2380
2377 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
2381 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
2378 '0': False, 'no': False, 'false': False, 'off': False,
2382 '0': False, 'no': False, 'false': False, 'off': False,
2379 'never': False}
2383 'never': False}
2380
2384
2381 def parsebool(s):
2385 def parsebool(s):
2382 """Parse s into a boolean.
2386 """Parse s into a boolean.
2383
2387
2384 If s is not a valid boolean, returns None.
2388 If s is not a valid boolean, returns None.
2385 """
2389 """
2386 return _booleans.get(s.lower(), None)
2390 return _booleans.get(s.lower(), None)
2387
2391
2388 _hextochr = dict((a + b, chr(int(a + b, 16)))
2392 _hextochr = dict((a + b, chr(int(a + b, 16)))
2389 for a in string.hexdigits for b in string.hexdigits)
2393 for a in string.hexdigits for b in string.hexdigits)
2390
2394
2391 class url(object):
2395 class url(object):
2392 r"""Reliable URL parser.
2396 r"""Reliable URL parser.
2393
2397
2394 This parses URLs and provides attributes for the following
2398 This parses URLs and provides attributes for the following
2395 components:
2399 components:
2396
2400
2397 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2401 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2398
2402
2399 Missing components are set to None. The only exception is
2403 Missing components are set to None. The only exception is
2400 fragment, which is set to '' if present but empty.
2404 fragment, which is set to '' if present but empty.
2401
2405
2402 If parsefragment is False, fragment is included in query. If
2406 If parsefragment is False, fragment is included in query. If
2403 parsequery is False, query is included in path. If both are
2407 parsequery is False, query is included in path. If both are
2404 False, both fragment and query are included in path.
2408 False, both fragment and query are included in path.
2405
2409
2406 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2410 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2407
2411
2408 Note that for backward compatibility reasons, bundle URLs do not
2412 Note that for backward compatibility reasons, bundle URLs do not
2409 take host names. That means 'bundle://../' has a path of '../'.
2413 take host names. That means 'bundle://../' has a path of '../'.
2410
2414
2411 Examples:
2415 Examples:
2412
2416
2413 >>> url('http://www.ietf.org/rfc/rfc2396.txt')
2417 >>> url('http://www.ietf.org/rfc/rfc2396.txt')
2414 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2418 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2415 >>> url('ssh://[::1]:2200//home/joe/repo')
2419 >>> url('ssh://[::1]:2200//home/joe/repo')
2416 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2420 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2417 >>> url('file:///home/joe/repo')
2421 >>> url('file:///home/joe/repo')
2418 <url scheme: 'file', path: '/home/joe/repo'>
2422 <url scheme: 'file', path: '/home/joe/repo'>
2419 >>> url('file:///c:/temp/foo/')
2423 >>> url('file:///c:/temp/foo/')
2420 <url scheme: 'file', path: 'c:/temp/foo/'>
2424 <url scheme: 'file', path: 'c:/temp/foo/'>
2421 >>> url('bundle:foo')
2425 >>> url('bundle:foo')
2422 <url scheme: 'bundle', path: 'foo'>
2426 <url scheme: 'bundle', path: 'foo'>
2423 >>> url('bundle://../foo')
2427 >>> url('bundle://../foo')
2424 <url scheme: 'bundle', path: '../foo'>
2428 <url scheme: 'bundle', path: '../foo'>
2425 >>> url(r'c:\foo\bar')
2429 >>> url(r'c:\foo\bar')
2426 <url path: 'c:\\foo\\bar'>
2430 <url path: 'c:\\foo\\bar'>
2427 >>> url(r'\\blah\blah\blah')
2431 >>> url(r'\\blah\blah\blah')
2428 <url path: '\\\\blah\\blah\\blah'>
2432 <url path: '\\\\blah\\blah\\blah'>
2429 >>> url(r'\\blah\blah\blah#baz')
2433 >>> url(r'\\blah\blah\blah#baz')
2430 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2434 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2431 >>> url(r'file:///C:\users\me')
2435 >>> url(r'file:///C:\users\me')
2432 <url scheme: 'file', path: 'C:\\users\\me'>
2436 <url scheme: 'file', path: 'C:\\users\\me'>
2433
2437
2434 Authentication credentials:
2438 Authentication credentials:
2435
2439
2436 >>> url('ssh://joe:xyz@x/repo')
2440 >>> url('ssh://joe:xyz@x/repo')
2437 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2441 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2438 >>> url('ssh://joe@x/repo')
2442 >>> url('ssh://joe@x/repo')
2439 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2443 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2440
2444
2441 Query strings and fragments:
2445 Query strings and fragments:
2442
2446
2443 >>> url('http://host/a?b#c')
2447 >>> url('http://host/a?b#c')
2444 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2448 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2445 >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
2449 >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
2446 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2450 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2447
2451
2448 Empty path:
2452 Empty path:
2449
2453
2450 >>> url('')
2454 >>> url('')
2451 <url path: ''>
2455 <url path: ''>
2452 >>> url('#a')
2456 >>> url('#a')
2453 <url path: '', fragment: 'a'>
2457 <url path: '', fragment: 'a'>
2454 >>> url('http://host/')
2458 >>> url('http://host/')
2455 <url scheme: 'http', host: 'host', path: ''>
2459 <url scheme: 'http', host: 'host', path: ''>
2456 >>> url('http://host/#a')
2460 >>> url('http://host/#a')
2457 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
2461 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
2458
2462
2459 Only scheme:
2463 Only scheme:
2460
2464
2461 >>> url('http:')
2465 >>> url('http:')
2462 <url scheme: 'http'>
2466 <url scheme: 'http'>
2463 """
2467 """
2464
2468
2465 _safechars = "!~*'()+"
2469 _safechars = "!~*'()+"
2466 _safepchars = "/!~*'()+:\\"
2470 _safepchars = "/!~*'()+:\\"
2467 _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match
2471 _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match
2468
2472
2469 def __init__(self, path, parsequery=True, parsefragment=True):
2473 def __init__(self, path, parsequery=True, parsefragment=True):
2470 # We slowly chomp away at path until we have only the path left
2474 # We slowly chomp away at path until we have only the path left
2471 self.scheme = self.user = self.passwd = self.host = None
2475 self.scheme = self.user = self.passwd = self.host = None
2472 self.port = self.path = self.query = self.fragment = None
2476 self.port = self.path = self.query = self.fragment = None
2473 self._localpath = True
2477 self._localpath = True
2474 self._hostport = ''
2478 self._hostport = ''
2475 self._origpath = path
2479 self._origpath = path
2476
2480
2477 if parsefragment and '#' in path:
2481 if parsefragment and '#' in path:
2478 path, self.fragment = path.split('#', 1)
2482 path, self.fragment = path.split('#', 1)
2479
2483
2480 # special case for Windows drive letters and UNC paths
2484 # special case for Windows drive letters and UNC paths
2481 if hasdriveletter(path) or path.startswith('\\\\'):
2485 if hasdriveletter(path) or path.startswith('\\\\'):
2482 self.path = path
2486 self.path = path
2483 return
2487 return
2484
2488
2485 # For compatibility reasons, we can't handle bundle paths as
2489 # For compatibility reasons, we can't handle bundle paths as
2486 # normal URLS
2490 # normal URLS
2487 if path.startswith('bundle:'):
2491 if path.startswith('bundle:'):
2488 self.scheme = 'bundle'
2492 self.scheme = 'bundle'
2489 path = path[7:]
2493 path = path[7:]
2490 if path.startswith('//'):
2494 if path.startswith('//'):
2491 path = path[2:]
2495 path = path[2:]
2492 self.path = path
2496 self.path = path
2493 return
2497 return
2494
2498
2495 if self._matchscheme(path):
2499 if self._matchscheme(path):
2496 parts = path.split(':', 1)
2500 parts = path.split(':', 1)
2497 if parts[0]:
2501 if parts[0]:
2498 self.scheme, path = parts
2502 self.scheme, path = parts
2499 self._localpath = False
2503 self._localpath = False
2500
2504
2501 if not path:
2505 if not path:
2502 path = None
2506 path = None
2503 if self._localpath:
2507 if self._localpath:
2504 self.path = ''
2508 self.path = ''
2505 return
2509 return
2506 else:
2510 else:
2507 if self._localpath:
2511 if self._localpath:
2508 self.path = path
2512 self.path = path
2509 return
2513 return
2510
2514
2511 if parsequery and '?' in path:
2515 if parsequery and '?' in path:
2512 path, self.query = path.split('?', 1)
2516 path, self.query = path.split('?', 1)
2513 if not path:
2517 if not path:
2514 path = None
2518 path = None
2515 if not self.query:
2519 if not self.query:
2516 self.query = None
2520 self.query = None
2517
2521
2518 # // is required to specify a host/authority
2522 # // is required to specify a host/authority
2519 if path and path.startswith('//'):
2523 if path and path.startswith('//'):
2520 parts = path[2:].split('/', 1)
2524 parts = path[2:].split('/', 1)
2521 if len(parts) > 1:
2525 if len(parts) > 1:
2522 self.host, path = parts
2526 self.host, path = parts
2523 else:
2527 else:
2524 self.host = parts[0]
2528 self.host = parts[0]
2525 path = None
2529 path = None
2526 if not self.host:
2530 if not self.host:
2527 self.host = None
2531 self.host = None
2528 # path of file:///d is /d
2532 # path of file:///d is /d
2529 # path of file:///d:/ is d:/, not /d:/
2533 # path of file:///d:/ is d:/, not /d:/
2530 if path and not hasdriveletter(path):
2534 if path and not hasdriveletter(path):
2531 path = '/' + path
2535 path = '/' + path
2532
2536
2533 if self.host and '@' in self.host:
2537 if self.host and '@' in self.host:
2534 self.user, self.host = self.host.rsplit('@', 1)
2538 self.user, self.host = self.host.rsplit('@', 1)
2535 if ':' in self.user:
2539 if ':' in self.user:
2536 self.user, self.passwd = self.user.split(':', 1)
2540 self.user, self.passwd = self.user.split(':', 1)
2537 if not self.host:
2541 if not self.host:
2538 self.host = None
2542 self.host = None
2539
2543
2540 # Don't split on colons in IPv6 addresses without ports
2544 # Don't split on colons in IPv6 addresses without ports
2541 if (self.host and ':' in self.host and
2545 if (self.host and ':' in self.host and
2542 not (self.host.startswith('[') and self.host.endswith(']'))):
2546 not (self.host.startswith('[') and self.host.endswith(']'))):
2543 self._hostport = self.host
2547 self._hostport = self.host
2544 self.host, self.port = self.host.rsplit(':', 1)
2548 self.host, self.port = self.host.rsplit(':', 1)
2545 if not self.host:
2549 if not self.host:
2546 self.host = None
2550 self.host = None
2547
2551
2548 if (self.host and self.scheme == 'file' and
2552 if (self.host and self.scheme == 'file' and
2549 self.host not in ('localhost', '127.0.0.1', '[::1]')):
2553 self.host not in ('localhost', '127.0.0.1', '[::1]')):
2550 raise Abort(_('file:// URLs can only refer to localhost'))
2554 raise Abort(_('file:// URLs can only refer to localhost'))
2551
2555
2552 self.path = path
2556 self.path = path
2553
2557
2554 # leave the query string escaped
2558 # leave the query string escaped
2555 for a in ('user', 'passwd', 'host', 'port',
2559 for a in ('user', 'passwd', 'host', 'port',
2556 'path', 'fragment'):
2560 'path', 'fragment'):
2557 v = getattr(self, a)
2561 v = getattr(self, a)
2558 if v is not None:
2562 if v is not None:
2559 setattr(self, a, pycompat.urlunquote(v))
2563 setattr(self, a, pycompat.urlunquote(v))
2560
2564
2561 def __repr__(self):
2565 def __repr__(self):
2562 attrs = []
2566 attrs = []
2563 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
2567 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
2564 'query', 'fragment'):
2568 'query', 'fragment'):
2565 v = getattr(self, a)
2569 v = getattr(self, a)
2566 if v is not None:
2570 if v is not None:
2567 attrs.append('%s: %r' % (a, v))
2571 attrs.append('%s: %r' % (a, v))
2568 return '<url %s>' % ', '.join(attrs)
2572 return '<url %s>' % ', '.join(attrs)
2569
2573
2570 def __str__(self):
2574 def __str__(self):
2571 r"""Join the URL's components back into a URL string.
2575 r"""Join the URL's components back into a URL string.
2572
2576
2573 Examples:
2577 Examples:
2574
2578
2575 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
2579 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
2576 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
2580 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
2577 >>> str(url('http://user:pw@host:80/?foo=bar&baz=42'))
2581 >>> str(url('http://user:pw@host:80/?foo=bar&baz=42'))
2578 'http://user:pw@host:80/?foo=bar&baz=42'
2582 'http://user:pw@host:80/?foo=bar&baz=42'
2579 >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz'))
2583 >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz'))
2580 'http://user:pw@host:80/?foo=bar%3dbaz'
2584 'http://user:pw@host:80/?foo=bar%3dbaz'
2581 >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
2585 >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
2582 'ssh://user:pw@[::1]:2200//home/joe#'
2586 'ssh://user:pw@[::1]:2200//home/joe#'
2583 >>> str(url('http://localhost:80//'))
2587 >>> str(url('http://localhost:80//'))
2584 'http://localhost:80//'
2588 'http://localhost:80//'
2585 >>> str(url('http://localhost:80/'))
2589 >>> str(url('http://localhost:80/'))
2586 'http://localhost:80/'
2590 'http://localhost:80/'
2587 >>> str(url('http://localhost:80'))
2591 >>> str(url('http://localhost:80'))
2588 'http://localhost:80/'
2592 'http://localhost:80/'
2589 >>> str(url('bundle:foo'))
2593 >>> str(url('bundle:foo'))
2590 'bundle:foo'
2594 'bundle:foo'
2591 >>> str(url('bundle://../foo'))
2595 >>> str(url('bundle://../foo'))
2592 'bundle:../foo'
2596 'bundle:../foo'
2593 >>> str(url('path'))
2597 >>> str(url('path'))
2594 'path'
2598 'path'
2595 >>> str(url('file:///tmp/foo/bar'))
2599 >>> str(url('file:///tmp/foo/bar'))
2596 'file:///tmp/foo/bar'
2600 'file:///tmp/foo/bar'
2597 >>> str(url('file:///c:/tmp/foo/bar'))
2601 >>> str(url('file:///c:/tmp/foo/bar'))
2598 'file:///c:/tmp/foo/bar'
2602 'file:///c:/tmp/foo/bar'
2599 >>> print url(r'bundle:foo\bar')
2603 >>> print url(r'bundle:foo\bar')
2600 bundle:foo\bar
2604 bundle:foo\bar
2601 >>> print url(r'file:///D:\data\hg')
2605 >>> print url(r'file:///D:\data\hg')
2602 file:///D:\data\hg
2606 file:///D:\data\hg
2603 """
2607 """
2604 if self._localpath:
2608 if self._localpath:
2605 s = self.path
2609 s = self.path
2606 if self.scheme == 'bundle':
2610 if self.scheme == 'bundle':
2607 s = 'bundle:' + s
2611 s = 'bundle:' + s
2608 if self.fragment:
2612 if self.fragment:
2609 s += '#' + self.fragment
2613 s += '#' + self.fragment
2610 return s
2614 return s
2611
2615
2612 s = self.scheme + ':'
2616 s = self.scheme + ':'
2613 if self.user or self.passwd or self.host:
2617 if self.user or self.passwd or self.host:
2614 s += '//'
2618 s += '//'
2615 elif self.scheme and (not self.path or self.path.startswith('/')
2619 elif self.scheme and (not self.path or self.path.startswith('/')
2616 or hasdriveletter(self.path)):
2620 or hasdriveletter(self.path)):
2617 s += '//'
2621 s += '//'
2618 if hasdriveletter(self.path):
2622 if hasdriveletter(self.path):
2619 s += '/'
2623 s += '/'
2620 if self.user:
2624 if self.user:
2621 s += urlreq.quote(self.user, safe=self._safechars)
2625 s += urlreq.quote(self.user, safe=self._safechars)
2622 if self.passwd:
2626 if self.passwd:
2623 s += ':' + urlreq.quote(self.passwd, safe=self._safechars)
2627 s += ':' + urlreq.quote(self.passwd, safe=self._safechars)
2624 if self.user or self.passwd:
2628 if self.user or self.passwd:
2625 s += '@'
2629 s += '@'
2626 if self.host:
2630 if self.host:
2627 if not (self.host.startswith('[') and self.host.endswith(']')):
2631 if not (self.host.startswith('[') and self.host.endswith(']')):
2628 s += urlreq.quote(self.host)
2632 s += urlreq.quote(self.host)
2629 else:
2633 else:
2630 s += self.host
2634 s += self.host
2631 if self.port:
2635 if self.port:
2632 s += ':' + urlreq.quote(self.port)
2636 s += ':' + urlreq.quote(self.port)
2633 if self.host:
2637 if self.host:
2634 s += '/'
2638 s += '/'
2635 if self.path:
2639 if self.path:
2636 # TODO: similar to the query string, we should not unescape the
2640 # TODO: similar to the query string, we should not unescape the
2637 # path when we store it, the path might contain '%2f' = '/',
2641 # path when we store it, the path might contain '%2f' = '/',
2638 # which we should *not* escape.
2642 # which we should *not* escape.
2639 s += urlreq.quote(self.path, safe=self._safepchars)
2643 s += urlreq.quote(self.path, safe=self._safepchars)
2640 if self.query:
2644 if self.query:
2641 # we store the query in escaped form.
2645 # we store the query in escaped form.
2642 s += '?' + self.query
2646 s += '?' + self.query
2643 if self.fragment is not None:
2647 if self.fragment is not None:
2644 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2648 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2645 return s
2649 return s
2646
2650
2647 def authinfo(self):
2651 def authinfo(self):
2648 user, passwd = self.user, self.passwd
2652 user, passwd = self.user, self.passwd
2649 try:
2653 try:
2650 self.user, self.passwd = None, None
2654 self.user, self.passwd = None, None
2651 s = str(self)
2655 s = str(self)
2652 finally:
2656 finally:
2653 self.user, self.passwd = user, passwd
2657 self.user, self.passwd = user, passwd
2654 if not self.user:
2658 if not self.user:
2655 return (s, None)
2659 return (s, None)
2656 # authinfo[1] is passed to urllib2 password manager, and its
2660 # authinfo[1] is passed to urllib2 password manager, and its
2657 # URIs must not contain credentials. The host is passed in the
2661 # URIs must not contain credentials. The host is passed in the
2658 # URIs list because Python < 2.4.3 uses only that to search for
2662 # URIs list because Python < 2.4.3 uses only that to search for
2659 # a password.
2663 # a password.
2660 return (s, (None, (s, self.host),
2664 return (s, (None, (s, self.host),
2661 self.user, self.passwd or ''))
2665 self.user, self.passwd or ''))
2662
2666
2663 def isabs(self):
2667 def isabs(self):
2664 if self.scheme and self.scheme != 'file':
2668 if self.scheme and self.scheme != 'file':
2665 return True # remote URL
2669 return True # remote URL
2666 if hasdriveletter(self.path):
2670 if hasdriveletter(self.path):
2667 return True # absolute for our purposes - can't be joined()
2671 return True # absolute for our purposes - can't be joined()
2668 if self.path.startswith(r'\\'):
2672 if self.path.startswith(r'\\'):
2669 return True # Windows UNC path
2673 return True # Windows UNC path
2670 if self.path.startswith('/'):
2674 if self.path.startswith('/'):
2671 return True # POSIX-style
2675 return True # POSIX-style
2672 return False
2676 return False
2673
2677
2674 def localpath(self):
2678 def localpath(self):
2675 if self.scheme == 'file' or self.scheme == 'bundle':
2679 if self.scheme == 'file' or self.scheme == 'bundle':
2676 path = self.path or '/'
2680 path = self.path or '/'
2677 # For Windows, we need to promote hosts containing drive
2681 # For Windows, we need to promote hosts containing drive
2678 # letters to paths with drive letters.
2682 # letters to paths with drive letters.
2679 if hasdriveletter(self._hostport):
2683 if hasdriveletter(self._hostport):
2680 path = self._hostport + '/' + self.path
2684 path = self._hostport + '/' + self.path
2681 elif (self.host is not None and self.path
2685 elif (self.host is not None and self.path
2682 and not hasdriveletter(path)):
2686 and not hasdriveletter(path)):
2683 path = '/' + path
2687 path = '/' + path
2684 return path
2688 return path
2685 return self._origpath
2689 return self._origpath
2686
2690
2687 def islocal(self):
2691 def islocal(self):
2688 '''whether localpath will return something that posixfile can open'''
2692 '''whether localpath will return something that posixfile can open'''
2689 return (not self.scheme or self.scheme == 'file'
2693 return (not self.scheme or self.scheme == 'file'
2690 or self.scheme == 'bundle')
2694 or self.scheme == 'bundle')
2691
2695
2692 def hasscheme(path):
2696 def hasscheme(path):
2693 return bool(url(path).scheme)
2697 return bool(url(path).scheme)
2694
2698
2695 def hasdriveletter(path):
2699 def hasdriveletter(path):
2696 return path and path[1:2] == ':' and path[0:1].isalpha()
2700 return path and path[1:2] == ':' and path[0:1].isalpha()
2697
2701
2698 def urllocalpath(path):
2702 def urllocalpath(path):
2699 return url(path, parsequery=False, parsefragment=False).localpath()
2703 return url(path, parsequery=False, parsefragment=False).localpath()
2700
2704
2701 def hidepassword(u):
2705 def hidepassword(u):
2702 '''hide user credential in a url string'''
2706 '''hide user credential in a url string'''
2703 u = url(u)
2707 u = url(u)
2704 if u.passwd:
2708 if u.passwd:
2705 u.passwd = '***'
2709 u.passwd = '***'
2706 return str(u)
2710 return str(u)
2707
2711
2708 def removeauth(u):
2712 def removeauth(u):
2709 '''remove all authentication information from a url string'''
2713 '''remove all authentication information from a url string'''
2710 u = url(u)
2714 u = url(u)
2711 u.user = u.passwd = None
2715 u.user = u.passwd = None
2712 return str(u)
2716 return str(u)
2713
2717
2714 def isatty(fp):
2718 def isatty(fp):
2715 try:
2719 try:
2716 return fp.isatty()
2720 return fp.isatty()
2717 except AttributeError:
2721 except AttributeError:
2718 return False
2722 return False
2719
2723
2720 timecount = unitcountfn(
2724 timecount = unitcountfn(
2721 (1, 1e3, _('%.0f s')),
2725 (1, 1e3, _('%.0f s')),
2722 (100, 1, _('%.1f s')),
2726 (100, 1, _('%.1f s')),
2723 (10, 1, _('%.2f s')),
2727 (10, 1, _('%.2f s')),
2724 (1, 1, _('%.3f s')),
2728 (1, 1, _('%.3f s')),
2725 (100, 0.001, _('%.1f ms')),
2729 (100, 0.001, _('%.1f ms')),
2726 (10, 0.001, _('%.2f ms')),
2730 (10, 0.001, _('%.2f ms')),
2727 (1, 0.001, _('%.3f ms')),
2731 (1, 0.001, _('%.3f ms')),
2728 (100, 0.000001, _('%.1f us')),
2732 (100, 0.000001, _('%.1f us')),
2729 (10, 0.000001, _('%.2f us')),
2733 (10, 0.000001, _('%.2f us')),
2730 (1, 0.000001, _('%.3f us')),
2734 (1, 0.000001, _('%.3f us')),
2731 (100, 0.000000001, _('%.1f ns')),
2735 (100, 0.000000001, _('%.1f ns')),
2732 (10, 0.000000001, _('%.2f ns')),
2736 (10, 0.000000001, _('%.2f ns')),
2733 (1, 0.000000001, _('%.3f ns')),
2737 (1, 0.000000001, _('%.3f ns')),
2734 )
2738 )
2735
2739
2736 _timenesting = [0]
2740 _timenesting = [0]
2737
2741
2738 def timed(func):
2742 def timed(func):
2739 '''Report the execution time of a function call to stderr.
2743 '''Report the execution time of a function call to stderr.
2740
2744
2741 During development, use as a decorator when you need to measure
2745 During development, use as a decorator when you need to measure
2742 the cost of a function, e.g. as follows:
2746 the cost of a function, e.g. as follows:
2743
2747
2744 @util.timed
2748 @util.timed
2745 def foo(a, b, c):
2749 def foo(a, b, c):
2746 pass
2750 pass
2747 '''
2751 '''
2748
2752
2749 def wrapper(*args, **kwargs):
2753 def wrapper(*args, **kwargs):
2750 start = time.time()
2754 start = time.time()
2751 indent = 2
2755 indent = 2
2752 _timenesting[0] += indent
2756 _timenesting[0] += indent
2753 try:
2757 try:
2754 return func(*args, **kwargs)
2758 return func(*args, **kwargs)
2755 finally:
2759 finally:
2756 elapsed = time.time() - start
2760 elapsed = time.time() - start
2757 _timenesting[0] -= indent
2761 _timenesting[0] -= indent
2758 sys.stderr.write('%s%s: %s\n' %
2762 sys.stderr.write('%s%s: %s\n' %
2759 (' ' * _timenesting[0], func.__name__,
2763 (' ' * _timenesting[0], func.__name__,
2760 timecount(elapsed)))
2764 timecount(elapsed)))
2761 return wrapper
2765 return wrapper
2762
2766
2763 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
2767 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
2764 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
2768 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
2765
2769
2766 def sizetoint(s):
2770 def sizetoint(s):
2767 '''Convert a space specifier to a byte count.
2771 '''Convert a space specifier to a byte count.
2768
2772
2769 >>> sizetoint('30')
2773 >>> sizetoint('30')
2770 30
2774 30
2771 >>> sizetoint('2.2kb')
2775 >>> sizetoint('2.2kb')
2772 2252
2776 2252
2773 >>> sizetoint('6M')
2777 >>> sizetoint('6M')
2774 6291456
2778 6291456
2775 '''
2779 '''
2776 t = s.strip().lower()
2780 t = s.strip().lower()
2777 try:
2781 try:
2778 for k, u in _sizeunits:
2782 for k, u in _sizeunits:
2779 if t.endswith(k):
2783 if t.endswith(k):
2780 return int(float(t[:-len(k)]) * u)
2784 return int(float(t[:-len(k)]) * u)
2781 return int(t)
2785 return int(t)
2782 except ValueError:
2786 except ValueError:
2783 raise error.ParseError(_("couldn't parse size: %s") % s)
2787 raise error.ParseError(_("couldn't parse size: %s") % s)
2784
2788
2785 class hooks(object):
2789 class hooks(object):
2786 '''A collection of hook functions that can be used to extend a
2790 '''A collection of hook functions that can be used to extend a
2787 function's behavior. Hooks are called in lexicographic order,
2791 function's behavior. Hooks are called in lexicographic order,
2788 based on the names of their sources.'''
2792 based on the names of their sources.'''
2789
2793
2790 def __init__(self):
2794 def __init__(self):
2791 self._hooks = []
2795 self._hooks = []
2792
2796
2793 def add(self, source, hook):
2797 def add(self, source, hook):
2794 self._hooks.append((source, hook))
2798 self._hooks.append((source, hook))
2795
2799
2796 def __call__(self, *args):
2800 def __call__(self, *args):
2797 self._hooks.sort(key=lambda x: x[0])
2801 self._hooks.sort(key=lambda x: x[0])
2798 results = []
2802 results = []
2799 for source, hook in self._hooks:
2803 for source, hook in self._hooks:
2800 results.append(hook(*args))
2804 results.append(hook(*args))
2801 return results
2805 return results
2802
2806
2803 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2807 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2804 '''Yields lines for a nicely formatted stacktrace.
2808 '''Yields lines for a nicely formatted stacktrace.
2805 Skips the 'skip' last entries.
2809 Skips the 'skip' last entries.
2806 Each file+linenumber is formatted according to fileline.
2810 Each file+linenumber is formatted according to fileline.
2807 Each line is formatted according to line.
2811 Each line is formatted according to line.
2808 If line is None, it yields:
2812 If line is None, it yields:
2809 length of longest filepath+line number,
2813 length of longest filepath+line number,
2810 filepath+linenumber,
2814 filepath+linenumber,
2811 function
2815 function
2812
2816
2813 Not be used in production code but very convenient while developing.
2817 Not be used in production code but very convenient while developing.
2814 '''
2818 '''
2815 entries = [(fileline % (fn, ln), func)
2819 entries = [(fileline % (fn, ln), func)
2816 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2820 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2817 if entries:
2821 if entries:
2818 fnmax = max(len(entry[0]) for entry in entries)
2822 fnmax = max(len(entry[0]) for entry in entries)
2819 for fnln, func in entries:
2823 for fnln, func in entries:
2820 if line is None:
2824 if line is None:
2821 yield (fnmax, fnln, func)
2825 yield (fnmax, fnln, func)
2822 else:
2826 else:
2823 yield line % (fnmax, fnln, func)
2827 yield line % (fnmax, fnln, func)
2824
2828
2825 def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout):
2829 def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout):
2826 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2830 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2827 Skips the 'skip' last entries. By default it will flush stdout first.
2831 Skips the 'skip' last entries. By default it will flush stdout first.
2828 It can be used everywhere and intentionally does not require an ui object.
2832 It can be used everywhere and intentionally does not require an ui object.
2829 Not be used in production code but very convenient while developing.
2833 Not be used in production code but very convenient while developing.
2830 '''
2834 '''
2831 if otherf:
2835 if otherf:
2832 otherf.flush()
2836 otherf.flush()
2833 f.write('%s at:\n' % msg)
2837 f.write('%s at:\n' % msg)
2834 for line in getstackframes(skip + 1):
2838 for line in getstackframes(skip + 1):
2835 f.write(line)
2839 f.write(line)
2836 f.flush()
2840 f.flush()
2837
2841
2838 class dirs(object):
2842 class dirs(object):
2839 '''a multiset of directory names from a dirstate or manifest'''
2843 '''a multiset of directory names from a dirstate or manifest'''
2840
2844
2841 def __init__(self, map, skip=None):
2845 def __init__(self, map, skip=None):
2842 self._dirs = {}
2846 self._dirs = {}
2843 addpath = self.addpath
2847 addpath = self.addpath
2844 if safehasattr(map, 'iteritems') and skip is not None:
2848 if safehasattr(map, 'iteritems') and skip is not None:
2845 for f, s in map.iteritems():
2849 for f, s in map.iteritems():
2846 if s[0] != skip:
2850 if s[0] != skip:
2847 addpath(f)
2851 addpath(f)
2848 else:
2852 else:
2849 for f in map:
2853 for f in map:
2850 addpath(f)
2854 addpath(f)
2851
2855
2852 def addpath(self, path):
2856 def addpath(self, path):
2853 dirs = self._dirs
2857 dirs = self._dirs
2854 for base in finddirs(path):
2858 for base in finddirs(path):
2855 if base in dirs:
2859 if base in dirs:
2856 dirs[base] += 1
2860 dirs[base] += 1
2857 return
2861 return
2858 dirs[base] = 1
2862 dirs[base] = 1
2859
2863
2860 def delpath(self, path):
2864 def delpath(self, path):
2861 dirs = self._dirs
2865 dirs = self._dirs
2862 for base in finddirs(path):
2866 for base in finddirs(path):
2863 if dirs[base] > 1:
2867 if dirs[base] > 1:
2864 dirs[base] -= 1
2868 dirs[base] -= 1
2865 return
2869 return
2866 del dirs[base]
2870 del dirs[base]
2867
2871
2868 def __iter__(self):
2872 def __iter__(self):
2869 return self._dirs.iterkeys()
2873 return self._dirs.iterkeys()
2870
2874
2871 def __contains__(self, d):
2875 def __contains__(self, d):
2872 return d in self._dirs
2876 return d in self._dirs
2873
2877
2874 if safehasattr(parsers, 'dirs'):
2878 if safehasattr(parsers, 'dirs'):
2875 dirs = parsers.dirs
2879 dirs = parsers.dirs
2876
2880
2877 def finddirs(path):
2881 def finddirs(path):
2878 pos = path.rfind('/')
2882 pos = path.rfind('/')
2879 while pos != -1:
2883 while pos != -1:
2880 yield path[:pos]
2884 yield path[:pos]
2881 pos = path.rfind('/', 0, pos)
2885 pos = path.rfind('/', 0, pos)
2882
2886
2883 class ctxmanager(object):
2887 class ctxmanager(object):
2884 '''A context manager for use in 'with' blocks to allow multiple
2888 '''A context manager for use in 'with' blocks to allow multiple
2885 contexts to be entered at once. This is both safer and more
2889 contexts to be entered at once. This is both safer and more
2886 flexible than contextlib.nested.
2890 flexible than contextlib.nested.
2887
2891
2888 Once Mercurial supports Python 2.7+, this will become mostly
2892 Once Mercurial supports Python 2.7+, this will become mostly
2889 unnecessary.
2893 unnecessary.
2890 '''
2894 '''
2891
2895
2892 def __init__(self, *args):
2896 def __init__(self, *args):
2893 '''Accepts a list of no-argument functions that return context
2897 '''Accepts a list of no-argument functions that return context
2894 managers. These will be invoked at __call__ time.'''
2898 managers. These will be invoked at __call__ time.'''
2895 self._pending = args
2899 self._pending = args
2896 self._atexit = []
2900 self._atexit = []
2897
2901
2898 def __enter__(self):
2902 def __enter__(self):
2899 return self
2903 return self
2900
2904
2901 def enter(self):
2905 def enter(self):
2902 '''Create and enter context managers in the order in which they were
2906 '''Create and enter context managers in the order in which they were
2903 passed to the constructor.'''
2907 passed to the constructor.'''
2904 values = []
2908 values = []
2905 for func in self._pending:
2909 for func in self._pending:
2906 obj = func()
2910 obj = func()
2907 values.append(obj.__enter__())
2911 values.append(obj.__enter__())
2908 self._atexit.append(obj.__exit__)
2912 self._atexit.append(obj.__exit__)
2909 del self._pending
2913 del self._pending
2910 return values
2914 return values
2911
2915
2912 def atexit(self, func, *args, **kwargs):
2916 def atexit(self, func, *args, **kwargs):
2913 '''Add a function to call when this context manager exits. The
2917 '''Add a function to call when this context manager exits. The
2914 ordering of multiple atexit calls is unspecified, save that
2918 ordering of multiple atexit calls is unspecified, save that
2915 they will happen before any __exit__ functions.'''
2919 they will happen before any __exit__ functions.'''
2916 def wrapper(exc_type, exc_val, exc_tb):
2920 def wrapper(exc_type, exc_val, exc_tb):
2917 func(*args, **kwargs)
2921 func(*args, **kwargs)
2918 self._atexit.append(wrapper)
2922 self._atexit.append(wrapper)
2919 return func
2923 return func
2920
2924
2921 def __exit__(self, exc_type, exc_val, exc_tb):
2925 def __exit__(self, exc_type, exc_val, exc_tb):
2922 '''Context managers are exited in the reverse order from which
2926 '''Context managers are exited in the reverse order from which
2923 they were created.'''
2927 they were created.'''
2924 received = exc_type is not None
2928 received = exc_type is not None
2925 suppressed = False
2929 suppressed = False
2926 pending = None
2930 pending = None
2927 self._atexit.reverse()
2931 self._atexit.reverse()
2928 for exitfunc in self._atexit:
2932 for exitfunc in self._atexit:
2929 try:
2933 try:
2930 if exitfunc(exc_type, exc_val, exc_tb):
2934 if exitfunc(exc_type, exc_val, exc_tb):
2931 suppressed = True
2935 suppressed = True
2932 exc_type = None
2936 exc_type = None
2933 exc_val = None
2937 exc_val = None
2934 exc_tb = None
2938 exc_tb = None
2935 except BaseException:
2939 except BaseException:
2936 pending = sys.exc_info()
2940 pending = sys.exc_info()
2937 exc_type, exc_val, exc_tb = pending = sys.exc_info()
2941 exc_type, exc_val, exc_tb = pending = sys.exc_info()
2938 del self._atexit
2942 del self._atexit
2939 if pending:
2943 if pending:
2940 raise exc_val
2944 raise exc_val
2941 return received and suppressed
2945 return received and suppressed
2942
2946
2943 # compression code
2947 # compression code
2944
2948
2945 class compressormanager(object):
2949 class compressormanager(object):
2946 """Holds registrations of various compression engines.
2950 """Holds registrations of various compression engines.
2947
2951
2948 This class essentially abstracts the differences between compression
2952 This class essentially abstracts the differences between compression
2949 engines to allow new compression formats to be added easily, possibly from
2953 engines to allow new compression formats to be added easily, possibly from
2950 extensions.
2954 extensions.
2951
2955
2952 Compressors are registered against the global instance by calling its
2956 Compressors are registered against the global instance by calling its
2953 ``register()`` method.
2957 ``register()`` method.
2954 """
2958 """
2955 def __init__(self):
2959 def __init__(self):
2956 self._engines = {}
2960 self._engines = {}
2957 # Bundle spec human name to engine name.
2961 # Bundle spec human name to engine name.
2958 self._bundlenames = {}
2962 self._bundlenames = {}
2959 # Internal bundle identifier to engine name.
2963 # Internal bundle identifier to engine name.
2960 self._bundletypes = {}
2964 self._bundletypes = {}
2961
2965
2962 def __getitem__(self, key):
2966 def __getitem__(self, key):
2963 return self._engines[key]
2967 return self._engines[key]
2964
2968
2965 def __contains__(self, key):
2969 def __contains__(self, key):
2966 return key in self._engines
2970 return key in self._engines
2967
2971
2968 def __iter__(self):
2972 def __iter__(self):
2969 return iter(self._engines.keys())
2973 return iter(self._engines.keys())
2970
2974
2971 def register(self, engine):
2975 def register(self, engine):
2972 """Register a compression engine with the manager.
2976 """Register a compression engine with the manager.
2973
2977
2974 The argument must be a ``compressionengine`` instance.
2978 The argument must be a ``compressionengine`` instance.
2975 """
2979 """
2976 if not isinstance(engine, compressionengine):
2980 if not isinstance(engine, compressionengine):
2977 raise ValueError(_('argument must be a compressionengine'))
2981 raise ValueError(_('argument must be a compressionengine'))
2978
2982
2979 name = engine.name()
2983 name = engine.name()
2980
2984
2981 if name in self._engines:
2985 if name in self._engines:
2982 raise error.Abort(_('compression engine %s already registered') %
2986 raise error.Abort(_('compression engine %s already registered') %
2983 name)
2987 name)
2984
2988
2985 bundleinfo = engine.bundletype()
2989 bundleinfo = engine.bundletype()
2986 if bundleinfo:
2990 if bundleinfo:
2987 bundlename, bundletype = bundleinfo
2991 bundlename, bundletype = bundleinfo
2988
2992
2989 if bundlename in self._bundlenames:
2993 if bundlename in self._bundlenames:
2990 raise error.Abort(_('bundle name %s already registered') %
2994 raise error.Abort(_('bundle name %s already registered') %
2991 bundlename)
2995 bundlename)
2992 if bundletype in self._bundletypes:
2996 if bundletype in self._bundletypes:
2993 raise error.Abort(_('bundle type %s already registered by %s') %
2997 raise error.Abort(_('bundle type %s already registered by %s') %
2994 (bundletype, self._bundletypes[bundletype]))
2998 (bundletype, self._bundletypes[bundletype]))
2995
2999
2996 # No external facing name declared.
3000 # No external facing name declared.
2997 if bundlename:
3001 if bundlename:
2998 self._bundlenames[bundlename] = name
3002 self._bundlenames[bundlename] = name
2999
3003
3000 self._bundletypes[bundletype] = name
3004 self._bundletypes[bundletype] = name
3001
3005
3002 self._engines[name] = engine
3006 self._engines[name] = engine
3003
3007
3004 @property
3008 @property
3005 def supportedbundlenames(self):
3009 def supportedbundlenames(self):
3006 return set(self._bundlenames.keys())
3010 return set(self._bundlenames.keys())
3007
3011
3008 @property
3012 @property
3009 def supportedbundletypes(self):
3013 def supportedbundletypes(self):
3010 return set(self._bundletypes.keys())
3014 return set(self._bundletypes.keys())
3011
3015
3012 def forbundlename(self, bundlename):
3016 def forbundlename(self, bundlename):
3013 """Obtain a compression engine registered to a bundle name.
3017 """Obtain a compression engine registered to a bundle name.
3014
3018
3015 Will raise KeyError if the bundle type isn't registered.
3019 Will raise KeyError if the bundle type isn't registered.
3016
3020
3017 Will abort if the engine is known but not available.
3021 Will abort if the engine is known but not available.
3018 """
3022 """
3019 engine = self._engines[self._bundlenames[bundlename]]
3023 engine = self._engines[self._bundlenames[bundlename]]
3020 if not engine.available():
3024 if not engine.available():
3021 raise error.Abort(_('compression engine %s could not be loaded') %
3025 raise error.Abort(_('compression engine %s could not be loaded') %
3022 engine.name())
3026 engine.name())
3023 return engine
3027 return engine
3024
3028
3025 def forbundletype(self, bundletype):
3029 def forbundletype(self, bundletype):
3026 """Obtain a compression engine registered to a bundle type.
3030 """Obtain a compression engine registered to a bundle type.
3027
3031
3028 Will raise KeyError if the bundle type isn't registered.
3032 Will raise KeyError if the bundle type isn't registered.
3029
3033
3030 Will abort if the engine is known but not available.
3034 Will abort if the engine is known but not available.
3031 """
3035 """
3032 engine = self._engines[self._bundletypes[bundletype]]
3036 engine = self._engines[self._bundletypes[bundletype]]
3033 if not engine.available():
3037 if not engine.available():
3034 raise error.Abort(_('compression engine %s could not be loaded') %
3038 raise error.Abort(_('compression engine %s could not be loaded') %
3035 engine.name())
3039 engine.name())
3036 return engine
3040 return engine
3037
3041
3038 compengines = compressormanager()
3042 compengines = compressormanager()
3039
3043
3040 class compressionengine(object):
3044 class compressionengine(object):
3041 """Base class for compression engines.
3045 """Base class for compression engines.
3042
3046
3043 Compression engines must implement the interface defined by this class.
3047 Compression engines must implement the interface defined by this class.
3044 """
3048 """
3045 def name(self):
3049 def name(self):
3046 """Returns the name of the compression engine.
3050 """Returns the name of the compression engine.
3047
3051
3048 This is the key the engine is registered under.
3052 This is the key the engine is registered under.
3049
3053
3050 This method must be implemented.
3054 This method must be implemented.
3051 """
3055 """
3052 raise NotImplementedError()
3056 raise NotImplementedError()
3053
3057
3054 def available(self):
3058 def available(self):
3055 """Whether the compression engine is available.
3059 """Whether the compression engine is available.
3056
3060
3057 The intent of this method is to allow optional compression engines
3061 The intent of this method is to allow optional compression engines
3058 that may not be available in all installations (such as engines relying
3062 that may not be available in all installations (such as engines relying
3059 on C extensions that may not be present).
3063 on C extensions that may not be present).
3060 """
3064 """
3061 return True
3065 return True
3062
3066
3063 def bundletype(self):
3067 def bundletype(self):
3064 """Describes bundle identifiers for this engine.
3068 """Describes bundle identifiers for this engine.
3065
3069
3066 If this compression engine isn't supported for bundles, returns None.
3070 If this compression engine isn't supported for bundles, returns None.
3067
3071
3068 If this engine can be used for bundles, returns a 2-tuple of strings of
3072 If this engine can be used for bundles, returns a 2-tuple of strings of
3069 the user-facing "bundle spec" compression name and an internal
3073 the user-facing "bundle spec" compression name and an internal
3070 identifier used to denote the compression format within bundles. To
3074 identifier used to denote the compression format within bundles. To
3071 exclude the name from external usage, set the first element to ``None``.
3075 exclude the name from external usage, set the first element to ``None``.
3072
3076
3073 If bundle compression is supported, the class must also implement
3077 If bundle compression is supported, the class must also implement
3074 ``compressstream`` and `decompressorreader``.
3078 ``compressstream`` and `decompressorreader``.
3075 """
3079 """
3076 return None
3080 return None
3077
3081
3078 def compressstream(self, it, opts=None):
3082 def compressstream(self, it, opts=None):
3079 """Compress an iterator of chunks.
3083 """Compress an iterator of chunks.
3080
3084
3081 The method receives an iterator (ideally a generator) of chunks of
3085 The method receives an iterator (ideally a generator) of chunks of
3082 bytes to be compressed. It returns an iterator (ideally a generator)
3086 bytes to be compressed. It returns an iterator (ideally a generator)
3083 of bytes of chunks representing the compressed output.
3087 of bytes of chunks representing the compressed output.
3084
3088
3085 Optionally accepts an argument defining how to perform compression.
3089 Optionally accepts an argument defining how to perform compression.
3086 Each engine treats this argument differently.
3090 Each engine treats this argument differently.
3087 """
3091 """
3088 raise NotImplementedError()
3092 raise NotImplementedError()
3089
3093
3090 def decompressorreader(self, fh):
3094 def decompressorreader(self, fh):
3091 """Perform decompression on a file object.
3095 """Perform decompression on a file object.
3092
3096
3093 Argument is an object with a ``read(size)`` method that returns
3097 Argument is an object with a ``read(size)`` method that returns
3094 compressed data. Return value is an object with a ``read(size)`` that
3098 compressed data. Return value is an object with a ``read(size)`` that
3095 returns uncompressed data.
3099 returns uncompressed data.
3096 """
3100 """
3097 raise NotImplementedError()
3101 raise NotImplementedError()
3098
3102
3099 class _zlibengine(compressionengine):
3103 class _zlibengine(compressionengine):
3100 def name(self):
3104 def name(self):
3101 return 'zlib'
3105 return 'zlib'
3102
3106
3103 def bundletype(self):
3107 def bundletype(self):
3104 return 'gzip', 'GZ'
3108 return 'gzip', 'GZ'
3105
3109
3106 def compressstream(self, it, opts=None):
3110 def compressstream(self, it, opts=None):
3107 opts = opts or {}
3111 opts = opts or {}
3108
3112
3109 z = zlib.compressobj(opts.get('level', -1))
3113 z = zlib.compressobj(opts.get('level', -1))
3110 for chunk in it:
3114 for chunk in it:
3111 data = z.compress(chunk)
3115 data = z.compress(chunk)
3112 # Not all calls to compress emit data. It is cheaper to inspect
3116 # Not all calls to compress emit data. It is cheaper to inspect
3113 # here than to feed empty chunks through generator.
3117 # here than to feed empty chunks through generator.
3114 if data:
3118 if data:
3115 yield data
3119 yield data
3116
3120
3117 yield z.flush()
3121 yield z.flush()
3118
3122
3119 def decompressorreader(self, fh):
3123 def decompressorreader(self, fh):
3120 def gen():
3124 def gen():
3121 d = zlib.decompressobj()
3125 d = zlib.decompressobj()
3122 for chunk in filechunkiter(fh):
3126 for chunk in filechunkiter(fh):
3123 yield d.decompress(chunk)
3127 yield d.decompress(chunk)
3124
3128
3125 return chunkbuffer(gen())
3129 return chunkbuffer(gen())
3126
3130
3127 compengines.register(_zlibengine())
3131 compengines.register(_zlibengine())
3128
3132
3129 class _bz2engine(compressionengine):
3133 class _bz2engine(compressionengine):
3130 def name(self):
3134 def name(self):
3131 return 'bz2'
3135 return 'bz2'
3132
3136
3133 def bundletype(self):
3137 def bundletype(self):
3134 return 'bzip2', 'BZ'
3138 return 'bzip2', 'BZ'
3135
3139
3136 def compressstream(self, it, opts=None):
3140 def compressstream(self, it, opts=None):
3137 opts = opts or {}
3141 opts = opts or {}
3138 z = bz2.BZ2Compressor(opts.get('level', 9))
3142 z = bz2.BZ2Compressor(opts.get('level', 9))
3139 for chunk in it:
3143 for chunk in it:
3140 data = z.compress(chunk)
3144 data = z.compress(chunk)
3141 if data:
3145 if data:
3142 yield data
3146 yield data
3143
3147
3144 yield z.flush()
3148 yield z.flush()
3145
3149
3146 def decompressorreader(self, fh):
3150 def decompressorreader(self, fh):
3147 def gen():
3151 def gen():
3148 d = bz2.BZ2Decompressor()
3152 d = bz2.BZ2Decompressor()
3149 for chunk in filechunkiter(fh):
3153 for chunk in filechunkiter(fh):
3150 yield d.decompress(chunk)
3154 yield d.decompress(chunk)
3151
3155
3152 return chunkbuffer(gen())
3156 return chunkbuffer(gen())
3153
3157
3154 compengines.register(_bz2engine())
3158 compengines.register(_bz2engine())
3155
3159
3156 class _truncatedbz2engine(compressionengine):
3160 class _truncatedbz2engine(compressionengine):
3157 def name(self):
3161 def name(self):
3158 return 'bz2truncated'
3162 return 'bz2truncated'
3159
3163
3160 def bundletype(self):
3164 def bundletype(self):
3161 return None, '_truncatedBZ'
3165 return None, '_truncatedBZ'
3162
3166
3163 # We don't implement compressstream because it is hackily handled elsewhere.
3167 # We don't implement compressstream because it is hackily handled elsewhere.
3164
3168
3165 def decompressorreader(self, fh):
3169 def decompressorreader(self, fh):
3166 def gen():
3170 def gen():
3167 # The input stream doesn't have the 'BZ' header. So add it back.
3171 # The input stream doesn't have the 'BZ' header. So add it back.
3168 d = bz2.BZ2Decompressor()
3172 d = bz2.BZ2Decompressor()
3169 d.decompress('BZ')
3173 d.decompress('BZ')
3170 for chunk in filechunkiter(fh):
3174 for chunk in filechunkiter(fh):
3171 yield d.decompress(chunk)
3175 yield d.decompress(chunk)
3172
3176
3173 return chunkbuffer(gen())
3177 return chunkbuffer(gen())
3174
3178
3175 compengines.register(_truncatedbz2engine())
3179 compengines.register(_truncatedbz2engine())
3176
3180
3177 class _noopengine(compressionengine):
3181 class _noopengine(compressionengine):
3178 def name(self):
3182 def name(self):
3179 return 'none'
3183 return 'none'
3180
3184
3181 def bundletype(self):
3185 def bundletype(self):
3182 return 'none', 'UN'
3186 return 'none', 'UN'
3183
3187
3184 def compressstream(self, it, opts=None):
3188 def compressstream(self, it, opts=None):
3185 return it
3189 return it
3186
3190
3187 def decompressorreader(self, fh):
3191 def decompressorreader(self, fh):
3188 return fh
3192 return fh
3189
3193
3190 compengines.register(_noopengine())
3194 compengines.register(_noopengine())
3191
3195
3192 class _zstdengine(compressionengine):
3196 class _zstdengine(compressionengine):
3193 def name(self):
3197 def name(self):
3194 return 'zstd'
3198 return 'zstd'
3195
3199
3196 @propertycache
3200 @propertycache
3197 def _module(self):
3201 def _module(self):
3198 # Not all installs have the zstd module available. So defer importing
3202 # Not all installs have the zstd module available. So defer importing
3199 # until first access.
3203 # until first access.
3200 try:
3204 try:
3201 from . import zstd
3205 from . import zstd
3202 # Force delayed import.
3206 # Force delayed import.
3203 zstd.__version__
3207 zstd.__version__
3204 return zstd
3208 return zstd
3205 except ImportError:
3209 except ImportError:
3206 return None
3210 return None
3207
3211
3208 def available(self):
3212 def available(self):
3209 return bool(self._module)
3213 return bool(self._module)
3210
3214
3211 def bundletype(self):
3215 def bundletype(self):
3212 return 'zstd', 'ZS'
3216 return 'zstd', 'ZS'
3213
3217
3214 def compressstream(self, it, opts=None):
3218 def compressstream(self, it, opts=None):
3215 opts = opts or {}
3219 opts = opts or {}
3216 # zstd level 3 is almost always significantly faster than zlib
3220 # zstd level 3 is almost always significantly faster than zlib
3217 # while providing no worse compression. It strikes a good balance
3221 # while providing no worse compression. It strikes a good balance
3218 # between speed and compression.
3222 # between speed and compression.
3219 level = opts.get('level', 3)
3223 level = opts.get('level', 3)
3220
3224
3221 zstd = self._module
3225 zstd = self._module
3222 z = zstd.ZstdCompressor(level=level).compressobj()
3226 z = zstd.ZstdCompressor(level=level).compressobj()
3223 for chunk in it:
3227 for chunk in it:
3224 data = z.compress(chunk)
3228 data = z.compress(chunk)
3225 if data:
3229 if data:
3226 yield data
3230 yield data
3227
3231
3228 yield z.flush()
3232 yield z.flush()
3229
3233
3230 def decompressorreader(self, fh):
3234 def decompressorreader(self, fh):
3231 zstd = self._module
3235 zstd = self._module
3232 dctx = zstd.ZstdDecompressor()
3236 dctx = zstd.ZstdDecompressor()
3233 return chunkbuffer(dctx.read_from(fh))
3237 return chunkbuffer(dctx.read_from(fh))
3234
3238
3235 compengines.register(_zstdengine())
3239 compengines.register(_zstdengine())
3236
3240
3237 # convenient shortcut
3241 # convenient shortcut
3238 dst = debugstacktrace
3242 dst = debugstacktrace
General Comments 0
You need to be logged in to leave comments. Login now