mercurial/win32.py
author Patrick Mezard <pmezard@gmail.com>
Thu, 19 Aug 2010 22:51:09 +0200
branchstable
changeset 11991 50523b4407f6
parent 11304 8c377f2feee1
child 11992 ccd8e592c3c5
permissions -rw-r--r--
win32: correctly break hardlinks on network drives (issue761) win32.nlinks() was often returning 1 instead of the correct hardlinks count when reading from network drives. This made commit or push to a repository on a network share to fail breaking the hardlinks in the datastore, possibly causing integrity errors in repositories linked locally on the remote side. Here is what the MSDN says about GetFileInformationByHandle(): Depending on the underlying network features of the operating system and the type of server connected to, the GetFileInformationByHandle function may fail, return partial information, or full information for the given file. In practice, we never got the correct hardlinks count when reading from and to many combinations of Window XP, 2003, Vista and 7, via network drives or RDP shares. It always returned 1 instead. The only setup returning an accurate links count was a samba on Debian. To avoid this, Mercurial now breaks the hardlinks unconditionally when writing to a network drive.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    34
        fh = win32file.CreateFile(pathname,
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    35
            win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    36
            None, win32file.OPEN_EXISTING, 0, None)
10219
3b94120864fc win32: close file when leaving _getfileinfo()
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
    37
        try:
3b94120864fc win32: close file when leaving _getfileinfo()
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
    38
            return win32file.GetFileInformationByHandle(fh)
3b94120864fc win32: close file when leaving _getfileinfo()
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
    39
        finally:
3b94120864fc win32: close file when leaving _getfileinfo()
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
    40
            fh.Close()
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    41
    except pywintypes.error:
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    42
        return None
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    43
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    44
def nlinks(pathname):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    45
    """Return number of hardlinks for the given file."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    46
    res = _getfileinfo(pathname)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    47
    if res is not None:
11991
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    48
        links = res[7]
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    49
    else:
11991
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    50
        links = os.lstat(pathname).st_nlink
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    51
    if links < 2:
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    52
        # Known to be wrong for most network drives
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    53
        dirname = os.path.dirname(pathname)
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    54
        if not dirname:
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    55
            dirname = '.'
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    56
        dt = win32file.GetDriveType(dirname + '\\')
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    57
        if dt == 4 or dt == 1:
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    58
            # Fake hardlink to force COW for network drives
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    59
            links = 2
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    60
    return links
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    61
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    62
def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    63
    """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
    64
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    65
    res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    66
    res2 = _getfileinfo(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    67
    if res1 is not None and res2 is not None:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    68
        # Index 4 is the volume serial number, and 8 and 9 contain the file ID
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    69
        return res1[4] == res2[4] and res1[8] == res2[8] and res1[9] == res2[9]
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    70
    else:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    71
        return False
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    72
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    73
def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    74
    """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
    75
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    76
    res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    77
    res2 = _getfileinfo(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    78
    if res1 is not None and res2 is not None:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    79
        return res1[4] == res2[4]
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    80
    else:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    81
        return False
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    82
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    83
def testpid(pid):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    84
    '''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
    85
    determine, False otherwise'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    86
    try:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    87
        handle = win32api.OpenProcess(
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    88
            win32con.PROCESS_QUERY_INFORMATION, False, pid)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    89
        if handle:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    90
            status = win32process.GetExitCodeProcess(handle)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    91
            return status == win32con.STILL_ACTIVE
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    92
    except pywintypes.error, details:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    93
        return details[0] != winerror.ERROR_INVALID_PARAMETER
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    94
    return True
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    95
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    96
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
    97
    ''' 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
    98
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    99
    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
   100
    is used.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   101
    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
   102
    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
   103
    LOCAL_MACHINE).
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   104
    '''
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   105
    try:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   106
        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
   107
            QueryValueEx, OpenKey
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   108
    except ImportError:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   109
        return None
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   110
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   111
    if scope is None:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   112
        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
   113
    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
   114
        scope = (scope,)
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   115
    for s in scope:
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   116
        try:
6881
d2375bbee6d4 Folding correction and missing import
Remy Roy <remyroy@remyroy.com>
parents: 6880
diff changeset
   117
            val = QueryValueEx(OpenKey(s, key), valname)[0]
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   118
            # 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
   119
            return encoding.tolocal(val.encode('UTF-8'))
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   120
        except EnvironmentError:
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   121
            pass
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   122
2117
760339ccc799 fix issue 217.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2054
diff changeset
   123
def system_rcpath_win32():
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   124
    '''return default os-specific hgrc search path'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   125
    proc = win32api.GetCurrentProcess()
2285
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   126
    try:
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   127
        # This will fail on windows < NT
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   128
        filename = win32process.GetModuleFileNameEx(proc, 0)
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   129
    except:
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   130
        filename = win32api.GetModuleFileName(0)
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   131
    # 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
   132
    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
   133
    if os.path.isfile(progrc):
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   134
        return [progrc]
10388
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   135
    # 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
   136
    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
   137
    if os.path.isdir(progrcd):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   138
        rcpath = []
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   139
        for f, kind in osutil.listdir(progrcd):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   140
            if f.endswith('.rc'):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   141
                rcpath.append(os.path.join(progrcd, f))
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   142
        return rcpath
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   143
    # 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
   144
    try:
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   145
        value = win32api.RegQueryValue(
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   146
                win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Mercurial')
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   147
        rcpath = []
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   148
        for p in value.split(os.pathsep):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   149
            if p.lower().endswith('mercurial.ini'):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   150
                rcpath.append(p)
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   151
            elif os.path.isdir(p):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   152
                for f, kind in osutil.listdir(p):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   153
                    if f.endswith('.rc'):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   154
                        rcpath.append(os.path.join(p, f))
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   155
        return rcpath
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   156
    except pywintypes.error:
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   157
        return []
2176
9b42304d9896 fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2117
diff changeset
   158
4098
c08b6af023bc util_win32.py: fix user_rcpath
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3891
diff changeset
   159
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
   160
    '''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
   161
    userdir = os.path.expanduser('~')
7427
f21e3d0e335b util_win32: fix Windows version checking (issue1358)
Patrick Mezard <pmezard@gmail.com>
parents: 7390
diff changeset
   162
    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
   163
        # 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
   164
        # 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
   165
        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
   166
            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
   167
        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
   168
    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
   169
            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
   170
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   171
def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   172
    '''return name of current user'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   173
    return win32api.GetUserName()
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   174
4803
7549cd526b7f Fix serve on Windows without win32* modules.
Nathan Jones <nathanj@insightbb.com>
parents: 4672
diff changeset
   175
def set_signal_handler_win32():
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   176
    """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
   177
    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
   178
    operations.
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   179
    """
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   180
    def handler(event):
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   181
        win32process.ExitProcess(1)
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   182
    win32api.SetConsoleCtrlHandler(handler)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   183
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   184
def hidewindow():
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   185
    def callback(*args, **kwargs):
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   186
        hwnd, pid = args
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   187
        wpid = win32process.GetWindowThreadProcessId(hwnd)[1]
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   188
        if pid == wpid:
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   189
            win32gui.ShowWindow(hwnd, win32con.SW_HIDE)
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   190
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   191
    pid =  win32process.GetCurrentProcessId()
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   192
    win32gui.EnumWindows(callback, pid)
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   193
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   194
def termwidth_():
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   195
    try:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   196
        # Query stderr to avoid problems with redirections
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   197
        screenbuf = win32console.GetStdHandle(win32console.STD_ERROR_HANDLE)
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   198
        try:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   199
            window = screenbuf.GetConsoleScreenBufferInfo()['Window']
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   200
            width = window.Right - window.Left
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   201
            return width
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   202
        finally:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   203
            screenbuf.Detach()
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   204
    except pywintypes.error:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   205
        return 79