mercurial/win32.py
author timeless <timeless@gmail.com>
Thu, 14 Oct 2010 10:56:39 +0300
changeset 12773 98aaf58a1d7c
parent 12689 c52c629ce19e
child 12938 bf826c0b9537
permissions -rw-r--r--
test-ssh: handle very slow ssh transfer rate
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)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    37
    except pywintypes.error:
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    38
        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
    39
    try:
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    40
        return win32file.GetFileInformationByHandle(fh)
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    41
    finally:
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    42
        fh.Close()
10218
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."""
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    46
    links = _getfileinfo(pathname)[7]
11991
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    47
    if links < 2:
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    48
        # 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
    49
        dirname = os.path.dirname(pathname)
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    50
        if not dirname:
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    51
            dirname = '.'
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    52
        dt = win32file.GetDriveType(dirname + '\\')
12401
4cdaf1adafc8 backout most of 4f8067c94729
Matt Mackall <mpm@selenic.com>
parents: 12387
diff changeset
    53
        if dt == 4 or dt == 1:
11991
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    54
            # 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
    55
            links = 2
50523b4407f6 win32: correctly break hardlinks on network drives (issue761)
Patrick Mezard <pmezard@gmail.com>
parents: 11304
diff changeset
    56
    return links
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    57
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    58
def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    59
    """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
    60
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    61
    res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    62
    res2 = _getfileinfo(fpath2)
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    63
    # 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
    64
    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
    65
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    66
def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    67
    """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
    68
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    69
    res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    70
    res2 = _getfileinfo(fpath2)
11992
ccd8e592c3c5 win32: remove useless lstat() fallback in nlinks()
Patrick Mezard <pmezard@gmail.com>
parents: 11991
diff changeset
    71
    return res1[4] == res2[4]
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    72
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    73
def testpid(pid):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    74
    '''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
    75
    determine, False otherwise'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    76
    try:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    77
        handle = win32api.OpenProcess(
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    78
            win32con.PROCESS_QUERY_INFORMATION, False, pid)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    79
        if handle:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    80
            status = win32process.GetExitCodeProcess(handle)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    81
            return status == win32con.STILL_ACTIVE
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    82
    except pywintypes.error, details:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    83
        return details[0] != winerror.ERROR_INVALID_PARAMETER
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    84
    return True
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    85
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    86
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
    87
    ''' 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
    88
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    89
    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
    90
    is used.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    91
    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
    92
    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
    93
    LOCAL_MACHINE).
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    94
    '''
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    95
    try:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    96
        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
    97
            QueryValueEx, OpenKey
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    98
    except ImportError:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    99
        return None
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   100
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   101
    if scope is None:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   102
        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
   103
    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
   104
        scope = (scope,)
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   105
    for s in scope:
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   106
        try:
6881
d2375bbee6d4 Folding correction and missing import
Remy Roy <remyroy@remyroy.com>
parents: 6880
diff changeset
   107
            val = QueryValueEx(OpenKey(s, key), valname)[0]
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   108
            # 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
   109
            return encoding.tolocal(val.encode('UTF-8'))
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   110
        except EnvironmentError:
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   111
            pass
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   112
2117
760339ccc799 fix issue 217.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2054
diff changeset
   113
def system_rcpath_win32():
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   114
    '''return default os-specific hgrc search path'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   115
    proc = win32api.GetCurrentProcess()
2285
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   116
    try:
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   117
        # 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
   118
        filename = win32process.GetModuleFileNameEx(proc, 0)
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   119
    except:
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   120
        filename = win32api.GetModuleFileName(0)
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   121
    # 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
   122
    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
   123
    if os.path.isfile(progrc):
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   124
        return [progrc]
10388
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   125
    # 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
   126
    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
   127
    if os.path.isdir(progrcd):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   128
        rcpath = []
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   129
        for f, kind in osutil.listdir(progrcd):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   130
            if f.endswith('.rc'):
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   131
                rcpath.append(os.path.join(progrcd, f))
07bd7608a0ea win32: allow hgrc.d on Windows
Steve Borho <steve@borho.org>
parents: 10264
diff changeset
   132
        return rcpath
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   133
    # 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
   134
    try:
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   135
        value = win32api.RegQueryValue(
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   136
                win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Mercurial')
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   137
        rcpath = []
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   138
        for p in value.split(os.pathsep):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   139
            if p.lower().endswith('mercurial.ini'):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   140
                rcpath.append(p)
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   141
            elif os.path.isdir(p):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   142
                for f, kind in osutil.listdir(p):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   143
                    if f.endswith('.rc'):
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   144
                        rcpath.append(os.path.join(p, f))
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   145
        return rcpath
1b5b81d9039b win32: read system rcpath from registry
Steve Borho <steve@borho.org>
parents: 4998
diff changeset
   146
    except pywintypes.error:
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   147
        return []
2176
9b42304d9896 fix file handling bugs on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2117
diff changeset
   148
4098
c08b6af023bc util_win32.py: fix user_rcpath
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3891
diff changeset
   149
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
   150
    '''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
   151
    userdir = os.path.expanduser('~')
7427
f21e3d0e335b util_win32: fix Windows version checking (issue1358)
Patrick Mezard <pmezard@gmail.com>
parents: 7390
diff changeset
   152
    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
   153
        # 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
   154
        # 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
   155
        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
   156
            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
   157
        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
   158
    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
   159
            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
   160
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   161
def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   162
    '''return name of current user'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   163
    return win32api.GetUserName()
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   164
4803
7549cd526b7f Fix serve on Windows without win32* modules.
Nathan Jones <nathanj@insightbb.com>
parents: 4672
diff changeset
   165
def set_signal_handler_win32():
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   166
    """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
   167
    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
   168
    operations.
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   169
    """
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   170
    def handler(event):
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   171
        win32process.ExitProcess(1)
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   172
    win32api.SetConsoleCtrlHandler(handler)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   173
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   174
def hidewindow():
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   175
    def callback(*args, **kwargs):
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   176
        hwnd, pid = args
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   177
        wpid = win32process.GetWindowThreadProcessId(hwnd)[1]
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   178
        if pid == wpid:
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   179
            win32gui.ShowWindow(hwnd, win32con.SW_HIDE)
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   180
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   181
    pid =  win32process.GetCurrentProcessId()
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   182
    win32gui.EnumWindows(callback, pid)
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   183
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12401
diff changeset
   184
def termwidth():
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   185
    try:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   186
        # Query stderr to avoid problems with redirections
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   187
        screenbuf = win32console.GetStdHandle(win32console.STD_ERROR_HANDLE)
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   188
        try:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   189
            window = screenbuf.GetConsoleScreenBufferInfo()['Window']
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   190
            width = window.Right - window.Left
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   191
            return width
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   192
        finally:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   193
            screenbuf.Detach()
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   194
    except pywintypes.error:
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   195
        return 79