mercurial/windows.py
author Matt Harbison <matt_harbison@yahoo.com>
Wed, 29 Apr 2015 21:14:59 -0400
branchstable
changeset 24885 eea3977e6fca
parent 24692 144883a8d0d4
child 24908 30b910fea244
permissions -rw-r--r--
windows: make shellquote() quote any path containing '\' (issue4629) The '~' in the bug report is being expanded to a path with Windows style slashes before being passed to shellquote() via util.shellquote(). But shlex.split() strips '\' out of the string, leaving an invalid path in dispatch.aliasargs(). This regressed in 1642eb429536. For now, the tests need to be conditionalized for Windows (because those paths are quoted). In the future, a more complex regex could probably skip the quotes if all component separators are double '\'. I opted to glob away the quotes in test-rename-merge2.t and test-up-local-change.t (which only exist on Windows), because they are in very large blocks of output and there are way too many diffs to conditionalize with #if directives. Maybe the entire path should be globbed away like the following paths in each changed line. Or, letting #if directives sit in the middle of the output as was mentioned a few months back would work too. Unfortunately, I couldn't figure out how to test the specific bug. All of the 'hg serve' tests have a #require serve declaration, causing them to be skipped on Windows. Adding an alias for 'expandtest = outgoing ~/bogusrepo' prints the repo as '$TESTTMP/bogusrepo', so the test runner must be changing the environment somehow.
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
# windows.py - Windows utility function implementations for Mercurial
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: 9594
diff changeset
     6
# GNU General Public License version 2 or any later version.
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
7914
48661519913f windows: add various missing import
Steve Borho <steve@borho.org>
parents: 7913
diff changeset
     8
from i18n import _
16807
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
     9
import osutil, encoding
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
    10
import errno, msvcrt, os, re, stat, sys, _winreg
8421
b6d0fa8c7685 posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents: 8364
diff changeset
    11
15016
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    12
import win32
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    13
executablepath = win32.executablepath
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    14
getuser = win32.getuser
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    15
hidewindow = win32.hidewindow
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    16
makedir = win32.makedir
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    17
nlinks = win32.nlinks
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    18
oslink = win32.oslink
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    19
samedevice = win32.samedevice
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    20
samefile = win32.samefile
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    21
setsignalhandler = win32.setsignalhandler
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    22
spawndetached = win32.spawndetached
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17537
diff changeset
    23
split = os.path.split
15016
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    24
termwidth = win32.termwidth
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    25
testpid = win32.testpid
871c77e78f5d windows: fix pyflakes warning on unused imports
Matt Mackall <mpm@selenic.com>
parents: 15011
diff changeset
    26
unlink = win32.unlink
14985
dbf91976f900 windows: eliminate win32 wildcard import
Adrian Buehlmann <adrian@cadifra.com>
parents: 14969
diff changeset
    27
15440
9ab2b3b730ee windows: use umask 022 in debugstate output
Mads Kiilerich <mads@kiilerich.com>
parents: 15016
diff changeset
    28
umask = 0022
24051
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    29
_SEEK_END = 2 # os.SEEK_END was introduced in Python 2.5
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    30
8421
b6d0fa8c7685 posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents: 8364
diff changeset
    31
def posixfile(name, mode='r', buffering=-1):
24069
c6666395fdd2 windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents: 24051
diff changeset
    32
    '''Open a file with even more POSIX-like semantics'''
8421
b6d0fa8c7685 posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents: 8364
diff changeset
    33
    try:
24069
c6666395fdd2 windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents: 24051
diff changeset
    34
        fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError
24051
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    35
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    36
        # The position when opening in append mode is implementation defined, so
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    37
        # make it consistent with other platforms, which position at EOF.
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    38
        if 'a' in mode:
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    39
            fp.seek(0, _SEEK_END)
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    40
7956d17431bc windows: seek to the end of posixfile when opening in append mode
Matt Harbison <matt_harbison@yahoo.com>
parents: 23682
diff changeset
    41
        return fp
8421
b6d0fa8c7685 posixfile: remove posixfile_nt and fix import bug in windows.py
Sune Foldager <cryo@cyanite.org>
parents: 8364
diff changeset
    42
    except WindowsError, err:
24069
c6666395fdd2 windows: adjust doc string and comments of posixfile()
Adrian Buehlmann <adrian@cadifra.com>
parents: 24051
diff changeset
    43
        # convert to a friendlier exception
9448
bc6b0fef9495 windows: provide filename in IOError exceptions
Steve Borho <steve@borho.org>
parents: 9174
diff changeset
    44
        raise IOError(err.errno, '%s: %s' % (name, err.strerror))
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8761
diff changeset
    46
class winstdout(object):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    47
    '''stdout on windows misbehaves if sent through a pipe'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    48
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    49
    def __init__(self, fp):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
        self.fp = fp
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
    def __getattr__(self, key):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
        return getattr(self.fp, key)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    54
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    55
    def close(self):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    56
        try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    57
            self.fp.close()
14004
97ed99d1f419 eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents: 13986
diff changeset
    58
        except IOError:
97ed99d1f419 eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents: 13986
diff changeset
    59
            pass
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    60
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
    def write(self, s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
        try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    63
            # This is workaround for "Not enough space" error on
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
            # writing large size of data to console.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    65
            limit = 16000
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    66
            l = len(s)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
            start = 0
10394
4612cded5176 fix coding style (reported by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
    68
            self.softspace = 0
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    69
            while start < l:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    70
                end = start + limit
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    71
                self.fp.write(s[start:end])
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    72
                start = end
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    73
        except IOError, inst:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    74
            if inst.errno != 0:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    75
                raise
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    76
            self.close()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    77
            raise IOError(errno.EPIPE, 'Broken pipe')
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    78
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    79
    def flush(self):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    80
        try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    81
            return self.fp.flush()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    82
        except IOError, inst:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    83
            if inst.errno != errno.EINVAL:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
    84
                raise
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    85
            self.close()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    86
            raise IOError(errno.EPIPE, 'Broken pipe')
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    87
14892
d2d592718e90 win32: assign winstdout to sys.__stdout__ as well (issue2888)
Idan Kamara <idankk86@gmail.com>
parents: 14271
diff changeset
    88
sys.__stdout__ = sys.stdout = winstdout(sys.stdout)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    89
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    90
def _is_win_9x():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    91
    '''return true if run on windows 95, 98 or me.'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    92
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    93
        return sys.getwindowsversion()[3] == 1
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    94
    except AttributeError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    95
        return 'command' in os.environ.get('comspec', '')
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    97
def openhardlinks():
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    98
    return not _is_win_9x()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    99
14231
8abe4db2d162 rename util.parse_patch_output to parsepatchoutput
Adrian Buehlmann <adrian@cadifra.com>
parents: 14064
diff changeset
   100
def parsepatchoutput(output_line):
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8657
diff changeset
   101
    """parses the output produced by patch and returns the filename"""
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
    pf = output_line[14:]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   103
    if pf[0] == '`':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   104
        pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   105
    return pf
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   106
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   107
def sshargs(sshcmd, host, user, port):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   108
    '''Build argument list for ssh or Plink'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   109
    pflag = 'plink' in sshcmd.lower() and '-P' or '-p'
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   110
    args = user and ("%s@%s" % (user, host)) or host
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   111
    return port and ("%s %s %s" % (args, pflag, port)) or args
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   112
14232
df2399663392 rename util.set_flags to setflags
Adrian Buehlmann <adrian@cadifra.com>
parents: 14231
diff changeset
   113
def setflags(f, l, x):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   114
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   115
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14985
diff changeset
   116
def copymode(src, dst, mode=None):
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14985
diff changeset
   117
    pass
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14985
diff changeset
   118
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13777
diff changeset
   119
def checkexec(path):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13777
diff changeset
   120
    return False
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13777
diff changeset
   121
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   122
def checklink(path):
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   123
    return False
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   124
14233
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 14232
diff changeset
   125
def setbinary(fd):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   126
    # When run without console, pipes may expose invalid
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   127
    # fileno(), usually set to -1.
14969
a3f97038c1c2 windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14927
diff changeset
   128
    fno = getattr(fd, 'fileno', None)
a3f97038c1c2 windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14927
diff changeset
   129
    if fno is not None and fno() >= 0:
a3f97038c1c2 windows: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14927
diff changeset
   130
        msvcrt.setmode(fno(), os.O_BINARY)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   131
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   132
def pconvert(path):
16076
e7701459fb42 windows: use 'str.replace()' instead of combination of split() and join()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15674
diff changeset
   133
    return path.replace(os.sep, '/')
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   134
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   135
def localpath(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   136
    return path.replace('/', '\\')
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   137
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   138
def normpath(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   139
    return pconvert(os.path.normpath(path))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   140
15671
3c5e818ac679 windows: use upper() instead of lower() or os.path.normcase()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15637
diff changeset
   141
def normcase(path):
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   142
    return encoding.upper(path)
15488
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15016
diff changeset
   143
24598
22f49c7dd11b windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24148
diff changeset
   144
# see posix.py for definitions
22f49c7dd11b windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24148
diff changeset
   145
normcasespec = encoding.normcasespecs.upper
22f49c7dd11b windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24148
diff changeset
   146
normcasefallback = encoding.upperfallback
22f49c7dd11b windows: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24148
diff changeset
   147
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   148
def samestat(s1, s2):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   149
    return False
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   150
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   151
# A sequence of backslashes is special iff it precedes a double quote:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   152
# - if there's an even number of backslashes, the double quote is not
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   153
#   quoted (i.e. it ends the quoted region)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   154
# - if there's an odd number of backslashes, the double quote is quoted
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   155
# - in both cases, every pair of backslashes is unquoted into a single
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   156
#   backslash
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   157
# (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx )
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   158
# So, to quote a string, we must surround it in double quotes, double
17505
ae791d371864 spelling: precede
timeless@mozdev.org
parents: 17223
diff changeset
   159
# the number of backslashes that precede double quotes and add another
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
# backslash before every double quote (being careful with the double
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   161
# quote we've appended to the end)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   162
_quotere = None
23682
1642eb429536 windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22245
diff changeset
   163
_needsshellquote = None
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   164
def shellquote(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   165
    global _quotere
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   166
    if _quotere is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   167
        _quotere = re.compile(r'(\\*)("|\\$)')
23682
1642eb429536 windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22245
diff changeset
   168
    global _needsshellquote
1642eb429536 windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22245
diff changeset
   169
    if _needsshellquote is None:
24885
eea3977e6fca windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents: 24692
diff changeset
   170
        # ":" is also treated as "safe character", because it is used as a part
eea3977e6fca windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents: 24692
diff changeset
   171
        # of path name on Windows.  "\" is also part of a path name, but isn't
eea3977e6fca windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents: 24692
diff changeset
   172
        # safe because shlex.split() (kind of) treats it as an escape char and
eea3977e6fca windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents: 24692
diff changeset
   173
        # drops it.  It will leave the next character, even if it is another
eea3977e6fca windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents: 24692
diff changeset
   174
        # "\".
eea3977e6fca windows: make shellquote() quote any path containing '\' (issue4629)
Matt Harbison <matt_harbison@yahoo.com>
parents: 24692
diff changeset
   175
        _needsshellquote = re.compile(r'[^a-zA-Z0-9._:/-]').search
24108
d65ecb814fc0 shellquote: fix missing quotes for empty string
Yuya Nishihara <yuya@tcha.org>
parents: 23682
diff changeset
   176
    if s and not _needsshellquote(s) and not _quotere.search(s):
23682
1642eb429536 windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22245
diff changeset
   177
        # "s" shouldn't have to be quoted
1642eb429536 windows: quote the specified string only when it has to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22245
diff changeset
   178
        return s
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   179
    return '"%s"' % _quotere.sub(r'\1\1\\\2', s)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   180
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   181
def quotecommand(cmd):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   182
    """Build a command string suitable for os.popen* calls."""
13188
6c9345f9edca util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents: 12689
diff changeset
   183
    if sys.version_info < (2, 7, 1):
6c9345f9edca util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents: 12689
diff changeset
   184
        # Python versions since 2.7.1 do this extra quoting themselves
6c9345f9edca util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents: 12689
diff changeset
   185
        return '"' + cmd + '"'
6c9345f9edca util: concentrate quoting knowledge to windows.py quotecommand()
Steve Borho <steve@borho.org>
parents: 12689
diff changeset
   186
    return cmd
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   187
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   188
def popen(command, mode='r'):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   189
    # Work around "popen spawned process may not write to stdout
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   190
    # under windows"
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   191
    # http://bugs.python.org/issue1366
17391
fc24c10424d2 util: replace util.nulldev with os.devnull
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17223
diff changeset
   192
    command += " 2> %s" % os.devnull
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   193
    return os.popen(quotecommand(command), mode)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   194
14234
600e64004eb5 rename explain_exit to explainexit
Adrian Buehlmann <adrian@cadifra.com>
parents: 14233
diff changeset
   195
def explainexit(code):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   196
    return _("exited with status %d") % code, code
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   197
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   198
# if you change this stub into a real check, please try to implement the
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   199
# username and groupname functions above, too.
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   200
def isowner(st):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   201
    return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   202
14271
4030630fb59c rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents: 14234
diff changeset
   203
def findexe(command):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   204
    '''Find executable for command searching like cmd.exe does.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   205
    If command is a basename then PATH is searched for command.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   206
    PATH isn't searched if command is an absolute or relative path.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   207
    An extension from PATHEXT is found and added if not present.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   208
    If command isn't found None is returned.'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   209
    pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD')
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   210
    pathexts = [ext for ext in pathext.lower().split(os.pathsep)]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   211
    if os.path.splitext(command)[1].lower() in pathexts:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   212
        pathexts = ['']
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   213
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   214
    def findexisting(pathcommand):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   215
        'Will append extension (if needed) and return existing file'
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   216
        for ext in pathexts:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   217
            executable = pathcommand + ext
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   218
            if os.path.exists(executable):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   219
                return executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   220
        return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   221
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   222
    if os.sep in command:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   223
        return findexisting(command)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   224
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   225
    for path in os.environ.get('PATH', '').split(os.pathsep):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   226
        executable = findexisting(os.path.join(path, command))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   227
        if executable is not None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   228
            return executable
10156
c31ac3f7fd8f windows: expand environment vars in find_exe
Steve Borho <steve@borho.org>
parents: 9594
diff changeset
   229
    return findexisting(os.path.expanduser(os.path.expandvars(command)))
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   230
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   231
_wantedkinds = set([stat.S_IFREG, stat.S_IFLNK])
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   232
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   233
def statfiles(files):
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   234
    '''Stat each file in files. Yield each stat, or None if a file
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   235
    does not exist or has a type we don't care about.
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   236
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   237
    Cluster and cache stat per directory to minimize number of OS stat calls.'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   238
    dircache = {} # dirname -> filename -> status | None if file does not exist
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   239
    getkind = stat.S_IFMT
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   240
    for nf in files:
15637
7f01ad702405 icasefs: use util.normcase() instead of str.lower() or os.path.normpath()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15488
diff changeset
   241
        nf  = normcase(nf)
9099
3d456bf32f18 Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents: 8951
diff changeset
   242
        dir, base = os.path.split(nf)
3d456bf32f18 Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents: 8951
diff changeset
   243
        if not dir:
3d456bf32f18 Use os.path.split() for MBCS with win32mbcs extension.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents: 8951
diff changeset
   244
            dir = '.'
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   245
        cache = dircache.get(dir, None)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   246
        if cache is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   247
            try:
15637
7f01ad702405 icasefs: use util.normcase() instead of str.lower() or os.path.normpath()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15488
diff changeset
   248
                dmap = dict([(normcase(n), s)
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   249
                             for n, k, s in osutil.listdir(dir, True)
18041
f0cfa27c712a windows: correctly pass a mode to S_IFMT in statfiles
Matt Mackall <mpm@selenic.com>
parents: 18017
diff changeset
   250
                             if getkind(s.st_mode) in _wantedkinds])
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   251
            except OSError, err:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   252
                # handle directory not found in Python version prior to 2.5
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   253
                # Python <= 2.4 returns native Windows code 3 in errno
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   254
                # Python >= 2.5 returns ENOENT and adds winerror field
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   255
                # EINVAL is raised if dir is not a directory.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   256
                if err.errno not in (3, errno.ENOENT, errno.EINVAL,
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   257
                                     errno.ENOTDIR):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   258
                    raise
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   259
                dmap = {}
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   260
            cache = dircache.setdefault(dir, dmap)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   261
        yield cache.get(base, None)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   262
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   263
def username(uid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   264
    """Return the name of the user with the given uid.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   265
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   266
    If uid is None, return the name of the current user."""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   267
    return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   268
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   269
def groupname(gid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   270
    """Return the name of the group with the given gid.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   271
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   272
    If gid is None, return the name of the current group."""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   273
    return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   274
24692
144883a8d0d4 util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24653
diff changeset
   275
def removedirs(name):
8364
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   276
    """special version of os.removedirs that does not remove symlinked
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   277
    directories or junction points if they actually contain files"""
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   278
    if osutil.listdir(name):
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   279
        return
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   280
    os.rmdir(name)
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   281
    head, tail = os.path.split(name)
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   282
    if not tail:
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   283
        head, tail = os.path.split(head)
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   284
    while head and tail:
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   285
        try:
9572
1f665246dab3 windows: fix unlink() not dropping empty tree (issue1861)
Patrick Mezard <pmezard@gmail.com>
parents: 9448
diff changeset
   286
            if osutil.listdir(head):
8364
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   287
                return
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   288
            os.rmdir(head)
14004
97ed99d1f419 eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents: 13986
diff changeset
   289
        except (ValueError, OSError):
8364
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   290
            break
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   291
        head, tail = os.path.split(head)
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   292
18143
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18041
diff changeset
   293
def unlinkpath(f, ignoremissing=False):
8364
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   294
    """unlink and remove the directory if it is empty"""
18143
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18041
diff changeset
   295
    try:
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18041
diff changeset
   296
        unlink(f)
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18041
diff changeset
   297
    except OSError, e:
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18041
diff changeset
   298
        if not (ignoremissing and e.errno == errno.ENOENT):
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18041
diff changeset
   299
            raise
8364
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   300
    # try removing directories that might now be empty
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   301
    try:
24692
144883a8d0d4 util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24653
diff changeset
   302
        removedirs(os.path.dirname(f))
8364
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   303
    except OSError:
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   304
        pass
fa901423ac23 windows: avoid deleting non-empty reparse points
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 8330
diff changeset
   305
9549
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9449
diff changeset
   306
def rename(src, dst):
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9449
diff changeset
   307
    '''atomically rename file src to dst, replacing dst if it exists'''
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9449
diff changeset
   308
    try:
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9449
diff changeset
   309
        os.rename(src, dst)
13278
e9a52ed28157 windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents: 13235
diff changeset
   310
    except OSError, e:
e9a52ed28157 windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents: 13235
diff changeset
   311
        if e.errno != errno.EEXIST:
e9a52ed28157 windows.rename: check OSError for EEXIST
Adrian Buehlmann <adrian@cadifra.com>
parents: 13235
diff changeset
   312
            raise
13280
6052bbc7aabd reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents: 13278
diff changeset
   313
        unlink(dst)
9549
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9449
diff changeset
   314
        os.rename(src, dst)
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9449
diff changeset
   315
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   316
def gethgcmd():
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   317
    return [sys.executable] + sys.argv[:1]
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   318
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11010
diff changeset
   319
def groupmembers(name):
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11010
diff changeset
   320
    # Don't support groups on Windows for now
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 16076
diff changeset
   321
    raise KeyError
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11010
diff changeset
   322
14926
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14892
diff changeset
   323
def isexec(f):
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14892
diff changeset
   324
    return False
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14892
diff changeset
   325
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   326
class cachestat(object):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   327
    def __init__(self, path):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   328
        pass
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   329
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   330
    def cacheable(self):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   331
        return False
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   332
16807
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   333
def lookupreg(key, valname=None, scope=None):
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   334
    ''' Look up a key/value name in the Windows registry.
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   335
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   336
    valname: value name. If unspecified, the default value for the key
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   337
    is used.
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   338
    scope: optionally specify scope for registry lookup, this can be
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   339
    a sequence of scopes to look up in order. Default (CURRENT_USER,
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   340
    LOCAL_MACHINE).
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   341
    '''
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   342
    if scope is None:
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   343
        scope = (_winreg.HKEY_CURRENT_USER, _winreg.HKEY_LOCAL_MACHINE)
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   344
    elif not isinstance(scope, (list, tuple)):
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   345
        scope = (scope,)
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   346
    for s in scope:
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   347
        try:
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   348
            val = _winreg.QueryValueEx(_winreg.OpenKey(s, key), valname)[0]
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   349
            # never let a Unicode string escape into the wild
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   350
            return encoding.tolocal(val.encode('UTF-8'))
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   351
        except EnvironmentError:
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   352
            pass
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16687
diff changeset
   353
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8559
diff changeset
   354
expandglobs = True
18868
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   355
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   356
def statislink(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   357
    '''check whether a stat result is a symlink'''
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   358
    return False
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   359
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   360
def statisexec(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   361
    '''check whether a stat result is an executable file'''
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18143
diff changeset
   362
    return False
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   363
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   364
def readpipe(pipe):
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   365
    """Read all available data from a pipe."""
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   366
    chunks = []
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   367
    while True:
24653
83f6c4733ecc windows: allow readpipe() to actually read data out of the pipe
Matt Harbison <matt_harbison@yahoo.com>
parents: 24598
diff changeset
   368
        size = win32.peekpipe(pipe)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   369
        if not size:
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   370
            break
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   371
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   372
        s = pipe.read(size)
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   373
        if not s:
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   374
            break
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   375
        chunks.append(s)
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   376
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   377
    return ''.join(chunks)