mercurial/win32.py
author Siddharth Agarwal <sid.bugzilla@gmail.com>
Fri, 08 Jan 2010 18:48:39 +0530
branchstable
changeset 10218 750b7a4f01f6
parent 9198 061eeb602354
child 10219 3b94120864fc
permissions -rw-r--r--
Add support for relinking on Windows. Test and minor code change by Patrick Mézard <pmezard@gmail.com>
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
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
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
8656
284fda4cd093 removed unused imports
Martin Geisler <mg@lazybytes.net>
parents: 8421
diff changeset
    19
import winerror
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)
5879
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    26
        # CreateHardLink sometimes succeeds on mapped drives but
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    27
        # following nlinks() returns 1. Check it now and bail out.
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    28
        if nlinks(src) < 2:
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    29
            try:
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    30
                win32file.DeleteFile(dst)
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    31
            except:
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    32
                pass
cacfeee38870 util_win32: make os_link more robust (issue 761)
Patrick Mezard <pmezard@gmail.com>
parents: 5611
diff changeset
    33
            # Fake hardlinking error
8951
835a51e63c5b windows: fix use of undefined exception (issue1707)
Henrik Stuart <hg@hstuart.dk>
parents: 8656
diff changeset
    34
            raise OSError(errno.EINVAL, 'Hardlinking not supported')
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    35
    except pywintypes.error, details:
8951
835a51e63c5b windows: fix use of undefined exception (issue1707)
Henrik Stuart <hg@hstuart.dk>
parents: 8656
diff changeset
    36
        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
    37
    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
    38
        raise OSError(errno.EINVAL, 'Hardlinking not supported')
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    39
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    40
def _getfileinfo(pathname):
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    41
    """Return number of hardlinks for the given file."""
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    42
    try:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    43
        fh = win32file.CreateFile(pathname,
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    44
            win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    45
            None, win32file.OPEN_EXISTING, 0, None)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    46
        res = win32file.GetFileInformationByHandle(fh)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    47
        fh.Close()
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    48
        return res
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    49
    except pywintypes.error:
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    50
        return None
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    51
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    52
def nlinks(pathname):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    53
    """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
    54
    res = _getfileinfo(pathname)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    55
    if res is not None:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    56
        return res[7]
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    57
    else:
2448
b77a2ef61b81 replace os.stat with os.lstat in some where.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2313
diff changeset
    58
        return os.lstat(pathname).st_nlink
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    59
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    60
def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    61
    """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
    62
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    63
    res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    64
    res2 = _getfileinfo(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    65
    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
    66
        # 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
    67
        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
    68
    else:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    69
        return False
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    70
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    71
def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    72
    """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
    73
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    74
    res1 = _getfileinfo(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    75
    res2 = _getfileinfo(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    76
    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
    77
        return res1[4] == res2[4]
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    78
    else:
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    79
        return False
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
    80
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    81
def testpid(pid):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    82
    '''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
    83
    determine, False otherwise'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    84
    try:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    85
        handle = win32api.OpenProcess(
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    86
            win32con.PROCESS_QUERY_INFORMATION, False, pid)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    87
        if handle:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    88
            status = win32process.GetExitCodeProcess(handle)
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    89
            return status == win32con.STILL_ACTIVE
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    90
    except pywintypes.error, details:
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    91
        return details[0] != winerror.ERROR_INVALID_PARAMETER
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    92
    return True
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    93
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    94
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
    95
    ''' 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
    96
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    97
    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
    98
    is used.
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
    99
    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
   100
    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
   101
    LOCAL_MACHINE).
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   102
    '''
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   103
    try:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   104
        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
   105
            QueryValueEx, OpenKey
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   106
    except ImportError:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   107
        return None
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   108
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   109
    if scope is None:
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   110
        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
   111
    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
   112
        scope = (scope,)
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   113
    for s in scope:
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   114
        try:
6881
d2375bbee6d4 Folding correction and missing import
Remy Roy <remyroy@remyroy.com>
parents: 6880
diff changeset
   115
            val = QueryValueEx(OpenKey(s, key), valname)[0]
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   116
            # 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
   117
            return encoding.tolocal(val.encode('UTF-8'))
6880
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   118
        except EnvironmentError:
892806b3fc0f util: disinfect lookup_reg strings (issue1126)
Matt Mackall <mpm@selenic.com>
parents: 6216
diff changeset
   119
            pass
6006
3c9dbb743d20 merge: add registry look up bits to tool search
Matt Mackall <mpm@selenic.com>
parents: 5969
diff changeset
   120
2117
760339ccc799 fix issue 217.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2054
diff changeset
   121
def system_rcpath_win32():
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   122
    '''return default os-specific hgrc search path'''
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   123
    proc = win32api.GetCurrentProcess()
2285
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   124
    try:
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   125
        # 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
   126
        filename = win32process.GetModuleFileNameEx(proc, 0)
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   127
    except:
0912f807b7ff win98: fall back to win32api.GetModuleFileName if needed.
Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
parents: 2284
diff changeset
   128
        filename = win32api.GetModuleFileName(0)
5619
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   129
    # 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
   130
    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
   131
    if os.path.isfile(progrc):
55d3e845736a win32: favor local mercurial.ini over registry key
Steve Borho <steve@borho.org>
parents: 5612
diff changeset
   132
        return [progrc]
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