Show More
@@ -1,42 +1,49 b'' | |||||
|
1 | # osutil.py - pure Python version of osutil.c | |||
|
2 | # | |||
|
3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others | |||
|
4 | # | |||
|
5 | # This software may be used and distributed according to the terms of the | |||
|
6 | # GNU General Public License version 2, incorporated herein by reference. | |||
|
7 | ||||
1 | import os |
|
8 | import os | |
2 | import stat as _stat |
|
9 | import stat as _stat | |
3 |
|
10 | |||
4 | def _mode_to_kind(mode): |
|
11 | def _mode_to_kind(mode): | |
5 | if _stat.S_ISREG(mode): return _stat.S_IFREG |
|
12 | if _stat.S_ISREG(mode): return _stat.S_IFREG | |
6 | if _stat.S_ISDIR(mode): return _stat.S_IFDIR |
|
13 | if _stat.S_ISDIR(mode): return _stat.S_IFDIR | |
7 | if _stat.S_ISLNK(mode): return _stat.S_IFLNK |
|
14 | if _stat.S_ISLNK(mode): return _stat.S_IFLNK | |
8 | if _stat.S_ISBLK(mode): return _stat.S_IFBLK |
|
15 | if _stat.S_ISBLK(mode): return _stat.S_IFBLK | |
9 | if _stat.S_ISCHR(mode): return _stat.S_IFCHR |
|
16 | if _stat.S_ISCHR(mode): return _stat.S_IFCHR | |
10 | if _stat.S_ISFIFO(mode): return _stat.S_IFIFO |
|
17 | if _stat.S_ISFIFO(mode): return _stat.S_IFIFO | |
11 | if _stat.S_ISSOCK(mode): return _stat.S_IFSOCK |
|
18 | if _stat.S_ISSOCK(mode): return _stat.S_IFSOCK | |
12 | return mode |
|
19 | return mode | |
13 |
|
20 | |||
14 | def listdir(path, stat=False, skip=None): |
|
21 | def listdir(path, stat=False, skip=None): | |
15 | '''listdir(path, stat=False) -> list_of_tuples |
|
22 | '''listdir(path, stat=False) -> list_of_tuples | |
16 |
|
23 | |||
17 | Return a sorted list containing information about the entries |
|
24 | Return a sorted list containing information about the entries | |
18 | in the directory. |
|
25 | in the directory. | |
19 |
|
26 | |||
20 | If stat is True, each element is a 3-tuple: |
|
27 | If stat is True, each element is a 3-tuple: | |
21 |
|
28 | |||
22 | (name, type, stat object) |
|
29 | (name, type, stat object) | |
23 |
|
30 | |||
24 | Otherwise, each element is a 2-tuple: |
|
31 | Otherwise, each element is a 2-tuple: | |
25 |
|
32 | |||
26 | (name, type) |
|
33 | (name, type) | |
27 | ''' |
|
34 | ''' | |
28 | result = [] |
|
35 | result = [] | |
29 | prefix = path |
|
36 | prefix = path | |
30 | if not prefix.endswith(os.sep): |
|
37 | if not prefix.endswith(os.sep): | |
31 | prefix += os.sep |
|
38 | prefix += os.sep | |
32 | names = os.listdir(path) |
|
39 | names = os.listdir(path) | |
33 | names.sort() |
|
40 | names.sort() | |
34 | for fn in names: |
|
41 | for fn in names: | |
35 | st = os.lstat(prefix + fn) |
|
42 | st = os.lstat(prefix + fn) | |
36 | if fn == skip and _stat.S_ISDIR(st.st_mode): |
|
43 | if fn == skip and _stat.S_ISDIR(st.st_mode): | |
37 | return [] |
|
44 | return [] | |
38 | if stat: |
|
45 | if stat: | |
39 | result.append((fn, _mode_to_kind(st.st_mode), st)) |
|
46 | result.append((fn, _mode_to_kind(st.st_mode), st)) | |
40 | else: |
|
47 | else: | |
41 | result.append((fn, _mode_to_kind(st.st_mode))) |
|
48 | result.append((fn, _mode_to_kind(st.st_mode))) | |
42 | return result |
|
49 | return result |
General Comments 0
You need to be logged in to leave comments.
Login now