http://scons.tigris.org/issues/show_bug.cgi?id=2329
[scons.git] / src / engine / SCons / Scanner / Dir.py
1 #
2 # __COPYRIGHT__
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 from __future__ import generators  ### KEEP FOR COMPATIBILITY FIXERS
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import SCons.Node.FS
28 import SCons.Scanner
29
30 def only_dirs(nodes):
31     is_Dir = lambda n: isinstance(n.disambiguate(), SCons.Node.FS.Dir)
32     return list(filter(is_Dir, nodes))
33
34 def DirScanner(**kw):
35     """Return a prototype Scanner instance for scanning
36     directories for on-disk files"""
37     kw['node_factory'] = SCons.Node.FS.Entry
38     kw['recursive'] = only_dirs
39     return SCons.Scanner.Base(scan_on_disk, "DirScanner", **kw)
40
41 def DirEntryScanner(**kw):
42     """Return a prototype Scanner instance for "scanning"
43     directory Nodes for their in-memory entries"""
44     kw['node_factory'] = SCons.Node.FS.Entry
45     kw['recursive'] = None
46     return SCons.Scanner.Base(scan_in_memory, "DirEntryScanner", **kw)
47
48 skip_entry = {}
49
50 skip_entry_list = [
51    '.',
52    '..',
53    '.sconsign',
54    # Used by the native dblite.py module.
55    '.sconsign.dblite',
56    # Used by dbm and dumbdbm.
57    '.sconsign.dir',
58    # Used by dbm.
59    '.sconsign.pag',
60    # Used by dumbdbm.
61    '.sconsign.dat',
62    '.sconsign.bak',
63    # Used by some dbm emulations using Berkeley DB.
64    '.sconsign.db',
65 ]
66
67 for skip in skip_entry_list:
68     skip_entry[skip] = 1
69     skip_entry[SCons.Node.FS._my_normcase(skip)] = 1
70
71 do_not_scan = lambda k: k not in skip_entry
72
73 def scan_on_disk(node, env, path=()):
74     """
75     Scans a directory for on-disk files and directories therein.
76
77     Looking up the entries will add these to the in-memory Node tree
78     representation of the file system, so all we have to do is just
79     that and then call the in-memory scanning function.
80     """
81     try:
82         flist = node.fs.listdir(node.abspath)
83     except (IOError, OSError):
84         return []
85     e = node.Entry
86     for f in  filter(do_not_scan, flist):
87         # Add ./ to the beginning of the file name so if it begins with a
88         # '#' we don't look it up relative to the top-level directory.
89         e('./' + f)
90     return scan_in_memory(node, env, path)
91
92 def scan_in_memory(node, env, path=()):
93     """
94     "Scans" a Node.FS.Dir for its in-memory entries.
95     """
96     try:
97         entries = node.entries
98     except AttributeError:
99         # It's not a Node.FS.Dir (or doesn't look enough like one for
100         # our purposes), which can happen if a target list containing
101         # mixed Node types (Dirs and Files, for example) has a Dir as
102         # the first entry.
103         return []
104     entry_list = sorted(filter(do_not_scan, entries.keys()))
105     return [entries[n] for n in entry_list]
106
107 # Local Variables:
108 # tab-width:4
109 # indent-tabs-mode:nil
110 # End:
111 # vim: set expandtab tabstop=4 shiftwidth=4: