mercurial/posix.py
author Pulkit Goyal <7895pulkit@gmail.com>
Sun, 04 Dec 2016 23:22:34 +0530
changeset 30571 91db2aa0edfe
parent 30555 6a672c3b7860
child 30612 d623cc6b3742
permissions -rw-r--r--
localrepository: remove None as default value of path argument in __init__() The path variable in localrepository.__init__() has a default value None. So it gives us a option to create an object to localrespository class without path variable. But things break if you try to do so. The second line in the init which will be executed when we try to create a localrepository object will call os.path.expandvars(path) which returns TypeError: argument of type 'NoneType' is not iterable I checked occurrences when it is called and can't find any piece of code which calls it without path variable. Also if something is calling it, its should break.
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
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     8
from __future__ import absolute_import
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     9
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    10
import errno
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    11
import fcntl
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    12
import getpass
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    13
import grp
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    14
import os
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    15
import pwd
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    16
import re
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
    17
import select
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    18
import stat
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    19
import sys
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    20
import tempfile
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    21
import unicodedata
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    22
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    23
from .i18n import _
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    24
from . import (
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    25
    encoding,
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    26
)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
9031
3b76321aa0de compat: use open() instead of file() everywhere
Alejandro Santos <alejolp@alejolp.com>
parents: 8761
diff changeset
    28
posixfile = open
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    29
normpath = os.path.normpath
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    30
samestat = os.path.samestat
27236
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    31
try:
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    32
    oslink = os.link
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    33
except AttributeError:
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    34
    # Some platforms build Python without os.link on systems that are
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    35
    # vaguely unix-like but don't have hardlink support. For those
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    36
    # poor souls, just say we tried and that it failed so we fall back
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    37
    # to copies.
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    38
    def oslink(src, dst):
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    39
        raise OSError(errno.EINVAL,
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    40
                      'hardlinks not supported: %s to %s' % (src, dst))
13280
6052bbc7aabd reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents: 13007
diff changeset
    41
unlink = os.unlink
9549
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9517
diff changeset
    42
rename = os.rename
24692
144883a8d0d4 util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24596
diff changeset
    43
removedirs = os.removedirs
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
    44
expandglobs = False
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    46
umask = os.umask(0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    47
os.umask(umask)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    48
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    49
def split(p):
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    50
    '''Same as posixpath.split, but faster
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    51
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    52
    >>> import posixpath
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    53
    >>> for f in ['/absolute/path/to/file',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    54
    ...           'relative/path/to/file',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    55
    ...           'file_alone',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    56
    ...           'path/to/directory/',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    57
    ...           '/multiple/path//separators',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    58
    ...           '/file_at_root',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    59
    ...           '///multiple_leading_separators_at_root',
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    60
    ...           '']:
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    61
    ...     assert split(f) == posixpath.split(f), f
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    62
    '''
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    63
    ht = p.rsplit('/', 1)
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    64
    if len(ht) == 1:
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    65
        return '', p
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    66
    nh = ht[0].rstrip('/')
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    67
    if nh:
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    68
        return nh, ht[1]
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    69
    return ht[0] + '/', ht[1]
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    70
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    71
def openhardlinks():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    72
    '''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
    73
    return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    74
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    75
def nlinks(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
    76
    '''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
    77
    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
    78
14231
8abe4db2d162 rename util.parse_patch_output to parsepatchoutput
Adrian Buehlmann <adrian@cadifra.com>
parents: 14165
diff changeset
    79
def parsepatchoutput(output_line):
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8657
diff changeset
    80
    """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
    81
    pf = output_line[14:]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    82
    if os.sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    83
        if pf[0] == '`':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    84
            pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    85
    else:
8219
21cf74ff2deb whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 7943
diff changeset
    86
        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
    87
            pf = pf[1:-1] # Remove the quotes
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    88
    return pf
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 sshargs(sshcmd, host, user, port):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    91
    '''Build argument list for ssh'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    92
    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
    93
    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
    94
14273
38af0f514134 rename util.is_exec to isexec
Adrian Buehlmann <adrian@cadifra.com>
parents: 14272
diff changeset
    95
def isexec(f):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
    """check whether a file is executable"""
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
    97
    return (os.lstat(f).st_mode & 0o100 != 0)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    98
14232
df2399663392 rename util.set_flags to setflags
Adrian Buehlmann <adrian@cadifra.com>
parents: 14231
diff changeset
    99
def setflags(f, l, x):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   100
    s = os.lstat(f).st_mode
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   101
    if l:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
        if not stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   103
            # switch file to link
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   104
            fp = open(f)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   105
            data = fp.read()
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   106
            fp.close()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   107
            os.unlink(f)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   108
            try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   109
                os.symlink(data, f)
14004
97ed99d1f419 eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents: 13986
diff changeset
   110
            except OSError:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   111
                # failed to make a link, rewrite file
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   112
                fp = open(f, "w")
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   113
                fp.write(data)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   114
                fp.close()
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   115
        # no chmod needed at this point
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   116
        return
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   117
    if stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   118
        # switch link to file
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   119
        data = os.readlink(f)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   120
        os.unlink(f)
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   121
        fp = open(f, "w")
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   122
        fp.write(data)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13375
diff changeset
   123
        fp.close()
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   124
        s = 0o666 & ~umask # avoid restatting for chmod
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   125
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   126
    sx = s & 0o100
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   127
    if x and not sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   128
        # 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
   129
        # and obey umask.
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   130
        os.chmod(f, s | (s & 0o444) >> 2 & ~umask)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   131
    elif not x and sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   132
        # Turn off all +x bits
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   133
        os.chmod(f, s & 0o666)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   134
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   135
def copymode(src, dst, mode=None):
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   136
    '''Copy the file mode from the file at path src to dst.
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   137
    If src doesn't exist, we're using mode instead. If mode is None, we're
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   138
    using umask.'''
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   139
    try:
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   140
        st_mode = os.lstat(src).st_mode & 0o777
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   141
    except OSError as inst:
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   142
        if inst.errno != errno.ENOENT:
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   143
            raise
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   144
        st_mode = mode
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   145
        if st_mode is None:
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   146
            st_mode = ~umask
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   147
        st_mode &= 0o666
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   148
    os.chmod(dst, st_mode)
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   149
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   150
def checkexec(path):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   151
    """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   152
    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
   153
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   154
    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
   155
    """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   156
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   157
    # 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
   158
    # 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
   159
    # 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
   160
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   161
    try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   162
        EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
30444
b1ce25a40826 posix: move checkexec test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30309
diff changeset
   163
        cachedir = os.path.join(path, '.hg', 'cache')
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   164
        if os.path.isdir(cachedir):
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   165
            checkisexec = os.path.join(cachedir, 'checkisexec')
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   166
            checknoexec = os.path.join(cachedir, 'checknoexec')
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   167
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   168
            try:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   169
                m = os.stat(checkisexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   170
            except OSError as e:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   171
                if e.errno != errno.ENOENT:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   172
                    raise
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   173
                # checkisexec does not exist - fall through ...
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   174
            else:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   175
                # checkisexec exists, check if it actually is exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   176
                if m & EXECFLAGS != 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   177
                    # ensure checkisexec exists, check it isn't exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   178
                    try:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   179
                        m = os.stat(checknoexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   180
                    except OSError as e:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   181
                        if e.errno != errno.ENOENT:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   182
                            raise
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   183
                        file(checknoexec, 'w').close() # might fail
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   184
                        m = os.stat(checknoexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   185
                    if m & EXECFLAGS == 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   186
                        # check-exec is exec and check-no-exec is not exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   187
                        return True
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   188
                    # checknoexec exists but is exec - delete it
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   189
                    os.unlink(checknoexec)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   190
                # checkisexec exists but is not exec - delete it
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   191
                os.unlink(checkisexec)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   192
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   193
            # check using one file, leave it as checkisexec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   194
            checkdir = cachedir
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   195
        else:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   196
            # check directly in path and don't leave checkisexec behind
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   197
            checkdir = path
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   198
            checkisexec = None
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   199
        fh, fn = tempfile.mkstemp(dir=checkdir, prefix='hg-checkexec-')
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   200
        try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   201
            os.close(fh)
30445
1ce4c2062ab0 posix: simplify checkexec check
Mads Kiilerich <madski@unity3d.com>
parents: 30444
diff changeset
   202
            m = os.stat(fn).st_mode
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   203
            if m & EXECFLAGS == 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   204
                os.chmod(fn, m & 0o777 | EXECFLAGS)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   205
                if os.stat(fn).st_mode & EXECFLAGS != 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   206
                    if checkisexec is not None:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   207
                        os.rename(fn, checkisexec)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   208
                        fn = None
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   209
                    return True
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   210
        finally:
30446
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   211
            if fn is not None:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30445
diff changeset
   212
                os.unlink(fn)
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   213
    except (IOError, OSError):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   214
        # 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
   215
        return False
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   216
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   217
def checklink(path):
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   218
    """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
   219
    # 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
   220
    # file already exists
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   221
    while True:
30447
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   222
        cachedir = os.path.join(path, '.hg', 'cache')
30448
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   223
        checklink = os.path.join(cachedir, 'checklink')
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   224
        # try fast path, read only
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   225
        if os.path.islink(checklink):
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   226
            return True
30447
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   227
        if os.path.isdir(cachedir):
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   228
            checkdir = cachedir
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   229
        else:
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   230
            checkdir = path
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   231
            cachedir = None
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30446
diff changeset
   232
        name = tempfile.mktemp(dir=checkdir, prefix='checklink-')
22946
77c121da6143 checklink: always close the NamedTemporaryFile
Augie Fackler <raf@durin42.com>
parents: 22781
diff changeset
   233
        try:
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   234
            fd = None
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   235
            if cachedir is None:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   236
                fd = tempfile.NamedTemporaryFile(dir=checkdir,
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   237
                                                 prefix='hg-checklink-')
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   238
                target = os.path.basename(fd.name)
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   239
            else:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   240
                # create a fixed file to link to; doesn't matter if it
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   241
                # already exists.
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   242
                target = 'checklink-target'
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   243
                open(os.path.join(cachedir, target), 'w').close()
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   244
            try:
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   245
                os.symlink(target, name)
30448
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   246
                if cachedir is None:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   247
                    os.unlink(name)
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   248
                else:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   249
                    try:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   250
                        os.rename(name, checklink)
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   251
                    except OSError:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30447
diff changeset
   252
                        os.unlink(name)
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   253
                return True
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   254
            except OSError as inst:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   255
                # link creation might race, try again
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   256
                if inst[0] == errno.EEXIST:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   257
                    continue
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   258
                raise
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   259
            finally:
30555
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   260
                if fd is not None:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30448
diff changeset
   261
                    fd.close()
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   262
        except AttributeError:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   263
            return False
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   264
        except OSError as inst:
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   265
            # sshfs might report failure while successfully creating the link
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   266
            if inst[0] == errno.EIO and os.path.exists(name):
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   267
                os.unlink(name)
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   268
            return False
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   269
13916
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   270
def checkosfilename(path):
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   271
    '''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
   272
    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
   273
    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
   274
14233
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 14232
diff changeset
   275
def setbinary(fd):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   276
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   277
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   278
def pconvert(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   279
    return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   280
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   281
def localpath(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   282
    return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   283
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   284
def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   285
    """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
   286
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   287
    return os.path.samefile(fpath1, fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   288
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   289
def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   290
    """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
   291
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   292
    st1 = os.lstat(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   293
    st2 = os.lstat(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   294
    return st1.st_dev == st2.st_dev
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   295
15488
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   296
# os.path.normcase is a no-op, which doesn't help us on non-native filesystems
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   297
def normcase(path):
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   298
    return path.lower()
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   299
24594
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   300
# what normcase does to ASCII strings
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   301
normcasespec = encoding.normcasespecs.lower
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   302
# fallback normcase function for non-ASCII strings
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   303
normcasefallback = normcase
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   304
9238
40196d036a71 util: add realpath() for getting the 'true' path.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 9031
diff changeset
   305
if sys.platform == 'darwin':
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   306
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   307
    def normcase(path):
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   308
        '''
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   309
        Normalize a filename for OS X-compatible comparison:
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   310
        - escape-encode invalid characters
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   311
        - decompose to NFD
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   312
        - lowercase
23597
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   313
        - omit ignored characters [200c-200f, 202a-202e, 206a-206f,feff]
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   314
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   315
        >>> normcase('UPPER')
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   316
        'upper'
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   317
        >>> normcase('Caf\xc3\xa9')
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   318
        'cafe\\xcc\\x81'
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   319
        >>> normcase('\xc3\x89')
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   320
        'e\\xcc\\x81'
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   321
        >>> normcase('\xb8\xca\xc3\xca\xbe\xc8.JPG') # issue3918
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   322
        '%b8%ca%c3\\xca\\xbe%c8.jpg'
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   323
        '''
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   324
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   325
        try:
22781
70624fda193d normcase: for darwin, use fast ASCII lower
Siddharth Agarwal <sid0@fb.com>
parents: 22246
diff changeset
   326
            return encoding.asciilower(path)  # exception for non-ASCII
18501
a3b2dc1aa909 OS X: try cheap ascii .lower() in normcase before making full unicode dance
Mads Kiilerich <madski@unity3d.com>
parents: 18442
diff changeset
   327
        except UnicodeDecodeError:
24595
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   328
            return normcasefallback(path)
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   329
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   330
    normcasespec = encoding.normcasespecs.lower
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   331
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   332
    def normcasefallback(path):
18501
a3b2dc1aa909 OS X: try cheap ascii .lower() in normcase before making full unicode dance
Mads Kiilerich <madski@unity3d.com>
parents: 18442
diff changeset
   333
        try:
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   334
            u = path.decode('utf-8')
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   335
        except UnicodeDecodeError:
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   336
            # OS X percent-encodes any bytes that aren't valid utf-8
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   337
            s = ''
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   338
            pos = 0
27380
c7129ed280b8 mac: fix percent-encoding of non-utf-8 characters (issue4999)
Matt Mackall <mpm@selenic.com>
parents: 27362
diff changeset
   339
            l = len(path)
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   340
            while pos < l:
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   341
                try:
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   342
                    c = encoding.getutf8char(path, pos)
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   343
                    pos += len(c)
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   344
                except ValueError:
27380
c7129ed280b8 mac: fix percent-encoding of non-utf-8 characters (issue4999)
Matt Mackall <mpm@selenic.com>
parents: 27362
diff changeset
   345
                    c = '%%%02X' % ord(path[pos])
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   346
                    pos += 1
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   347
                s += c
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   348
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   349
            u = s.decode('utf-8')
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   350
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   351
        # Decompose then lowercase (HFS+ technote specifies lower)
23597
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   352
        enc = unicodedata.normalize('NFD', u).lower().encode('utf-8')
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   353
        # drop HFS+ ignored characters
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   354
        return encoding.hfsignoreclean(enc)
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   355
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   356
if sys.platform == 'cygwin':
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   357
    # workaround for cygwin, in which mount point part of path is
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   358
    # treated as case sensitive, even though underlying NTFS is case
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   359
    # insensitive.
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   360
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   361
    # default mount points
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   362
    cygwinmountpoints = sorted([
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   363
            "/usr/bin",
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   364
            "/usr/lib",
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   365
            "/cygdrive",
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   366
            ], reverse=True)
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   367
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   368
    # use upper-ing as normcase as same as NTFS workaround
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   369
    def normcase(path):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   370
        pathlen = len(path)
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   371
        if (pathlen == 0) or (path[0] != os.sep):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   372
            # treat as relative
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
   373
            return encoding.upper(path)
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   374
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   375
        # to preserve case of mountpoint part
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   376
        for mp in cygwinmountpoints:
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   377
            if not path.startswith(mp):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   378
                continue
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   379
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   380
            mplen = len(mp)
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   381
            if mplen == pathlen: # mount point itself
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   382
                return mp
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   383
            if path[mplen] == os.sep:
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
   384
                return mp + encoding.upper(path[mplen:])
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   385
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
   386
        return encoding.upper(path)
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   387
24596
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
   388
    normcasespec = encoding.normcasespecs.other
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
   389
    normcasefallback = normcase
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
   390
16240
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   391
    # Cygwin translates native ACLs to POSIX permissions,
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   392
    # but these translations are not supported by native
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   393
    # tools, so the exec bit tends to be set erroneously.
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   394
    # Therefore, disable executable bit access on Cygwin.
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   395
    def checkexec(path):
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   396
        return False
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   397
16241
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   398
    # Similarly, Cygwin's symlink emulation is likely to create
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   399
    # problems when Mercurial is used from both Cygwin and native
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   400
    # Windows, with other native tools, or on shared volumes
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   401
    def checklink(path):
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   402
        return False
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   403
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   404
_needsshellquote = None
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   405
def shellquote(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   406
    if os.sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   407
        return '"%s"' % s
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   408
    global _needsshellquote
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   409
    if _needsshellquote is None:
26248
99b6afff09ae posix: shellquote do not require quoting for "+" (issue4818)
timeless@mozdev.org
parents: 25967
diff changeset
   410
        _needsshellquote = re.compile(r'[^a-zA-Z0-9._/+-]').search
24108
d65ecb814fc0 shellquote: fix missing quotes for empty string
Yuya Nishihara <yuya@tcha.org>
parents: 23683
diff changeset
   411
    if s and not _needsshellquote(s):
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   412
        # "s" shouldn't have to be quoted
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   413
        return s
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   414
    else:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   415
        return "'%s'" % s.replace("'", "'\\''")
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   416
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   417
def quotecommand(cmd):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   418
    return cmd
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   419
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   420
def popen(command, mode='r'):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   421
    return os.popen(command, mode)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   422
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   423
def testpid(pid):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   424
    '''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
   425
    if os.sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   426
        return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   427
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   428
        os.kill(pid, 0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   429
        return True
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   430
    except OSError as inst:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   431
        return inst.errno != errno.ESRCH
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   432
14234
600e64004eb5 rename explain_exit to explainexit
Adrian Buehlmann <adrian@cadifra.com>
parents: 14233
diff changeset
   433
def explainexit(code):
9517
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   434
    """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
   435
    (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
   436
    if code >= 0:
4368f582c806 util.system: Use subprocess instead of os.system
Mads Kiilerich <mads@kiilerich.com>
parents: 9238
diff changeset
   437
        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
   438
    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
   439
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   440
def isowner(st):
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   441
    """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
   442
    return st.st_uid == os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   443
14271
4030630fb59c rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents: 14237
diff changeset
   444
def findexe(command):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   445
    '''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
   446
    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
   447
    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
   448
    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
   449
    if sys.platform == 'OpenVMS':
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   450
        return command
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   451
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   452
    def findexisting(executable):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   453
        'Will return executable if existing file'
15499
58f96703a9ab posix: fix findexe() to check for file type and access
Marc-Antoine Ruel <maruel@google.com>
parents: 15488
diff changeset
   454
        if os.path.isfile(executable) and os.access(executable, os.X_OK):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   455
            return executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   456
        return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   457
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   458
    if os.sep in command:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   459
        return findexisting(command)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   460
16383
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16241
diff changeset
   461
    if sys.platform == 'plan9':
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16241
diff changeset
   462
        return findexisting(os.path.join('/bin', command))
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16241
diff changeset
   463
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   464
    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
   465
        executable = findexisting(os.path.join(path, command))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   466
        if executable is not None:
15499
58f96703a9ab posix: fix findexe() to check for file type and access
Marc-Antoine Ruel <maruel@google.com>
parents: 15488
diff changeset
   467
            return executable
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   468
    return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   469
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14235
diff changeset
   470
def setsignalhandler():
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   471
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   472
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   473
_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
   474
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   475
def statfiles(files):
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   476
    '''Stat each file in files. Yield each stat, or None if a file does not
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   477
    exist or has a type we don't care about.'''
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   478
    lstat = os.lstat
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   479
    getkind = stat.S_IFMT
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   480
    for nf in files:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   481
        try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   482
            st = lstat(nf)
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   483
            if getkind(st.st_mode) not in _wantedkinds:
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   484
                st = None
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   485
        except OSError as err:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   486
            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
   487
                raise
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   488
            st = None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   489
        yield st
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   490
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   491
def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   492
    '''return name of current user'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   493
    return getpass.getuser()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   494
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   495
def username(uid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   496
    """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
   497
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   498
    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
   499
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   500
    if uid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   501
        uid = os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   502
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   503
        return pwd.getpwuid(uid)[0]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   504
    except KeyError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   505
        return str(uid)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   506
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   507
def groupname(gid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   508
    """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
   509
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   510
    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
   511
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   512
    if gid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   513
        gid = os.getgid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   514
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   515
        return grp.getgrgid(gid)[0]
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   516
    except KeyError:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   517
        return str(gid)
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   518
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   519
def groupmembers(name):
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   520
    """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
   521
    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
   522
    """
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   523
    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
   524
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   525
def spawndetached(args):
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   526
    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
   527
                      args[0], args)
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   528
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   529
def gethgcmd():
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   530
    return sys.argv[:1]
11010
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   531
14908
e2b5605501df util: move "default" makedir to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14273
diff changeset
   532
def makedir(path, notindexed):
e2b5605501df util: move "default" makedir to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14273
diff changeset
   533
    os.mkdir(path)
14909
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   534
18143
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18097
diff changeset
   535
def unlinkpath(f, ignoremissing=False):
14909
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   536
    """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: 18097
diff changeset
   537
    try:
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18097
diff changeset
   538
        os.unlink(f)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   539
    except OSError as e:
18143
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 18097
diff changeset
   540
        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: 18097
diff changeset
   541
            raise
14909
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   542
    # try removing directories that might now be empty
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   543
    try:
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   544
        os.removedirs(os.path.dirname(f))
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   545
    except OSError:
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   546
        pass
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   547
14910
570ea0259b0a util: move "default" lookupreg to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14909
diff changeset
   548
def lookupreg(key, name=None, scope=None):
570ea0259b0a util: move "default" lookupreg to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14909
diff changeset
   549
    return None
14911
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   550
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   551
def hidewindow():
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   552
    """Hide current shell window.
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   553
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   554
    Used to hide the window opened when starting asynchronous
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   555
    child process under Windows, unneeded on other systems.
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   556
    """
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   557
    pass
14926
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14911
diff changeset
   558
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   559
class cachestat(object):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   560
    def __init__(self, path):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   561
        self.stat = os.stat(path)
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   562
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   563
    def cacheable(self):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   564
        return bool(self.stat.st_ino)
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   565
15791
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15711
diff changeset
   566
    __hash__ = object.__hash__
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15711
diff changeset
   567
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   568
    def __eq__(self, other):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   569
        try:
18442
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   570
            # Only dev, ino, size, mtime and atime are likely to change. Out
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   571
            # of these, we shouldn't compare atime but should compare the
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   572
            # rest. However, one of the other fields changing indicates
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   573
            # something fishy going on, so return False if anything but atime
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   574
            # changes.
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   575
            return (self.stat.st_mode == other.stat.st_mode and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   576
                    self.stat.st_ino == other.stat.st_ino and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   577
                    self.stat.st_dev == other.stat.st_dev and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   578
                    self.stat.st_nlink == other.stat.st_nlink and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   579
                    self.stat.st_uid == other.stat.st_uid and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   580
                    self.stat.st_gid == other.stat.st_gid and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   581
                    self.stat.st_size == other.stat.st_size and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   582
                    self.stat.st_mtime == other.stat.st_mtime and
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   583
                    self.stat.st_ctime == other.stat.st_ctime)
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   584
        except AttributeError:
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   585
            return False
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   586
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   587
    def __ne__(self, other):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   588
        return not self == other
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   589
14926
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14911
diff changeset
   590
def executablepath():
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14911
diff changeset
   591
    return None # available on Windows only
18097
ae54cff742e2 posix: move server side of unix domain sockets out of inotify
Bryan O'Sullivan <bryano@fb.com>
parents: 18017
diff changeset
   592
18868
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   593
def statislink(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   594
    '''check whether a stat result is a symlink'''
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   595
    return st and stat.S_ISLNK(st.st_mode)
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   596
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   597
def statisexec(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   598
    '''check whether a stat result is an executable file'''
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   599
    return st and (st.st_mode & 0o100 != 0)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   600
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   601
def poll(fds):
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   602
    """block until something happens on any file descriptor
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   603
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   604
    This is a generic helper that will check for any activity
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   605
    (read, write.  exception) and return the list of touched files.
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   606
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   607
    In unsupported cases, it will raise a NotImplementedError"""
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   608
    try:
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   609
        res = select.select(fds, fds, fds)
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   610
    except ValueError: # out of range file descriptor
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   611
        raise NotImplementedError()
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   612
    return sorted(list(set(sum(res, []))))
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   613
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   614
def readpipe(pipe):
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   615
    """Read all available data from a pipe."""
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   616
    # We can't fstat() a pipe because Linux will always report 0.
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   617
    # So, we set the pipe to non-blocking mode and read everything
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   618
    # that's available.
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   619
    flags = fcntl.fcntl(pipe, fcntl.F_GETFL)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   620
    flags |= os.O_NONBLOCK
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   621
    oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   622
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   623
    try:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   624
        chunks = []
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   625
        while True:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   626
            try:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   627
                s = pipe.read()
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   628
                if not s:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   629
                    break
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   630
                chunks.append(s)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   631
            except IOError:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   632
                break
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   633
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   634
        return ''.join(chunks)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   635
    finally:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   636
        fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags)
29530
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   637
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   638
def bindunixsocket(sock, path):
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   639
    """Bind the UNIX domain socket to the specified path"""
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   640
    # use relative path instead of full path at bind() if possible, since
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   641
    # AF_UNIX path has very small length limit (107 chars) on common
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   642
    # platforms (see sys/un.h)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   643
    dirname, basename = os.path.split(path)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   644
    bakwdfd = None
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   645
    if dirname:
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   646
        bakwdfd = os.open('.', os.O_DIRECTORY)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   647
        os.chdir(dirname)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   648
    sock.bind(basename)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   649
    if bakwdfd:
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   650
        os.fchdir(bakwdfd)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   651
        os.close(bakwdfd)