1
# Lazygal, a lazy static web gallery generator.
2
# Copyright (C) 2011-2012 Alexandre Rossi <alexandre.rossi@gmail.com>
1
# Deejayd, a media player daemon
2
# Copyright (C) 2013 Mickael Royer <mickael.royer@gmail.com>
3
# Alexandre Rossi <alexandre.rossi@gmail.com>
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
9
10
# This program is distributed in the hope that it will be useful,
10
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
13
# GNU General Public License for more details.
14
15
# You should have received a copy of the GNU General Public License along
15
16
# with this program; if not, write to the Free Software Foundation, Inc.,
16
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
26
def is_root_posix(path):
33
37
is_root = is_root_posix
36
def path2unicode(path):
40
def path2unicode(path, errors='strict'):
37
41
if type(path) is unicode:
40
return path.decode(sys.getfilesystemencoding())
44
return path.decode(sys.getfilesystemencoding(), errors)
43
47
def is_subdir_of(dir_path, path):
74
78
return posixpath.join(*path_list)
81
def walk(top, walked=None, topdown=False):
83
This is a wrapper around os.walk() from the standard library:
84
- following symbolic links on directories
85
- whith barriers in place against walking twice the same directory,
86
which may happen when two directory trees have symbolic links to
87
each other's contents.
89
if walked is None: walked = []
91
for root, dirs, files in os.walk(top, topdown=topdown):
92
walked.append(os.path.realpath(root))
94
# Follow symlinks if they have not been walked yet
96
d_path = os.path.join(root, d)
97
if os.path.islink(d_path):
98
if os.path.realpath(d_path) not in walked:
99
for x in walk(d_path, walked):
102
logging.error("Not following symlink '%s' because directory has already been processed." % d_path)
104
yield root, dirs, files
107
def walk_and_do(top=None, walked=None, dcb=None, fcb=None, topdown=False):
109
This walk calls dcb on each found directory and fcb on each found
110
file with the following arguments :
113
for root, dirs, files in walk(top, walked, topdown=topdown):
115
dcb(root, dirs, files)
117
map(lambda f: fcb(os.path.join(root, f)), files)
77
120
# vim: ts=4 sw=4 expandtab