##// END OF EJS Templates
listdir: add support for aborting if a certain path is found...
Matt Mackall -
r7034:0d513661 default
parent child Browse files
Show More
@@ -9,7 +9,7 b' of the GNU General Public License, incor'
9
9
10 from node import nullid
10 from node import nullid
11 from i18n import _
11 from i18n import _
12 import struct, os, bisect, stat, util, errno, ignore
12 import struct, os, stat, util, errno, ignore
13 import cStringIO, osutil, sys
13 import cStringIO, osutil, sys
14
14
15 _unknown = ('?', 0, 0, 0)
15 _unknown = ('?', 0, 0, 0)
@@ -458,7 +458,6 b' class dirstate(object):'
458 normalize = self.normalize
458 normalize = self.normalize
459 listdir = osutil.listdir
459 listdir = osutil.listdir
460 lstat = os.lstat
460 lstat = os.lstat
461 bisect_left = bisect.bisect_left
462 pconvert = util.pconvert
461 pconvert = util.pconvert
463 getkind = stat.S_IFMT
462 getkind = stat.S_IFMT
464 dirkind = stat.S_IFDIR
463 dirkind = stat.S_IFDIR
@@ -510,17 +509,11 b' class dirstate(object):'
510 nd = work.pop()
509 nd = work.pop()
511 if hasattr(match, 'dir'):
510 if hasattr(match, 'dir'):
512 match.dir(nd)
511 match.dir(nd)
513 entries = listdir(join(nd), stat=True)
514 if nd == '.':
512 if nd == '.':
515 nd = ''
513 nd = ''
514 entries = listdir(join(nd), stat=True)
516 else:
515 else:
517 # do not recurse into a repo contained in this
516 entries = listdir(join(nd), stat=True, skip ='.hg')
518 # one. use bisect to find .hg directory so speed
519 # is good on big directory.
520 hg = bisect_left(entries, ('.hg'))
521 if hg < len(entries) and entries[hg][0] == '.hg' \
522 and entries[hg][1] == dirkind:
523 continue
524 for f, kind, st in entries:
517 for f, kind, st in entries:
525 nf = normalize(nd and (nd + "/" + f) or f)
518 nf = normalize(nd and (nd + "/" + f) or f)
526 if nf not in results:
519 if nf not in results:
@@ -114,17 +114,18 b' int entkind(struct dirent *ent)'
114
114
115 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
115 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
116 {
116 {
117 static char *kwlist[] = { "path", "stat", NULL };
117 static char *kwlist[] = { "path", "stat", "skip", NULL };
118 PyObject *statflag = NULL, *list, *elem, *stat, *ret = NULL;
118 PyObject *statflag = NULL, *list, *elem, *stat, *ret = NULL;
119 char fullpath[PATH_MAX + 10], *path;
119 char fullpath[PATH_MAX + 10], *path, *skip = NULL;
120 int pathlen, keepstat, kind, dfd = -1, err;
120 int pathlen, keepstat, kind, dfd = -1, err;
121 struct stat st;
121 struct stat st;
122 struct dirent *ent;
122 struct dirent *ent;
123 DIR *dir;
123 DIR *dir;
124
124
125 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|O:listdir", kwlist,
125 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|Os:listdir", kwlist,
126 &path, &pathlen, &statflag))
126 &path, &pathlen, &statflag, &skip))
127 goto error_parse;
127 goto error_parse;
128
128 if (pathlen >= PATH_MAX)
129 if (pathlen >= PATH_MAX)
129 goto error_parse;
130 goto error_parse;
130
131
@@ -177,6 +178,12 b' static PyObject *listdir(PyObject *self,'
177 kind = st.st_mode & S_IFMT;
178 kind = st.st_mode & S_IFMT;
178 }
179 }
179
180
181 /* quit early? */
182 if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) {
183 ret = PyList_New(0);
184 goto error;
185 }
186
180 if (keepstat) {
187 if (keepstat) {
181 stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
188 stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
182 if (!stat)
189 if (!stat)
@@ -192,7 +199,6 b' static PyObject *listdir(PyObject *self,'
192 Py_DECREF(elem);
199 Py_DECREF(elem);
193 }
200 }
194
201
195 PyList_Sort(list);
196 ret = list;
202 ret = list;
197 Py_INCREF(ret);
203 Py_INCREF(ret);
198
204
@@ -10,7 +10,7 b' def _mode_to_kind(mode):'
10 if stat.S_ISSOCK(mode): return stat.S_IFSOCK
10 if stat.S_ISSOCK(mode): return stat.S_IFSOCK
11 return mode
11 return mode
12
12
13 def listdir(path, stat=False):
13 def listdir(path, stat=False, skip=None):
14 '''listdir(path, stat=False) -> list_of_tuples
14 '''listdir(path, stat=False) -> list_of_tuples
15
15
16 Return a sorted list containing information about the entries
16 Return a sorted list containing information about the entries
@@ -30,6 +30,8 b' def listdir(path, stat=False):'
30 names.sort()
30 names.sort()
31 for fn in names:
31 for fn in names:
32 st = os.lstat(prefix + fn)
32 st = os.lstat(prefix + fn)
33 if fn == skip and stat.S_ISDIR(st.st_mode):
34 return []
33 if stat:
35 if stat:
34 result.append((fn, _mode_to_kind(st.st_mode), st))
36 result.append((fn, _mode_to_kind(st.st_mode), st))
35 else:
37 else:
General Comments 0
You need to be logged in to leave comments. Login now