mercurial/osutil.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Sun, 04 Jan 2009 21:49:37 +0100
changeset 7601 49355875c805
parent 7301 00d76fa3ffba
permissions -rw-r--r--
merge with crew
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 = []
7301
00d76fa3ffba Fix util._statfiles_clustered() failing at root of a windows drive
Patrick Mezard <pmezard@gmail.com>
parents: 7057
diff changeset
    29
    prefix = path
00d76fa3ffba Fix util._statfiles_clustered() failing at root of a windows drive
Patrick Mezard <pmezard@gmail.com>
parents: 7057
diff changeset
    30
    if not prefix.endswith(os.sep):
00d76fa3ffba Fix util._statfiles_clustered() failing at root of a windows drive
Patrick Mezard <pmezard@gmail.com>
parents: 7057
diff changeset
    31
        prefix += os.sep
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    32
    names = os.listdir(path)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    33
    names.sort()
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    34
    for fn in names:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    35
        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
    36
        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
    37
            return []
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
        if stat:
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), st))
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    40
        else:
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    41
            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
    42
    return result