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