35d500861c6c8d98d9f44a9c968c32f850bf2ec2
[senf.git] / scons / scons-1.2.0 / engine / SCons / Scanner / Dir.py
1 #
2 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __revision__ = "src/engine/SCons/Scanner/Dir.py 3842 2008/12/20 22:59:52 scons"
25
26 import SCons.Node.FS
27 import SCons.Scanner
28
29 def only_dirs(nodes):
30     is_Dir = lambda n: isinstance(n.disambiguate(), SCons.Node.FS.Dir)
31     return filter(is_Dir, nodes)
32
33 def DirScanner(**kw):
34     """Return a prototype Scanner instance for scanning
35     directories for on-disk files"""
36     kw['node_factory'] = SCons.Node.FS.Entry
37     kw['recursive'] = only_dirs
38     return apply(SCons.Scanner.Base, (scan_on_disk, "DirScanner"), kw)
39
40 def DirEntryScanner(**kw):
41     """Return a prototype Scanner instance for "scanning"
42     directory Nodes for their in-memory entries"""
43     kw['node_factory'] = SCons.Node.FS.Entry
44     kw['recursive'] = None
45     return apply(SCons.Scanner.Base, (scan_in_memory, "DirEntryScanner"), kw)
46
47 skip_entry = {}
48
49 skip_entry_list = [
50    '.',
51    '..',
52    '.sconsign',
53    # Used by the native dblite.py module.
54    '.sconsign.dblite',
55    # Used by dbm and dumbdbm.
56    '.sconsign.dir',
57    # Used by dbm.
58    '.sconsign.pag',
59    # Used by dumbdbm.
60    '.sconsign.dat',
61    '.sconsign.bak',
62    # Used by some dbm emulations using Berkeley DB.
63    '.sconsign.db',
64 ]
65
66 for skip in skip_entry_list:
67     skip_entry[skip] = 1
68     skip_entry[SCons.Node.FS._my_normcase(skip)] = 1
69
70 do_not_scan = lambda k: not skip_entry.has_key(k)
71
72 def scan_on_disk(node, env, path=()):
73     """
74     Scans a directory for on-disk files and directories therein.
75
76     Looking up the entries will add these to the in-memory Node tree
77     representation of the file system, so all we have to do is just
78     that and then call the in-memory scanning function.
79     """
80     try:
81         flist = node.fs.listdir(node.abspath)
82     except (IOError, OSError):
83         return []
84     e = node.Entry
85     for f in  filter(do_not_scan, flist):
86         # Add ./ to the beginning of the file name so if it begins with a
87         # '#' we don't look it up relative to the top-level directory.
88         e('./' + f)
89     return scan_in_memory(node, env, path)
90
91 def scan_in_memory(node, env, path=()):
92     """
93     "Scans" a Node.FS.Dir for its in-memory entries.
94     """
95     try:
96         entries = node.entries
97     except AttributeError:
98         # It's not a Node.FS.Dir (or doesn't look enough like one for
99         # our purposes), which can happen if a target list containing
100         # mixed Node types (Dirs and Files, for example) has a Dir as
101         # the first entry.
102         return []
103     entry_list = filter(do_not_scan, entries.keys())
104     entry_list.sort()
105     return map(lambda n, e=entries: e[n], entry_list)