mercurial/osutil.py
author Thomas Arendsen Hein <thomas@intevation.de>
Sun, 19 Oct 2008 12:19:55 +0200
changeset 7164 cae820101762
parent 7057 094af6eeb7d7
child 7301 00d76fa3ffba
permissions -rw-r--r--
Add util.popen3 fallback, simplify import of Popen3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7057
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     1
import os
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     2
import stat as _stat
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
def _mode_to_kind(mode):
7057
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     5
    if _stat.S_ISREG(mode): return _stat.S_IFREG
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     6
    if _stat.S_ISDIR(mode): return _stat.S_IFDIR
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     7
    if _stat.S_ISLNK(mode): return _stat.S_IFLNK
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     8
    if _stat.S_ISBLK(mode): return _stat.S_IFBLK
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
     9
    if _stat.S_ISCHR(mode): return _stat.S_IFCHR
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
    10
    if _stat.S_ISFIFO(mode): return _stat.S_IFIFO
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
    11
    if _stat.S_ISSOCK(mode): return _stat.S_IFSOCK
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
    return mode
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
7034
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 5396
diff changeset
    14
def listdir(path, stat=False, skip=None):
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    15
    '''listdir(path, stat=False) -> list_of_tuples
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
    Return a sorted list containing information about the entries
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
    in the directory.
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    19
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    20
    If stat is True, each element is a 3-tuple:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    21
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    22
      (name, type, stat object)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    23
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    24
    Otherwise, each element is a 2-tuple:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    25
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    26
      (name, type)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    27
    '''
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    28
    result = []
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    29
    prefix = path + os.sep
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    30
    names = os.listdir(path)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    31
    names.sort()
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    32
    for fn in names:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    33
        st = os.lstat(prefix + fn)
7057
094af6eeb7d7 fix conflicting variables when no native osutil is available
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7034
diff changeset
    34
        if fn == skip and _stat.S_ISDIR(st.st_mode):
7034
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 5396
diff changeset
    35
            return []
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    36
        if stat:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    37
            result.append((fn, _mode_to_kind(st.st_mode), st))
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
        else:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    39
            result.append((fn, _mode_to_kind(st.st_mode)))
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    40
    return result