Mercurial > hg-stable
annotate mercurial/windows.py @ 13280:6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
windows: factor it out of rename
posix: add alias 'unlink' for os.unlink
Note that this new unlink function now has different semantics than the
unlink() we had before changeset 6bf39d88c857 ("rename util.unlink to
unlinkpath").
author | Adrian Buehlmann <adrian@cadifra.com> |
---|---|
date | Tue, 11 Jan 2011 14:10:16 +0100 |
parents | e9a52ed28157 |
children | f1fa8f481c7c |
rev | line source |
---|---|
8226
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
1 # windows.py - Windows utility function implementations for Mercurial |
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-2009 Matt Mackall <mpm@selenic.com> and others |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
4 # |
8b2cd04a6e97
put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents:
8225
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
7914
48661519913f
windows: add various missing import
Steve Borho <steve@borho.org>
parents:
7913
diff
changeset
|
8 from i18n import _ |
8312
b87a50b7125c
separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents:
8226
diff
changeset
|
9 import osutil, error |
10237
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
10 import errno, msvcrt, os, re, sys, random, subprocess |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
11 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 nulldev = 'NUL:' |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
13 umask = 002 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
15 # wrap osutil.posixfile to provide friendlier exceptions |
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
16 def posixfile(name, mode='r', buffering=-1): |
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
17 try: |
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
18 return osutil.posixfile(name, mode, buffering) |
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
19 except WindowsError, err: |
9448
bc6b0fef9495
windows: provide filename in IOError exceptions
Steve Borho <steve@borho.org>
parents:
9174
diff
changeset
|
20 raise IOError(err.errno, '%s: %s' % (name, err.strerror)) |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
21 posixfile.__doc__ = osutil.posixfile.__doc__ |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 |
8778
c5f36402daad
use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8761
diff
changeset
|
23 class winstdout(object): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 '''stdout on windows misbehaves if sent through a pipe''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 def __init__(self, fp): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 self.fp = fp |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 def __getattr__(self, key): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 return getattr(self.fp, key) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
32 def close(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
33 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
34 self.fp.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
35 except: pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 def write(self, s): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 # This is workaround for "Not enough space" error on |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 # writing large size of data to console. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 limit = 16000 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 l = len(s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 start = 0 |
10394
4612cded5176
fix coding style (reported by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10282
diff
changeset
|
44 self.softspace = 0 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 while start < l: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 end = start + limit |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 self.fp.write(s[start:end]) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 start = end |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 except IOError, inst: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
50 if inst.errno != 0: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
51 raise |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 self.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 raise IOError(errno.EPIPE, 'Broken pipe') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 def flush(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 return self.fp.flush() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
58 except IOError, inst: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
59 if inst.errno != errno.EINVAL: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
60 raise |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 self.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 raise IOError(errno.EPIPE, 'Broken pipe') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 sys.stdout = winstdout(sys.stdout) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 def _is_win_9x(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 '''return true if run on windows 95, 98 or me.''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 return sys.getwindowsversion()[3] == 1 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 except AttributeError: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 return 'command' in os.environ.get('comspec', '') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 def openhardlinks(): |
8559
4429751f5da7
windows: make openhardlinks work
Henrik Stuart <hg@hstuart.dk>
parents:
8421
diff
changeset
|
74 return not _is_win_9x() and "win32api" in globals() |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 def system_rcpath(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
77 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
78 return system_rcpath_win32() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 except: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
80 return [r'c:\mercurial\mercurial.ini'] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 def user_rcpath(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
83 '''return os-specific hgrc search path to the user dir''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 path = user_rcpath_win32() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
86 except: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
87 home = os.path.expanduser('~') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 path = [os.path.join(home, 'mercurial.ini'), |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 os.path.join(home, '.hgrc')] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
90 userprofile = os.environ.get('USERPROFILE') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
91 if userprofile: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
92 path.append(os.path.join(userprofile, 'mercurial.ini')) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 path.append(os.path.join(userprofile, '.hgrc')) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
94 return path |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
95 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
96 def parse_patch_output(output_line): |
8761
0289f384e1e5
Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents:
8657
diff
changeset
|
97 """parses the output produced by patch and returns the filename""" |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
98 pf = output_line[14:] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
99 if pf[0] == '`': |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
100 pf = pf[1:-1] # Remove the quotes |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
101 return pf |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
102 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
103 def sshargs(sshcmd, host, user, port): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
104 '''Build argument list for ssh or Plink''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
105 pflag = 'plink' in sshcmd.lower() and '-P' or '-p' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
106 args = user and ("%s@%s" % (user, host)) or host |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
107 return port and ("%s %s %s" % (args, pflag, port)) or args |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
108 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
109 def testpid(pid): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
110 '''return False if pid dead, True if running or not known''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
111 return True |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
112 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
113 def set_flags(f, l, x): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
114 pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
115 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
116 def set_binary(fd): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
117 # When run without console, pipes may expose invalid |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
118 # fileno(), usually set to -1. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
119 if hasattr(fd, 'fileno') and fd.fileno() >= 0: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
120 msvcrt.setmode(fd.fileno(), os.O_BINARY) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
121 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
122 def pconvert(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
123 return '/'.join(path.split(os.sep)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
124 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
125 def localpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
126 return path.replace('/', '\\') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
127 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
128 def normpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
129 return pconvert(os.path.normpath(path)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
130 |
9238
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
131 def realpath(path): |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
132 ''' |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
133 Returns the true, canonical file system path equivalent to the given |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
134 path. |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
135 ''' |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
136 # TODO: There may be a more clever way to do this that also handles other, |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
137 # less common file systems. |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
138 return os.path.normpath(os.path.normcase(os.path.realpath(path))) |
40196d036a71
util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
9174
diff
changeset
|
139 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
140 def samestat(s1, s2): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
141 return False |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
142 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
143 # A sequence of backslashes is special iff it precedes a double quote: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
144 # - if there's an even number of backslashes, the double quote is not |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
145 # quoted (i.e. it ends the quoted region) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
146 # - if there's an odd number of backslashes, the double quote is quoted |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
147 # - in both cases, every pair of backslashes is unquoted into a single |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
148 # backslash |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
149 # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx ) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
150 # So, to quote a string, we must surround it in double quotes, double |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
151 # the number of backslashes that preceed double quotes and add another |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
152 # backslash before every double quote (being careful with the double |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
153 # quote we've appended to the end) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
154 _quotere = None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
155 def shellquote(s): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
156 global _quotere |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
157 if _quotere is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
158 _quotere = re.compile(r'(\\*)("|\\$)') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
159 return '"%s"' % _quotere.sub(r'\1\1\\\2', s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
160 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
161 def quotecommand(cmd): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
162 """Build a command string suitable for os.popen* calls.""" |
13188
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
163 if sys.version_info < (2, 7, 1): |
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
164 # Python versions since 2.7.1 do this extra quoting themselves |
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
165 return '"' + cmd + '"' |
6c9345f9edca
util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents:
12689
diff
changeset
|
166 return cmd |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
167 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
168 def popen(command, mode='r'): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
169 # Work around "popen spawned process may not write to stdout |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
170 # under windows" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
171 # http://bugs.python.org/issue1366 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
172 command += " 2> %s" % nulldev |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
173 return os.popen(quotecommand(command), mode) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
174 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
175 def explain_exit(code): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
176 return _("exited with status %d") % code, code |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
177 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
178 # if you change this stub into a real check, please try to implement the |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
179 # username and groupname functions above, too. |
8657
3fa92c618624
posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents:
8614
diff
changeset
|
180 def isowner(st): |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
181 return True |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
182 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
183 def find_exe(command): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
184 '''Find executable for command searching like cmd.exe does. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
185 If command is a basename then PATH is searched for command. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
186 PATH isn't searched if command is an absolute or relative path. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
187 An extension from PATHEXT is found and added if not present. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
188 If command isn't found None is returned.''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
189 pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
190 pathexts = [ext for ext in pathext.lower().split(os.pathsep)] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
191 if os.path.splitext(command)[1].lower() in pathexts: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
192 pathexts = [''] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
193 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
194 def findexisting(pathcommand): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
195 'Will append extension (if needed) and return existing file' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
196 for ext in pathexts: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
197 executable = pathcommand + ext |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
198 if os.path.exists(executable): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
199 return executable |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
200 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
201 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
202 if os.sep in command: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
203 return findexisting(command) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
204 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
205 for path in os.environ.get('PATH', '').split(os.pathsep): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
206 executable = findexisting(os.path.join(path, command)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
207 if executable is not None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
208 return executable |
10156
c31ac3f7fd8f
windows: expand environment vars in find_exe
Steve Borho <steve@borho.org>
parents:
9594
diff
changeset
|
209 return findexisting(os.path.expanduser(os.path.expandvars(command))) |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
210 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
211 def set_signal_handler(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
212 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
213 set_signal_handler_win32() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
214 except NameError: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
215 pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
216 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
217 def statfiles(files): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
218 '''Stat each file in files and yield stat or None if file does not exist. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
219 Cluster and cache stat per directory to minimize number of OS stat calls.''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
220 ncase = os.path.normcase |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
221 dircache = {} # dirname -> filename -> status | None if file does not exist |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
222 for nf in files: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
223 nf = ncase(nf) |
9099
3d456bf32f18
Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
8951
diff
changeset
|
224 dir, base = os.path.split(nf) |
3d456bf32f18
Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
8951
diff
changeset
|
225 if not dir: |
3d456bf32f18
Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents:
8951
diff
changeset
|
226 dir = '.' |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
227 cache = dircache.get(dir, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
228 if cache is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
229 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
230 dmap = dict([(ncase(n), s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
231 for n, k, s in osutil.listdir(dir, True)]) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
232 except OSError, err: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
233 # handle directory not found in Python version prior to 2.5 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
234 # Python <= 2.4 returns native Windows code 3 in errno |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
235 # Python >= 2.5 returns ENOENT and adds winerror field |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
236 # EINVAL is raised if dir is not a directory. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
237 if err.errno not in (3, errno.ENOENT, errno.EINVAL, |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
238 errno.ENOTDIR): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
239 raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
240 dmap = {} |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
241 cache = dircache.setdefault(dir, dmap) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
242 yield cache.get(base, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
243 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
244 def getuser(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
245 '''return name of current user''' |
7949
443c0c8636ac
windows: get Abort from error
Matt Mackall <mpm@selenic.com>
parents:
7934
diff
changeset
|
246 raise error.Abort(_('user name not available - set USERNAME ' |
7934
ec4784bb7d75
windows: break import cycle with util
Martin Geisler <mg@daimi.au.dk>
parents:
7933
diff
changeset
|
247 'environment variable')) |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
248 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
249 def username(uid=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
250 """Return the name of the user with the given uid. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
251 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
252 If uid is None, return the name of the current user.""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
253 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
254 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
255 def groupname(gid=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
256 """Return the name of the group with the given gid. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
257 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
258 If gid is None, return the name of the current group.""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
259 return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
260 |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
261 def _removedirs(name): |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
262 """special version of os.removedirs that does not remove symlinked |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
263 directories or junction points if they actually contain files""" |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
264 if osutil.listdir(name): |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
265 return |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
266 os.rmdir(name) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
267 head, tail = os.path.split(name) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
268 if not tail: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
269 head, tail = os.path.split(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
270 while head and tail: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
271 try: |
9572
1f665246dab3
windows: fix unlink() not dropping empty tree (issue1861)
Patrick Mezard <pmezard@gmail.com>
parents:
9448
diff
changeset
|
272 if osutil.listdir(head): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
273 return |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
274 os.rmdir(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
275 except: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
276 break |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
277 head, tail = os.path.split(head) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
278 |
13235
6bf39d88c857
rename util.unlink to unlinkpath
Adrian Buehlmann <adrian@cadifra.com>
parents:
13206
diff
changeset
|
279 def unlinkpath(f): |
8364
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
280 """unlink and remove the directory if it is empty""" |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
281 os.unlink(f) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
282 # try removing directories that might now be empty |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
283 try: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
284 _removedirs(os.path.dirname(f)) |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
285 except OSError: |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
286 pass |
fa901423ac23
windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents:
8330
diff
changeset
|
287 |
13280
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
288 def unlink(f): |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
289 '''try to implement POSIX' unlink semantics on Windows''' |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
290 |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
291 # POSIX allows to unlink and rename open files. Windows has serious |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
292 # problems with doing that: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
293 # - Calling os.unlink (or os.rename) on a file f fails if f or any |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
294 # hardlinked copy of f has been opened with Python's open(). There is no |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
295 # way such a file can be deleted or renamed on Windows (other than |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
296 # scheduling the delete or rename for the next reboot). |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
297 # - Calling os.unlink on a file that has been opened with Mercurial's |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
298 # posixfile (or comparable methods) will delay the actual deletion of |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
299 # the file for as long as the file is held open. The filename is blocked |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
300 # during that time and cannot be used for recreating a new file under |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
301 # that same name ("zombie file"). Directories containing such zombie files |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
302 # cannot be removed or moved. |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
303 # A file that has been opened with posixfile can be renamed, so we rename |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
304 # f to a random temporary name before calling os.unlink on it. This allows |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
305 # callers to recreate f immediately while having other readers do their |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
306 # implicit zombie filename blocking on a temporary name. |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
307 |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
308 for tries in xrange(10): |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
309 temp = '%s-%08x' % (f, random.randint(0, 0xffffffff)) |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
310 try: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
311 os.rename(f, temp) # raises OSError EEXIST if temp exists |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
312 break |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
313 except OSError, e: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
314 if e.errno != errno.EEXIST: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
315 raise |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
316 else: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
317 raise IOError, (errno.EEXIST, "No usable temporary filename found") |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
318 |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
319 try: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
320 os.unlink(temp) |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
321 except: |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
322 # Some very rude AV-scanners on Windows may cause this unlink to fail. |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
323 # Not aborting here just leaks the temp file, whereas aborting at this |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
324 # point may leave serious inconsistencies. Ideally, we would notify |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
325 # the user in this case here. |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
326 pass |
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
327 |
9549
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
328 def rename(src, dst): |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
329 '''atomically rename file src to dst, replacing dst if it exists''' |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
330 try: |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
331 os.rename(src, dst) |
13278
e9a52ed28157
windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents:
13235
diff
changeset
|
332 except OSError, e: |
e9a52ed28157
windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents:
13235
diff
changeset
|
333 if e.errno != errno.EEXIST: |
e9a52ed28157
windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents:
13235
diff
changeset
|
334 raise |
13280
6052bbc7aabd
reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents:
13278
diff
changeset
|
335 unlink(dst) |
9549
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
336 os.rename(src, dst) |
8b8920209317
util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents:
9449
diff
changeset
|
337 |
10237
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
338 def spawndetached(args): |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
339 # No standard library function really spawns a fully detached |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
340 # process under win32 because they allocate pipes or other objects |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
341 # to handle standard streams communications. Passing these objects |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
342 # to the child process requires handle inheritance to be enabled |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
343 # which makes really detached processes impossible. |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
344 class STARTUPINFO: |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
345 dwFlags = subprocess.STARTF_USESHOWWINDOW |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
346 hStdInput = None |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
347 hStdOutput = None |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
348 hStdError = None |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
349 wShowWindow = subprocess.SW_HIDE |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
350 |
10237
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
351 args = subprocess.list2cmdline(args) |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
352 # Not running the command in shell mode makes python26 hang when |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
353 # writing to hgweb output socket. |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
354 comspec = os.environ.get("COMSPEC", "cmd.exe") |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
355 args = comspec + " /c " + args |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
356 hp, ht, pid, tid = subprocess.CreateProcess( |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
357 None, args, |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
358 # no special security |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
359 None, None, |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
360 # Do not inherit handles |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
361 0, |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
362 # DETACHED_PROCESS |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
363 0x00000008, |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
364 os.environ, |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
365 os.getcwd(), |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
366 STARTUPINFO()) |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
367 return pid |
2f7a38f336f4
serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents:
10156
diff
changeset
|
368 |
10239
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
369 def gethgcmd(): |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
370 return [sys.executable] + sys.argv[:1] |
8e4be44a676f
Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents:
10237
diff
changeset
|
371 |
12689
c52c629ce19e
termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents:
12063
diff
changeset
|
372 def termwidth(): |
11010
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10394
diff
changeset
|
373 # cmd.exe does not handle CR like a unix console, the CR is |
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10394
diff
changeset
|
374 # counted in the line length. On 80 columns consoles, if 80 |
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10394
diff
changeset
|
375 # characters are written, the following CR won't apply on the |
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10394
diff
changeset
|
376 # current line but on the new one. Keep room for it. |
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10394
diff
changeset
|
377 return 79 |
18e81d42ee5c
util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents:
10394
diff
changeset
|
378 |
11138
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
379 def groupmembers(name): |
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
380 # Don't support groups on Windows for now |
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
381 raise KeyError() |
99eee847beaa
acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents:
11010
diff
changeset
|
382 |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
383 try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
384 # override functions with win32 versions if possible |
7932
96501a4abbec
windows: fix import of win32.py (was util_win32.py)
Sune Foldager <cryo@cyanite.org>
parents:
7914
diff
changeset
|
385 from win32 import * |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
386 except ImportError: |
8421
b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents:
8364
diff
changeset
|
387 pass |
8614
573734e7e6d0
cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents:
8559
diff
changeset
|
388 |
573734e7e6d0
cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents:
8559
diff
changeset
|
389 expandglobs = True |