You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.7 KiB

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