Mercurial > hg
annotate mercurial/util.py @ 13984:af60153b5e3b
move rcpath from util to scmutil
author | Adrian Buehlmann <adrian@cadifra.com> |
---|---|
date | Thu, 21 Apr 2011 20:14:29 +0200 |
parents | 938fbeacac84 |
children | 26335a817dd0 |
rev | line source |
---|---|
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
1 # util.py - Mercurial utility functions and platform specfic implementations |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
2 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
6 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
7 # This software may be used and distributed according to the terms of the |
10263 | 8 # GNU General Public License version 2 or any later version. |
1082 | 9 |
8227
0a9542703300
turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents:
8226
diff
changeset
|
10 """Mercurial utility functions and platform specfic implementations. |
1082 | 11 |
8227
0a9542703300
turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents:
8226
diff
changeset
|
12 This contains helper routines that are independent of the SCM core and |
0a9542703300
turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents:
8226
diff
changeset
|
13 hide platform-specific details from the core. |
1082 | 14 """ |
419
28511fc21073
[PATCH] file seperator handling for the other 'OS'
mpm@selenic.com
parents:
diff
changeset
|
15 |
3891 | 16 from i18n import _ |
9480
44758742ad2e
util: do not corrupt multi-byte characters in wrap
Martin Geisler <mg@lazybytes.net>
parents:
9383
diff
changeset
|
17 import error, osutil, encoding |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
18 import errno, re, shutil, sys, tempfile, traceback |
13972
d1f4e7fd970a
move path_auditor from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13971
diff
changeset
|
19 import os, time, calendar, textwrap, unicodedata, signal |
12076
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
20 import imp, socket |
3769 | 21 |
6470
ac0bcd951c2c
python 2.6 compatibility: compatibility wrappers for hash functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6339
diff
changeset
|
22 # Python compatibility |
3769 | 23 |
6470
ac0bcd951c2c
python 2.6 compatibility: compatibility wrappers for hash functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6339
diff
changeset
|
24 def sha1(s): |
8297
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
25 return _fastsha1(s) |
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
26 |
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
27 def _fastsha1(s): |
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
28 # This function will import sha1 from hashlib or sha (whichever is |
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
29 # available) and overwrite itself with it on the first call. |
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
30 # Subsequent calls will go directly to the imported function. |
12051
ff5cec76b1c5
util: avoid using hashlib on Python < 2.5 (issue2278)
Sol Jerome <sol.jerome@gmail.com>
parents:
11758
diff
changeset
|
31 if sys.version_info >= (2, 5): |
8297
7f27e69dd27f
util: stop overwriting sha1, overwrite _fastsha1 instead
Martin Geisler <mg@lazybytes.net>
parents:
8296
diff
changeset
|
32 from hashlib import sha1 as _sha1 |
12051
ff5cec76b1c5
util: avoid using hashlib on Python < 2.5 (issue2278)
Sol Jerome <sol.jerome@gmail.com>
parents:
11758
diff
changeset
|
33 else: |
8295
1ea7e7d90007
util: remove warnings when importing md5 and sha
Sune Foldager <cryo@cyanite.org>
parents:
8281
diff
changeset
|
34 from sha import sha as _sha1 |
8309
4ff63d699256
util: overwrite sha1 and _fastsha1
Simon Heimberg <simohe@besonet.ch>
parents:
8302
diff
changeset
|
35 global _fastsha1, sha1 |
4ff63d699256
util: overwrite sha1 and _fastsha1
Simon Heimberg <simohe@besonet.ch>
parents:
8302
diff
changeset
|
36 _fastsha1 = sha1 = _sha1 |
6470
ac0bcd951c2c
python 2.6 compatibility: compatibility wrappers for hash functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6339
diff
changeset
|
37 return _sha1(s) |
ac0bcd951c2c
python 2.6 compatibility: compatibility wrappers for hash functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6339
diff
changeset
|
38 |
10756
cb681cc59a8d
util: fake the builtin buffer if it's missing (jython)
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
10487
diff
changeset
|
39 import __builtin__ |
cb681cc59a8d
util: fake the builtin buffer if it's missing (jython)
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
10487
diff
changeset
|
40 |
11566
4d11fde55cc5
util: use fakebuffer as buffer in py3k
Renato Cunha <renatoc@gmail.com>
parents:
11565
diff
changeset
|
41 if sys.version_info[0] < 3: |
4d11fde55cc5
util: use fakebuffer as buffer in py3k
Renato Cunha <renatoc@gmail.com>
parents:
11565
diff
changeset
|
42 def fakebuffer(sliceable, offset=0): |
4d11fde55cc5
util: use fakebuffer as buffer in py3k
Renato Cunha <renatoc@gmail.com>
parents:
11565
diff
changeset
|
43 return sliceable[offset:] |
4d11fde55cc5
util: use fakebuffer as buffer in py3k
Renato Cunha <renatoc@gmail.com>
parents:
11565
diff
changeset
|
44 else: |
4d11fde55cc5
util: use fakebuffer as buffer in py3k
Renato Cunha <renatoc@gmail.com>
parents:
11565
diff
changeset
|
45 def fakebuffer(sliceable, offset=0): |
4d11fde55cc5
util: use fakebuffer as buffer in py3k
Renato Cunha <renatoc@gmail.com>
parents:
11565
diff
changeset
|
46 return memoryview(sliceable)[offset:] |
11565
7546d4a272c8
util: improved the check for the existence of the 'buffer' builtin
Renato Cunha <renatoc@gmail.com>
parents:
11469
diff
changeset
|
47 try: |
7546d4a272c8
util: improved the check for the existence of the 'buffer' builtin
Renato Cunha <renatoc@gmail.com>
parents:
11469
diff
changeset
|
48 buffer |
7546d4a272c8
util: improved the check for the existence of the 'buffer' builtin
Renato Cunha <renatoc@gmail.com>
parents:
11469
diff
changeset
|
49 except NameError: |
10756
cb681cc59a8d
util: fake the builtin buffer if it's missing (jython)
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
10487
diff
changeset
|
50 __builtin__.buffer = fakebuffer |
cb681cc59a8d
util: fake the builtin buffer if it's missing (jython)
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
10487
diff
changeset
|
51 |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
52 import subprocess |
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
53 closefds = os.name == 'posix' |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
54 |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
55 def popen2(cmd, env=None, newlines=False): |
9089
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
56 # Setting bufsize to -1 lets the system decide the buffer size. |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
57 # The default for bufsize is 0, meaning unbuffered. This leads to |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
58 # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
59 p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
9083
ec171737aaf1
Backed out changeset fce065538bcf: it caused a 5x performance regression on OS X
Bryan O'Sullivan <bos@serpentine.com>
parents:
8340
diff
changeset
|
60 close_fds=closefds, |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
61 stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
62 universal_newlines=newlines, |
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
63 env=env) |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
64 return p.stdin, p.stdout |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
65 |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
66 def popen3(cmd, env=None, newlines=False): |
9089
8ec39725d966
util: remove unused bufsize argument
Martin Geisler <mg@lazybytes.net>
parents:
9084
diff
changeset
|
67 p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
9083
ec171737aaf1
Backed out changeset fce065538bcf: it caused a 5x performance regression on OS X
Bryan O'Sullivan <bos@serpentine.com>
parents:
8340
diff
changeset
|
68 close_fds=closefds, |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
69 stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
10197
29e3c4a7699b
subrepo: normalize svn output line-endings
Patrick Mezard <pmezard@gmail.com>
parents:
9996
diff
changeset
|
70 stderr=subprocess.PIPE, |
10199
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
71 universal_newlines=newlines, |
c2e2a5e6c36b
subrepo: force en_US.UTF-8 locale when calling svn
Patrick Mezard <pmezard@gmail.com>
parents:
10197
diff
changeset
|
72 env=env) |
8280
0b02d98d44d0
util: always use subprocess
Martin Geisler <mg@lazybytes.net>
parents:
8257
diff
changeset
|
73 return p.stdin, p.stdout, p.stderr |
7106
4674706b5b95
python2.6: use subprocess if available
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
74 |
7632 | 75 def version(): |
76 """Return version information if available.""" | |
77 try: | |
78 import __version__ | |
79 return __version__.version | |
80 except ImportError: | |
81 return 'unknown' | |
82 | |
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
83 # used by parsedate |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
84 defaultdateformats = ( |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
85 '%Y-%m-%d %H:%M:%S', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
86 '%Y-%m-%d %I:%M:%S%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
87 '%Y-%m-%d %H:%M', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
88 '%Y-%m-%d %I:%M%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
89 '%Y-%m-%d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
90 '%m-%d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
91 '%m/%d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
92 '%m/%d/%y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
93 '%m/%d/%Y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
94 '%a %b %d %H:%M:%S %Y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
95 '%a %b %d %I:%M:%S%p %Y', |
4708
01f9ee4de1ad
Add support for RFC2822 to util.parsedate().
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents:
4686
diff
changeset
|
96 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
97 '%b %d %H:%M:%S %Y', |
3812 | 98 '%b %d %I:%M:%S%p %Y', |
99 '%b %d %H:%M:%S', | |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
100 '%b %d %I:%M:%S%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
101 '%b %d %H:%M', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
102 '%b %d %I:%M%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
103 '%b %d %Y', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
104 '%b %d', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
105 '%H:%M:%S', |
9383
7116494c48ab
util: Fix date format for 12-hour time.
Carey Evans <carey@carey.geek.nz>
parents:
9097
diff
changeset
|
106 '%I:%M:%S%p', |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
107 '%H:%M', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
108 '%I:%M%p', |
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
109 ) |
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
110 |
3812 | 111 extendeddateformats = defaultdateformats + ( |
112 "%Y", | |
113 "%Y-%m", | |
114 "%b", | |
115 "%b %Y", | |
116 ) | |
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
117 |
3145
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
118 def cachefunc(func): |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
119 '''cache the result of function calls''' |
3147
97420a49188d
add comments in cachefunc
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3145
diff
changeset
|
120 # XXX doesn't handle keywords args |
3145
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
121 cache = {} |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
122 if func.func_code.co_argcount == 1: |
3147
97420a49188d
add comments in cachefunc
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3145
diff
changeset
|
123 # we gain a small amount of time because |
97420a49188d
add comments in cachefunc
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3145
diff
changeset
|
124 # we don't need to pack/unpack the list |
3145
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
125 def f(arg): |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
126 if arg not in cache: |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
127 cache[arg] = func(arg) |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
128 return cache[arg] |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
129 else: |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
130 def f(*args): |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
131 if args not in cache: |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
132 cache[args] = func(*args) |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
133 return cache[args] |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
134 |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
135 return f |
e4ea47c21480
Add cachefunc to abstract function call cache
Brendan Cully <brendan@kublai.com>
parents:
3131
diff
changeset
|
136 |
9097
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
137 def lrucachefunc(func): |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
138 '''cache most recent results of function calls''' |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
139 cache = {} |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
140 order = [] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
141 if func.func_code.co_argcount == 1: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
142 def f(arg): |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
143 if arg not in cache: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
144 if len(cache) > 20: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
145 del cache[order.pop(0)] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
146 cache[arg] = func(arg) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
147 else: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
148 order.remove(arg) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
149 order.append(arg) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
150 return cache[arg] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
151 else: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
152 def f(*args): |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
153 if args not in cache: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
154 if len(cache) > 20: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
155 del cache[order.pop(0)] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
156 cache[args] = func(*args) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
157 else: |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
158 order.remove(args) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
159 order.append(args) |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
160 return cache[args] |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
161 |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
162 return f |
431462bd8478
fix memory usage of revlog caches by limiting cache size [issue1639]
Matt Mackall <mpm@selenic.com>
parents:
9089
diff
changeset
|
163 |
8207
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
164 class propertycache(object): |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
165 def __init__(self, func): |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
166 self.func = func |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
167 self.name = func.__name__ |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
168 def __get__(self, obj, type=None): |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
169 result = self.func(obj) |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
170 setattr(obj, self.name, result) |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
171 return result |
dd8d5be57d65
util: take propertycache from context.py
Matt Mackall <mpm@selenic.com>
parents:
8181
diff
changeset
|
172 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
173 def pipefilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
174 '''filter string S through command CMD, returning its output''' |
8302
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
175 p = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
176 stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
177 pout, perr = p.communicate(s) |
d2ad8c066676
util: simplify pipefilter and avoid subprocess race
Martin Geisler <mg@lazybytes.net>
parents:
8299
diff
changeset
|
178 return pout |
419
28511fc21073
[PATCH] file seperator handling for the other 'OS'
mpm@selenic.com
parents:
diff
changeset
|
179 |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
180 def tempfilter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
181 '''filter string S through a pair of temporary files with CMD. |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
182 CMD is used as a template to create the real command to be run, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
183 with the strings INFILE and OUTFILE replaced by the real names of |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
184 the temporary files generated.''' |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
185 inname, outname = None, None |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
186 try: |
2165
d821918e3bee
Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2153
diff
changeset
|
187 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
188 fp = os.fdopen(infd, 'wb') |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
189 fp.write(s) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
190 fp.close() |
2165
d821918e3bee
Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2153
diff
changeset
|
191 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
192 os.close(outfd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
193 cmd = cmd.replace('INFILE', inname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
194 cmd = cmd.replace('OUTFILE', outname) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
195 code = os.system(cmd) |
4720
72fb6f10fac1
OpenVMS patches
Jean-Francois PIERONNE <jf.pieronne@laposte.net>
parents:
4708
diff
changeset
|
196 if sys.platform == 'OpenVMS' and code & 1: |
72fb6f10fac1
OpenVMS patches
Jean-Francois PIERONNE <jf.pieronne@laposte.net>
parents:
4708
diff
changeset
|
197 code = 0 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
198 if code: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
199 raise Abort(_("command '%s' failed: %s") % |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
200 (cmd, explain_exit(code))) |
13400
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
201 fp = open(outname, 'rb') |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
202 r = fp.read() |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
203 fp.close() |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
204 return r |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
205 finally: |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
206 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
207 if inname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
208 os.unlink(inname) |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
209 except: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
210 pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
211 try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
212 if outname: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
213 os.unlink(outname) |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
214 except: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
215 pass |
1293
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
216 |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
217 filtertable = { |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
218 'tempfile:': tempfilter, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
219 'pipe:': pipefilter, |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
220 } |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
221 |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
222 def filter(s, cmd): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
223 "filter a string through a command that transforms its input to its output" |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
224 for name, fn in filtertable.iteritems(): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
225 if cmd.startswith(name): |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
226 return fn(s, cmd[len(name):].lstrip()) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
227 return pipefilter(s, cmd) |
a6ffcebd3315
Enhance the file filtering capabilities.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1292
diff
changeset
|
228 |
1015
22571b8d35d3
Add automatic binary file detection to diff and export
mpm@selenic.com
parents:
917
diff
changeset
|
229 def binary(s): |
6507
9699864de219
Let util.binary check entire data for \0 (issue1066, issue1079)
Christian Ebert <blacktrash@gmx.net>
parents:
6501
diff
changeset
|
230 """return true if a string is binary data""" |
8118
35f7fda52c92
util: return boolean result directly in util.binary
Martin Geisler <mg@lazybytes.net>
parents:
8011
diff
changeset
|
231 return bool(s and '\0' in s) |
6762 | 232 |
7396
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
233 def increasingchunks(source, min=1024, max=65536): |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
234 '''return no less than min bytes per chunk while data remains, |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
235 doubling min after each chunk until it reaches max''' |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
236 def log2(x): |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
237 if not x: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
238 return 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
239 i = 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
240 while x: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
241 x >>= 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
242 i += 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
243 return i - 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
244 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
245 buf = [] |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
246 blen = 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
247 for chunk in source: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
248 buf.append(chunk) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
249 blen += len(chunk) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
250 if blen >= min: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
251 if min < max: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
252 min = min << 1 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
253 nmin = 1 << log2(blen) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
254 if nmin > min: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
255 min = nmin |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
256 if min > max: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
257 min = max |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
258 yield ''.join(buf) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
259 blen = 0 |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
260 buf = [] |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
261 if buf: |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
262 yield ''.join(buf) |
526c40a74bd0
templater: return data in increasing chunk sizes
Brendan Cully <brendan@kublai.com>
parents:
7301
diff
changeset
|
263 |
7947
a454eeb1b827
move util.Abort to error.py
Matt Mackall <mpm@selenic.com>
parents:
7913
diff
changeset
|
264 Abort = error.Abort |
508 | 265 |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
266 def always(fn): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
267 return True |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
268 |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
269 def never(fn): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
270 return False |
724
1c0c413cccdd
Get add and locate to use new repo and dirstate walk code.
Bryan O'Sullivan <bos@serpentine.com>
parents:
705
diff
changeset
|
271 |
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
272 def pathto(root, n1, n2): |
886
509de8ab6f31
Fix walk path handling on Windows
Bryan O'Sullivan <bos@serpentine.com>
parents:
884
diff
changeset
|
273 '''return the relative path from one place to another. |
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
274 root should use os.sep to separate directories |
3669
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
275 n1 should use os.sep to separate directories |
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
276 n2 should use "/" to separate directories |
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
277 returns an os.sep-separated path. |
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
278 |
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
279 If n1 is a relative path, it's assumed it's |
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
280 relative to root. |
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4129
diff
changeset
|
281 n2 should always be relative to root. |
3669
48768b1ab23c
fix util.pathto
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3629
diff
changeset
|
282 ''' |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
283 if not n1: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
284 return localpath(n2) |
4230
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
285 if os.path.isabs(n1): |
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
286 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
287 return os.path.join(root, localpath(n2)) |
c93562fb12cc
Fix handling of paths when run outside the repo.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
288 n2 = '/'.join((pconvert(root), n2)) |
5844
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
289 a, b = splitpath(n1), n2.split('/') |
1541
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
290 a.reverse() |
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
291 b.reverse() |
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
878
diff
changeset
|
292 while a and b and a[-1] == b[-1]: |
1541
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
293 a.pop() |
bf4e7ef08741
fixed some stuff pychecker shows, marked unclear/wrong stuff with XXX
twaldmann@thinkmo.de
parents:
1528
diff
changeset
|
294 b.pop() |
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
878
diff
changeset
|
295 b.reverse() |
6111
213ea6eed412
util.pathto: return '.' instead of an empty string
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6091
diff
changeset
|
296 return os.sep.join((['..'] * len(a)) + b) or '.' |
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
878
diff
changeset
|
297 |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
298 _hgexecutable = None |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
299 |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
300 def main_is_frozen(): |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
301 """return True if we are a frozen executable. |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
302 |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
303 The code supports py2exe (most common, Windows only) and tools/freeze |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
304 (portable, not much used). |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
305 """ |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
306 return (hasattr(sys, "frozen") or # new py2exe |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
307 hasattr(sys, "importers") or # old py2exe |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
308 imp.is_frozen("__main__")) # tools/freeze |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
309 |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
310 def hgexecutable(): |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
311 """return location of the 'hg' executable. |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
312 |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
313 Defaults to $HG or 'hg' in the search path. |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
314 """ |
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
315 if _hgexecutable is None: |
6500 | 316 hg = os.environ.get('HG') |
317 if hg: | |
318 set_hgexecutable(hg) | |
6499
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
319 elif main_is_frozen(): |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
320 set_hgexecutable(sys.executable) |
479847ccabe0
Added hgexecutable support for py2exe/frozen scripts
"Paul Moore <p.f.moore@gmail.com>"
parents:
5659
diff
changeset
|
321 else: |
9632
16698d87ad20
util: use sys.argv[0] if $HG is unset and 'hg' is not in PATH
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9610
diff
changeset
|
322 exe = find_exe('hg') or os.path.basename(sys.argv[0]) |
16698d87ad20
util: use sys.argv[0] if $HG is unset and 'hg' is not in PATH
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
9610
diff
changeset
|
323 set_hgexecutable(exe) |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
324 return _hgexecutable |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
325 |
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
326 def set_hgexecutable(path): |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
327 """set location of the 'hg' executable""" |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
328 global _hgexecutable |
5062
3d35c8cb5eb4
Simplify/correct finding the hg executable (fixes issue644)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4832
diff
changeset
|
329 _hgexecutable = path |
4686
849f011dbf79
Remember path to 'hg' executable and pass to external tools and hooks as $HG.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4673
diff
changeset
|
330 |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
331 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None, out=None): |
1882
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
332 '''enhanced shell command execution. |
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
333 run with environment maybe modified, maybe in different dir. |
508 | 334 |
1882
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
335 if command fails and onerr is None, return status. if ui object, |
c0320567931f
merge util.esystem and util.system.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1880
diff
changeset
|
336 print error message and return status, else raise onerr object as |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
337 exception. |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
338 |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
339 if out is specified, it is assumed to be a file-like object that has a |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
340 write() method. stdout and stderr will be redirected to out.''' |
13439
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
341 try: |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
342 sys.stdout.flush() |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
343 except Exception: |
d724a69309e0
util: flush stdout before calling external processes
Mads Kiilerich <mads@kiilerich.com>
parents:
13400
diff
changeset
|
344 pass |
2601
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
345 def py2shell(val): |
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
346 'convert python object into string that is useful to shell' |
8534
22ec9cf4d0ce
util: use "is" for True/False/None comparisons
Martin Geisler <mg@lazybytes.net>
parents:
8516
diff
changeset
|
347 if val is None or val is False: |
2601
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
348 return '0' |
8534
22ec9cf4d0ce
util: use "is" for True/False/None comparisons
Martin Geisler <mg@lazybytes.net>
parents:
8516
diff
changeset
|
349 if val is True: |
2601
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
350 return '1' |
00fc88b0b256
move most of tag code to localrepository class.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2579
diff
changeset
|
351 return str(val) |
3905
a8c0365b2ace
util.system: fix quoting on windows
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3860
diff
changeset
|
352 origcmd = cmd |
13188
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
13128
diff
changeset
|
353 cmd = quotecommand(cmd) |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
354 env = dict(os.environ) |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
355 env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
356 env['HG'] = hgexecutable() |
11469
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
357 if out is None: |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
358 rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
359 env=env, cwd=cwd) |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
360 else: |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
361 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
362 env=env, cwd=cwd, stdout=subprocess.PIPE, |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
363 stderr=subprocess.STDOUT) |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
364 for line in proc.stdout: |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
365 out.write(line) |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
366 proc.wait() |
c37f35d7f2f5
http: deliver hook output to client
Maxim Khitrov <mkhitrov@gmail.com>
parents:
11297
diff
changeset
|
367 rc = proc.returncode |
9517
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
368 if sys.platform == 'OpenVMS' and rc & 1: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
369 rc = 0 |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
370 if rc and onerr: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
371 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
372 explain_exit(rc)[0]) |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
373 if errprefix: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
374 errmsg = '%s: %s' % (errprefix, errmsg) |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
375 try: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
376 onerr.warn(errmsg + '\n') |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
377 except AttributeError: |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
378 raise onerr(errmsg) |
4368f582c806
util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents:
9508
diff
changeset
|
379 return rc |
1880
05c7d75be925
fix broken environment save/restore when a hook runs.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1877
diff
changeset
|
380 |
7388
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
381 def checksignature(func): |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
382 '''wrap a function with code to check for calling errors''' |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
383 def check(*args, **kwargs): |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
384 try: |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
385 return func(*args, **kwargs) |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
386 except TypeError: |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
387 if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
7646 | 388 raise error.SignatureError |
7388
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
389 raise |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
390 |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
391 return check |
5751631246de
dispatch: generalize signature checking for extension command wrapping
Matt Mackall <mpm@selenic.com>
parents:
7301
diff
changeset
|
392 |
13795
43b5fe18ea6c
set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13780
diff
changeset
|
393 def makedir(path, notindexed): |
43b5fe18ea6c
set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13780
diff
changeset
|
394 os.mkdir(path) |
43b5fe18ea6c
set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13780
diff
changeset
|
395 |
13235
6bf39d88c857
rename util.unlink to unlinkpath
Adrian Buehlmann <adrian@cadifra.com>
parents:
13228
diff
changeset
|
396 def unlinkpath(f): |
1415
c6e6ca96a033
refactor some unlink/remove code and make sure we prune empty dir
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1413
diff
changeset
|
397 """unlink and remove the directory if it is empty""" |
c6e6ca96a033
refactor some unlink/remove code and make sure we prune empty dir
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1413
diff
changeset
|
398 os.unlink(f) |
c6e6ca96a033
refactor some unlink/remove code and make sure we prune empty dir
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1413
diff
changeset
|
399 # try removing directories that might now be empty |
2064
547ede0123a2
util.unlink should only catch OSError.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2054
diff
changeset
|
400 try: |
547ede0123a2
util.unlink should only catch OSError.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2054
diff
changeset
|
401 os.removedirs(os.path.dirname(f)) |
547ede0123a2
util.unlink should only catch OSError.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2054
diff
changeset
|
402 except OSError: |
547ede0123a2
util.unlink should only catch OSError.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2054
diff
changeset
|
403 pass |
1415
c6e6ca96a033
refactor some unlink/remove code and make sure we prune empty dir
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1413
diff
changeset
|
404 |
3629
4cfb72bcb978
util: add copyfile function
Matt Mackall <mpm@selenic.com>
parents:
3568
diff
changeset
|
405 def copyfile(src, dest): |
7767
b2410ed2cbe9
Use shutil.copystat in copyfile().
Will Maier <willmaier@ml1.net>
parents:
7732
diff
changeset
|
406 "copy a file, preserving mode and atime/mtime" |
4271
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
407 if os.path.islink(src): |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
408 try: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
409 os.unlink(dest) |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
410 except: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
411 pass |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
412 os.symlink(os.readlink(src), dest) |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
413 else: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
414 try: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
415 shutil.copyfile(src, dest) |
13099
a08b49d2f116
record: move copystat() hack out of util.copyfile() and into record
Brodie Rao <brodie@bitheap.org>
parents:
13098
diff
changeset
|
416 shutil.copymode(src, dest) |
4271
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
417 except shutil.Error, inst: |
1eaa8d90c689
fix util.copyfile to deal with symlinks
Eric St-Jean <esj@wwd.ca>
parents:
4256
diff
changeset
|
418 raise Abort(str(inst)) |
3629
4cfb72bcb978
util: add copyfile function
Matt Mackall <mpm@selenic.com>
parents:
3568
diff
changeset
|
419 |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
420 def copyfiles(src, dst, hardlink=None): |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
421 """Copy a directory tree using hardlinks if possible""" |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
422 |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
423 if hardlink is None: |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
424 hardlink = (os.stat(src).st_dev == |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
425 os.stat(os.path.dirname(dst)).st_dev) |
698
df78d8ccac4c
Use python function instead of external 'cp' command when cloning repos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
667
diff
changeset
|
426 |
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
427 num = 0 |
1207 | 428 if os.path.isdir(src): |
429 os.mkdir(dst) | |
5396
5105b119edd2
Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
5360
diff
changeset
|
430 for name, kind in osutil.listdir(src): |
1207 | 431 srcname = os.path.join(src, name) |
432 dstname = os.path.join(dst, name) | |
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
433 hardlink, n = copyfiles(srcname, dstname, hardlink) |
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
434 num += n |
1207 | 435 else: |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
436 if hardlink: |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
437 try: |
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
438 os_link(src, dst) |
2050
e49d0fa38176
util.copyfiles: only switch to copy if hardlink raises IOError or OSError.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2026
diff
changeset
|
439 except (IOError, OSError): |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
440 hardlink = False |
1591
5a3229cf1492
do not copy atime and mtime in util.copyfiles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1585
diff
changeset
|
441 shutil.copy(src, dst) |
1241
3b4f05ff3130
Add support for cloning with hardlinks on windows.
Stephen Darnell
parents:
1207
diff
changeset
|
442 else: |
1591
5a3229cf1492
do not copy atime and mtime in util.copyfiles
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1585
diff
changeset
|
443 shutil.copy(src, dst) |
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
444 num += 1 |
698
df78d8ccac4c
Use python function instead of external 'cp' command when cloning repos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
667
diff
changeset
|
445 |
11251
c61442f6d106
clone: print number of linked/copied files on --debug
Adrian Buehlmann <adrian@cadifra.com>
parents:
11232
diff
changeset
|
446 return hardlink, num |
11254
640d419725d0
util.copyfiles: don't try os_link() again if it failed before
Adrian Buehlmann <adrian@cadifra.com>
parents:
11010
diff
changeset
|
447 |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
448 _windows_reserved_filenames = '''con prn aux nul |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
449 com1 com2 com3 com4 com5 com6 com7 com8 com9 |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
450 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
451 _windows_reserved_chars = ':*?"<>|' |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
452 def checkwinfilename(path): |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
453 '''Check that the base-relative path is a valid filename on Windows. |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
454 Returns None if the path is ok, or a UI string describing the problem. |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
455 |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
456 >>> checkwinfilename("just/a/normal/path") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
457 >>> checkwinfilename("foo/bar/con.xml") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
458 "filename contains 'con', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
459 >>> checkwinfilename("foo/con.xml/bar") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
460 "filename contains 'con', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
461 >>> checkwinfilename("foo/bar/xml.con") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
462 >>> checkwinfilename("foo/bar/AUX/bla.txt") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
463 "filename contains 'AUX', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
464 >>> checkwinfilename("foo/bar/bla:.txt") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
465 "filename contains ':', which is reserved on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
466 >>> checkwinfilename("foo/bar/b\07la.txt") |
13947
d2d1ef6a5238
checkwinfilename: use %r in format string
Adrian Buehlmann <adrian@cadifra.com>
parents:
13944
diff
changeset
|
467 "filename contains '\\\\x07', which is invalid on Windows" |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
468 >>> checkwinfilename("foo/bar/bla ") |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
469 "filename ends with ' ', which is not allowed on Windows" |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
470 ''' |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
471 for n in path.replace('\\', '/').split('/'): |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
472 if not n: |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
473 continue |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
474 for c in n: |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
475 if c in _windows_reserved_chars: |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
476 return _("filename contains '%s', which is reserved " |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
477 "on Windows") % c |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
478 if ord(c) <= 31: |
13947
d2d1ef6a5238
checkwinfilename: use %r in format string
Adrian Buehlmann <adrian@cadifra.com>
parents:
13944
diff
changeset
|
479 return _("filename contains %r, which is invalid " |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
480 "on Windows") % c |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
481 base = n.split('.')[0] |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
482 if base and base.lower() in _windows_reserved_filenames: |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
483 return _("filename contains '%s', which is reserved " |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
484 "on Windows") % base |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
485 t = n[-1] |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
486 if t in '. ': |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
487 return _("filename ends with '%s', which is not allowed " |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
488 "on Windows") % t |
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
489 |
8011
25b63941b17b
util: don't overwrite os-specific functions with general ones
Sune Foldager <cryo@cyanite.org>
parents:
7953
diff
changeset
|
490 def lookup_reg(key, name=None, scope=None): |
25b63941b17b
util: don't overwrite os-specific functions with general ones
Sune Foldager <cryo@cyanite.org>
parents:
7953
diff
changeset
|
491 return None |
25b63941b17b
util: don't overwrite os-specific functions with general ones
Sune Foldager <cryo@cyanite.org>
parents:
7953
diff
changeset
|
492 |
10240
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
493 def hidewindow(): |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
494 """Hide current shell window. |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
495 |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
496 Used to hide the window opened when starting asynchronous |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
497 child process under Windows, unneeded on other systems. |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
498 """ |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
499 pass |
3af4b39afe2a
cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents:
10239
diff
changeset
|
500 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
501 if os.name == 'nt': |
13916
98ee3dd5bab4
path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13910
diff
changeset
|
502 checkosfilename = checkwinfilename |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
503 from windows import * |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
504 else: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
505 from posix import * |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
506 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
507 def makelock(info, pathname): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
508 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
509 return os.symlink(info, pathname) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
510 except OSError, why: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
511 if why.errno == errno.EEXIST: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
512 raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
513 except AttributeError: # no symlink in os |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
514 pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
515 |
704
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
516 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
517 os.write(ld, info) |
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
518 os.close(ld) |
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
519 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
520 def readlock(pathname): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
521 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
522 return os.readlink(pathname) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
523 except OSError, why: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
524 if why.errno not in (errno.EINVAL, errno.ENOSYS): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
525 raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
526 except AttributeError: # no symlink in os |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
527 pass |
13400
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
528 fp = posixfile(pathname) |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
529 r = fp.read() |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
530 fp.close() |
14f3795a5ed7
explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13392
diff
changeset
|
531 return r |
704
5ca319a641e1
Make makelock and readlock work on filesystems without symlink support.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
698
diff
changeset
|
532 |
2176
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
533 def fstat(fp): |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
534 '''stat file object that may not have fileno method.''' |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
535 try: |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
536 return os.fstat(fp.fileno()) |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
537 except AttributeError: |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
538 return os.stat(fp.name) |
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
539 |
3784 | 540 # File system features |
541 | |
6746
1dca460e7d1e
rename checkfolding to checkcase
Matt Mackall <mpm@selenic.com>
parents:
6743
diff
changeset
|
542 def checkcase(path): |
3784 | 543 """ |
544 Check whether the given path is on a case-sensitive filesystem | |
545 | |
546 Requires a path (like /foo/.hg) ending with a foldable final | |
547 directory component. | |
548 """ | |
549 s1 = os.stat(path) | |
550 d, b = os.path.split(path) | |
551 p2 = os.path.join(d, b.upper()) | |
552 if path == p2: | |
553 p2 = os.path.join(d, b.lower()) | |
554 try: | |
555 s2 = os.stat(p2) | |
556 if s2 == s1: | |
557 return False | |
558 return True | |
559 except: | |
560 return True | |
561 | |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
562 _fspathcache = {} |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
563 def fspath(name, root): |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
564 '''Get name in the case stored in the filesystem |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
565 |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
566 The name is either relative to root, or it is an absolute path starting |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
567 with root. Note that this function is unnecessary, and should not be |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
568 called, for case-sensitive filesystems (simply because it's expensive). |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
569 ''' |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
570 # If name is absolute, make it relative |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
571 if name.lower().startswith(root.lower()): |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
572 l = len(root) |
12401
4cdaf1adafc8
backout most of 4f8067c94729
Matt Mackall <mpm@selenic.com>
parents:
12387
diff
changeset
|
573 if name[l] == os.sep or name[l] == os.altsep: |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
574 l = l + 1 |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
575 name = name[l:] |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
576 |
12344
b6173aee4a47
Use lexists() instead of exists() where appropriate
Patrick Mezard <pmezard@gmail.com>
parents:
12051
diff
changeset
|
577 if not os.path.lexists(os.path.join(root, name)): |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
578 return None |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
579 |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
580 seps = os.sep |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
581 if os.altsep: |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
582 seps = seps + os.altsep |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
583 # Protect backslashes. This gets silly very quickly. |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
584 seps.replace('\\','\\\\') |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
585 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps)) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
586 dir = os.path.normcase(os.path.normpath(root)) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
587 result = [] |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
588 for part, sep in pattern.findall(name): |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
589 if sep: |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
590 result.append(sep) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
591 continue |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
592 |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
593 if dir not in _fspathcache: |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
594 _fspathcache[dir] = os.listdir(dir) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
595 contents = _fspathcache[dir] |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
596 |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
597 lpart = part.lower() |
9397
5b117c90f036
util: quicker fspath, do not lower names when the length is different
Simon Heimberg <simohe@besonet.ch>
parents:
9386
diff
changeset
|
598 lenp = len(part) |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
599 for n in contents: |
9397
5b117c90f036
util: quicker fspath, do not lower names when the length is different
Simon Heimberg <simohe@besonet.ch>
parents:
9386
diff
changeset
|
600 if lenp == len(n) and n.lower() == lpart: |
6676
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
601 result.append(n) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
602 break |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
603 else: |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
604 # Cannot happen, as the file exists! |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
605 result.append(part) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
606 dir = os.path.join(dir, lpart) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
607 |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
608 return ''.join(result) |
33045179d079
Add a new function, fspath
Paul Moore <p.f.moore@gmail.com>
parents:
6595
diff
changeset
|
609 |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
610 def checknlink(testfile): |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
611 '''check whether hardlink count reporting works properly''' |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
612 |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
613 # testfile may be open, so we need a separate file for checking to |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
614 # work around issue2543 (or testfile may get lost on Samba shares) |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
615 f1 = testfile + ".hgtmp1" |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
616 if os.path.lexists(f1): |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
617 return False |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
618 try: |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
619 posixfile(f1, 'w').close() |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
620 except IOError: |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
621 return False |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
622 |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
623 f2 = testfile + ".hgtmp2" |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
624 fd = None |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
625 try: |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
626 try: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
627 os_link(f1, f2) |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
628 except OSError: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
629 return False |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
630 |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
631 # nlinks() may behave differently for files on Windows shares if |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
632 # the file is open. |
13342
2dc7a2a96cfd
opener: use posixfile to hold file open when calling nlinks()
Adrian Buehlmann <adrian@cadifra.com>
parents:
13316
diff
changeset
|
633 fd = posixfile(f2) |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
634 return nlinks(f2) > 1 |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
635 finally: |
13204
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
636 if fd is not None: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
637 fd.close() |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
638 for f in (f1, f2): |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
639 try: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
640 os.unlink(f) |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
641 except OSError: |
5b83ab614dab
checknlink: use two testfiles (issue2543)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13188
diff
changeset
|
642 pass |
12938
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
643 |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
644 return False |
bf826c0b9537
opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents:
12927
diff
changeset
|
645 |
5843
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
646 def endswithsep(path): |
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
647 '''Check path ends with os.sep or os.altsep.''' |
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
648 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep) |
83c354c4d529
Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5802
diff
changeset
|
649 |
5844
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
650 def splitpath(path): |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
651 '''Split path by os.sep. |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
652 Note that this function does not use os.altsep because this is |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
653 an alternative of simple "xxx.split(os.sep)". |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
654 It is recommended to use os.path.normpath() before using this |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
655 function if need.''' |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
656 return path.split(os.sep) |
07d8eb78dd68
Add util.splitpath() and use it instead of using os.sep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
5843
diff
changeset
|
657 |
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
658 def gui(): |
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
659 '''Are we running in a GUI?''' |
13734
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
660 if sys.platform == 'darwin': |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
661 if 'SSH_CONNECTION' in os.environ: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
662 # handle SSH access to a box where the user is logged in |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
663 return False |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
664 elif getattr(osutil, 'isgui', None): |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
665 # check if a CoreGraphics session is available |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
666 return osutil.isgui() |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
667 else: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
668 # pure build; use a safe default |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
669 return True |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
670 else: |
16118b4859a1
util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13439
diff
changeset
|
671 return os.name == "nt" or os.environ.get("DISPLAY") |
6007
090b1a665901
filemerge: add config item for GUI tools
Matt Mackall <mpm@selenic.com>
parents:
6006
diff
changeset
|
672 |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
673 def mktempcopy(name, emptyok=False, createmode=None): |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
674 """Create a temporary file with the same contents from name |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
675 |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
676 The permission bits are copied from the original file. |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
677 |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
678 If the temporary file is going to be truncated immediately, you |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
679 can use emptyok=True as an optimization. |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
680 |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
681 Returns the name of the temporary file. |
2176
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
682 """ |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
683 d, fn = os.path.split(name) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
684 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
685 os.close(fd) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
686 # Temporary files are created with mode 0600, which is usually not |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
687 # what we want. If the original file already exists, just copy |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
688 # its mode. Otherwise, manually obey umask. |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
689 try: |
5740
9046a4f6a07c
atomictempfile: avoid chmod weirdness on Linux vfat
Matt Mackall <mpm@selenic.com>
parents:
5739
diff
changeset
|
690 st_mode = os.lstat(name).st_mode & 0777 |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
691 except OSError, inst: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
692 if inst.errno != errno.ENOENT: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
693 raise |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
694 st_mode = createmode |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
695 if st_mode is None: |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
7879
diff
changeset
|
696 st_mode = ~umask |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
697 st_mode &= 0666 |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
698 os.chmod(temp, st_mode) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
699 if emptyok: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
700 return temp |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
701 try: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
702 try: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
703 ifp = posixfile(name, "rb") |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
704 except IOError, inst: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
705 if inst.errno == errno.ENOENT: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
706 return temp |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
707 if not getattr(inst, 'filename', None): |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
708 inst.filename = name |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
709 raise |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
710 ofp = posixfile(temp, "wb") |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
711 for chunk in filechunkiter(ifp): |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
712 ofp.write(chunk) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
713 ifp.close() |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
714 ofp.close() |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
715 except: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
716 try: os.unlink(temp) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
717 except: pass |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
718 raise |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
719 return temp |
2176
9b42304d9896
fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2117
diff
changeset
|
720 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8631
diff
changeset
|
721 class atomictempfile(object): |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
722 """file-like object that atomically updates a file |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
723 |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
724 All writes will be redirected to a temporary copy of the original |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
725 file. When rename is called, the copy is renamed to the original |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
726 name, making the changes visible. |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
727 """ |
11212
4d8db9676171
util: give appropriate default args to atomictempfile()
Yuya Nishihara <yuya@tcha.org>
parents:
11011
diff
changeset
|
728 def __init__(self, name, mode='w+b', createmode=None): |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
729 self.__name = name |
8420
f53bc3e37655
util: make atomictempfile saner if mktempcopy fails
Bryan O'Sullivan <bos@serpentine.com>
parents:
8419
diff
changeset
|
730 self._fp = None |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
731 self.temp = mktempcopy(name, emptyok=('w' in mode), |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
732 createmode=createmode) |
8327
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
733 self._fp = posixfile(self.temp, mode) |
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
734 |
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
735 def __getattr__(self, name): |
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
736 return getattr(self._fp, name) |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
737 |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
738 def rename(self): |
8785
7a9151bc5b37
atomictempfile: fix exception in __del__ if mktempcopy fails (self._fp is None)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8778
diff
changeset
|
739 if not self._fp.closed: |
8327
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
740 self._fp.close() |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
741 rename(self.temp, localpath(self.__name)) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
742 |
13098
f7d6750dcd01
util: make atomicfiles closable
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13072
diff
changeset
|
743 def close(self): |
8785
7a9151bc5b37
atomictempfile: fix exception in __del__ if mktempcopy fails (self._fp is None)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8778
diff
changeset
|
744 if not self._fp: |
7a9151bc5b37
atomictempfile: fix exception in __del__ if mktempcopy fails (self._fp is None)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8778
diff
changeset
|
745 return |
7a9151bc5b37
atomictempfile: fix exception in __del__ if mktempcopy fails (self._fp is None)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8778
diff
changeset
|
746 if not self._fp.closed: |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
747 try: |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
748 os.unlink(self.temp) |
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
749 except: pass |
8327
aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
Bryan O'Sullivan <bos@serpentine.com>
parents:
8312
diff
changeset
|
750 self._fp.close() |
4827
89defeae88f3
turn util.opener into a class
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4803
diff
changeset
|
751 |
13098
f7d6750dcd01
util: make atomicfiles closable
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13072
diff
changeset
|
752 def __del__(self): |
f7d6750dcd01
util: make atomicfiles closable
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13072
diff
changeset
|
753 self.close() |
f7d6750dcd01
util: make atomicfiles closable
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
13072
diff
changeset
|
754 |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
755 def makedirs(name, mode=None): |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
756 """recursive directory creation with parent mode inheritance""" |
13068
adff480db558
makedirs: abort if parent == name (issue2531)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13063
diff
changeset
|
757 parent = os.path.abspath(os.path.dirname(name)) |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
758 try: |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
759 os.mkdir(name) |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
760 if mode is not None: |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
761 os.chmod(name, mode) |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
762 return |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
763 except OSError, err: |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
764 if err.errno == errno.EEXIST: |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
765 return |
13068
adff480db558
makedirs: abort if parent == name (issue2531)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13063
diff
changeset
|
766 if not name or parent == name or err.errno != errno.ENOENT: |
6062
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
767 raise |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
768 makedirs(parent, mode) |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
769 makedirs(name, mode) |
3c3b126e5619
Make files in .hg inherit the permissions from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
6007
diff
changeset
|
770 |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
771 class chunkbuffer(object): |
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
772 """Allow arbitrary sized chunks of data to be efficiently read from an |
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
773 iterator over chunks of arbitrary size.""" |
1200 | 774 |
5446
fa836e050c50
chunkbuffer: removed unused method and arg
Matt Mackall <mpm@selenic.com>
parents:
5420
diff
changeset
|
775 def __init__(self, in_iter): |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
776 """in_iter is the iterator that's iterating over the input chunks. |
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
777 targetsize is how big a buffer to try to maintain.""" |
11670
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
778 def splitbig(chunks): |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
779 for chunk in chunks: |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
780 if len(chunk) > 2**20: |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
781 pos = 0 |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
782 while pos < len(chunk): |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
783 end = pos + 2 ** 18 |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
784 yield chunk[pos:end] |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
785 pos = end |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
786 else: |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
787 yield chunk |
1b3b843e1100
chunkbuffer: split big strings directly in chunkbuffer
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
11668
diff
changeset
|
788 self.iter = splitbig(in_iter) |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
789 self._queue = [] |
1200 | 790 |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
791 def read(self, l): |
1200 | 792 """Read L bytes of data from the iterator of chunks of data. |
1308
2073e5a71008
Cleanup of tabs and trailing spaces.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1285
diff
changeset
|
793 Returns less than L bytes if the iterator runs dry.""" |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
794 left = l |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
795 buf = '' |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
796 queue = self._queue |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
797 while left > 0: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
798 # refill the queue |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
799 if not queue: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
800 target = 2**18 |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
801 for chunk in self.iter: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
802 queue.append(chunk) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
803 target -= len(chunk) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
804 if target <= 0: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
805 break |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
806 if not queue: |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
807 break |
11758
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
808 |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
809 chunk = queue.pop(0) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
810 left -= len(chunk) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
811 if left < 0: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
812 queue.insert(0, chunk[left:]) |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
813 buf += chunk[:left] |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
814 else: |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
815 buf += chunk |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
816 |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
817 return buf |
a79214972da2
chunkbuffer: use += rather than cStringIO to reduce memory footprint
Matt Mackall <mpm@selenic.com>
parents:
11469
diff
changeset
|
818 |
2462
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
819 def filechunkiter(f, size=65536, limit=None): |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
820 """Create a generator that produces the data in the file size |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
821 (default 65536) bytes at a time, up to optional limit (default is |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
822 to read all data). Chunks may be less than size bytes if the |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
823 chunk is the last chunk in the file, or the file is a socket or |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
824 some other type of file that sometimes reads less data than is |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
825 requested.""" |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
826 assert size >= 0 |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
827 assert limit is None or limit >= 0 |
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
828 while True: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
829 if limit is None: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
830 nbytes = size |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
831 else: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
832 nbytes = min(limit, size) |
2462
d610bcfd66a8
util: add limit to amount filechunkiter will read
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2448
diff
changeset
|
833 s = nbytes and f.read(nbytes) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
834 if not s: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
835 break |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
836 if limit: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
837 limit -= len(s) |
1199
78ceaf83f28f
Created a class in util called chunkbuffer that buffers reads from an
Eric Hopper <hopper@omnifarious.org>
parents:
1169
diff
changeset
|
838 yield s |
1320
5f277e73778f
Fix up representation of dates in hgweb.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1312
diff
changeset
|
839 |
1321
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
840 def makedate(): |
1482
4d38b85e60aa
fix handling of daylight saving time
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1479
diff
changeset
|
841 lt = time.localtime() |
4d38b85e60aa
fix handling of daylight saving time
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1479
diff
changeset
|
842 if lt[8] == 1 and time.daylight: |
4d38b85e60aa
fix handling of daylight saving time
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1479
diff
changeset
|
843 tz = time.altzone |
4d38b85e60aa
fix handling of daylight saving time
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1479
diff
changeset
|
844 else: |
4d38b85e60aa
fix handling of daylight saving time
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1479
diff
changeset
|
845 tz = time.timezone |
13063
e98581d44f0b
makedate: abort on negative timestamps (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13062
diff
changeset
|
846 t = time.mktime(lt) |
e98581d44f0b
makedate: abort on negative timestamps (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13062
diff
changeset
|
847 if t < 0: |
e98581d44f0b
makedate: abort on negative timestamps (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13062
diff
changeset
|
848 hint = _("check your clock") |
e98581d44f0b
makedate: abort on negative timestamps (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13062
diff
changeset
|
849 raise Abort(_("negative timestamp: %d") % t, hint=hint) |
e98581d44f0b
makedate: abort on negative timestamps (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13062
diff
changeset
|
850 return t, tz |
1329
8f06817bf266
Allow files to be opened in text mode, even on Windows.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1321
diff
changeset
|
851 |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
852 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): |
1321
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
853 """represent a (unixtime, offset) tuple as a localized time. |
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
854 unixtime is seconds since the epoch, and offset is the time zone's |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1976
diff
changeset
|
855 number of seconds away from UTC. if timezone is false, do not |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1976
diff
changeset
|
856 append time zone to string.""" |
1321
b47f96a178a3
Clean up date and timezone handling.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1320
diff
changeset
|
857 t, tz = date or makedate() |
13039
dd24f3e7ca9e
util.datestr: do not crash on revisions with negative timestamp (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13038
diff
changeset
|
858 if t < 0: |
dd24f3e7ca9e
util.datestr: do not crash on revisions with negative timestamp (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13038
diff
changeset
|
859 t = 0 # time.gmtime(lt) fails on Windows for lt < -43200 |
dd24f3e7ca9e
util.datestr: do not crash on revisions with negative timestamp (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13038
diff
changeset
|
860 tz = 0 |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
861 if "%1" in format or "%2" in format: |
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
862 sign = (tz > 0) and "-" or "+" |
9029
0001e49f1c11
compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents:
8938
diff
changeset
|
863 minutes = abs(tz) // 60 |
0001e49f1c11
compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents:
8938
diff
changeset
|
864 format = format.replace("%1", "%c%02d" % (sign, minutes // 60)) |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
865 format = format.replace("%2", "%02d" % (minutes % 60)) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1976
diff
changeset
|
866 s = time.strftime(format, time.gmtime(float(t) - tz)) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1976
diff
changeset
|
867 return s |
1829
b0f6af327fd4
hgwebdir: export collections of repos
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
868 |
6134
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6111
diff
changeset
|
869 def shortdate(date=None): |
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6111
diff
changeset
|
870 """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
871 return datestr(date, format='%Y-%m-%d') |
6134
7b937b26adf7
Make annotae/grep print short dates with -q/--quiet.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6111
diff
changeset
|
872 |
5357
c6adf2be6069
util: add default argument to strdate
Bryan O'Sullivan <bos@serpentine.com>
parents:
5293
diff
changeset
|
873 def strdate(string, format, defaults=[]): |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
874 """parse a localized time string and return a (unixtime, offset) tuple. |
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
875 if the string cannot be parsed, ValueError is raised.""" |
3809
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
876 def timezone(string): |
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
877 tz = string.split()[-1] |
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
878 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): |
6229
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
879 sign = (tz[0] == "+") and 1 or -1 |
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
880 hours = int(tz[1:3]) |
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
881 minutes = int(tz[3:5]) |
c3182eeb70ea
dates: improve timezone handling
Matt Mackall <mpm@selenic.com>
parents:
6224
diff
changeset
|
882 return -sign * (hours * 60 + minutes) * 60 |
12401
4cdaf1adafc8
backout most of 4f8067c94729
Matt Mackall <mpm@selenic.com>
parents:
12387
diff
changeset
|
883 if tz == "GMT" or tz == "UTC": |
3809
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
884 return 0 |
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
885 return None |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
886 |
3255
e96d2956eb4a
util.strdate: compute timestamp using UTC, not local timezone
Jose M. Prieto <jmprieto@gmx.net>
parents:
3176
diff
changeset
|
887 # NOTE: unixtime = localunixtime + offset |
3809
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
888 offset, date = timezone(string), string |
13031
3da456d0c885
code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents:
12957
diff
changeset
|
889 if offset is not None: |
3809
4d93b37b5963
parsedate: add UTC and GMT timezones
Matt Mackall <mpm@selenic.com>
parents:
3808
diff
changeset
|
890 date = " ".join(string.split()[:-1]) |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
891 |
3812 | 892 # add missing elements from defaults |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
893 usenow = False # default to using biased defaults |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
894 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
3812 | 895 found = [True for p in part if ("%"+p) in format] |
896 if not found: | |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
897 date += "@" + defaults[part][usenow] |
3812 | 898 format += "@%" + part[0] |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
899 else: |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
900 # We've found a specific time element, less specific time |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
901 # elements are relative to today |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
902 usenow = True |
3808
d6529582942a
improve date parsing for numerous new date formats
Matt Mackall <mpm@selenic.com>
parents:
3807
diff
changeset
|
903 |
3256
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
904 timetuple = time.strptime(date, format) |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
905 localunixtime = int(calendar.timegm(timetuple)) |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
906 if offset is None: |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
907 # local timezone |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
908 unixtime = int(time.mktime(timetuple)) |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
909 offset = unixtime - localunixtime |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
910 else: |
e5c9a084ffe3
util.strdate: assume local time when no timezone specified
Jose M. Prieto <jmprieto@gmx.net>
parents:
3255
diff
changeset
|
911 unixtime = localunixtime + offset |
3255
e96d2956eb4a
util.strdate: compute timestamp using UTC, not local timezone
Jose M. Prieto <jmprieto@gmx.net>
parents:
3176
diff
changeset
|
912 return unixtime, offset |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
913 |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
914 def parsedate(date, formats=None, bias={}): |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
915 """parse a localized date/time and return a (unixtime, offset) tuple. |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
916 |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
917 The date may be a "unixtime offset" string or in one of the specified |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
918 formats. If the date already is a (unixtime, offset) tuple, it is returned. |
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
919 """ |
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
920 if not date: |
3807
e43b48f0f718
parsedate: allow '' for epoch
Matt Mackall <mpm@selenic.com>
parents:
3806
diff
changeset
|
921 return 0, 0 |
6230
c7253d1a774e
dates: Fix bare times to be relative to "today"
Matt Mackall <mpm@selenic.com>
parents:
6229
diff
changeset
|
922 if isinstance(date, tuple) and len(date) == 2: |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
923 return date |
2609
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
924 if not formats: |
6c5b1b5cc160
util.parsedate should understand dates from hg export
Chris Mason <mason@suse.com>
parents:
2601
diff
changeset
|
925 formats = defaultdateformats |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
926 date = date.strip() |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
927 try: |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
928 when, offset = map(int, date.split(' ')) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
929 except ValueError: |
3812 | 930 # fill out defaults |
931 now = makedate() | |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
932 defaults = {} |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
933 nowmap = {} |
13200
6f011cf52f9a
avoid .split() in for loops and use tuples instead
David Soria Parra <dsp@php.net>
parents:
13197
diff
changeset
|
934 for part in ("d", "mb", "yY", "HI", "M", "S"): |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
935 # this piece is for rounding the specific end of unknowns |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
936 b = bias.get(part) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
937 if b is None: |
3812 | 938 if part[0] in "HMS": |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
939 b = "00" |
3812 | 940 else: |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
941 b = "0" |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
942 |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
943 # this piece is for matching the generic end to today's date |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
944 n = datestr(now, "%" + part[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
945 |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
946 defaults[part] = (b, n) |
3812 | 947 |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
948 for format in formats: |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
949 try: |
6139
989467e8e3a9
Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
6135
diff
changeset
|
950 when, offset = strdate(date, format, defaults) |
6087
12856a1742dc
better handle errors with date parsing (issue983)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5917
diff
changeset
|
951 except (ValueError, OverflowError): |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
952 pass |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
953 else: |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
954 break |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
955 else: |
12105
6f58430dfdd0
util: get rid of extra trailing whitespace in parsedate abort message
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
12088
diff
changeset
|
956 raise Abort(_('invalid date: %r') % date) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
957 # validate explicit (probably user-specified) date and |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
958 # time zone offset. values must fit in signed 32 bits for |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
959 # current 32-bit linux runtimes. timezones go from UTC-12 |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
960 # to UTC+14 |
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
961 if abs(when) > 0x7fffffff: |
3806
92a3532a01d9
parsedate: use Abort rather than ValueError
Matt Mackall <mpm@selenic.com>
parents:
3784
diff
changeset
|
962 raise Abort(_('date exceeds 32 bits: %d') % when) |
13062
e1002cf9fe54
parsedate: abort on negative dates (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13053
diff
changeset
|
963 if when < 0: |
e1002cf9fe54
parsedate: abort on negative dates (issue2513)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13053
diff
changeset
|
964 raise Abort(_('negative date value: %d') % when) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
965 if offset < -50400 or offset > 43200: |
3806
92a3532a01d9
parsedate: use Abort rather than ValueError
Matt Mackall <mpm@selenic.com>
parents:
3784
diff
changeset
|
966 raise Abort(_('impossible time zone offset: %d') % offset) |
2523
4ab59a3acd16
validate the resulting date in parsedate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2522
diff
changeset
|
967 return when, offset |
2522
85f796baab10
Allow the use of human readable dates (issue 251)
Jose M. Prieto <jmprieto@gmx.net>
parents:
2480
diff
changeset
|
968 |
3812 | 969 def matchdate(date): |
970 """Return a function that matches a given date match specifier | |
971 | |
972 Formats include: | |
973 | |
974 '{date}' match a given date to the accuracy provided | |
975 | |
976 '<{date}' on or before a given date | |
977 | |
978 '>{date}' on or after a given date | |
979 | |
13212
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
980 >>> p1 = parsedate("10:29:59") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
981 >>> p2 = parsedate("10:30:00") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
982 >>> p3 = parsedate("10:30:59") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
983 >>> p4 = parsedate("10:31:00") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
984 >>> p5 = parsedate("Sep 15 10:30:00 1999") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
985 >>> f = matchdate("10:30") |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
986 >>> f(p1[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
987 False |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
988 >>> f(p2[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
989 True |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
990 >>> f(p3[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
991 True |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
992 >>> f(p4[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
993 False |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
994 >>> f(p5[0]) |
5d0a30fad7de
date: fix matching of underspecified date ranges
Matt Mackall <mpm@selenic.com>
parents:
13204
diff
changeset
|
995 False |
3812 | 996 """ |
997 | |
998 def lower(date): | |
6230
c7253d1a774e
dates: Fix bare times to be relative to "today"
Matt Mackall <mpm@selenic.com>
parents:
6229
diff
changeset
|
999 d = dict(mb="1", d="1") |
c7253d1a774e
dates: Fix bare times to be relative to "today"
Matt Mackall <mpm@selenic.com>
parents:
6229
diff
changeset
|
1000 return parsedate(date, extendeddateformats, d)[0] |
3812 | 1001 |
1002 def upper(date): | |
1003 d = dict(mb="12", HI="23", M="59", S="59") | |
13200
6f011cf52f9a
avoid .split() in for loops and use tuples instead
David Soria Parra <dsp@php.net>
parents:
13197
diff
changeset
|
1004 for days in ("31", "30", "29"): |
3812 | 1005 try: |
1006 d["d"] = days | |
1007 return parsedate(date, extendeddateformats, d)[0] | |
1008 except: | |
1009 pass | |
1010 d["d"] = "28" | |
1011 return parsedate(date, extendeddateformats, d)[0] | |
1012 | |
7953
8c6f823efcc9
Correct a bug on date formats with '>' or '<' accompanied by space characters.
Justin Peng <justin.peng.sw@gmail.com>
parents:
7948
diff
changeset
|
1013 date = date.strip() |
13780
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1014 |
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1015 if not date: |
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1016 raise Abort(_("dates cannot consist entirely of whitespace")) |
bc7b5d1c1999
util: dates cannot consist entirely of whitespace (issue2732)
Idan Kamara <idankk86@gmail.com>
parents:
13734
diff
changeset
|
1017 elif date[0] == "<": |
13869
b470894c33f8
date: fixup breakage from ">" fix
Matt Mackall <mpm@selenic.com>
parents:
13867
diff
changeset
|
1018 if not date[1:]: |
13886
fe48c57390f2
help/dates: use DATE as place-holder in help and abort texts
Martin Geisler <mg@aragost.com>
parents:
13879
diff
changeset
|
1019 raise Abort(_("invalid day spec, use '<DATE'")) |
3812 | 1020 when = upper(date[1:]) |
1021 return lambda x: x <= when | |
1022 elif date[0] == ">": | |
13869
b470894c33f8
date: fixup breakage from ">" fix
Matt Mackall <mpm@selenic.com>
parents:
13867
diff
changeset
|
1023 if not date[1:]: |
13886
fe48c57390f2
help/dates: use DATE as place-holder in help and abort texts
Martin Geisler <mg@aragost.com>
parents:
13879
diff
changeset
|
1024 raise Abort(_("invalid day spec, use '>DATE'")) |
3812 | 1025 when = lower(date[1:]) |
1026 return lambda x: x >= when | |
1027 elif date[0] == "-": | |
1028 try: | |
1029 days = int(date[1:]) | |
1030 except ValueError: | |
1031 raise Abort(_("invalid day spec: %s") % date[1:]) | |
13889
9a96efc4af8a
util: make 'hg log -d --2' abort (issue2734)
Yun Lee <yunlee.bj@gmail.com>
parents:
13886
diff
changeset
|
1032 if days < 0: |
9a96efc4af8a
util: make 'hg log -d --2' abort (issue2734)
Yun Lee <yunlee.bj@gmail.com>
parents:
13886
diff
changeset
|
1033 raise Abort(_("%s must be nonnegative (see 'hg help dates')") |
9a96efc4af8a
util: make 'hg log -d --2' abort (issue2734)
Yun Lee <yunlee.bj@gmail.com>
parents:
13886
diff
changeset
|
1034 % date[1:]) |
3812 | 1035 when = makedate()[0] - days * 3600 * 24 |
3813 | 1036 return lambda x: x >= when |
3812 | 1037 elif " to " in date: |
1038 a, b = date.split(" to ") | |
1039 start, stop = lower(a), upper(b) | |
1040 return lambda x: x >= start and x <= stop | |
1041 else: | |
1042 start, stop = lower(date), upper(date) | |
1043 return lambda x: x >= start and x <= stop | |
1044 | |
1903
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1045 def shortuser(user): |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1046 """Return a short representation of a user name or email address.""" |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1047 f = user.find('@') |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1048 if f >= 0: |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1049 user = user[:f] |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1050 f = user.find('<') |
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1051 if f >= 0: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1052 user = user[f + 1:] |
3176
7492b33bdd9f
shortuser should stop before the first space character.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3147
diff
changeset
|
1053 f = user.find(' ') |
7492b33bdd9f
shortuser should stop before the first space character.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3147
diff
changeset
|
1054 if f >= 0: |
7492b33bdd9f
shortuser should stop before the first space character.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3147
diff
changeset
|
1055 user = user[:f] |
3533
bb44489b901f
shortname: truncate at '.' too
Matt Mackall <mpm@selenic.com>
parents:
3466
diff
changeset
|
1056 f = user.find('.') |
bb44489b901f
shortname: truncate at '.' too
Matt Mackall <mpm@selenic.com>
parents:
3466
diff
changeset
|
1057 if f >= 0: |
bb44489b901f
shortname: truncate at '.' too
Matt Mackall <mpm@selenic.com>
parents:
3466
diff
changeset
|
1058 user = user[:f] |
1903
e4abeafd6eb1
move shortuser into util module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1635
diff
changeset
|
1059 return user |
1920 | 1060 |
5975
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1061 def email(author): |
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1062 '''get email of author.''' |
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1063 r = author.find('>') |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1064 if r == -1: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1065 r = None |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1066 return author[author.find('<') + 1:r] |
5975
75d9fe70c654
templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents:
5949
diff
changeset
|
1067 |
13225
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1068 def _ellipsis(text, maxlength): |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1069 if len(text) <= maxlength: |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1070 return text, False |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1071 else: |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1072 return "%s..." % (text[:maxlength - 3]), True |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1073 |
3767
1861fa38a6a7
Move ellipsis code to util.ellipsis() and improve maxlength handling.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3721
diff
changeset
|
1074 def ellipsis(text, maxlength=400): |
1861fa38a6a7
Move ellipsis code to util.ellipsis() and improve maxlength handling.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3721
diff
changeset
|
1075 """Trim string to at most maxlength (default: 400) characters.""" |
13225
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1076 try: |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1077 # use unicode not to split at intermediate multi-byte sequence |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1078 utext, truncated = _ellipsis(text.decode(encoding.encoding), |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1079 maxlength) |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1080 if not truncated: |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1081 return text |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1082 return utext.encode(encoding.encoding) |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1083 except (UnicodeDecodeError, UnicodeEncodeError): |
e3bf16703e26
util: fix ellipsis() not to break multi-byte sequence (issue2564)
Yuya Nishihara <yuya@tcha.org>
parents:
13212
diff
changeset
|
1084 return _ellipsis(text, maxlength)[0] |
3767
1861fa38a6a7
Move ellipsis code to util.ellipsis() and improve maxlength handling.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3721
diff
changeset
|
1085 |
4097
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1086 def os_rcpath(): |
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1087 '''return default os-specific hgrc search path''' |
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1088 path = system_rcpath() |
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1089 path.extend(user_rcpath()) |
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1090 path = [os.path.normpath(f) for f in path] |
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1091 return path |
403c4ddd74bb
Combined the two os_rcpath methods into a single one near rcpath in mercurial/util.py
Shane Holloway <shane.holloway@ieee.org>
parents:
4096
diff
changeset
|
1092 |
2612
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1093 def bytecount(nbytes): |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1094 '''return byte count formatted as readable string, with units''' |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1095 |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1096 units = ( |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1097 (100, 1 << 30, _('%.0f GB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1098 (10, 1 << 30, _('%.1f GB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1099 (1, 1 << 30, _('%.2f GB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1100 (100, 1 << 20, _('%.0f MB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1101 (10, 1 << 20, _('%.1f MB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1102 (1, 1 << 20, _('%.2f MB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1103 (100, 1 << 10, _('%.0f KB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1104 (10, 1 << 10, _('%.1f KB')), |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
1105 (1, 1 << 10, _('%.2f KB')), |
2612
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1106 (1, 1, _('%.0f bytes')), |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1107 ) |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1108 |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1109 for multiplier, divisor, format in units: |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1110 if nbytes >= divisor * multiplier: |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1111 return format % (nbytes / float(divisor)) |
ffb895f16925
add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2609
diff
changeset
|
1112 return units[-1][2] % nbytes |
2740
386f04d6ecb3
clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2655
diff
changeset
|
1113 |
5291
23651848d638
extdiff: avoid repr() doubling paths backslashes under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
5213
diff
changeset
|
1114 def uirepr(s): |
23651848d638
extdiff: avoid repr() doubling paths backslashes under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
5213
diff
changeset
|
1115 # Avoid double backslash in Windows path repr() |
23651848d638
extdiff: avoid repr() doubling paths backslashes under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
5213
diff
changeset
|
1116 return repr(s).replace('\\\\', '\\') |
7547
4949729ee9ee
python implementation of diffstat
Alexander Solovyov <piranha@piranha.org.ua>
parents:
7537
diff
changeset
|
1117 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1118 # delay import of textwrap |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1119 def MBTextWrapper(**kwargs): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1120 class tw(textwrap.TextWrapper): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1121 """ |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1122 Extend TextWrapper for double-width characters. |
12957
9f2ac318b92e
util: clarify purpose of MBTextWrapper class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
12938
diff
changeset
|
1123 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1124 Some Asian characters use two terminal columns instead of one. |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1125 A good example of this behavior can be seen with u'\u65e5\u672c', |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1126 the two Japanese characters for "Japan": |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1127 len() returns 2, but when printed to a terminal, they eat 4 columns. |
12957
9f2ac318b92e
util: clarify purpose of MBTextWrapper class
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
12938
diff
changeset
|
1128 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1129 (Note that this has nothing to do whatsoever with unicode |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1130 representation, or encoding of the underlying string) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1131 """ |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1132 def __init__(self, **kwargs): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1133 textwrap.TextWrapper.__init__(self, **kwargs) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1134 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1135 def _cutdown(self, str, space_left): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1136 l = 0 |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1137 ucstr = unicode(str, encoding.encoding) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1138 colwidth = unicodedata.east_asian_width |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1139 for i in xrange(len(ucstr)): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1140 l += colwidth(ucstr[i]) in 'WFA' and 2 or 1 |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1141 if space_left < l: |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1142 return (ucstr[:i].encode(encoding.encoding), |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1143 ucstr[i:].encode(encoding.encoding)) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1144 return str, '' |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1145 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1146 # overriding of base class |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1147 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1148 space_left = max(width - cur_len, 1) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1149 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1150 if self.break_long_words: |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1151 cut, res = self._cutdown(reversed_chunks[-1], space_left) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1152 cur_line.append(cut) |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1153 reversed_chunks[-1] = res |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1154 elif not cur_line: |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1155 cur_line.append(reversed_chunks.pop()) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1156 |
13316
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1157 global MBTextWrapper |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1158 MBTextWrapper = tw |
d119403fd266
util: delay loading of textwrap
Matt Mackall <mpm@selenic.com>
parents:
13313
diff
changeset
|
1159 return tw(**kwargs) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1160 |
12698
7aef77e74cf3
util: make wrap() require a width argument
Matt Mackall <mpm@selenic.com>
parents:
12689
diff
changeset
|
1161 def wrap(line, width, initindent='', hangindent=''): |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1162 maxindent = max(len(hangindent), len(initindent)) |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1163 if width <= maxindent: |
9417
4c3fb45123e5
util, minirst: do not crash with COLUMNS=0
Martin Geisler <mg@lazybytes.net>
parents:
9397
diff
changeset
|
1164 # adjust for weird terminal size |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1165 width = max(78, maxindent + 1) |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1166 wrapper = MBTextWrapper(width=width, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1167 initial_indent=initindent, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1168 subsequent_indent=hangindent) |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11256
diff
changeset
|
1169 return wrapper.fill(line) |
8938
9b8c9266c59d
commands: wrap short descriptions in 'hg help'
Martin Geisler <mg@lazybytes.net>
parents:
8785
diff
changeset
|
1170 |
7879
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
1171 def iterlines(iterator): |
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
1172 for chunk in iterator: |
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
1173 for line in chunk.splitlines(): |
5c4026a289a4
templater: ability to display diffstat for log-like commands
Alexander Solovyov <piranha at piranha.org.ua>
parents:
7875
diff
changeset
|
1174 yield line |
9610
d78fe60f6bda
make path expanding more consistent
Alexander Solovyov <piranha@piranha.org.ua>
parents:
9569
diff
changeset
|
1175 |
d78fe60f6bda
make path expanding more consistent
Alexander Solovyov <piranha@piranha.org.ua>
parents:
9569
diff
changeset
|
1176 def expandpath(path): |
d78fe60f6bda
make path expanding more consistent
Alexander Solovyov <piranha@piranha.org.ua>
parents:
9569
diff
changeset
|
1177 return os.path.expanduser(os.path.expandvars(path)) |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1178 |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1179 def hgcmd(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1180 """Return the command used to execute current hg |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1181 |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1182 This is different from hgexecutable() because on Windows we want |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1183 to avoid things opening new shell windows like batch files, so we |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1184 get either the python call or current executable. |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1185 """ |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1186 if main_is_frozen(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1187 return [sys.executable] |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10199
diff
changeset
|
1188 return gethgcmd() |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1189 |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1190 def rundetached(args, condfn): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1191 """Execute the argument list in a detached process. |
10422
600142e7a028
util: fix trailing whitespace found by check-code
Augie Fackler <durin42@gmail.com>
parents:
10344
diff
changeset
|
1192 |
10344
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1193 condfn is a callable which is called repeatedly and should return |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1194 True once the child process is known to have started successfully. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1195 At this point, the child process PID is returned. If the child |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1196 process fails to start or finishes before condfn() evaluates to |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1197 True, return -1. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1198 """ |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1199 # Windows case is easier because the child process is either |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1200 # successfully starting and validating the condition or exiting |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1201 # on failure. We just poll on its PID. On Unix, if the child |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1202 # process fails to start, it will be left in a zombie state until |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1203 # the parent wait on it, which we cannot do since we expect a long |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1204 # running process on success. Instead we listen for SIGCHLD telling |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1205 # us our child process terminated. |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1206 terminated = set() |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1207 def handler(signum, frame): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1208 terminated.add(os.wait()) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1209 prevhandler = None |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1210 if hasattr(signal, 'SIGCHLD'): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1211 prevhandler = signal.signal(signal.SIGCHLD, handler) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1212 try: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1213 pid = spawndetached(args) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1214 while not condfn(): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1215 if ((pid in terminated or not testpid(pid)) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1216 and not condfn()): |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1217 return -1 |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1218 time.sleep(0.1) |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1219 return pid |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1220 finally: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1221 if prevhandler is not None: |
9501cde4c034
util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents:
10282
diff
changeset
|
1222 signal.signal(signal.SIGCHLD, prevhandler) |
10438
e6dc44147234
util: add any() and all() functions for Python 2.4 compatibility
Steve Losh <steve@stevelosh.com>
parents:
10422
diff
changeset
|
1223 |
10487
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1224 try: |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1225 any, all = any, all |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1226 except NameError: |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1227 def any(iterable): |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1228 for i in iterable: |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1229 if i: |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1230 return True |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1231 return False |
10438
e6dc44147234
util: add any() and all() functions for Python 2.4 compatibility
Steve Losh <steve@stevelosh.com>
parents:
10422
diff
changeset
|
1232 |
10487
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1233 def all(iterable): |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1234 for i in iterable: |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1235 if not i: |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1236 return False |
7a6b5f85c3ab
util: use the built-in any() and all() methods if they are available
Steve Losh <steve@stevelosh.com>
parents:
10438
diff
changeset
|
1237 return True |
11010
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10901
diff
changeset
|
1238 |
13392
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1239 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
11988
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1240 """Return the result of interpolating items in the mapping into string s. |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1241 |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1242 prefix is a single character string, or a two character string with |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1243 a backslash as the first character if the prefix needs to be escaped in |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1244 a regular expression. |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1245 |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1246 fn is an optional function that will be applied to the replacement text |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1247 just before replacement. |
13392
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1248 |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1249 escape_prefix is an optional flag that allows using doubled prefix for |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1250 its escaping. |
11988
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1251 """ |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1252 fn = fn or (lambda s: s) |
13392
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1253 patterns = '|'.join(mapping.keys()) |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1254 if escape_prefix: |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1255 patterns += '|' + prefix |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1256 if len(prefix) > 1: |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1257 prefix_char = prefix[1:] |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1258 else: |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1259 prefix_char = prefix |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1260 mapping[prefix_char] = prefix_char |
777cef34a890
dispatch: support for $ escaping in shell-alias definition
Roman Sokolov <sokolov.r.v@gmail.com>
parents:
13375
diff
changeset
|
1261 r = re.compile(r'%s(%s)' % (prefix, patterns)) |
11988
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1262 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
8380ed691df8
util: add an interpolate() function to for replacing multiple values
Steve Losh <steve@stevelosh.com>
parents:
11946
diff
changeset
|
1263 |
12076
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1264 def getport(port): |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1265 """Return the port for a given network service. |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1266 |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1267 If port is an integer, it's returned as is. If it's a string, it's |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1268 looked up using socket.getservbyname(). If there's no matching |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1269 service, util.Abort is raised. |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1270 """ |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1271 try: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1272 return int(port) |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1273 except ValueError: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1274 pass |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1275 |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1276 try: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1277 return socket.getservbyname(port) |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1278 except socket.error: |
49463314c24f
mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents:
12054
diff
changeset
|
1279 raise Abort(_("no port number associated with service '%s'") % port) |
12087
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1280 |
12088
1f71dffabc53
parsebool: accept always as true and never as false
Augie Fackler <durin42@gmail.com>
parents:
12087
diff
changeset
|
1281 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, |
1f71dffabc53
parsebool: accept always as true and never as false
Augie Fackler <durin42@gmail.com>
parents:
12087
diff
changeset
|
1282 '0': False, 'no': False, 'false': False, 'off': False, |
1f71dffabc53
parsebool: accept always as true and never as false
Augie Fackler <durin42@gmail.com>
parents:
12087
diff
changeset
|
1283 'never': False} |
12087
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1284 |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1285 def parsebool(s): |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1286 """Parse s into a boolean. |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1287 |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1288 If s is not a valid boolean, returns None. |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1289 """ |
a88a4720c2f0
parsebool: create new function and use it for config parsing
Augie Fackler <durin42@gmail.com>
parents:
12086
diff
changeset
|
1290 return _booleans.get(s.lower(), None) |