16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
from shutil import copyfile
20
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
21
S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
22
19
from cStringIO import StringIO
22
from os import listdir
26
from shutil import copyfile
28
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
29
S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
91
94
stat.S_IFLNK:'symlink',
92
95
stat.S_IFSOCK:'socket',
94
def file_kind(f, _formats=_formats, _unknown='unknown', _lstat=os.lstat):
99
def file_kind_from_stat_mode(stat_mode, _formats=_formats, _unknown='unknown'):
100
"""Generate a file kind from a stat mode. This is used in walkdirs.
102
Its performance is critical: Do not mutate without careful benchmarking.
96
return _formats[_lstat(f).st_mode & 0170000]
105
return _formats[stat_mode & 0170000]
110
def file_kind(f, _lstat=os.lstat, _mapper=file_kind_from_stat_mode):
111
return _mapper(_lstat(f).st_mode)
101
114
def kind_marker(kind):
102
115
if kind == 'file':
104
elif kind == 'directory':
117
elif kind == _directory_kind:
106
119
elif kind == 'symlink':
789
802
if _validWin32PathRE.match(path) is None:
790
803
raise IllegalPath(path)
807
"""Yield data about all the directories in a tree.
809
This yields all the data about the contents of a directory at a time.
810
After each directory has been yielded, if the caller has mutated the list
811
to exclude some directories, they are then not descended into.
813
The data yielded is of the form:
814
[(relpath, basename, kind, lstat, path_from_top), ...]
816
:return: an iterator over the dirs.
820
_directory = _directory_kind
822
pending = [("", "", _directory, None, top)]
825
currentdir = pending.pop()
826
# 0 - relpath, 1- basename, 2- kind, 3- stat, 4-toppath
829
relroot = currentdir[0] + '/'
832
for name in sorted(_listdir(top)):
833
abspath = top + '/' + name
834
statvalue = lstat(abspath)
835
dirblock.append ((relroot + name, name, file_kind_from_stat_mode(statvalue.st_mode), statvalue, abspath))
837
# push the user specified dirs from dirblock
838
for dir in reversed(dirblock):
839
if dir[2] == _directory: