mercurial/posix.py
author Adrian Buehlmann <adrian@cadifra.com>
Wed, 06 Apr 2011 18:09:43 +0200
changeset 13916 98ee3dd5bab4
parent 13890 31eb145b50b6
child 13986 9c374cf76b7d
permissions -rw-r--r--
path_auditor: check filenames for basic platform validity (issue2755) Example (on Windows): $ hg parents $ hg manifest tip con.xml $ hg update abort: filename contains 'con', which is reserved on Windows: con.xml Before this patch, update produced (as explained in issue2755): $ hg update abort: No usable temporary filename found I've added the new function checkwinfilename to util.py and not to windows.py, so that we can later call it when running on posix platforms too, for when we decide to implement a (configurable) warning message on 'hg add'. As per this patch, checkwinfilename is currently only used when running on Windwows. path_auditor calls checkosfilename, which is a NOP on posix and an alias for checkwinfilename on Windows.
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
# posix.py - Posix 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: 10218
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
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     8
from i18n import _
8312
b87a50b7125c separate import lines from mercurial and general python modules
Simon Heimberg <simohe@besonet.ch>
parents: 8226
diff changeset
     9
import osutil
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
    10
import os, sys, errno, stat, getpass, pwd, grp, tempfile
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    11
9031
3b76321aa0de compat: use open() instead of file() everywhere
Alejandro Santos <alejolp@alejolp.com>
parents: 8761
diff changeset
    12
posixfile = open
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    13
nulldev = '/dev/null'
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    14
normpath = os.path.normpath
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    15
samestat = os.path.samestat
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    16
os_link = os.link
13280
6052bbc7aabd reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents: 13007
diff changeset
    17
unlink = os.unlink
9549
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9517
diff changeset
    18
rename = os.rename
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
    19
expandglobs = False
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    20
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    21
umask = os.umask(0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    22
os.umask(umask)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    23
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    24
def openhardlinks():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    25
    '''return true if it is safe to hold open file handles to hardlinks'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    26
    return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    28
def nlinks(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    29
    '''return number of hardlinks for the given file'''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    30
    return os.lstat(name).st_nlink
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    31
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    32
def rcfiles(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    33
    rcs = [os.path.join(path, 'hgrc')]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    34
    rcdir = os.path.join(path, 'hgrc.d')
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    35
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    36
        rcs.extend([os.path.join(rcdir, f)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    37
                    for f, kind in osutil.listdir(rcdir)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    38
                    if f.endswith(".rc")])
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    39
    except OSError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    40
        pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    41
    return rcs
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    42
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    43
def system_rcpath():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    44
    path = []
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
    # old mod_python does not set sys.argv
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    46
    if len(getattr(sys, 'argv', [])) > 0:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    47
        path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    48
                              '/../etc/mercurial'))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    49
    path.extend(rcfiles('/etc/mercurial'))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
    return path
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 user_rcpath():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
    return [os.path.expanduser('~/.hgrc')]
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 parse_patch_output(output_line):
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8657
diff changeset
    56
    """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
    57
    pf = output_line[14:]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    58
    if os.sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    59
        if pf[0] == '`':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    60
            pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
    else:
8219
21cf74ff2deb whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 7943
diff changeset
    62
        if pf.startswith("'") and pf.endswith("'") and " " in pf:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    63
            pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
    return pf
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    65
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    66
def sshargs(sshcmd, host, user, port):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
    '''Build argument list for ssh'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    68
    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
    69
    return port and ("%s -p %s" % (args, port)) or args
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    70
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    71
def is_exec(f):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    72
    """check whether a file is executable"""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    73
    return (os.lstat(f).st_mode & 0100 != 0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    74
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    75
def set_flags(f, l, x):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    76
    s = os.lstat(f).st_mode
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    77
    if l:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    78
        if not stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    79
            # switch file to link
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    80
            fp = open(f)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    81
            data = fp.read()
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    82
            fp.close()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    83
            os.unlink(f)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    84
            try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    85
                os.symlink(data, f)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    86
            except:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    87
                # failed to make a link, rewrite file
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    88
                fp = open(f, "w")
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    89
                fp.write(data)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    90
                fp.close()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    91
        # no chmod needed at this point
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    92
        return
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    93
    if stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    94
        # switch link to file
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    95
        data = os.readlink(f)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
        os.unlink(f)
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    97
        fp = open(f, "w")
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    98
        fp.write(data)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
    99
        fp.close()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   100
        s = 0666 & ~umask # avoid restatting for chmod
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   101
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
    sx = s & 0100
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   103
    if x and not sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   104
        # Turn on +x for every +r bit when making a file executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   105
        # and obey umask.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   106
        os.chmod(f, s | (s & 0444) >> 2 & ~umask)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   107
    elif not x and sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   108
        # Turn off all +x bits
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   109
        os.chmod(f, s & 0666)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   110
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   111
def checkexec(path):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   112
    """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   113
    Check whether the given path is on a filesystem with UNIX-like exec flags
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   114
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   115
    Requires a directory (like /foo/.hg)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   116
    """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   117
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   118
    # VFAT on some Linux versions can flip mode but it doesn't persist
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   119
    # a FS remount. Frequently we can detect it if files are created
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   120
    # with exec bit on.
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   121
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   122
    try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   123
        EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   124
        fh, fn = tempfile.mkstemp(dir=path, prefix='hg-checkexec-')
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   125
        try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   126
            os.close(fh)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   127
            m = os.stat(fn).st_mode & 0777
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   128
            new_file_has_exec = m & EXECFLAGS
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   129
            os.chmod(fn, m ^ EXECFLAGS)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   130
            exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   131
        finally:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   132
            os.unlink(fn)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   133
    except (IOError, OSError):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   134
        # we don't care, the user probably won't be able to commit anyway
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   135
        return False
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   136
    return not (new_file_has_exec or exec_flags_cannot_flip)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   137
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   138
def checklink(path):
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   139
    """check whether the given path is on a symlink-capable filesystem"""
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   140
    # mktemp is not racy because symlink creation will fail if the
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   141
    # file already exists
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   142
    name = tempfile.mktemp(dir=path, prefix='hg-checklink-')
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   143
    try:
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   144
        os.symlink(".", name)
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   145
        os.unlink(name)
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   146
        return True
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   147
    except (OSError, AttributeError):
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   148
        return False
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   149
13916
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   150
def checkosfilename(path):
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   151
    '''Check that the base-relative path is a valid filename on this platform.
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   152
    Returns None if the path is ok, or a UI string describing the problem.'''
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   153
    pass # on posix platforms, every path is ok
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   154
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   155
def set_binary(fd):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   156
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   157
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   158
def pconvert(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   159
    return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   161
def localpath(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   162
    return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   163
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   164
def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   165
    """Returns whether path1 and path2 refer to the same file. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   166
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   167
    return os.path.samefile(fpath1, fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   168
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   169
def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   170
    """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: 9549
diff changeset
   171
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   172
    st1 = os.lstat(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   173
    st2 = os.lstat(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   174
    return st1.st_dev == st2.st_dev
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   175
9238
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   176
if sys.platform == 'darwin':
10757
ab3782458827 posix: move a global fcntl import to keep it from breaking jython
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 10264
diff changeset
   177
    import fcntl # only needed on darwin, missing on jython
9238
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   178
    def realpath(path):
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   179
        '''
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   180
        Returns the true, canonical file system path equivalent to the given
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   181
        path.
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   182
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   183
        Equivalent means, in this case, resulting in the same, unique
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   184
        file system link to the path. Every file system entry, whether a file,
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   185
        directory, hard link or symbolic link or special, will have a single
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   186
        path preferred by the system, but may allow multiple, differing path
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   187
        lookups to point to it.
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   188
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   189
        Most regular UNIX file systems only allow a file system entry to be
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   190
        looked up by its distinct path. Obviously, this does not apply to case
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   191
        insensitive file systems, whether case preserving or not. The most
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   192
        complex issue to deal with is file systems transparently reencoding the
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   193
        path, such as the non-standard Unicode normalisation required for HFS+
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   194
        and HFSX.
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   195
        '''
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   196
        # Constants copied from /usr/include/sys/fcntl.h
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   197
        F_GETPATH = 50
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   198
        O_SYMLINK = 0x200000
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   199
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   200
        try:
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   201
            fd = os.open(path, O_SYMLINK)
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   202
        except OSError, err:
13007
e98bf6948092 posix: remove is-comparison between integers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12689
diff changeset
   203
            if err.errno == errno.ENOENT:
9238
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   204
                return path
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   205
            raise
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   206
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   207
        try:
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   208
            return fcntl.fcntl(fd, F_GETPATH, '\0' * 1024).rstrip('\0')
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   209
        finally:
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   210
            os.close(fd)
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   211
else:
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   212
    # Fallback to the likely inadequate Python builtin function.
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   213
    realpath = os.path.realpath
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   214
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   215
def shellquote(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   216
    if os.sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   217
        return '"%s"' % s
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   218
    else:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   219
        return "'%s'" % s.replace("'", "'\\''")
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   220
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   221
def quotecommand(cmd):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   222
    return cmd
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   223
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   224
def popen(command, mode='r'):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   225
    return os.popen(command, mode)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   226
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   227
def testpid(pid):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   228
    '''return False if pid dead, True if running or not sure'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   229
    if os.sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   230
        return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   231
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   232
        os.kill(pid, 0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   233
        return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   234
    except OSError, inst:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   235
        return inst.errno != errno.ESRCH
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   236
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   237
def explain_exit(code):
9517
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   238
    """return a 2-tuple (desc, code) describing a subprocess status
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   239
    (codes from kill are negative - not os.system/wait encoding)"""
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   240
    if code >= 0:
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   241
        return _("exited with status %d") % code, code
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   242
    return _("killed by signal %d") % -code, -code
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   243
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   244
def isowner(st):
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   245
    """Return True if the stat object st is from the current user."""
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   246
    return st.st_uid == os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   247
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   248
def find_exe(command):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   249
    '''Find executable for command searching like which does.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   250
    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
   251
    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
   252
    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
   253
    if sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   254
        return command
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   255
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   256
    def findexisting(executable):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   257
        'Will return executable if existing file'
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   258
        if os.path.exists(executable):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   259
            return executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   260
        return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   261
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   262
    if os.sep in command:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   263
        return findexisting(command)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   264
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   265
    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
   266
        executable = findexisting(os.path.join(path, command))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   267
        if executable is not None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   268
            return executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   269
    return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   270
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   271
def set_signal_handler():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   272
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   273
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   274
def statfiles(files):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   275
    'Stat each file in files and yield stat or None if file does not exist.'
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   276
    lstat = os.lstat
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   277
    for nf in files:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   278
        try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   279
            st = lstat(nf)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   280
        except OSError, err:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   281
            if err.errno not in (errno.ENOENT, errno.ENOTDIR):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   282
                raise
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   283
            st = None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   284
        yield st
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   285
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   286
def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   287
    '''return name of current user'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   288
    return getpass.getuser()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   289
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   290
def expand_glob(pats):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   291
    '''On Windows, expand the implicit globs in a list of patterns'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   292
    return list(pats)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   293
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   294
def username(uid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   295
    """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
   296
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   297
    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
   298
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   299
    if uid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   300
        uid = os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   301
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   302
        return pwd.getpwuid(uid)[0]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   303
    except KeyError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   304
        return str(uid)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   305
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   306
def groupname(gid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   307
    """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
   308
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   309
    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
   310
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   311
    if gid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   312
        gid = os.getgid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   313
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   314
        return grp.getgrgid(gid)[0]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   315
    except KeyError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   316
        return str(gid)
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   317
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   318
def groupmembers(name):
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   319
    """Return the list of members of the group with the given
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   320
    name, KeyError if the group does not exist.
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   321
    """
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   322
    return list(grp.getgrnam(name).gr_mem)
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   323
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   324
def spawndetached(args):
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   325
    return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   326
                      args[0], args)
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   327
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   328
def gethgcmd():
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   329
    return sys.argv[:1]
11010
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   330
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 11138
diff changeset
   331
def termwidth():
11010
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   332
    try:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   333
        import termios, array, fcntl
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   334
        for dev in (sys.stderr, sys.stdout, sys.stdin):
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   335
            try:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   336
                try:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   337
                    fd = dev.fileno()
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   338
                except AttributeError:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   339
                    continue
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   340
                if not os.isatty(fd):
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   341
                    continue
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   342
                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   343
                return array.array('h', arri)[1]
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   344
            except ValueError:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   345
                pass
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   346
            except IOError, e:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   347
                if e[0] == errno.EINVAL:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   348
                    pass
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   349
                else:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   350
                    raise
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   351
    except ImportError:
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   352
        pass
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   353
    return 80