annotate mercurial/win32.py @ 13374:1c613c1ae43d

win32: optimize parameters for the CreateFile call in _getfileinfo Set dwDesiredAccess to 0 instead of GENERIC_READ. Zero is enough for querying the file metadata. We don't even need to access the -contents- of the file. Set dwShareMode to FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE instead of the overly restrictive FILE_SHARE_READ. There is no need to cause write or delete accesses by other processes to fail while we are querying file metadata. See http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx
author Adrian Buehlmann <adrian@cadifra.com>
date Mon, 14 Feb 2011 11:12:22 +0100
parents 2ef915184ff2
children f1fa8f481c7c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
1 # win32.py - utility functions that use win32 API
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
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10219
diff changeset
6 # GNU General Public License version 2 or any later version.
8227
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
7
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
8 """Utility functions that use win32 API.
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
9
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
10 Mark Hammond's win32all package allows better functionality on
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
11 Windows. This module overrides definitions in util.py. If not
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
12 available, import of this module will fail, and generic code will be
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
13 used.
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
14 """
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
15
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
16 import win32api
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
17
6216
a88259018f79 Add missing import of sys
Joel Rosdahl <joel@rosdahl.net>
parents: 6212
diff changeset
18 import errno, os, sys, pywintypes, win32con, win32file, win32process
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
19 import winerror, win32gui, win32console
7948
de377b1a9a84 move encoding bits from util to encoding
Matt Mackall <mpm@selenic.com>
parents: 7890
diff changeset
20 import osutil, encoding
9198
061eeb602354 coding style: use a space after comma
Martin Geisler <mg@lazybytes.net>
parents: 8951
diff changeset
21 from win32com.shell import shell, shellcon
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
22
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
23 def os_link(src, dst):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
24 try:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
25 win32file.CreateHardLink(dst, src)
11304
8c377f2feee1 cleanups: unused variables
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11012
diff changeset
26 except pywintypes.error:
8951
835a51e63c5b windows: fix use of undefined exception (issue1707)
Henrik Stuart <hg@hstuart.dk>
parents: 8656
diff changeset
27 raise OSError(errno.EINVAL, 'target implements hardlinks improperly')
7778
82f7145b304c Don't fail on clone on win98 (issue1492)
Matt Mackall <mpm@selenic.com>
parents: 7473
diff changeset
28 except NotImplementedError: # Another fake error win Win98
8951
835a51e63c5b windows: fix use of undefined exception (issue1707)
Henrik Stuart <hg@hstuart.dk>
parents: 8656
diff changeset
29 raise OSError(errno.EINVAL, 'Hardlinking not supported')
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
30
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
31 def _getfileinfo(pathname):
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
32 """Return number of hardlinks for the given file."""
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
33 try:
13374
1c613c1ae43d win32: optimize parameters for the CreateFile call in _getfileinfo
Adrian Buehlmann <adrian@cadifra.com>
parents: 13285
diff changeset
34 fh = win32file.CreateFile(pathname, 0,
1c613c1ae43d win32: optimize parameters for the CreateFile call in _getfileinfo
Adrian Buehlmann <adrian@cadifra.com>
parents: 13285
diff changeset
35 win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE |
1c613c1ae43d win32: optimize parameters for the CreateFile call in _getfileinfo
Adrian Buehlmann <adrian@cadifra.com>
parents: 13285
diff changeset
36 win32file.FILE_SHARE_DELETE,
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
37 None, win32file.OPEN_EXISTING, 0, None)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
38 except pywintypes.error:
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
39 raise OSError(errno.ENOENT, 'The system cannot find the file specified')
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
40 try:
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
41 return win32file.GetFileInformationByHandle(fh)
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
42 finally:
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
43 fh.Close()
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
44
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
45 def nlinks(pathname):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
46 """Return number of hardlinks for the given file."""
12938
bf826c0b9537 opener: check hardlink count reporting (issue1866)
Adrian Buehlmann <adrian@cadifra.com>
parents: 12689
diff changeset
47 return _getfileinfo(pathname)[7]
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
48
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
49 def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
50 """Returns whether fpath1 and fpath2 refer to the same file. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
51 guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
52 res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
53 res2 = _getfileinfo(fpath2)
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
54 # Index 4 is the volume serial number, and 8 and 9 contain the file ID
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
55 return res1[4] == res2[4] and res1[8] == res2[8] and res1[9] == res2[9]
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
56
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
57 def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
58 """Returns whether fpath1 and fpath2 are on the same device. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
59 guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
60 res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
61 res2 = _getfileinfo(fpath2)
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
62 return res1[4] == res2[4]
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
63
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
64 def testpid(pid):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
65 '''return True if pid is still running or unable to
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
66 determine, False otherwise'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
67 try:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
68 handle = win32api.OpenProcess(
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
69 win32con.PROCESS_QUERY_INFORMATION, False, pid)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
70 if handle:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
71 status = win32process.GetExitCodeProcess(handle)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
72 return status == win32con.STILL_ACTIVE
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
73 except pywintypes.error, details:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
74 return details[0] != winerror.ERROR_INVALID_PARAMETER
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
75 return True
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
76
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
77 def lookup_reg(key, valname=None, scope=None):
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
78 ''' Look up a key/value name in the Windows registry.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
79
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
80 valname: value name. If unspecified, the default value for the key
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
81 is used.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
82 scope: optionally specify scope for registry lookup, this can be
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
83 a sequence of scopes to look up in order. Default (CURRENT_USER,
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
84 LOCAL_MACHINE).
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
85 '''
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
86 try:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
87 from _winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, \
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
88 QueryValueEx, OpenKey
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
89 except ImportError:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
90 return None
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
91
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
92 if scope is None:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
93 scope = (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE)
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
94 elif not isinstance(scope, (list, tuple)):
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
95 scope = (scope,)
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
96 for s in scope:
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
97 try:
6881
d2375bbee6d4 Folding correction and missing import
Remy Roy <remyroy@remyroy.com>
parents: 6880
diff changeset
98 val = QueryValueEx(OpenKey(s, key), valname)[0]
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
99 # never let a Unicode string escape into the wild
7948
de377b1a9a84 move encoding bits from util to encoding
Matt Mackall <mpm@selenic.com>
parents: 7890
diff changeset
100 return encoding.tolocal(val.encode('UTF-8'))
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
101 except EnvironmentError:
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
102 pass
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
103
2117
760339ccc799 fix issue 217.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2054
diff changeset
104 def system_rcpath_win32():
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
105 '''return default os-specific hgrc search path'''
12966
5f80f44d23c5 win32: remove try-catch block of GetModuleFileNameEx (issue2480)
Yuya Nishihara <yuya@tcha.org>
parents: 12938
diff changeset
106 filename = win32api.GetModuleFileName(0)
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
107 # Use mercurial.ini found in directory with hg.exe
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
108 progrc = os.path.join(os.path.dirname(filename), 'mercurial.ini')
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
109 if os.path.isfile(progrc):
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
110 return [progrc]
10388
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
111 # Use hgrc.d found in directory with hg.exe
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
112 progrcd = os.path.join(os.path.dirname(filename), 'hgrc.d')
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
113 if os.path.isdir(progrcd):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
114 rcpath = []
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
115 for f, kind in osutil.listdir(progrcd):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
116 if f.endswith('.rc'):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
117 rcpath.append(os.path.join(progrcd, f))
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
118 return rcpath
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
119 # else look for a system rcpath in the registry
5583
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
120 try:
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
121 value = win32api.RegQueryValue(
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
122 win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Mercurial')
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
123 rcpath = []
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
124 for p in value.split(os.pathsep):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
125 if p.lower().endswith('mercurial.ini'):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
126 rcpath.append(p)
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
127 elif os.path.isdir(p):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
128 for f, kind in osutil.listdir(p):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
129 if f.endswith('.rc'):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
130 rcpath.append(os.path.join(p, f))
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
131 return rcpath
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
132 except pywintypes.error:
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
133 return []
2176
9b42304d9896 fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2117
diff changeset
134
4098
c08b6af023bc util_win32.py: fix user_rcpath
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3891
diff changeset
135 def user_rcpath_win32():
2284
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
136 '''return os-specific hgrc search path to the user dir'''
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
137 userdir = os.path.expanduser('~')
7427
f21e3d0e335b util_win32: fix Windows version checking (issue1358)
Patrick Mezard <pmezard@gmail.com>
parents: 7390
diff changeset
138 if sys.getwindowsversion()[3] != 2 and userdir == '~':
2284
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
139 # We are on win < nt: fetch the APPDATA directory location and use
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
140 # the parent directory as the user home dir.
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
141 appdir = shell.SHGetPathFromIDList(
2313
a600d9997521 Fixed typo (qshell instead of shell) in win98 code (see issue244).
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2285
diff changeset
142 shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA))
2284
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
143 userdir = os.path.dirname(appdir)
6153
09a8be3e5bfb Also search for .hgrc if mercurial.ini not found on windows
Stefan Rank <strank(AT)strank(DOT)info>
parents: 6012
diff changeset
144 return [os.path.join(userdir, 'mercurial.ini'),
09a8be3e5bfb Also search for .hgrc if mercurial.ini not found on windows
Stefan Rank <strank(AT)strank(DOT)info>
parents: 6012
diff changeset
145 os.path.join(userdir, '.hgrc')]
2284
d6392a7c03dd On win98 os.path.expanuser('~') does not result in a useable directory.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2250
diff changeset
146
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
147 def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
148 '''return name of current user'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
149 return win32api.GetUserName()
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
150
4803
7549cd526b7f Fix serve on Windows without win32* modules.
Nathan Jones <nathanj@insightbb.com>
parents: 4672
diff changeset
151 def set_signal_handler_win32():
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
152 """Register a termination handler for console events including
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
153 CTRL+C. python signal handlers do not work well with socket
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
154 operations.
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
155 """
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
156 def handler(event):
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
157 win32process.ExitProcess(1)
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
158 win32api.SetConsoleCtrlHandler(handler)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
159
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
160 def hidewindow():
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
161 def callback(*args, **kwargs):
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
162 hwnd, pid = args
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
163 wpid = win32process.GetWindowThreadProcessId(hwnd)[1]
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
164 if pid == wpid:
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
165 win32gui.ShowWindow(hwnd, win32con.SW_HIDE)
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
166
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
167 pid = win32process.GetCurrentProcessId()
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
168 win32gui.EnumWindows(callback, pid)
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
169
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12401
diff changeset
170 def termwidth():
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
171 try:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
172 # Query stderr to avoid problems with redirections
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
173 screenbuf = win32console.GetStdHandle(win32console.STD_ERROR_HANDLE)
13285
2ef915184ff2 win32: win32console.GetStdHandle() can return None
Steve Borho <steve@borho.org>
parents: 12966
diff changeset
174 if screenbuf is None:
2ef915184ff2 win32: win32console.GetStdHandle() can return None
Steve Borho <steve@borho.org>
parents: 12966
diff changeset
175 return 79
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
176 try:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
177 window = screenbuf.GetConsoleScreenBufferInfo()['Window']
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
178 width = window.Right - window.Left
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
179 return width
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
180 finally:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
181 screenbuf.Detach()
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
182 except pywintypes.error:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
183 return 79