author | Sune Foldager <cryo@cyanite.org> |
Thu, 02 Apr 2009 17:09:17 +0200 | |
changeset 7933 | 5325596c354c |
parent 7932 | 96501a4abbec |
child 7934 | ec4784bb7d75 |
permissions | -rw-r--r-- |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 |
""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 |
windows.py - Windows utility function implementations for Mercurial |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 |
Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 |
This software may be used and distributed according to the terms of |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
the GNU General Public License version 2, incorporated herein by |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
8 |
reference. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 |
""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 |
|
7933
5325596c354c
windows: fix missing import of util.Abort
Sune Foldager <cryo@cyanite.org>
parents:
7932
diff
changeset
|
11 |
from util import Abort |
7914
48661519913f
windows: add various missing import
Steve Borho <steve@borho.org>
parents:
7913
diff
changeset
|
12 |
from i18n import _ |
48661519913f
windows: add various missing import
Steve Borho <steve@borho.org>
parents:
7913
diff
changeset
|
13 |
import errno, msvcrt, os, osutil, re, sys |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 |
nulldev = 'NUL:' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 |
umask = 002 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 |
class winstdout: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 |
'''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
|
20 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
21 |
def __init__(self, fp): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 |
self.fp = fp |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
23 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 |
def __getattr__(self, key): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
return getattr(self.fp, key) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 |
def close(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 |
self.fp.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 |
except: pass |
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 write(self, s): |
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 |
# 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
|
35 |
# writing large size of data to console. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 |
limit = 16000 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 |
l = len(s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 |
start = 0 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 |
while start < l: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 |
end = start + limit |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 |
self.fp.write(s[start:end]) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 |
start = end |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 |
except IOError, inst: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
44 |
if inst.errno != 0: raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 |
self.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 |
raise IOError(errno.EPIPE, 'Broken pipe') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 |
def flush(self): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 |
return self.fp.flush() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 |
except IOError, inst: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
if inst.errno != errno.EINVAL: raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 |
self.close() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
raise IOError(errno.EPIPE, 'Broken pipe') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 |
sys.stdout = winstdout(sys.stdout) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
58 |
def _is_win_9x(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 |
'''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
|
60 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 |
return sys.getwindowsversion()[3] == 1 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 |
except AttributeError: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
return 'command' in os.environ.get('comspec', '') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 |
def openhardlinks(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 |
return not _is_win_9x and "win32api" in locals() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
def system_rcpath(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 |
return system_rcpath_win32() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
except: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
return [r'c:\mercurial\mercurial.ini'] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 |
def user_rcpath(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 |
'''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
|
76 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
77 |
path = user_rcpath_win32() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
78 |
except: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 |
home = os.path.expanduser('~') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
80 |
path = [os.path.join(home, 'mercurial.ini'), |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
os.path.join(home, '.hgrc')] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 |
userprofile = os.environ.get('USERPROFILE') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
83 |
if userprofile: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 |
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
|
85 |
path.append(os.path.join(userprofile, '.hgrc')) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
86 |
return path |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
87 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 |
def parse_patch_output(output_line): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 |
"""parses the output produced by patch and returns the file name""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
90 |
pf = output_line[14:] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
91 |
if pf[0] == '`': |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
92 |
pf = pf[1:-1] # Remove the quotes |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 |
return pf |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
94 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
95 |
def sshargs(sshcmd, host, user, port): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
96 |
'''Build argument list for ssh or Plink''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
101 |
def testpid(pid): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
102 |
'''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
|
103 |
return True |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
104 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
105 |
def set_flags(f, l, x): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
106 |
pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
107 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
108 |
def set_binary(fd): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
109 |
# 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
|
110 |
# fileno(), usually set to -1. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
111 |
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
|
112 |
msvcrt.setmode(fd.fileno(), os.O_BINARY) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
113 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
114 |
def pconvert(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
115 |
return '/'.join(path.split(os.sep)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
116 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
117 |
def localpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
118 |
return path.replace('/', '\\') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
119 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
120 |
def normpath(path): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
121 |
return pconvert(os.path.normpath(path)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
122 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
123 |
def samestat(s1, s2): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
124 |
return False |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
125 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
126 |
# 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
|
127 |
# - 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
|
128 |
# 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
|
129 |
# - 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
|
130 |
# - 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
|
131 |
# backslash |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
132 |
# (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
|
133 |
# 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
|
134 |
# 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
|
135 |
# 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
|
136 |
# quote we've appended to the end) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
137 |
_quotere = None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
138 |
def shellquote(s): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
139 |
global _quotere |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
140 |
if _quotere is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
141 |
_quotere = re.compile(r'(\\*)("|\\$)') |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
142 |
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
|
143 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
144 |
def quotecommand(cmd): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
145 |
"""Build a command string suitable for os.popen* calls.""" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
146 |
# The extra quotes are needed because popen* runs the command |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
147 |
# through the current COMSPEC. cmd.exe suppress enclosing quotes. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
148 |
return '"' + cmd + '"' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
149 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
150 |
def popen(command, mode='r'): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
151 |
# 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
|
152 |
# under windows" |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
153 |
# http://bugs.python.org/issue1366 |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
154 |
command += " 2> %s" % nulldev |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
155 |
return os.popen(quotecommand(command), mode) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
156 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
157 |
def explain_exit(code): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
158 |
return _("exited with status %d") % code, code |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
159 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
160 |
# 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
|
161 |
# username and groupname functions above, too. |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
162 |
def isowner(fp, st=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
163 |
return True |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
164 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
165 |
def find_exe(command): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
166 |
'''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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
pathexts = [''] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
175 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
176 |
def findexisting(pathcommand): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
177 |
'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
|
178 |
for ext in pathexts: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
179 |
executable = pathcommand + ext |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
180 |
if os.path.exists(executable): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
181 |
return executable |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
182 |
return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
183 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
184 |
if os.sep in command: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
185 |
return findexisting(command) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
186 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
187 |
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
|
188 |
executable = findexisting(os.path.join(path, command)) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
189 |
if executable is not None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
190 |
return executable |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
191 |
return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
192 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
193 |
def set_signal_handler(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
194 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
195 |
set_signal_handler_win32() |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
196 |
except NameError: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
197 |
pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
198 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
199 |
def statfiles(files): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
200 |
'''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
|
201 |
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
|
202 |
ncase = os.path.normcase |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
203 |
sep = os.sep |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
204 |
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
|
205 |
for nf in files: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
206 |
nf = ncase(nf) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
207 |
pos = nf.rfind(sep) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
208 |
if pos == -1: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
209 |
dir, base = '.', nf |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
210 |
else: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
211 |
dir, base = nf[:pos+1], nf[pos+1:] |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
212 |
cache = dircache.get(dir, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
213 |
if cache is None: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
214 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
215 |
dmap = dict([(ncase(n), s) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
216 |
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
|
217 |
except OSError, err: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
218 |
# 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
|
219 |
# 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
|
220 |
# 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
|
221 |
# 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
|
222 |
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
|
223 |
errno.ENOTDIR): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
224 |
raise |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
225 |
dmap = {} |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
226 |
cache = dircache.setdefault(dir, dmap) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
227 |
yield cache.get(base, None) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
228 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
229 |
def getuser(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
230 |
'''return name of current user''' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
231 |
raise Abort(_('user name not available - set USERNAME ' |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
232 |
'environment variable')) |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
233 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
234 |
def username(uid=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
235 |
"""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
|
236 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
237 |
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
|
238 |
return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
239 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
240 |
def groupname(gid=None): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
241 |
"""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
|
242 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
243 |
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
|
244 |
return None |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
245 |
|
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
246 |
posixfile = file |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
247 |
try: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
248 |
# 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
|
249 |
from win32 import * |
7890
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
250 |
if not _is_win_9x(): |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
251 |
posixfile = posixfile_nt |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
252 |
except ImportError: |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
253 |
pass |
e710f0f592b2
util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
254 |