summaryrefslogtreecommitdiff
path: root/obnam/walk.py
blob: 4a2d0e23e2efc9d73f3b3af1d3f6cc278d331252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (C) 2008  Lars Wirzenius <liw@iki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


"""Walk a directory tree."""


import os


def depth_first(top, prune=None):
    """Walk a directory tree depth-first, except for unwanted subdirs.
    
    This is, essentially, 'os.walk(top, topdown=False)', except that
    if the prune argument is set, we call it before descending to 
    sub-directories to allow it to remove any directories and files
    the caller does not want to know about.
    
    If set, prune must be a function that gets three arguments (current
    directory, list of sub-directory names, list of files in directory),
    and must modify the two lists _in_place_. For example:
    
        def prune(dirname, dirnames, filenames):
            if ".bzr" in dirnames:
                dirnames.remove(".bzr")
                
    The dirnames and filenames lists contain basenames, relative to
    dirname.
    
    """

    # We walk topdown, since that's the only way os.walk allows us to
    # do any pruning. We use os.walk to get the exact same error handling
    # and other logic it uses.
    for dirname, dirnames, filenames in os.walk(top):

        # Prune. This modifies dirnames and filenames in place.
        if prune:
            prune(dirname, dirnames, filenames)

        # Make a duplicate of the dirnames, then empty the existing list.
        # This way, os.walk won't try to walk to subdirectories. We'll
        # do that manually.
        real_dirnames = dirnames[:]
        del dirnames[:]

        # Process subdirectories, recursively.
        for subdirname in real_dirnames:
            subdirpath = os.path.join(dirname, subdirname)
            for x in depth_first(subdirpath, prune=prune):
                yield x

        # Return current directory last.
        yield dirname, real_dirnames, filenames