Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/osutil.py @ 5396:5105b119edd2
Add osutil module, containing a listdir function.
This is similar to os.listdir, only it returns a sorted list of tuples.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Fri, 05 Oct 2007 15:01:06 -0700 |
parents | |
children | 0d513661d6c2 |
comparison
equal
deleted
inserted
replaced
5395:e73a83af7926 | 5396:5105b119edd2 |
---|---|
1 import os, stat | |
2 | |
3 def _mode_to_kind(mode): | |
4 if stat.S_ISREG(mode): return stat.S_IFREG | |
5 if stat.S_ISDIR(mode): return stat.S_IFDIR | |
6 if stat.S_ISLNK(mode): return stat.S_IFLNK | |
7 if stat.S_ISBLK(mode): return stat.S_IFBLK | |
8 if stat.S_ISCHR(mode): return stat.S_IFCHR | |
9 if stat.S_ISFIFO(mode): return stat.S_IFIFO | |
10 if stat.S_ISSOCK(mode): return stat.S_IFSOCK | |
11 return mode | |
12 | |
13 def listdir(path, stat=False): | |
14 '''listdir(path, stat=False) -> list_of_tuples | |
15 | |
16 Return a sorted list containing information about the entries | |
17 in the directory. | |
18 | |
19 If stat is True, each element is a 3-tuple: | |
20 | |
21 (name, type, stat object) | |
22 | |
23 Otherwise, each element is a 2-tuple: | |
24 | |
25 (name, type) | |
26 ''' | |
27 result = [] | |
28 prefix = path + os.sep | |
29 names = os.listdir(path) | |
30 names.sort() | |
31 for fn in names: | |
32 st = os.lstat(prefix + fn) | |
33 if stat: | |
34 result.append((fn, _mode_to_kind(st.st_mode), st)) | |
35 else: | |
36 result.append((fn, _mode_to_kind(st.st_mode))) | |
37 return result |