PageRenderTime 49ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/couchjs/scons/scons-local-2.0.1/SCons/Node/FS.py

http://github.com/cloudant/bigcouch
Python | 3142 lines | 3135 code | 0 blank | 7 comment | 3 complexity | d19f553cac14f1ab777ac4f52b971912 MD5 | raw file
Possible License(s): Apache-2.0
  1. """scons.Node.FS
  2. File system nodes.
  3. These Nodes represent the canonical external objects that people think
  4. of when they think of building software: files and directories.
  5. This holds a "default_fs" variable that should be initialized with an FS
  6. that can be used by scripts or modules looking for the canonical default.
  7. """
  8. #
  9. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining
  12. # a copy of this software and associated documentation files (the
  13. # "Software"), to deal in the Software without restriction, including
  14. # without limitation the rights to use, copy, modify, merge, publish,
  15. # distribute, sublicense, and/or sell copies of the Software, and to
  16. # permit persons to whom the Software is furnished to do so, subject to
  17. # the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included
  20. # in all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  23. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  24. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. __revision__ = "src/engine/SCons/Node/FS.py 5134 2010/08/16 23:02:40 bdeegan"
  30. import fnmatch
  31. import os
  32. import re
  33. import shutil
  34. import stat
  35. import sys
  36. import time
  37. import codecs
  38. import SCons.Action
  39. from SCons.Debug import logInstanceCreation
  40. import SCons.Errors
  41. import SCons.Memoize
  42. import SCons.Node
  43. import SCons.Node.Alias
  44. import SCons.Subst
  45. import SCons.Util
  46. import SCons.Warnings
  47. from SCons.Debug import Trace
  48. do_store_info = True
  49. class EntryProxyAttributeError(AttributeError):
  50. """
  51. An AttributeError subclass for recording and displaying the name
  52. of the underlying Entry involved in an AttributeError exception.
  53. """
  54. def __init__(self, entry_proxy, attribute):
  55. AttributeError.__init__(self)
  56. self.entry_proxy = entry_proxy
  57. self.attribute = attribute
  58. def __str__(self):
  59. entry = self.entry_proxy.get()
  60. fmt = "%s instance %s has no attribute %s"
  61. return fmt % (entry.__class__.__name__,
  62. repr(entry.name),
  63. repr(self.attribute))
  64. # The max_drift value: by default, use a cached signature value for
  65. # any file that's been untouched for more than two days.
  66. default_max_drift = 2*24*60*60
  67. #
  68. # We stringify these file system Nodes a lot. Turning a file system Node
  69. # into a string is non-trivial, because the final string representation
  70. # can depend on a lot of factors: whether it's a derived target or not,
  71. # whether it's linked to a repository or source directory, and whether
  72. # there's duplication going on. The normal technique for optimizing
  73. # calculations like this is to memoize (cache) the string value, so you
  74. # only have to do the calculation once.
  75. #
  76. # A number of the above factors, however, can be set after we've already
  77. # been asked to return a string for a Node, because a Repository() or
  78. # VariantDir() call or the like may not occur until later in SConscript
  79. # files. So this variable controls whether we bother trying to save
  80. # string values for Nodes. The wrapper interface can set this whenever
  81. # they're done mucking with Repository and VariantDir and the other stuff,
  82. # to let this module know it can start returning saved string values
  83. # for Nodes.
  84. #
  85. Save_Strings = None
  86. def save_strings(val):
  87. global Save_Strings
  88. Save_Strings = val
  89. #
  90. # Avoid unnecessary function calls by recording a Boolean value that
  91. # tells us whether or not os.path.splitdrive() actually does anything
  92. # on this system, and therefore whether we need to bother calling it
  93. # when looking up path names in various methods below.
  94. #
  95. do_splitdrive = None
  96. def initialize_do_splitdrive():
  97. global do_splitdrive
  98. drive, path = os.path.splitdrive('X:/foo')
  99. do_splitdrive = not not drive
  100. initialize_do_splitdrive()
  101. #
  102. needs_normpath_check = None
  103. def initialize_normpath_check():
  104. """
  105. Initialize the normpath_check regular expression.
  106. This function is used by the unit tests to re-initialize the pattern
  107. when testing for behavior with different values of os.sep.
  108. """
  109. global needs_normpath_check
  110. if os.sep == '/':
  111. pattern = r'.*/|\.$|\.\.$'
  112. else:
  113. pattern = r'.*[/%s]|\.$|\.\.$' % re.escape(os.sep)
  114. needs_normpath_check = re.compile(pattern)
  115. initialize_normpath_check()
  116. #
  117. # SCons.Action objects for interacting with the outside world.
  118. #
  119. # The Node.FS methods in this module should use these actions to
  120. # create and/or remove files and directories; they should *not* use
  121. # os.{link,symlink,unlink,mkdir}(), etc., directly.
  122. #
  123. # Using these SCons.Action objects ensures that descriptions of these
  124. # external activities are properly displayed, that the displays are
  125. # suppressed when the -s (silent) option is used, and (most importantly)
  126. # the actions are disabled when the the -n option is used, in which case
  127. # there should be *no* changes to the external file system(s)...
  128. #
  129. if hasattr(os, 'link'):
  130. def _hardlink_func(fs, src, dst):
  131. # If the source is a symlink, we can't just hard-link to it
  132. # because a relative symlink may point somewhere completely
  133. # different. We must disambiguate the symlink and then
  134. # hard-link the final destination file.
  135. while fs.islink(src):
  136. link = fs.readlink(src)
  137. if not os.path.isabs(link):
  138. src = link
  139. else:
  140. src = os.path.join(os.path.dirname(src), link)
  141. fs.link(src, dst)
  142. else:
  143. _hardlink_func = None
  144. if hasattr(os, 'symlink'):
  145. def _softlink_func(fs, src, dst):
  146. fs.symlink(src, dst)
  147. else:
  148. _softlink_func = None
  149. def _copy_func(fs, src, dest):
  150. shutil.copy2(src, dest)
  151. st = fs.stat(src)
  152. fs.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
  153. Valid_Duplicates = ['hard-soft-copy', 'soft-hard-copy',
  154. 'hard-copy', 'soft-copy', 'copy']
  155. Link_Funcs = [] # contains the callables of the specified duplication style
  156. def set_duplicate(duplicate):
  157. # Fill in the Link_Funcs list according to the argument
  158. # (discarding those not available on the platform).
  159. # Set up the dictionary that maps the argument names to the
  160. # underlying implementations. We do this inside this function,
  161. # not in the top-level module code, so that we can remap os.link
  162. # and os.symlink for testing purposes.
  163. link_dict = {
  164. 'hard' : _hardlink_func,
  165. 'soft' : _softlink_func,
  166. 'copy' : _copy_func
  167. }
  168. if not duplicate in Valid_Duplicates:
  169. raise SCons.Errors.InternalError("The argument of set_duplicate "
  170. "should be in Valid_Duplicates")
  171. global Link_Funcs
  172. Link_Funcs = []
  173. for func in duplicate.split('-'):
  174. if link_dict[func]:
  175. Link_Funcs.append(link_dict[func])
  176. def LinkFunc(target, source, env):
  177. # Relative paths cause problems with symbolic links, so
  178. # we use absolute paths, which may be a problem for people
  179. # who want to move their soft-linked src-trees around. Those
  180. # people should use the 'hard-copy' mode, softlinks cannot be
  181. # used for that; at least I have no idea how ...
  182. src = source[0].abspath
  183. dest = target[0].abspath
  184. dir, file = os.path.split(dest)
  185. if dir and not target[0].fs.isdir(dir):
  186. os.makedirs(dir)
  187. if not Link_Funcs:
  188. # Set a default order of link functions.
  189. set_duplicate('hard-soft-copy')
  190. fs = source[0].fs
  191. # Now link the files with the previously specified order.
  192. for func in Link_Funcs:
  193. try:
  194. func(fs, src, dest)
  195. break
  196. except (IOError, OSError):
  197. # An OSError indicates something happened like a permissions
  198. # problem or an attempt to symlink across file-system
  199. # boundaries. An IOError indicates something like the file
  200. # not existing. In either case, keeping trying additional
  201. # functions in the list and only raise an error if the last
  202. # one failed.
  203. if func == Link_Funcs[-1]:
  204. # exception of the last link method (copy) are fatal
  205. raise
  206. return 0
  207. Link = SCons.Action.Action(LinkFunc, None)
  208. def LocalString(target, source, env):
  209. return 'Local copy of %s from %s' % (target[0], source[0])
  210. LocalCopy = SCons.Action.Action(LinkFunc, LocalString)
  211. def UnlinkFunc(target, source, env):
  212. t = target[0]
  213. t.fs.unlink(t.abspath)
  214. return 0
  215. Unlink = SCons.Action.Action(UnlinkFunc, None)
  216. def MkdirFunc(target, source, env):
  217. t = target[0]
  218. if not t.exists():
  219. t.fs.mkdir(t.abspath)
  220. return 0
  221. Mkdir = SCons.Action.Action(MkdirFunc, None, presub=None)
  222. MkdirBuilder = None
  223. def get_MkdirBuilder():
  224. global MkdirBuilder
  225. if MkdirBuilder is None:
  226. import SCons.Builder
  227. import SCons.Defaults
  228. # "env" will get filled in by Executor.get_build_env()
  229. # calling SCons.Defaults.DefaultEnvironment() when necessary.
  230. MkdirBuilder = SCons.Builder.Builder(action = Mkdir,
  231. env = None,
  232. explain = None,
  233. is_explicit = None,
  234. target_scanner = SCons.Defaults.DirEntryScanner,
  235. name = "MkdirBuilder")
  236. return MkdirBuilder
  237. class _Null(object):
  238. pass
  239. _null = _Null()
  240. DefaultSCCSBuilder = None
  241. DefaultRCSBuilder = None
  242. def get_DefaultSCCSBuilder():
  243. global DefaultSCCSBuilder
  244. if DefaultSCCSBuilder is None:
  245. import SCons.Builder
  246. # "env" will get filled in by Executor.get_build_env()
  247. # calling SCons.Defaults.DefaultEnvironment() when necessary.
  248. act = SCons.Action.Action('$SCCSCOM', '$SCCSCOMSTR')
  249. DefaultSCCSBuilder = SCons.Builder.Builder(action = act,
  250. env = None,
  251. name = "DefaultSCCSBuilder")
  252. return DefaultSCCSBuilder
  253. def get_DefaultRCSBuilder():
  254. global DefaultRCSBuilder
  255. if DefaultRCSBuilder is None:
  256. import SCons.Builder
  257. # "env" will get filled in by Executor.get_build_env()
  258. # calling SCons.Defaults.DefaultEnvironment() when necessary.
  259. act = SCons.Action.Action('$RCS_COCOM', '$RCS_COCOMSTR')
  260. DefaultRCSBuilder = SCons.Builder.Builder(action = act,
  261. env = None,
  262. name = "DefaultRCSBuilder")
  263. return DefaultRCSBuilder
  264. # Cygwin's os.path.normcase pretends it's on a case-sensitive filesystem.
  265. _is_cygwin = sys.platform == "cygwin"
  266. if os.path.normcase("TeSt") == os.path.normpath("TeSt") and not _is_cygwin:
  267. def _my_normcase(x):
  268. return x
  269. else:
  270. def _my_normcase(x):
  271. return x.upper()
  272. class DiskChecker(object):
  273. def __init__(self, type, do, ignore):
  274. self.type = type
  275. self.do = do
  276. self.ignore = ignore
  277. self.func = do
  278. def __call__(self, *args, **kw):
  279. return self.func(*args, **kw)
  280. def set(self, list):
  281. if self.type in list:
  282. self.func = self.do
  283. else:
  284. self.func = self.ignore
  285. def do_diskcheck_match(node, predicate, errorfmt):
  286. result = predicate()
  287. try:
  288. # If calling the predicate() cached a None value from stat(),
  289. # remove it so it doesn't interfere with later attempts to
  290. # build this Node as we walk the DAG. (This isn't a great way
  291. # to do this, we're reaching into an interface that doesn't
  292. # really belong to us, but it's all about performance, so
  293. # for now we'll just document the dependency...)
  294. if node._memo['stat'] is None:
  295. del node._memo['stat']
  296. except (AttributeError, KeyError):
  297. pass
  298. if result:
  299. raise TypeError(errorfmt % node.abspath)
  300. def ignore_diskcheck_match(node, predicate, errorfmt):
  301. pass
  302. def do_diskcheck_rcs(node, name):
  303. try:
  304. rcs_dir = node.rcs_dir
  305. except AttributeError:
  306. if node.entry_exists_on_disk('RCS'):
  307. rcs_dir = node.Dir('RCS')
  308. else:
  309. rcs_dir = None
  310. node.rcs_dir = rcs_dir
  311. if rcs_dir:
  312. return rcs_dir.entry_exists_on_disk(name+',v')
  313. return None
  314. def ignore_diskcheck_rcs(node, name):
  315. return None
  316. def do_diskcheck_sccs(node, name):
  317. try:
  318. sccs_dir = node.sccs_dir
  319. except AttributeError:
  320. if node.entry_exists_on_disk('SCCS'):
  321. sccs_dir = node.Dir('SCCS')
  322. else:
  323. sccs_dir = None
  324. node.sccs_dir = sccs_dir
  325. if sccs_dir:
  326. return sccs_dir.entry_exists_on_disk('s.'+name)
  327. return None
  328. def ignore_diskcheck_sccs(node, name):
  329. return None
  330. diskcheck_match = DiskChecker('match', do_diskcheck_match, ignore_diskcheck_match)
  331. diskcheck_rcs = DiskChecker('rcs', do_diskcheck_rcs, ignore_diskcheck_rcs)
  332. diskcheck_sccs = DiskChecker('sccs', do_diskcheck_sccs, ignore_diskcheck_sccs)
  333. diskcheckers = [
  334. diskcheck_match,
  335. diskcheck_rcs,
  336. diskcheck_sccs,
  337. ]
  338. def set_diskcheck(list):
  339. for dc in diskcheckers:
  340. dc.set(list)
  341. def diskcheck_types():
  342. return [dc.type for dc in diskcheckers]
  343. class EntryProxy(SCons.Util.Proxy):
  344. __str__ = SCons.Util.Delegate('__str__')
  345. def __get_abspath(self):
  346. entry = self.get()
  347. return SCons.Subst.SpecialAttrWrapper(entry.get_abspath(),
  348. entry.name + "_abspath")
  349. def __get_filebase(self):
  350. name = self.get().name
  351. return SCons.Subst.SpecialAttrWrapper(SCons.Util.splitext(name)[0],
  352. name + "_filebase")
  353. def __get_suffix(self):
  354. name = self.get().name
  355. return SCons.Subst.SpecialAttrWrapper(SCons.Util.splitext(name)[1],
  356. name + "_suffix")
  357. def __get_file(self):
  358. name = self.get().name
  359. return SCons.Subst.SpecialAttrWrapper(name, name + "_file")
  360. def __get_base_path(self):
  361. """Return the file's directory and file name, with the
  362. suffix stripped."""
  363. entry = self.get()
  364. return SCons.Subst.SpecialAttrWrapper(SCons.Util.splitext(entry.get_path())[0],
  365. entry.name + "_base")
  366. def __get_posix_path(self):
  367. """Return the path with / as the path separator,
  368. regardless of platform."""
  369. if os.sep == '/':
  370. return self
  371. else:
  372. entry = self.get()
  373. r = entry.get_path().replace(os.sep, '/')
  374. return SCons.Subst.SpecialAttrWrapper(r, entry.name + "_posix")
  375. def __get_windows_path(self):
  376. """Return the path with \ as the path separator,
  377. regardless of platform."""
  378. if os.sep == '\\':
  379. return self
  380. else:
  381. entry = self.get()
  382. r = entry.get_path().replace(os.sep, '\\')
  383. return SCons.Subst.SpecialAttrWrapper(r, entry.name + "_windows")
  384. def __get_srcnode(self):
  385. return EntryProxy(self.get().srcnode())
  386. def __get_srcdir(self):
  387. """Returns the directory containing the source node linked to this
  388. node via VariantDir(), or the directory of this node if not linked."""
  389. return EntryProxy(self.get().srcnode().dir)
  390. def __get_rsrcnode(self):
  391. return EntryProxy(self.get().srcnode().rfile())
  392. def __get_rsrcdir(self):
  393. """Returns the directory containing the source node linked to this
  394. node via VariantDir(), or the directory of this node if not linked."""
  395. return EntryProxy(self.get().srcnode().rfile().dir)
  396. def __get_dir(self):
  397. return EntryProxy(self.get().dir)
  398. dictSpecialAttrs = { "base" : __get_base_path,
  399. "posix" : __get_posix_path,
  400. "windows" : __get_windows_path,
  401. "win32" : __get_windows_path,
  402. "srcpath" : __get_srcnode,
  403. "srcdir" : __get_srcdir,
  404. "dir" : __get_dir,
  405. "abspath" : __get_abspath,
  406. "filebase" : __get_filebase,
  407. "suffix" : __get_suffix,
  408. "file" : __get_file,
  409. "rsrcpath" : __get_rsrcnode,
  410. "rsrcdir" : __get_rsrcdir,
  411. }
  412. def __getattr__(self, name):
  413. # This is how we implement the "special" attributes
  414. # such as base, posix, srcdir, etc.
  415. try:
  416. attr_function = self.dictSpecialAttrs[name]
  417. except KeyError:
  418. try:
  419. attr = SCons.Util.Proxy.__getattr__(self, name)
  420. except AttributeError, e:
  421. # Raise our own AttributeError subclass with an
  422. # overridden __str__() method that identifies the
  423. # name of the entry that caused the exception.
  424. raise EntryProxyAttributeError(self, name)
  425. return attr
  426. else:
  427. return attr_function(self)
  428. class Base(SCons.Node.Node):
  429. """A generic class for file system entries. This class is for
  430. when we don't know yet whether the entry being looked up is a file
  431. or a directory. Instances of this class can morph into either
  432. Dir or File objects by a later, more precise lookup.
  433. Note: this class does not define __cmp__ and __hash__ for
  434. efficiency reasons. SCons does a lot of comparing of
  435. Node.FS.{Base,Entry,File,Dir} objects, so those operations must be
  436. as fast as possible, which means we want to use Python's built-in
  437. object identity comparisons.
  438. """
  439. memoizer_counters = []
  440. def __init__(self, name, directory, fs):
  441. """Initialize a generic Node.FS.Base object.
  442. Call the superclass initialization, take care of setting up
  443. our relative and absolute paths, identify our parent
  444. directory, and indicate that this node should use
  445. signatures."""
  446. if __debug__: logInstanceCreation(self, 'Node.FS.Base')
  447. SCons.Node.Node.__init__(self)
  448. # Filenames and paths are probably reused and are intern'ed to
  449. # save some memory.
  450. self.name = SCons.Util.silent_intern(name)
  451. self.suffix = SCons.Util.silent_intern(SCons.Util.splitext(name)[1])
  452. self.fs = fs
  453. assert directory, "A directory must be provided"
  454. self.abspath = SCons.Util.silent_intern(directory.entry_abspath(name))
  455. self.labspath = SCons.Util.silent_intern(directory.entry_labspath(name))
  456. if directory.path == '.':
  457. self.path = SCons.Util.silent_intern(name)
  458. else:
  459. self.path = SCons.Util.silent_intern(directory.entry_path(name))
  460. if directory.tpath == '.':
  461. self.tpath = SCons.Util.silent_intern(name)
  462. else:
  463. self.tpath = SCons.Util.silent_intern(directory.entry_tpath(name))
  464. self.path_elements = directory.path_elements + [self]
  465. self.dir = directory
  466. self.cwd = None # will hold the SConscript directory for target nodes
  467. self.duplicate = directory.duplicate
  468. def str_for_display(self):
  469. return '"' + self.__str__() + '"'
  470. def must_be_same(self, klass):
  471. """
  472. This node, which already existed, is being looked up as the
  473. specified klass. Raise an exception if it isn't.
  474. """
  475. if isinstance(self, klass) or klass is Entry:
  476. return
  477. raise TypeError("Tried to lookup %s '%s' as a %s." %\
  478. (self.__class__.__name__, self.path, klass.__name__))
  479. def get_dir(self):
  480. return self.dir
  481. def get_suffix(self):
  482. return self.suffix
  483. def rfile(self):
  484. return self
  485. def __str__(self):
  486. """A Node.FS.Base object's string representation is its path
  487. name."""
  488. global Save_Strings
  489. if Save_Strings:
  490. return self._save_str()
  491. return self._get_str()
  492. memoizer_counters.append(SCons.Memoize.CountValue('_save_str'))
  493. def _save_str(self):
  494. try:
  495. return self._memo['_save_str']
  496. except KeyError:
  497. pass
  498. result = sys.intern(self._get_str())
  499. self._memo['_save_str'] = result
  500. return result
  501. def _get_str(self):
  502. global Save_Strings
  503. if self.duplicate or self.is_derived():
  504. return self.get_path()
  505. srcnode = self.srcnode()
  506. if srcnode.stat() is None and self.stat() is not None:
  507. result = self.get_path()
  508. else:
  509. result = srcnode.get_path()
  510. if not Save_Strings:
  511. # We're not at the point where we're saving the string string
  512. # representations of FS Nodes (because we haven't finished
  513. # reading the SConscript files and need to have str() return
  514. # things relative to them). That also means we can't yet
  515. # cache values returned (or not returned) by stat(), since
  516. # Python code in the SConscript files might still create
  517. # or otherwise affect the on-disk file. So get rid of the
  518. # values that the underlying stat() method saved.
  519. try: del self._memo['stat']
  520. except KeyError: pass
  521. if self is not srcnode:
  522. try: del srcnode._memo['stat']
  523. except KeyError: pass
  524. return result
  525. rstr = __str__
  526. memoizer_counters.append(SCons.Memoize.CountValue('stat'))
  527. def stat(self):
  528. try: return self._memo['stat']
  529. except KeyError: pass
  530. try: result = self.fs.stat(self.abspath)
  531. except os.error: result = None
  532. self._memo['stat'] = result
  533. return result
  534. def exists(self):
  535. return self.stat() is not None
  536. def rexists(self):
  537. return self.rfile().exists()
  538. def getmtime(self):
  539. st = self.stat()
  540. if st: return st[stat.ST_MTIME]
  541. else: return None
  542. def getsize(self):
  543. st = self.stat()
  544. if st: return st[stat.ST_SIZE]
  545. else: return None
  546. def isdir(self):
  547. st = self.stat()
  548. return st is not None and stat.S_ISDIR(st[stat.ST_MODE])
  549. def isfile(self):
  550. st = self.stat()
  551. return st is not None and stat.S_ISREG(st[stat.ST_MODE])
  552. if hasattr(os, 'symlink'):
  553. def islink(self):
  554. try: st = self.fs.lstat(self.abspath)
  555. except os.error: return 0
  556. return stat.S_ISLNK(st[stat.ST_MODE])
  557. else:
  558. def islink(self):
  559. return 0 # no symlinks
  560. def is_under(self, dir):
  561. if self is dir:
  562. return 1
  563. else:
  564. return self.dir.is_under(dir)
  565. def set_local(self):
  566. self._local = 1
  567. def srcnode(self):
  568. """If this node is in a build path, return the node
  569. corresponding to its source file. Otherwise, return
  570. ourself.
  571. """
  572. srcdir_list = self.dir.srcdir_list()
  573. if srcdir_list:
  574. srcnode = srcdir_list[0].Entry(self.name)
  575. srcnode.must_be_same(self.__class__)
  576. return srcnode
  577. return self
  578. def get_path(self, dir=None):
  579. """Return path relative to the current working directory of the
  580. Node.FS.Base object that owns us."""
  581. if not dir:
  582. dir = self.fs.getcwd()
  583. if self == dir:
  584. return '.'
  585. path_elems = self.path_elements
  586. try: i = path_elems.index(dir)
  587. except ValueError: pass
  588. else: path_elems = path_elems[i+1:]
  589. path_elems = [n.name for n in path_elems]
  590. return os.sep.join(path_elems)
  591. def set_src_builder(self, builder):
  592. """Set the source code builder for this node."""
  593. self.sbuilder = builder
  594. if not self.has_builder():
  595. self.builder_set(builder)
  596. def src_builder(self):
  597. """Fetch the source code builder for this node.
  598. If there isn't one, we cache the source code builder specified
  599. for the directory (which in turn will cache the value from its
  600. parent directory, and so on up to the file system root).
  601. """
  602. try:
  603. scb = self.sbuilder
  604. except AttributeError:
  605. scb = self.dir.src_builder()
  606. self.sbuilder = scb
  607. return scb
  608. def get_abspath(self):
  609. """Get the absolute path of the file."""
  610. return self.abspath
  611. def for_signature(self):
  612. # Return just our name. Even an absolute path would not work,
  613. # because that can change thanks to symlinks or remapped network
  614. # paths.
  615. return self.name
  616. def get_subst_proxy(self):
  617. try:
  618. return self._proxy
  619. except AttributeError:
  620. ret = EntryProxy(self)
  621. self._proxy = ret
  622. return ret
  623. def target_from_source(self, prefix, suffix, splitext=SCons.Util.splitext):
  624. """
  625. Generates a target entry that corresponds to this entry (usually
  626. a source file) with the specified prefix and suffix.
  627. Note that this method can be overridden dynamically for generated
  628. files that need different behavior. See Tool/swig.py for
  629. an example.
  630. """
  631. return self.dir.Entry(prefix + splitext(self.name)[0] + suffix)
  632. def _Rfindalldirs_key(self, pathlist):
  633. return pathlist
  634. memoizer_counters.append(SCons.Memoize.CountDict('Rfindalldirs', _Rfindalldirs_key))
  635. def Rfindalldirs(self, pathlist):
  636. """
  637. Return all of the directories for a given path list, including
  638. corresponding "backing" directories in any repositories.
  639. The Node lookups are relative to this Node (typically a
  640. directory), so memoizing result saves cycles from looking
  641. up the same path for each target in a given directory.
  642. """
  643. try:
  644. memo_dict = self._memo['Rfindalldirs']
  645. except KeyError:
  646. memo_dict = {}
  647. self._memo['Rfindalldirs'] = memo_dict
  648. else:
  649. try:
  650. return memo_dict[pathlist]
  651. except KeyError:
  652. pass
  653. create_dir_relative_to_self = self.Dir
  654. result = []
  655. for path in pathlist:
  656. if isinstance(path, SCons.Node.Node):
  657. result.append(path)
  658. else:
  659. dir = create_dir_relative_to_self(path)
  660. result.extend(dir.get_all_rdirs())
  661. memo_dict[pathlist] = result
  662. return result
  663. def RDirs(self, pathlist):
  664. """Search for a list of directories in the Repository list."""
  665. cwd = self.cwd or self.fs._cwd
  666. return cwd.Rfindalldirs(pathlist)
  667. memoizer_counters.append(SCons.Memoize.CountValue('rentry'))
  668. def rentry(self):
  669. try:
  670. return self._memo['rentry']
  671. except KeyError:
  672. pass
  673. result = self
  674. if not self.exists():
  675. norm_name = _my_normcase(self.name)
  676. for dir in self.dir.get_all_rdirs():
  677. try:
  678. node = dir.entries[norm_name]
  679. except KeyError:
  680. if dir.entry_exists_on_disk(self.name):
  681. result = dir.Entry(self.name)
  682. break
  683. self._memo['rentry'] = result
  684. return result
  685. def _glob1(self, pattern, ondisk=True, source=False, strings=False):
  686. return []
  687. class Entry(Base):
  688. """This is the class for generic Node.FS entries--that is, things
  689. that could be a File or a Dir, but we're just not sure yet.
  690. Consequently, the methods in this class really exist just to
  691. transform their associated object into the right class when the
  692. time comes, and then call the same-named method in the transformed
  693. class."""
  694. def diskcheck_match(self):
  695. pass
  696. def disambiguate(self, must_exist=None):
  697. """
  698. """
  699. if self.isdir():
  700. self.__class__ = Dir
  701. self._morph()
  702. elif self.isfile():
  703. self.__class__ = File
  704. self._morph()
  705. self.clear()
  706. else:
  707. # There was nothing on-disk at this location, so look in
  708. # the src directory.
  709. #
  710. # We can't just use self.srcnode() straight away because
  711. # that would create an actual Node for this file in the src
  712. # directory, and there might not be one. Instead, use the
  713. # dir_on_disk() method to see if there's something on-disk
  714. # with that name, in which case we can go ahead and call
  715. # self.srcnode() to create the right type of entry.
  716. srcdir = self.dir.srcnode()
  717. if srcdir != self.dir and \
  718. srcdir.entry_exists_on_disk(self.name) and \
  719. self.srcnode().isdir():
  720. self.__class__ = Dir
  721. self._morph()
  722. elif must_exist:
  723. msg = "No such file or directory: '%s'" % self.abspath
  724. raise SCons.Errors.UserError(msg)
  725. else:
  726. self.__class__ = File
  727. self._morph()
  728. self.clear()
  729. return self
  730. def rfile(self):
  731. """We're a generic Entry, but the caller is actually looking for
  732. a File at this point, so morph into one."""
  733. self.__class__ = File
  734. self._morph()
  735. self.clear()
  736. return File.rfile(self)
  737. def scanner_key(self):
  738. return self.get_suffix()
  739. def get_contents(self):
  740. """Fetch the contents of the entry. Returns the exact binary
  741. contents of the file."""
  742. try:
  743. self = self.disambiguate(must_exist=1)
  744. except SCons.Errors.UserError:
  745. # There was nothing on disk with which to disambiguate
  746. # this entry. Leave it as an Entry, but return a null
  747. # string so calls to get_contents() in emitters and the
  748. # like (e.g. in qt.py) don't have to disambiguate by hand
  749. # or catch the exception.
  750. return ''
  751. else:
  752. return self.get_contents()
  753. def get_text_contents(self):
  754. """Fetch the decoded text contents of a Unicode encoded Entry.
  755. Since this should return the text contents from the file
  756. system, we check to see into what sort of subclass we should
  757. morph this Entry."""
  758. try:
  759. self = self.disambiguate(must_exist=1)
  760. except SCons.Errors.UserError:
  761. # There was nothing on disk with which to disambiguate
  762. # this entry. Leave it as an Entry, but return a null
  763. # string so calls to get_text_contents() in emitters and
  764. # the like (e.g. in qt.py) don't have to disambiguate by
  765. # hand or catch the exception.
  766. return ''
  767. else:
  768. return self.get_text_contents()
  769. def must_be_same(self, klass):
  770. """Called to make sure a Node is a Dir. Since we're an
  771. Entry, we can morph into one."""
  772. if self.__class__ is not klass:
  773. self.__class__ = klass
  774. self._morph()
  775. self.clear()
  776. # The following methods can get called before the Taskmaster has
  777. # had a chance to call disambiguate() directly to see if this Entry
  778. # should really be a Dir or a File. We therefore use these to call
  779. # disambiguate() transparently (from our caller's point of view).
  780. #
  781. # Right now, this minimal set of methods has been derived by just
  782. # looking at some of the methods that will obviously be called early
  783. # in any of the various Taskmasters' calling sequences, and then
  784. # empirically figuring out which additional methods are necessary
  785. # to make various tests pass.
  786. def exists(self):
  787. """Return if the Entry exists. Check the file system to see
  788. what we should turn into first. Assume a file if there's no
  789. directory."""
  790. return self.disambiguate().exists()
  791. def rel_path(self, other):
  792. d = self.disambiguate()
  793. if d.__class__ is Entry:
  794. raise Exception("rel_path() could not disambiguate File/Dir")
  795. return d.rel_path(other)
  796. def new_ninfo(self):
  797. return self.disambiguate().new_ninfo()
  798. def changed_since_last_build(self, target, prev_ni):
  799. return self.disambiguate().changed_since_last_build(target, prev_ni)
  800. def _glob1(self, pattern, ondisk=True, source=False, strings=False):
  801. return self.disambiguate()._glob1(pattern, ondisk, source, strings)
  802. def get_subst_proxy(self):
  803. return self.disambiguate().get_subst_proxy()
  804. # This is for later so we can differentiate between Entry the class and Entry
  805. # the method of the FS class.
  806. _classEntry = Entry
  807. class LocalFS(object):
  808. if SCons.Memoize.use_memoizer:
  809. __metaclass__ = SCons.Memoize.Memoized_Metaclass
  810. # This class implements an abstraction layer for operations involving
  811. # a local file system. Essentially, this wraps any function in
  812. # the os, os.path or shutil modules that we use to actually go do
  813. # anything with or to the local file system.
  814. #
  815. # Note that there's a very good chance we'll refactor this part of
  816. # the architecture in some way as we really implement the interface(s)
  817. # for remote file system Nodes. For example, the right architecture
  818. # might be to have this be a subclass instead of a base class.
  819. # Nevertheless, we're using this as a first step in that direction.
  820. #
  821. # We're not using chdir() yet because the calling subclass method
  822. # needs to use os.chdir() directly to avoid recursion. Will we
  823. # really need this one?
  824. #def chdir(self, path):
  825. # return os.chdir(path)
  826. def chmod(self, path, mode):
  827. return os.chmod(path, mode)
  828. def copy(self, src, dst):
  829. return shutil.copy(src, dst)
  830. def copy2(self, src, dst):
  831. return shutil.copy2(src, dst)
  832. def exists(self, path):
  833. return os.path.exists(path)
  834. def getmtime(self, path):
  835. return os.path.getmtime(path)
  836. def getsize(self, path):
  837. return os.path.getsize(path)
  838. def isdir(self, path):
  839. return os.path.isdir(path)
  840. def isfile(self, path):
  841. return os.path.isfile(path)
  842. def link(self, src, dst):
  843. return os.link(src, dst)
  844. def lstat(self, path):
  845. return os.lstat(path)
  846. def listdir(self, path):
  847. return os.listdir(path)
  848. def makedirs(self, path):
  849. return os.makedirs(path)
  850. def mkdir(self, path):
  851. return os.mkdir(path)
  852. def rename(self, old, new):
  853. return os.rename(old, new)
  854. def stat(self, path):
  855. return os.stat(path)
  856. def symlink(self, src, dst):
  857. return os.symlink(src, dst)
  858. def open(self, path):
  859. return open(path)
  860. def unlink(self, path):
  861. return os.unlink(path)
  862. if hasattr(os, 'symlink'):
  863. def islink(self, path):
  864. return os.path.islink(path)
  865. else:
  866. def islink(self, path):
  867. return 0 # no symlinks
  868. if hasattr(os, 'readlink'):
  869. def readlink(self, file):
  870. return os.readlink(file)
  871. else:
  872. def readlink(self, file):
  873. return ''
  874. #class RemoteFS:
  875. # # Skeleton for the obvious methods we might need from the
  876. # # abstraction layer for a remote filesystem.
  877. # def upload(self, local_src, remote_dst):
  878. # pass
  879. # def download(self, remote_src, local_dst):
  880. # pass
  881. class FS(LocalFS):
  882. memoizer_counters = []
  883. def __init__(self, path = None):
  884. """Initialize the Node.FS subsystem.
  885. The supplied path is the top of the source tree, where we
  886. expect to find the top-level build file. If no path is
  887. supplied, the current directory is the default.
  888. The path argument must be a valid absolute path.
  889. """
  890. if __debug__: logInstanceCreation(self, 'Node.FS')
  891. self._memo = {}
  892. self.Root = {}
  893. self.SConstruct_dir = None
  894. self.max_drift = default_max_drift
  895. self.Top = None
  896. if path is None:
  897. self.pathTop = os.getcwd()
  898. else:
  899. self.pathTop = path
  900. self.defaultDrive = _my_normcase(os.path.splitdrive(self.pathTop)[0])
  901. self.Top = self.Dir(self.pathTop)
  902. self.Top.path = '.'
  903. self.Top.tpath = '.'
  904. self._cwd = self.Top
  905. DirNodeInfo.fs = self
  906. FileNodeInfo.fs = self
  907. def set_SConstruct_dir(self, dir):
  908. self.SConstruct_dir = dir
  909. def get_max_drift(self):
  910. return self.max_drift
  911. def set_max_drift(self, max_drift):
  912. self.max_drift = max_drift
  913. def getcwd(self):
  914. return self._cwd
  915. def chdir(self, dir, change_os_dir=0):
  916. """Change the current working directory for lookups.
  917. If change_os_dir is true, we will also change the "real" cwd
  918. to match.
  919. """
  920. curr=self._cwd
  921. try:
  922. if dir is not None:
  923. self._cwd = dir
  924. if change_os_dir:
  925. os.chdir(dir.abspath)
  926. except OSError:
  927. self._cwd = curr
  928. raise
  929. def get_root(self, drive):
  930. """
  931. Returns the root directory for the specified drive, creating
  932. it if necessary.
  933. """
  934. drive = _my_normcase(drive)
  935. try:
  936. return self.Root[drive]
  937. except KeyError:
  938. root = RootDir(drive, self)
  939. self.Root[drive] = root
  940. if not drive:
  941. self.Root[self.defaultDrive] = root
  942. elif drive == self.defaultDrive:
  943. self.Root[''] = root
  944. return root
  945. def _lookup(self, p, directory, fsclass, create=1):
  946. """
  947. The generic entry point for Node lookup with user-supplied data.
  948. This translates arbitrary input into a canonical Node.FS object
  949. of the specified fsclass. The general approach for strings is
  950. to turn it into a fully normalized absolute path and then call
  951. the root directory's lookup_abs() method for the heavy lifting.
  952. If the path name begins with '#', it is unconditionally
  953. interpreted relative to the top-level directory of this FS. '#'
  954. is treated as a synonym for the top-level SConstruct directory,
  955. much like '~' is treated as a synonym for the user's home
  956. directory in a UNIX shell. So both '#foo' and '#/foo' refer
  957. to the 'foo' subdirectory underneath the top-level SConstruct
  958. directory.
  959. If the path name is relative, then the path is looked up relative
  960. to the specified directory, or the current directory (self._cwd,
  961. typically the SConscript directory) if the specified directory
  962. is None.
  963. """
  964. if isinstance(p, Base):
  965. # It's already a Node.FS object. Make sure it's the right
  966. # class and return.
  967. p.must_be_same(fsclass)
  968. return p
  969. # str(p) in case it's something like a proxy object
  970. p = str(p)
  971. initial_hash = (p[0:1] == '#')
  972. if initial_hash:
  973. # There was an initial '#', so we strip it and override
  974. # whatever directory they may have specified with the
  975. # top-level SConstruct directory.
  976. p = p[1:]
  977. directory = self.Top
  978. if directory and not isinstance(directory, Dir):
  979. directory = self.Dir(directory)
  980. if do_splitdrive:
  981. drive, p = os.path.splitdrive(p)
  982. else:
  983. drive = ''
  984. if drive and not p:
  985. # This causes a naked drive letter to be treated as a synonym
  986. # for the root directory on that drive.
  987. p = os.sep
  988. absolute = os.path.isabs(p)
  989. needs_normpath = needs_normpath_check.match(p)
  990. if initial_hash or not absolute:
  991. # This is a relative lookup, either to the top-level
  992. # SConstruct directory (because of the initial '#') or to
  993. # the current directory (the path name is not absolute).
  994. # Add the string to the appropriate directory lookup path,
  995. # after which the whole thing gets normalized.
  996. if not directory:
  997. directory = self._cwd
  998. if p:
  999. p = directory.labspath + '/' + p
  1000. else:
  1001. p = directory.labspath
  1002. if needs_normpath:
  1003. p = os.path.normpath(p)
  1004. if drive or absolute:
  1005. root = self.get_root(drive)
  1006. else:
  1007. if not directory:
  1008. directory = self._cwd
  1009. root = directory.root
  1010. if os.sep != '/':
  1011. p = p.replace(os.sep, '/')
  1012. return root._lookup_abs(p, fsclass, create)
  1013. def Entry(self, name, directory = None, create = 1):
  1014. """Look up or create a generic Entry node with the specified name.
  1015. If the name is a relative path (begins with ./, ../, or a file
  1016. name), then it is looked up relative to the supplied directory
  1017. node, or to the top level directory of the FS (supplied at
  1018. construction time) if no directory is supplied.
  1019. """
  1020. return self._lookup(name, directory, Entry, create)
  1021. def File(self, name, directory = None, create = 1):
  1022. """Look up or create a File node with the specified name. If
  1023. the name is a relative path (begins with ./, ../, or a file name),
  1024. then it is looked up relative to the supplied directory node,
  1025. or to the top level directory of the FS (supplied at construction
  1026. time) if no directory is supplied.
  1027. This method will raise TypeError if a directory is found at the
  1028. specified path.
  1029. """
  1030. return self._lookup(name, directory, File, create)
  1031. def Dir(self, name, directory = None, create = True):
  1032. """Look up or create a Dir node with the specified name. If
  1033. the name is a relative path (begins with ./, ../, or a file name),
  1034. then it is looked up relative to the supplied directory node,
  1035. or to the top level directory of the FS (supplied at construction
  1036. time) if no directory is supplied.
  1037. This method will raise TypeError if a normal file is found at the
  1038. specified path.
  1039. """
  1040. return self._lookup(name, directory, Dir, create)
  1041. def VariantDir(self, variant_dir, src_dir, duplicate=1):
  1042. """Link the supplied variant directory to the source directory
  1043. for purposes of building files."""
  1044. if not isinstance(src_dir, SCons.Node.Node):
  1045. src_dir = self.Dir(src_dir)
  1046. if not isinstance(variant_dir, SCons.Node.Node):
  1047. variant_dir = self.Dir(variant_dir)
  1048. if src_dir.is_under(variant_dir):
  1049. raise SCons.Errors.UserError("Source directory cannot be under variant directory.")
  1050. if variant_dir.srcdir:
  1051. if variant_dir.srcdir == src_dir:
  1052. return # We already did this.
  1053. raise SCons.Errors.UserError("'%s' already has a source directory: '%s'."%(variant_dir, variant_dir.srcdir))
  1054. variant_dir.link(src_dir, duplicate)
  1055. def Repository(self, *dirs):
  1056. """Specify Repository directories to search."""
  1057. for d in dirs:
  1058. if not isinstance(d, SCons.Node.Node):
  1059. d = self.Dir(d)
  1060. self.Top.addRepository(d)
  1061. def variant_dir_target_climb(self, orig, dir, tail):
  1062. """Create targets in corresponding variant directories
  1063. Climb the directory tree, and look up path names
  1064. relative to any linked variant directories we find.
  1065. Even though this loops and walks up the tree, we don't memoize
  1066. the return value because this is really only used to process
  1067. the command-line targets.
  1068. """
  1069. targets = []
  1070. message = None
  1071. fmt = "building associated VariantDir targets: %s"
  1072. start_dir = dir
  1073. while dir:
  1074. for bd in dir.variant_dirs:
  1075. if start_dir.is_under(bd):
  1076. # If already in the build-dir location, don't reflect
  1077. return [orig], fmt % str(orig)
  1078. p = os.path.join(bd.path, *tail)
  1079. targets.append(self.Entry(p))
  1080. tail = [dir.name] + tail
  1081. dir = dir.up()
  1082. if targets:
  1083. message = fmt % ' '.join(map(str, targets))
  1084. return targets, message
  1085. def Glob(self, pathname, ondisk=True, source=True, strings=False, cwd=None):
  1086. """
  1087. Globs
  1088. This is mainly a shim layer
  1089. """
  1090. if cwd is None:
  1091. cwd = self.getcwd()
  1092. return cwd.glob(pathname, ondisk, source, strings)
  1093. class DirNodeInfo(SCons.Node.NodeInfoBase):
  1094. # This should get reset by the FS initialization.
  1095. current_version_id = 1
  1096. fs = None
  1097. def str_to_node(self, s):
  1098. top = self.fs.Top
  1099. root = top.root
  1100. if do_splitdrive:
  1101. drive, s = os.path.splitdrive(s)
  1102. if drive:
  1103. root = self.fs.get_root(drive)
  1104. if not os.path.isabs(s):
  1105. s = top.labspath + '/' + s
  1106. return root._lookup_abs(s, Entry)
  1107. class DirBuildInfo(SCons.Node.BuildInfoBase):
  1108. current_version_id = 1
  1109. glob_magic_check = re.compile('[*?[]')
  1110. def has_glob_magic(s):
  1111. return glob_magic_check.search(s) is not None
  1112. class Dir(Base):
  1113. """A class for directories in a file system.
  1114. """
  1115. memoizer_counters = []
  1116. NodeInfo = DirNodeInfo
  1117. BuildInfo = DirBuildInfo
  1118. def __init__(self, name, directory, fs):
  1119. if __debug__: logInstanceCreation(self, 'Node.FS.Dir')
  1120. Base.__init__(self, name, directory, fs)
  1121. self._morph()
  1122. def _morph(self):
  1123. """Turn a file system Node (either a freshly initialized directory
  1124. object or a separate Entry object) into a proper directory object.
  1125. Set up this directory's entries and hook it into the file
  1126. system tree. Specify that directories (this Node) don't use
  1127. signatures for calculating whether they're current.
  1128. """
  1129. self.repositories = []
  1130. self.srcdir = None
  1131. self.entries = {}
  1132. self.entries['.'] = self
  1133. self.entries['..'] = self.dir
  1134. self.cwd = self
  1135. self.searched = 0
  1136. self._sconsign = None
  1137. self.variant_dirs = []
  1138. self.root = self.dir.root
  1139. # Don't just reset the executor, replace its action list,
  1140. # because it might have some pre-or post-actions that need to
  1141. # be preserved.
  1142. self.builder = get_MkdirBuilder()
  1143. self.get_executor().set_action_list(self.builder.action)
  1144. def diskcheck_match(self):
  1145. diskcheck_match(self, self.isfile,
  1146. "File %s found where directory expected.")
  1147. def __clearRepositoryCache(self, duplicate=None):
  1148. """Called when we change the repository(ies) for a directory.
  1149. This clears any cached information that is invalidated by changing
  1150. the repository."""
  1151. for node in self.entries.values():
  1152. if node != self.dir:
  1153. if node != self and isinstance(node, Dir):
  1154. node.__clearRepositoryCache(duplicate)
  1155. else:
  1156. node.clear()
  1157. try:
  1158. del node._srcreps
  1159. except AttributeError:
  1160. pass
  1161. if duplicate is not None:
  1162. node.duplicate=duplicate
  1163. def __resetDuplicate(self, node):
  1164. if node != self:
  1165. node.duplicate = node.get_dir().duplicate
  1166. def Entry(self, name):
  1167. """
  1168. Looks up or creates an entry node named 'name' relative to
  1169. this directory.
  1170. """
  1171. return self.fs.Entry(name, self)
  1172. def Dir(self, name, create=True):
  1173. """
  1174. Looks up or creates a directory node named 'name' relative to
  1175. this directory.
  1176. """
  1177. return self.fs.Dir(name, self, create)
  1178. def File(self, name):
  1179. """
  1180. Looks up or creates a file node named 'name' relative to
  1181. this directory.
  1182. """
  1183. return self.fs.File(name, self)
  1184. def _lookup_rel(self, name, klass, create=1):
  1185. """
  1186. Looks up a *normalized* relative path name, relative to this
  1187. directory.
  1188. This method is intended for use by internal lookups with
  1189. already-normalized path data. For general-purpose lookups,
  1190. use the Entry(), Dir() and File() methods above.
  1191. This method does *no* input checking and will die or give
  1192. incorrect results if it's passed a non-normalized path name (e.g.,
  1193. a path containing '..'), an absolute path name, a top-relative
  1194. ('#foo') path name, or any kind of object.
  1195. """
  1196. name = self.entry_labspath(name)
  1197. return self.root._lookup_abs(name, klass, create)
  1198. def link(self, srcdir, duplicate):
  1199. """Set this directory as the variant directory for the
  1200. supplied source directory."""
  1201. self.srcdir = srcdir
  1202. self.duplicate = duplicate
  1203. self.__clearRepositoryCache(duplicate)
  1204. srcdir.variant_dirs.append(self)
  1205. def getRepositories(self):
  1206. """Returns a list of repositories for this directory.
  1207. """
  1208. if self.srcdir and not self.duplicate:
  1209. return self.srcdir.get_all_rdirs() + self.repositories
  1210. return self.repositories
  1211. memoizer_counters.append(SCons.Memoize.CountValue('get_all_rdirs'))
  1212. def get_all_rdirs(self):
  1213. try:
  1214. return list(self._memo['get_all_rdirs'])
  1215. except KeyError:
  1216. pass
  1217. result = [self]
  1218. fname = '.'
  1219. dir = self
  1220. while dir:
  1221. for rep in dir.getRepositories():
  1222. result.append(rep.Dir(fname))
  1223. if fname == '.':
  1224. fname = dir.name
  1225. else:
  1226. fname = dir.name + os.sep + fname
  1227. dir = dir.up()
  1228. self._memo['get_all_rdirs'] = list(result)
  1229. return result
  1230. def addRepository(self, dir):
  1231. if dir != self and not dir in self.repositories:
  1232. self.repositories.append(dir)
  1233. dir.tpath = '.'
  1234. self.__clearRepositoryCache()
  1235. def up(self):
  1236. return self.entries['..']
  1237. def _rel_path_key(self, other):
  1238. return str(other)
  1239. memoizer_counters.append(SCons.Memoize.CountDict('rel_path', _rel_path_key))
  1240. def rel_path(self, other):
  1241. """Return a path to "other" relative to this directory.
  1242. """
  1243. # This complicated and expensive method, which constructs relative
  1244. # paths between arbitrary Node.FS objects, is no longer used
  1245. # by SCons itself. It was introduced to store dependency paths
  1246. # in .sconsign files relative to the target, but that ended up
  1247. # being significantly inefficient.
  1248. #
  1249. # We're continuing to support the method because some SConstruct
  1250. # files out there started using it when it was available, and
  1251. # we're all about backwards compatibility..
  1252. try:
  1253. memo_dict = self._memo['rel_path']
  1254. except KeyError:
  1255. memo_dict = {}
  1256. self._memo['rel_path'] = memo_dict
  1257. else:
  1258. try:
  1259. return memo_dict[other]
  1260. except KeyError:
  1261. pass
  1262. if self is other:
  1263. result = '.'
  1264. elif not other in self.path_elements:
  1265. try:
  1266. other_dir = other.get_dir()
  1267. except AttributeError:
  1268. result = str(other)
  1269. else:
  1270. if other_dir is None:
  1271. result = other.name
  1272. else:
  1273. dir_rel_path = self.rel_path(other_dir)
  1274. if dir_rel_path == '.':
  1275. result = other.name
  1276. else:
  1277. result = dir_rel_path + os.sep + other.name
  1278. else:
  1279. i = self.path_elements.index(other) + 1
  1280. path_elems = ['..'] * (len(self.path_elements) - i) \
  1281. + [n.name for n in other.path_elements[i:]]
  1282. result = os.sep.join(path_elems)
  1283. memo_dict[other] = result
  1284. return result
  1285. def get_env_scanner(self, env, kw={}):
  1286. import SCons.Defaults
  1287. return SCons.Defaults.DirEntryScanner
  1288. def get_target_scanner(self):
  1289. import SCons.Defaults
  1290. return SCons.Defaults.DirEntryScanner
  1291. def get_found_includes(self, env, scanner, path):
  1292. """Return this directory's implicit dependencies.
  1293. We don't bother caching the results because the scan typically
  1294. shouldn't be requested more than once (as opposed to scanning
  1295. .h file contents, which can be requested as many times as the
  1296. files is #included by other files).
  1297. """
  1298. if not scanner:
  1299. return []
  1300. # Clear cached info for this Dir. If we already visited this
  1301. # directory on our walk down the tree (because we didn't know at
  1302. # that point it was being used as the source for another Node)
  1303. # then we may have calculated build signature before realizing
  1304. # we had to scan the disk. Now that we have to, though, we need
  1305. # to invalidate the old calculated signature so that any node
  1306. # dependent on our directory structure gets one that includes
  1307. # info about everything on disk.
  1308. self.clear()
  1309. return scanner(self, env, path)
  1310. #
  1311. # Taskmaster interface subsystem
  1312. #
  1313. def prepare(self):
  1314. pass
  1315. def build(self, **kw):
  1316. """A null "builder" for directories."""
  1317. global MkdirBuilder
  1318. if self.builder is not MkdirBuilder:
  1319. SCons.Node.Node.build(self, **kw)
  1320. #
  1321. #
  1322. #
  1323. def _create(self):
  1324. """Create this directory, silently and without worrying about
  1325. whether the builder is the default or not."""
  1326. listDirs = []
  1327. parent = self
  1328. while parent:
  1329. if parent.exists():
  1330. break
  1331. listDirs.append(parent)
  1332. p = parent.up()
  1333. if p is None:
  1334. # Don't use while: - else: for this condition because
  1335. # if so, then parent is None and has no .path attribute.
  1336. raise SCons.Errors.StopError(parent.path)
  1337. parent = p
  1338. listDirs.reverse()
  1339. for dirnode in listDirs:
  1340. try:
  1341. # Don't call dirnode.build(), call the base Node method
  1342. # directly because we definitely *must* create this
  1343. # directory. The dirnode.build() method will suppress
  1344. # the build if it's the default builder.
  1345. SCons.Node.Node.build(dirnode)
  1346. dirnode.get_executor().nullify()
  1347. # The build() action may or may not have actually
  1348. # created the directory, depending on whether the -n
  1349. # option was used or not. Delete the _exists and
  1350. # _rexists attributes so they can be reevaluated.
  1351. dirnode.clear()
  1352. except OSError:
  1353. pass
  1354. def multiple_side_effect_has_builder(self):
  1355. global MkdirBuilder
  1356. return self.builder is not MkdirBuilder and self.has_builder()
  1357. def alter_targets(self):
  1358. """Return any corresponding targets in a variant directory.
  1359. """
  1360. return self.fs.variant_dir_target_climb(self, self, [])
  1361. def scanner_key(self):
  1362. """A directory does not get scanned."""
  1363. return None
  1364. def get_text_contents(self):
  1365. """We already emit things in text, so just return the binary
  1366. version."""
  1367. return self.get_contents()
  1368. def get_contents(self):
  1369. """Return content signatures and names of all our children
  1370. separated by new-lines. Ensure that the nodes are sorted."""
  1371. contents = []
  1372. for node in sorted(self.children(), key=lambda t: t.name):
  1373. contents.append('%s %s\n' % (node.get_csig(), node.name))
  1374. return ''.join(contents)
  1375. def get_csig(self):
  1376. """Compute the content signature for Directory nodes. In
  1377. general, this is not needed and the content signature is not
  1378. stored in the DirNodeInfo. However, if get_contents on a Dir
  1379. node is called which has a child directory, the child
  1380. directory should return the hash of its contents."""
  1381. contents = self.get_contents()
  1382. return SCons.Util.MD5signature(contents)
  1383. def do_duplicate(self, src):
  1384. pass
  1385. changed_since_last_build = SCons.Node.Node.state_has_changed
  1386. def is_up_to_date(self):
  1387. """If any child is not up-to-date, then this directory isn't,
  1388. either."""
  1389. if self.builder is not MkdirBuilder and not self.exists():
  1390. return 0
  1391. up_to_date = SCons.Node.up_to_date
  1392. for kid in self.children():
  1393. if kid.get_state() > up_to_date:
  1394. return 0
  1395. return 1
  1396. def rdir(self):
  1397. if not self.exists():
  1398. norm_name = _my_normcase(self.name)
  1399. for dir in self.dir.get_all_rdirs():
  1400. try: node = dir.entries[norm_name]
  1401. except KeyError: node = dir.dir_on_disk(self.name)
  1402. if node and node.exists() and \
  1403. (isinstance(dir, Dir) or isinstance(dir, Entry)):
  1404. return node
  1405. return self
  1406. def sconsign(self):
  1407. """Return the .sconsign file info for this directory,
  1408. creating it first if necessary."""
  1409. if not self._sconsign:
  1410. import SCons.SConsign
  1411. self._sconsign = SCons.SConsign.ForDirectory(self)
  1412. return self._sconsign
  1413. def srcnode(self):
  1414. """Dir has a special need for srcnode()...if we
  1415. have a srcdir attribute set, then that *is* our srcnode."""
  1416. if self.srcdir:
  1417. return self.srcdir
  1418. return Base.srcnode(self)
  1419. def get_timestamp(self):
  1420. """Return the latest timestamp from among our children"""
  1421. stamp = 0
  1422. for kid in self.children():
  1423. if kid.get_timestamp() > stamp:
  1424. stamp = kid.get_timestamp()
  1425. return stamp
  1426. def entry_abspath(self, name):
  1427. return self.abspath + os.sep + name
  1428. def entry_labspath(self, name):
  1429. return self.labspath + '/' + name
  1430. def entry_path(self, name):
  1431. return self.path + os.sep + name
  1432. def entry_tpath(self, name):
  1433. return self.tpath + os.sep + name
  1434. def entry_exists_on_disk(self, name):
  1435. try:
  1436. d = self.on_disk_entries
  1437. except AttributeError:
  1438. d = {}
  1439. try:
  1440. entries = os.listdir(self.abspath)
  1441. except OSError:
  1442. pass
  1443. else:
  1444. for entry in map(_my_normcase, entries):
  1445. d[entry] = True
  1446. self.on_disk_entries = d
  1447. if sys.platform == 'win32':
  1448. name = _my_normcase(name)
  1449. result = d.get(name)
  1450. if result is None:
  1451. # Belt-and-suspenders for Windows: check directly for
  1452. # 8.3 file names that don't show up in os.listdir().
  1453. result = os.path.exists(self.abspath + os.sep + name)
  1454. d[name] = result
  1455. return result
  1456. else:
  1457. return name in d
  1458. memoizer_counters.append(SCons.Memoize.CountValue('srcdir_list'))
  1459. def srcdir_list(self):
  1460. try:
  1461. return self._memo['srcdir_list']
  1462. except KeyError:
  1463. pass
  1464. result = []
  1465. dirname = '.'
  1466. dir = self
  1467. while dir:
  1468. if dir.srcdir:
  1469. result.append(dir.srcdir.Dir(dirname))
  1470. dirname = dir.name + os.sep + dirname
  1471. dir = dir.up()
  1472. self._memo['srcdir_list'] = result
  1473. return result
  1474. def srcdir_duplicate(self, name):
  1475. for dir in self.srcdir_list():
  1476. if self.is_under(dir):
  1477. # We shouldn't source from something in the build path;
  1478. # variant_dir is probably under src_dir, in which case
  1479. # we are reflecting.
  1480. break
  1481. if dir.entry_exists_on_disk(name):
  1482. srcnode = dir.Entry(name).disambiguate()
  1483. if self.duplicate:
  1484. node = self.Entry(name).disambiguate()
  1485. node.do_duplicate(srcnode)
  1486. return node
  1487. else:
  1488. return srcnode
  1489. return None
  1490. def _srcdir_find_file_key(self, filename):
  1491. return filename
  1492. memoizer_counters.append(SCons.Memoize.CountDict('srcdir_find_file', _srcdir_find_file_key))
  1493. def srcdir_find_file(self, filename):
  1494. try:
  1495. memo_dict = self._memo['srcdir_find_file']
  1496. except KeyError:
  1497. memo_dict = {}
  1498. self._memo['srcdir_find_file'] = memo_dict
  1499. else:
  1500. try:
  1501. return memo_dict[filename]
  1502. except KeyError:
  1503. pass
  1504. def func(node):
  1505. if (isinstance(node, File) or isinstance(node, Entry)) and \
  1506. (node.is_derived() or node.exists()):
  1507. return node
  1508. return None
  1509. norm_name = _my_normcase(filename)
  1510. for rdir in self.get_all_rdirs():
  1511. try: node = rdir.entries[norm_name]
  1512. except KeyError: node = rdir.file_on_disk(filename)
  1513. else: node = func(node)
  1514. if node:
  1515. result = (node, self)
  1516. memo_dict[filename] = result
  1517. return result
  1518. for srcdir in self.srcdir_list():
  1519. for rdir in srcdir.get_all_rdirs():
  1520. try: node = rdir.entries[norm_name]
  1521. except KeyError: node = rdir.file_on_disk(filename)
  1522. else: node = func(node)
  1523. if node:
  1524. result = (File(filename, self, self.fs), srcdir)
  1525. memo_dict[filename] = result
  1526. return result
  1527. result = (None, None)
  1528. memo_dict[filename] = result
  1529. return result
  1530. def dir_on_disk(self, name):
  1531. if self.entry_exists_on_disk(name):
  1532. try: return self.Dir(name)
  1533. except TypeError: pass
  1534. node = self.srcdir_duplicate(name)
  1535. if isinstance(node, File):
  1536. return None
  1537. return node
  1538. def file_on_disk(self, name):
  1539. if self.entry_exists_on_disk(name) or \
  1540. diskcheck_rcs(self, name) or \
  1541. diskcheck_sccs(self, name):
  1542. try: return self.File(name)
  1543. except TypeError: pass
  1544. node = self.srcdir_duplicate(name)
  1545. if isinstance(node, Dir):
  1546. return None
  1547. return node
  1548. def walk(self, func, arg):
  1549. """
  1550. Walk this directory tree by calling the specified function
  1551. for each directory in the tree.
  1552. This behaves like the os.path.walk() function, but for in-memory
  1553. Node.FS.Dir objects. The function takes the same arguments as
  1554. the functions passed to os.path.walk():
  1555. func(arg, dirname, fnames)
  1556. Except that "dirname" will actually be the directory *Node*,
  1557. not the string. The '.' and '..' entries are excluded from
  1558. fnames. The fnames list may be modified in-place to filter the
  1559. subdirectories visited or otherwise impose a specific order.
  1560. The "arg" argument is always passed to func() and may be used
  1561. in any way (or ignored, passing None is common).
  1562. """
  1563. entries = self.entries
  1564. names = list(entries.keys())
  1565. names.remove('.')
  1566. names.remove('..')
  1567. func(arg, self, names)
  1568. for dirname in [n for n in names if isinstance(entries[n], Dir)]:
  1569. entries[dirname].walk(func, arg)
  1570. def glob(self, pathname, ondisk=True, source=False, strings=False):
  1571. """
  1572. Returns a list of Nodes (or strings) matching a specified
  1573. pathname pattern.
  1574. Pathname patterns follow UNIX shell semantics: * matches
  1575. any-length strings of any characters, ? matches any character,
  1576. and [] can enclose lists or ranges of characters. Matches do
  1577. not span directory separators.
  1578. The matches take into account Repositories, returning local
  1579. Nodes if a corresponding entry exists in a Repository (either
  1580. an in-memory Node or something on disk).
  1581. By defafult, the glob() function matches entries that exist
  1582. on-disk, in addition to in-memory Nodes. Setting the "ondisk"
  1583. argument to False (or some other non-true value) causes the glob()
  1584. function to only match in-memory Nodes. The default behavior is
  1585. to return both the on-disk and in-memory Nodes.
  1586. The "source" argument, when true, specifies that corresponding
  1587. source Nodes must be returned if you're globbing in a build
  1588. directory (initialized with VariantDir()). The default behavior
  1589. is to return Nodes local to the VariantDir().
  1590. The "strings" argument, when true, returns the matches as strings,
  1591. not Nodes. The strings are path names relative to this directory.
  1592. The underlying algorithm is adapted from the glob.glob() function
  1593. in the Python library (but heavily modified), and uses fnmatch()
  1594. under the covers.
  1595. """
  1596. dirname, basename = os.path.split(pathname)
  1597. if not dirname:
  1598. return sorted(self._glob1(basename, ondisk, source, strings),
  1599. key=lambda t: str(t))
  1600. if has_glob_magic(dirname):
  1601. list = self.glob(dirname, ondisk, source, strings=False)
  1602. else:
  1603. list = [self.Dir(dirname, create=True)]
  1604. result = []
  1605. for dir in list:
  1606. r = dir._glob1(basename, ondisk, source, strings)
  1607. if strings:
  1608. r = [os.path.join(str(dir), x) for x in r]
  1609. result.extend(r)
  1610. return sorted(result, key=lambda a: str(a))
  1611. def _glob1(self, pattern, ondisk=True, source=False, strings=False):
  1612. """
  1613. Globs for and returns a list of entry names matching a single
  1614. pattern in this directory.
  1615. This searches any repositories and source directories for
  1616. corresponding entries and returns a Node (or string) relative
  1617. to the current directory if an entry is found anywhere.
  1618. TODO: handle pattern with no wildcard
  1619. """
  1620. search_dir_list = self.get_all_rdirs()
  1621. for srcdir in self.srcdir_list():
  1622. search_dir_list.extend(srcdir.get_all_rdirs())
  1623. selfEntry = self.Entry
  1624. names = []
  1625. for dir in search_dir_list:
  1626. # We use the .name attribute from the Node because the keys of
  1627. # the dir.entries dictionary are normalized (that is, all upper
  1628. # case) on case-insensitive systems like Windows.
  1629. node_names = [ v.name for k, v in dir.entries.items()
  1630. if k not in ('.', '..') ]
  1631. names.extend(node_names)
  1632. if not strings:
  1633. # Make sure the working directory (self) actually has
  1634. # entries for all Nodes in repositories or variant dirs.
  1635. for name in node_names: selfEntry(name)
  1636. if ondisk:
  1637. try:
  1638. disk_names = os.listdir(dir.abspath)
  1639. except os.error:
  1640. continue
  1641. names.extend(disk_names)
  1642. if not strings:
  1643. # We're going to return corresponding Nodes in
  1644. # the local directory, so we need to make sure
  1645. # those Nodes exist. We only want to create
  1646. # Nodes for the entries that will match the
  1647. # specified pattern, though, which means we
  1648. # need to filter the list here, even though
  1649. # the overall list will also be filtered later,
  1650. # after we exit this loop.
  1651. if pattern[0] != '.':
  1652. #disk_names = [ d for d in disk_names if d[0] != '.' ]
  1653. disk_names = [x for x in disk_names if x[0] != '.']
  1654. disk_names = fnmatch.filter(disk_names, pattern)
  1655. dirEntry = dir.Entry
  1656. for name in disk_names:
  1657. # Add './' before disk filename so that '#' at
  1658. # beginning of filename isn't interpreted.
  1659. name = './' + name
  1660. node = dirEntry(name).disambiguate()
  1661. n = selfEntry(name)
  1662. if n.__class__ != node.__class__:
  1663. n.__class__ = node.__class__
  1664. n._morph()
  1665. names = set(names)
  1666. if pattern[0] != '.':
  1667. #names = [ n for n in names if n[0] != '.' ]
  1668. names = [x for x in names if x[0] != '.']
  1669. names = fnmatch.filter(names, pattern)
  1670. if strings:
  1671. return names
  1672. #return [ self.entries[_my_normcase(n)] for n in names ]
  1673. return [self.entries[_my_normcase(n)] for n in names]
  1674. class RootDir(Dir):
  1675. """A class for the root directory of a file system.
  1676. This is the same as a Dir class, except that the path separator
  1677. ('/' or '\\') is actually part of the name, so we don't need to
  1678. add a separator when creating the path names of entries within
  1679. this directory.
  1680. """
  1681. def __init__(self, name, fs):
  1682. if __debug__: logInstanceCreation(self, 'Node.FS.RootDir')
  1683. # We're going to be our own parent directory (".." entry and .dir
  1684. # attribute) so we have to set up some values so Base.__init__()
  1685. # won't gag won't it calls some of our methods.
  1686. self.abspath = ''
  1687. self.labspath = ''
  1688. self.path = ''
  1689. self.tpath = ''
  1690. self.path_elements = []
  1691. self.duplicate = 0
  1692. self.root = self
  1693. Base.__init__(self, name, self, fs)
  1694. # Now set our paths to what we really want them to be: the
  1695. # initial drive letter (the name) plus the directory separator,
  1696. # except for the "lookup abspath," which does not have the
  1697. # drive letter.
  1698. self.abspath = name + os.sep
  1699. self.labspath = ''
  1700. self.path = name + os.sep
  1701. self.tpath = name + os.sep
  1702. self._morph()
  1703. self._lookupDict = {}
  1704. # The // and os.sep + os.sep entries are necessary because
  1705. # os.path.normpath() seems to preserve double slashes at the
  1706. # beginning of a path (presumably for UNC path names), but
  1707. # collapses triple slashes to a single slash.
  1708. self._lookupDict[''] = self
  1709. self._lookupDict['/'] = self
  1710. self._lookupDict['//'] = self
  1711. self._lookupDict[os.sep] = self
  1712. self._lookupDict[os.sep + os.sep] = self
  1713. def must_be_same(self, klass):
  1714. if klass is Dir:
  1715. return
  1716. Base.must_be_same(self, klass)
  1717. def _lookup_abs(self, p, klass, create=1):
  1718. """
  1719. Fast (?) lookup of a *normalized* absolute path.
  1720. This method is intended for use by internal lookups with
  1721. already-normalized path data. For general-purpose lookups,
  1722. use the FS.Entry(), FS.Dir() or FS.File() methods.
  1723. The caller is responsible for making sure we're passed a
  1724. normalized absolute path; we merely let Python's dictionary look
  1725. up and return the One True Node.FS object for the path.
  1726. If no Node for the specified "p" doesn't already exist, and
  1727. "create" is specified, the Node may be created after recursive
  1728. invocation to find or create the parent directory or directories.
  1729. """
  1730. k = _my_normcase(p)
  1731. try:
  1732. result = self._lookupDict[k]
  1733. except KeyError:
  1734. if not create:
  1735. msg = "No such file or directory: '%s' in '%s' (and create is False)" % (p, str(self))
  1736. raise SCons.Errors.UserError(msg)
  1737. # There is no Node for this path name, and we're allowed
  1738. # to create it.
  1739. dir_name, file_name = os.path.split(p)
  1740. dir_node = self._lookup_abs(dir_name, Dir)
  1741. result = klass(file_name, dir_node, self.fs)
  1742. # Double-check on disk (as configured) that the Node we
  1743. # created matches whatever is out there in the real world.
  1744. result.diskcheck_match()
  1745. self._lookupDict[k] = result
  1746. dir_node.entries[_my_normcase(file_name)] = result
  1747. dir_node.implicit = None
  1748. else:
  1749. # There is already a Node for this path name. Allow it to
  1750. # complain if we were looking for an inappropriate type.
  1751. result.must_be_same(klass)
  1752. return result
  1753. def __str__(self):
  1754. return self.abspath
  1755. def entry_abspath(self, name):
  1756. return self.abspath + name
  1757. def entry_labspath(self, name):
  1758. return '/' + name
  1759. def entry_path(self, name):
  1760. return self.path + name
  1761. def entry_tpath(self, name):
  1762. return self.tpath + name
  1763. def is_under(self, dir):
  1764. if self is dir:
  1765. return 1
  1766. else:
  1767. return 0
  1768. def up(self):
  1769. return None
  1770. def get_dir(self):
  1771. return None
  1772. def src_builder(self):
  1773. return _null
  1774. class FileNodeInfo(SCons.Node.NodeInfoBase):
  1775. current_version_id = 1
  1776. field_list = ['csig', 'timestamp', 'size']
  1777. # This should get reset by the FS initialization.
  1778. fs = None
  1779. def str_to_node(self, s):
  1780. top = self.fs.Top
  1781. root = top.root
  1782. if do_splitdrive:
  1783. drive, s = os.path.splitdrive(s)
  1784. if drive:
  1785. root = self.fs.get_root(drive)
  1786. if not os.path.isabs(s):
  1787. s = top.labspath + '/' + s
  1788. return root._lookup_abs(s, Entry)
  1789. class FileBuildInfo(SCons.Node.BuildInfoBase):
  1790. current_version_id = 1
  1791. def convert_to_sconsign(self):
  1792. """
  1793. Converts this FileBuildInfo object for writing to a .sconsign file
  1794. This replaces each Node in our various dependency lists with its
  1795. usual string representation: relative to the top-level SConstruct
  1796. directory, or an absolute path if it's outside.
  1797. """
  1798. if os.sep == '/':
  1799. node_to_str = str
  1800. else:
  1801. def node_to_str(n):
  1802. try:
  1803. s = n.path
  1804. except AttributeError:
  1805. s = str(n)
  1806. else:
  1807. s = s.replace(os.sep, '/')
  1808. return s
  1809. for attr in ['bsources', 'bdepends', 'bimplicit']:
  1810. try:
  1811. val = getattr(self, attr)
  1812. except AttributeError:
  1813. pass
  1814. else:
  1815. setattr(self, attr, list(map(node_to_str, val)))
  1816. def convert_from_sconsign(self, dir, name):
  1817. """
  1818. Converts a newly-read FileBuildInfo object for in-SCons use
  1819. For normal up-to-date checking, we don't have any conversion to
  1820. perform--but we're leaving this method here to make that clear.
  1821. """
  1822. pass
  1823. def prepare_dependencies(self):
  1824. """
  1825. Prepares a FileBuildInfo object for explaining what changed
  1826. The bsources, bdepends and bimplicit lists have all been
  1827. stored on disk as paths relative to the top-level SConstruct
  1828. directory. Convert the strings to actual Nodes (for use by the
  1829. --debug=explain code and --implicit-cache).
  1830. """
  1831. attrs = [
  1832. ('bsources', 'bsourcesigs'),
  1833. ('bdepends', 'bdependsigs'),
  1834. ('bimplicit', 'bimplicitsigs'),
  1835. ]
  1836. for (nattr, sattr) in attrs:
  1837. try:
  1838. strings = getattr(self, nattr)
  1839. nodeinfos = getattr(self, sattr)
  1840. except AttributeError:
  1841. continue
  1842. nodes = []
  1843. for s, ni in zip(strings, nodeinfos):
  1844. if not isinstance(s, SCons.Node.Node):
  1845. s = ni.str_to_node(s)
  1846. nodes.append(s)
  1847. setattr(self, nattr, nodes)
  1848. def format(self, names=0):
  1849. result = []
  1850. bkids = self.bsources + self.bdepends + self.bimplicit
  1851. bkidsigs = self.bsourcesigs + self.bdependsigs + self.bimplicitsigs
  1852. for bkid, bkidsig in zip(bkids, bkidsigs):
  1853. result.append(str(bkid) + ': ' +
  1854. ' '.join(bkidsig.format(names=names)))
  1855. result.append('%s [%s]' % (self.bactsig, self.bact))
  1856. return '\n'.join(result)
  1857. class File(Base):
  1858. """A class for files in a file system.
  1859. """
  1860. memoizer_counters = []
  1861. NodeInfo = FileNodeInfo
  1862. BuildInfo = FileBuildInfo
  1863. md5_chunksize = 64
  1864. def diskcheck_match(self):
  1865. diskcheck_match(self, self.isdir,
  1866. "Directory %s found where file expected.")
  1867. def __init__(self, name, directory, fs):
  1868. if __debug__: logInstanceCreation(self, 'Node.FS.File')
  1869. Base.__init__(self, name, directory, fs)
  1870. self._morph()
  1871. def Entry(self, name):
  1872. """Create an entry node named 'name' relative to
  1873. the directory of this file."""
  1874. return self.dir.Entry(name)
  1875. def Dir(self, name, create=True):
  1876. """Create a directory node named 'name' relative to
  1877. the directory of this file."""
  1878. return self.dir.Dir(name, create=create)
  1879. def Dirs(self, pathlist):
  1880. """Create a list of directories relative to the SConscript
  1881. directory of this file."""
  1882. return [self.Dir(p) for p in pathlist]
  1883. def File(self, name):
  1884. """Create a file node named 'name' relative to
  1885. the directory of this file."""
  1886. return self.dir.File(name)
  1887. #def generate_build_dict(self):
  1888. # """Return an appropriate dictionary of values for building
  1889. # this File."""
  1890. # return {'Dir' : self.Dir,
  1891. # 'File' : self.File,
  1892. # 'RDirs' : self.RDirs}
  1893. def _morph(self):
  1894. """Turn a file system node into a File object."""
  1895. self.scanner_paths = {}
  1896. if not hasattr(self, '_local'):
  1897. self._local = 0
  1898. # If there was already a Builder set on this entry, then
  1899. # we need to make sure we call the target-decider function,
  1900. # not the source-decider. Reaching in and doing this by hand
  1901. # is a little bogus. We'd prefer to handle this by adding
  1902. # an Entry.builder_set() method that disambiguates like the
  1903. # other methods, but that starts running into problems with the
  1904. # fragile way we initialize Dir Nodes with their Mkdir builders,
  1905. # yet still allow them to be overridden by the user. Since it's
  1906. # not clear right now how to fix that, stick with what works
  1907. # until it becomes clear...
  1908. if self.has_builder():
  1909. self.changed_since_last_build = self.decide_target
  1910. def scanner_key(self):
  1911. return self.get_suffix()
  1912. def get_contents(self):
  1913. if not self.rexists():
  1914. return ''
  1915. fname = self.rfile().abspath
  1916. try:
  1917. contents = open(fname, "rb").read()
  1918. except EnvironmentError, e:
  1919. if not e.filename:
  1920. e.filename = fname
  1921. raise
  1922. return contents
  1923. # This attempts to figure out what the encoding of the text is
  1924. # based upon the BOM bytes, and then decodes the contents so that
  1925. # it's a valid python string.
  1926. def get_text_contents(self):
  1927. contents = self.get_contents()
  1928. # The behavior of various decode() methods and functions
  1929. # w.r.t. the initial BOM bytes is different for different
  1930. # encodings and/or Python versions. ('utf-8' does not strip
  1931. # them, but has a 'utf-8-sig' which does; 'utf-16' seems to
  1932. # strip them; etc.) Just sidestep all the complication by
  1933. # explicitly stripping the BOM before we decode().
  1934. if contents.startswith(codecs.BOM_UTF8):
  1935. return contents[len(codecs.BOM_UTF8):].decode('utf-8')
  1936. if contents.startswith(codecs.BOM_UTF16_LE):
  1937. return contents[len(codecs.BOM_UTF16_LE):].decode('utf-16-le')
  1938. if contents.startswith(codecs.BOM_UTF16_BE):
  1939. return contents[len(codecs.BOM_UTF16_BE):].decode('utf-16-be')
  1940. return contents
  1941. def get_content_hash(self):
  1942. """
  1943. Compute and return the MD5 hash for this file.
  1944. """
  1945. if not self.rexists():
  1946. return SCons.Util.MD5signature('')
  1947. fname = self.rfile().abspath
  1948. try:
  1949. cs = SCons.Util.MD5filesignature(fname,
  1950. chunksize=SCons.Node.FS.File.md5_chunksize*1024)
  1951. except EnvironmentError, e:
  1952. if not e.filename:
  1953. e.filename = fname
  1954. raise
  1955. return cs
  1956. memoizer_counters.append(SCons.Memoize.CountValue('get_size'))
  1957. def get_size(self):
  1958. try:
  1959. return self._memo['get_size']
  1960. except KeyError:
  1961. pass
  1962. if self.rexists():
  1963. size = self.rfile().getsize()
  1964. else:
  1965. size = 0
  1966. self._memo['get_size'] = size
  1967. return size
  1968. memoizer_counters.append(SCons.Memoize.CountValue('get_timestamp'))
  1969. def get_timestamp(self):
  1970. try:
  1971. return self._memo['get_timestamp']
  1972. except KeyError:
  1973. pass
  1974. if self.rexists():
  1975. timestamp = self.rfile().getmtime()
  1976. else:
  1977. timestamp = 0
  1978. self._memo['get_timestamp'] = timestamp
  1979. return timestamp
  1980. def store_info(self):
  1981. # Merge our build information into the already-stored entry.
  1982. # This accomodates "chained builds" where a file that's a target
  1983. # in one build (SConstruct file) is a source in a different build.
  1984. # See test/chained-build.py for the use case.
  1985. if do_store_info:
  1986. self.dir.sconsign().store_info(self.name, self)
  1987. convert_copy_attrs = [
  1988. 'bsources',
  1989. 'bimplicit',
  1990. 'bdepends',
  1991. 'bact',
  1992. 'bactsig',
  1993. 'ninfo',
  1994. ]
  1995. convert_sig_attrs = [
  1996. 'bsourcesigs',
  1997. 'bimplicitsigs',
  1998. 'bdependsigs',
  1999. ]
  2000. def convert_old_entry(self, old_entry):
  2001. # Convert a .sconsign entry from before the Big Signature
  2002. # Refactoring, doing what we can to convert its information
  2003. # to the new .sconsign entry format.
  2004. #
  2005. # The old format looked essentially like this:
  2006. #
  2007. # BuildInfo
  2008. # .ninfo (NodeInfo)
  2009. # .bsig
  2010. # .csig
  2011. # .timestamp
  2012. # .size
  2013. # .bsources
  2014. # .bsourcesigs ("signature" list)
  2015. # .bdepends
  2016. # .bdependsigs ("signature" list)
  2017. # .bimplicit
  2018. # .bimplicitsigs ("signature" list)
  2019. # .bact
  2020. # .bactsig
  2021. #
  2022. # The new format looks like this:
  2023. #
  2024. # .ninfo (NodeInfo)
  2025. # .bsig
  2026. # .csig
  2027. # .timestamp
  2028. # .size
  2029. # .binfo (BuildInfo)
  2030. # .bsources
  2031. # .bsourcesigs (NodeInfo list)
  2032. # .bsig
  2033. # .csig
  2034. # .timestamp
  2035. # .size
  2036. # .bdepends
  2037. # .bdependsigs (NodeInfo list)
  2038. # .bsig
  2039. # .csig
  2040. # .timestamp
  2041. # .size
  2042. # .bimplicit
  2043. # .bimplicitsigs (NodeInfo list)
  2044. # .bsig
  2045. # .csig
  2046. # .timestamp
  2047. # .size
  2048. # .bact
  2049. # .bactsig
  2050. #
  2051. # The basic idea of the new structure is that a NodeInfo always
  2052. # holds all available information about the state of a given Node
  2053. # at a certain point in time. The various .b*sigs lists can just
  2054. # be a list of pointers to the .ninfo attributes of the different
  2055. # dependent nodes, without any copying of information until it's
  2056. # time to pickle it for writing out to a .sconsign file.
  2057. #
  2058. # The complicating issue is that the *old* format only stored one
  2059. # "signature" per dependency, based on however the *last* build
  2060. # was configured. We don't know from just looking at it whether
  2061. # it was a build signature, a content signature, or a timestamp
  2062. # "signature". Since we no longer use build signatures, the
  2063. # best we can do is look at the length and if it's thirty two,
  2064. # assume that it was (or might have been) a content signature.
  2065. # If it was actually a build signature, then it will cause a
  2066. # rebuild anyway when it doesn't match the new content signature,
  2067. # but that's probably the best we can do.
  2068. import SCons.SConsign
  2069. new_entry = SCons.SConsign.SConsignEntry()
  2070. new_entry.binfo = self.new_binfo()
  2071. binfo = new_entry.binfo
  2072. for attr in self.convert_copy_attrs:
  2073. try:
  2074. value = getattr(old_entry, attr)
  2075. except AttributeError:
  2076. continue
  2077. setattr(binfo, attr, value)
  2078. delattr(old_entry, attr)
  2079. for attr in self.convert_sig_attrs:
  2080. try:
  2081. sig_list = getattr(old_entry, attr)
  2082. except AttributeError:
  2083. continue
  2084. value = []
  2085. for sig in sig_list:
  2086. ninfo = self.new_ninfo()
  2087. if len(sig) == 32:
  2088. ninfo.csig = sig
  2089. else:
  2090. ninfo.timestamp = sig
  2091. value.append(ninfo)
  2092. setattr(binfo, attr, value)
  2093. delattr(old_entry, attr)
  2094. return new_entry
  2095. memoizer_counters.append(SCons.Memoize.CountValue('get_stored_info'))
  2096. def get_stored_info(self):
  2097. try:
  2098. return self._memo['get_stored_info']
  2099. except KeyError:
  2100. pass
  2101. try:
  2102. sconsign_entry = self.dir.sconsign().get_entry(self.name)
  2103. except (KeyError, EnvironmentError):
  2104. import SCons.SConsign
  2105. sconsign_entry = SCons.SConsign.SConsignEntry()
  2106. sconsign_entry.binfo = self.new_binfo()
  2107. sconsign_entry.ninfo = self.new_ninfo()
  2108. else:
  2109. if isinstance(sconsign_entry, FileBuildInfo):
  2110. # This is a .sconsign file from before the Big Signature
  2111. # Refactoring; convert it as best we can.
  2112. sconsign_entry = self.convert_old_entry(sconsign_entry)
  2113. try:
  2114. delattr(sconsign_entry.ninfo, 'bsig')
  2115. except AttributeError:
  2116. pass
  2117. self._memo['get_stored_info'] = sconsign_entry
  2118. return sconsign_entry
  2119. def get_stored_implicit(self):
  2120. binfo = self.get_stored_info().binfo
  2121. binfo.prepare_dependencies()
  2122. try: return binfo.bimplicit
  2123. except AttributeError: return None
  2124. def rel_path(self, other):
  2125. return self.dir.rel_path(other)
  2126. def _get_found_includes_key(self, env, scanner, path):
  2127. return (id(env), id(scanner), path)
  2128. memoizer_counters.append(SCons.Memoize.CountDict('get_found_includes', _get_found_includes_key))
  2129. def get_found_includes(self, env, scanner, path):
  2130. """Return the included implicit dependencies in this file.
  2131. Cache results so we only scan the file once per path
  2132. regardless of how many times this information is requested.
  2133. """
  2134. memo_key = (id(env), id(scanner), path)
  2135. try:
  2136. memo_dict = self._memo['get_found_includes']
  2137. except KeyError:
  2138. memo_dict = {}
  2139. self._memo['get_found_includes'] = memo_dict
  2140. else:
  2141. try:
  2142. return memo_dict[memo_key]
  2143. except KeyError:
  2144. pass
  2145. if scanner:
  2146. # result = [n.disambiguate() for n in scanner(self, env, path)]
  2147. result = scanner(self, env, path)
  2148. result = [N.disambiguate() for N in result]
  2149. else:
  2150. result = []
  2151. memo_dict[memo_key] = result
  2152. return result
  2153. def _createDir(self):
  2154. # ensure that the directories for this node are
  2155. # created.
  2156. self.dir._create()
  2157. def push_to_cache(self):
  2158. """Try to push the node into a cache
  2159. """
  2160. # This should get called before the Nodes' .built() method is
  2161. # called, which would clear the build signature if the file has
  2162. # a source scanner.
  2163. #
  2164. # We have to clear the local memoized values *before* we push
  2165. # the node to cache so that the memoization of the self.exists()
  2166. # return value doesn't interfere.
  2167. if self.nocache:
  2168. return
  2169. self.clear_memoized_values()
  2170. if self.exists():
  2171. self.get_build_env().get_CacheDir().push(self)
  2172. def retrieve_from_cache(self):
  2173. """Try to retrieve the node's content from a cache
  2174. This method is called from multiple threads in a parallel build,
  2175. so only do thread safe stuff here. Do thread unsafe stuff in
  2176. built().
  2177. Returns true iff the node was successfully retrieved.
  2178. """
  2179. if self.nocache:
  2180. return None
  2181. if not self.is_derived():
  2182. return None
  2183. return self.get_build_env().get_CacheDir().retrieve(self)
  2184. def visited(self):
  2185. if self.exists():
  2186. self.get_build_env().get_CacheDir().push_if_forced(self)
  2187. ninfo = self.get_ninfo()
  2188. csig = self.get_max_drift_csig()
  2189. if csig:
  2190. ninfo.csig = csig
  2191. ninfo.timestamp = self.get_timestamp()
  2192. ninfo.size = self.get_size()
  2193. if not self.has_builder():
  2194. # This is a source file, but it might have been a target file
  2195. # in another build that included more of the DAG. Copy
  2196. # any build information that's stored in the .sconsign file
  2197. # into our binfo object so it doesn't get lost.
  2198. old = self.get_stored_info()
  2199. self.get_binfo().__dict__.update(old.binfo.__dict__)
  2200. self.store_info()
  2201. def find_src_builder(self):
  2202. if self.rexists():
  2203. return None
  2204. scb = self.dir.src_builder()
  2205. if scb is _null:
  2206. if diskcheck_sccs(self.dir, self.name):
  2207. scb = get_DefaultSCCSBuilder()
  2208. elif diskcheck_rcs(self.dir, self.name):
  2209. scb = get_DefaultRCSBuilder()
  2210. else:
  2211. scb = None
  2212. if scb is not None:
  2213. try:
  2214. b = self.builder
  2215. except AttributeError:
  2216. b = None
  2217. if b is None:
  2218. self.builder_set(scb)
  2219. return scb
  2220. def has_src_builder(self):
  2221. """Return whether this Node has a source builder or not.
  2222. If this Node doesn't have an explicit source code builder, this
  2223. is where we figure out, on the fly, if there's a transparent
  2224. source code builder for it.
  2225. Note that if we found a source builder, we also set the
  2226. self.builder attribute, so that all of the methods that actually
  2227. *build* this file don't have to do anything different.
  2228. """
  2229. try:
  2230. scb = self.sbuilder
  2231. except AttributeError:
  2232. scb = self.sbuilder = self.find_src_builder()
  2233. return scb is not None
  2234. def alter_targets(self):
  2235. """Return any corresponding targets in a variant directory.
  2236. """
  2237. if self.is_derived():
  2238. return [], None
  2239. return self.fs.variant_dir_target_climb(self, self.dir, [self.name])
  2240. def _rmv_existing(self):
  2241. self.clear_memoized_values()
  2242. e = Unlink(self, [], None)
  2243. if isinstance(e, SCons.Errors.BuildError):
  2244. raise e
  2245. #
  2246. # Taskmaster interface subsystem
  2247. #
  2248. def make_ready(self):
  2249. self.has_src_builder()
  2250. self.get_binfo()
  2251. def prepare(self):
  2252. """Prepare for this file to be created."""
  2253. SCons.Node.Node.prepare(self)
  2254. if self.get_state() != SCons.Node.up_to_date:
  2255. if self.exists():
  2256. if self.is_derived() and not self.precious:
  2257. self._rmv_existing()
  2258. else:
  2259. try:
  2260. self._createDir()
  2261. except SCons.Errors.StopError, drive:
  2262. desc = "No drive `%s' for target `%s'." % (drive, self)
  2263. raise SCons.Errors.StopError(desc)
  2264. #
  2265. #
  2266. #
  2267. def remove(self):
  2268. """Remove this file."""
  2269. if self.exists() or self.islink():
  2270. self.fs.unlink(self.path)
  2271. return 1
  2272. return None
  2273. def do_duplicate(self, src):
  2274. self._createDir()
  2275. Unlink(self, None, None)
  2276. e = Link(self, src, None)
  2277. if isinstance(e, SCons.Errors.BuildError):
  2278. desc = "Cannot duplicate `%s' in `%s': %s." % (src.path, self.dir.path, e.errstr)
  2279. raise SCons.Errors.StopError(desc)
  2280. self.linked = 1
  2281. # The Link() action may or may not have actually
  2282. # created the file, depending on whether the -n
  2283. # option was used or not. Delete the _exists and
  2284. # _rexists attributes so they can be reevaluated.
  2285. self.clear()
  2286. memoizer_counters.append(SCons.Memoize.CountValue('exists'))
  2287. def exists(self):
  2288. try:
  2289. return self._memo['exists']
  2290. except KeyError:
  2291. pass
  2292. # Duplicate from source path if we are set up to do this.
  2293. if self.duplicate and not self.is_derived() and not self.linked:
  2294. src = self.srcnode()
  2295. if src is not self:
  2296. # At this point, src is meant to be copied in a variant directory.
  2297. src = src.rfile()
  2298. if src.abspath != self.abspath:
  2299. if src.exists():
  2300. self.do_duplicate(src)
  2301. # Can't return 1 here because the duplication might
  2302. # not actually occur if the -n option is being used.
  2303. else:
  2304. # The source file does not exist. Make sure no old
  2305. # copy remains in the variant directory.
  2306. if Base.exists(self) or self.islink():
  2307. self.fs.unlink(self.path)
  2308. # Return None explicitly because the Base.exists() call
  2309. # above will have cached its value if the file existed.
  2310. self._memo['exists'] = None
  2311. return None
  2312. result = Base.exists(self)
  2313. self._memo['exists'] = result
  2314. return result
  2315. #
  2316. # SIGNATURE SUBSYSTEM
  2317. #
  2318. def get_max_drift_csig(self):
  2319. """
  2320. Returns the content signature currently stored for this node
  2321. if it's been unmodified longer than the max_drift value, or the
  2322. max_drift value is 0. Returns None otherwise.
  2323. """
  2324. old = self.get_stored_info()
  2325. mtime = self.get_timestamp()
  2326. max_drift = self.fs.max_drift
  2327. if max_drift > 0:
  2328. if (time.time() - mtime) > max_drift:
  2329. try:
  2330. n = old.ninfo
  2331. if n.timestamp and n.csig and n.timestamp == mtime:
  2332. return n.csig
  2333. except AttributeError:
  2334. pass
  2335. elif max_drift == 0:
  2336. try:
  2337. return old.ninfo.csig
  2338. except AttributeError:
  2339. pass
  2340. return None
  2341. def get_csig(self):
  2342. """
  2343. Generate a node's content signature, the digested signature
  2344. of its content.
  2345. node - the node
  2346. cache - alternate node to use for the signature cache
  2347. returns - the content signature
  2348. """
  2349. ninfo = self.get_ninfo()
  2350. try:
  2351. return ninfo.csig
  2352. except AttributeError:
  2353. pass
  2354. csig = self.get_max_drift_csig()
  2355. if csig is None:
  2356. try:
  2357. if self.get_size() < SCons.Node.FS.File.md5_chunksize:
  2358. contents = self.get_contents()
  2359. else:
  2360. csig = self.get_content_hash()
  2361. except IOError:
  2362. # This can happen if there's actually a directory on-disk,
  2363. # which can be the case if they've disabled disk checks,
  2364. # or if an action with a File target actually happens to
  2365. # create a same-named directory by mistake.
  2366. csig = ''
  2367. else:
  2368. if not csig:
  2369. csig = SCons.Util.MD5signature(contents)
  2370. ninfo.csig = csig
  2371. return csig
  2372. #
  2373. # DECISION SUBSYSTEM
  2374. #
  2375. def builder_set(self, builder):
  2376. SCons.Node.Node.builder_set(self, builder)
  2377. self.changed_since_last_build = self.decide_target
  2378. def changed_content(self, target, prev_ni):
  2379. cur_csig = self.get_csig()
  2380. try:
  2381. return cur_csig != prev_ni.csig
  2382. except AttributeError:
  2383. return 1
  2384. def changed_state(self, target, prev_ni):
  2385. return self.state != SCons.Node.up_to_date
  2386. def changed_timestamp_then_content(self, target, prev_ni):
  2387. if not self.changed_timestamp_match(target, prev_ni):
  2388. try:
  2389. self.get_ninfo().csig = prev_ni.csig
  2390. except AttributeError:
  2391. pass
  2392. return False
  2393. return self.changed_content(target, prev_ni)
  2394. def changed_timestamp_newer(self, target, prev_ni):
  2395. try:
  2396. return self.get_timestamp() > target.get_timestamp()
  2397. except AttributeError:
  2398. return 1
  2399. def changed_timestamp_match(self, target, prev_ni):
  2400. try:
  2401. return self.get_timestamp() != prev_ni.timestamp
  2402. except AttributeError:
  2403. return 1
  2404. def decide_source(self, target, prev_ni):
  2405. return target.get_build_env().decide_source(self, target, prev_ni)
  2406. def decide_target(self, target, prev_ni):
  2407. return target.get_build_env().decide_target(self, target, prev_ni)
  2408. # Initialize this Node's decider function to decide_source() because
  2409. # every file is a source file until it has a Builder attached...
  2410. changed_since_last_build = decide_source
  2411. def is_up_to_date(self):
  2412. T = 0
  2413. if T: Trace('is_up_to_date(%s):' % self)
  2414. if not self.exists():
  2415. if T: Trace(' not self.exists():')
  2416. # The file doesn't exist locally...
  2417. r = self.rfile()
  2418. if r != self:
  2419. # ...but there is one in a Repository...
  2420. if not self.changed(r):
  2421. if T: Trace(' changed(%s):' % r)
  2422. # ...and it's even up-to-date...
  2423. if self._local:
  2424. # ...and they'd like a local copy.
  2425. e = LocalCopy(self, r, None)
  2426. if isinstance(e, SCons.Errors.BuildError):
  2427. raise
  2428. self.store_info()
  2429. if T: Trace(' 1\n')
  2430. return 1
  2431. self.changed()
  2432. if T: Trace(' None\n')
  2433. return None
  2434. else:
  2435. r = self.changed()
  2436. if T: Trace(' self.exists(): %s\n' % r)
  2437. return not r
  2438. memoizer_counters.append(SCons.Memoize.CountValue('rfile'))
  2439. def rfile(self):
  2440. try:
  2441. return self._memo['rfile']
  2442. except KeyError:
  2443. pass
  2444. result = self
  2445. if not self.exists():
  2446. norm_name = _my_normcase(self.name)
  2447. for dir in self.dir.get_all_rdirs():
  2448. try: node = dir.entries[norm_name]
  2449. except KeyError: node = dir.file_on_disk(self.name)
  2450. if node and node.exists() and \
  2451. (isinstance(node, File) or isinstance(node, Entry) \
  2452. or not node.is_derived()):
  2453. result = node
  2454. # Copy over our local attributes to the repository
  2455. # Node so we identify shared object files in the
  2456. # repository and don't assume they're static.
  2457. #
  2458. # This isn't perfect; the attribute would ideally
  2459. # be attached to the object in the repository in
  2460. # case it was built statically in the repository
  2461. # and we changed it to shared locally, but that's
  2462. # rarely the case and would only occur if you
  2463. # intentionally used the same suffix for both
  2464. # shared and static objects anyway. So this
  2465. # should work well in practice.
  2466. result.attributes = self.attributes
  2467. break
  2468. self._memo['rfile'] = result
  2469. return result
  2470. def rstr(self):
  2471. return str(self.rfile())
  2472. def get_cachedir_csig(self):
  2473. """
  2474. Fetch a Node's content signature for purposes of computing
  2475. another Node's cachesig.
  2476. This is a wrapper around the normal get_csig() method that handles
  2477. the somewhat obscure case of using CacheDir with the -n option.
  2478. Any files that don't exist would normally be "built" by fetching
  2479. them from the cache, but the normal get_csig() method will try
  2480. to open up the local file, which doesn't exist because the -n
  2481. option meant we didn't actually pull the file from cachedir.
  2482. But since the file *does* actually exist in the cachedir, we
  2483. can use its contents for the csig.
  2484. """
  2485. try:
  2486. return self.cachedir_csig
  2487. except AttributeError:
  2488. pass
  2489. cachedir, cachefile = self.get_build_env().get_CacheDir().cachepath(self)
  2490. if not self.exists() and cachefile and os.path.exists(cachefile):
  2491. self.cachedir_csig = SCons.Util.MD5filesignature(cachefile, \
  2492. SCons.Node.FS.File.md5_chunksize * 1024)
  2493. else:
  2494. self.cachedir_csig = self.get_csig()
  2495. return self.cachedir_csig
  2496. def get_cachedir_bsig(self):
  2497. try:
  2498. return self.cachesig
  2499. except AttributeError:
  2500. pass
  2501. # Add the path to the cache signature, because multiple
  2502. # targets built by the same action will all have the same
  2503. # build signature, and we have to differentiate them somehow.
  2504. children = self.children()
  2505. executor = self.get_executor()
  2506. # sigs = [n.get_cachedir_csig() for n in children]
  2507. sigs = [n.get_cachedir_csig() for n in children]
  2508. sigs.append(SCons.Util.MD5signature(executor.get_contents()))
  2509. sigs.append(self.path)
  2510. result = self.cachesig = SCons.Util.MD5collect(sigs)
  2511. return result
  2512. default_fs = None
  2513. def get_default_fs():
  2514. global default_fs
  2515. if not default_fs:
  2516. default_fs = FS()
  2517. return default_fs
  2518. class FileFinder(object):
  2519. """
  2520. """
  2521. if SCons.Memoize.use_memoizer:
  2522. __metaclass__ = SCons.Memoize.Memoized_Metaclass
  2523. memoizer_counters = []
  2524. def __init__(self):
  2525. self._memo = {}
  2526. def filedir_lookup(self, p, fd=None):
  2527. """
  2528. A helper method for find_file() that looks up a directory for
  2529. a file we're trying to find. This only creates the Dir Node if
  2530. it exists on-disk, since if the directory doesn't exist we know
  2531. we won't find any files in it... :-)
  2532. It would be more compact to just use this as a nested function
  2533. with a default keyword argument (see the commented-out version
  2534. below), but that doesn't work unless you have nested scopes,
  2535. so we define it here just so this work under Python 1.5.2.
  2536. """
  2537. if fd is None:
  2538. fd = self.default_filedir
  2539. dir, name = os.path.split(fd)
  2540. drive, d = os.path.splitdrive(dir)
  2541. if not name and d[:1] in ('/', os.sep):
  2542. #return p.fs.get_root(drive).dir_on_disk(name)
  2543. return p.fs.get_root(drive)
  2544. if dir:
  2545. p = self.filedir_lookup(p, dir)
  2546. if not p:
  2547. return None
  2548. norm_name = _my_normcase(name)
  2549. try:
  2550. node = p.entries[norm_name]
  2551. except KeyError:
  2552. return p.dir_on_disk(name)
  2553. if isinstance(node, Dir):
  2554. return node
  2555. if isinstance(node, Entry):
  2556. node.must_be_same(Dir)
  2557. return node
  2558. return None
  2559. def _find_file_key(self, filename, paths, verbose=None):
  2560. return (filename, paths)
  2561. memoizer_counters.append(SCons.Memoize.CountDict('find_file', _find_file_key))
  2562. def find_file(self, filename, paths, verbose=None):
  2563. """
  2564. find_file(str, [Dir()]) -> [nodes]
  2565. filename - a filename to find
  2566. paths - a list of directory path *nodes* to search in. Can be
  2567. represented as a list, a tuple, or a callable that is
  2568. called with no arguments and returns the list or tuple.
  2569. returns - the node created from the found file.
  2570. Find a node corresponding to either a derived file or a file
  2571. that exists already.
  2572. Only the first file found is returned, and none is returned
  2573. if no file is found.
  2574. """
  2575. memo_key = self._find_file_key(filename, paths)
  2576. try:
  2577. memo_dict = self._memo['find_file']
  2578. except KeyError:
  2579. memo_dict = {}
  2580. self._memo['find_file'] = memo_dict
  2581. else:
  2582. try:
  2583. return memo_dict[memo_key]
  2584. except KeyError:
  2585. pass
  2586. if verbose and not callable(verbose):
  2587. if not SCons.Util.is_String(verbose):
  2588. verbose = "find_file"
  2589. _verbose = u' %s: ' % verbose
  2590. verbose = lambda s: sys.stdout.write(_verbose + s)
  2591. filedir, filename = os.path.split(filename)
  2592. if filedir:
  2593. # More compact code that we can't use until we drop
  2594. # support for Python 1.5.2:
  2595. #
  2596. #def filedir_lookup(p, fd=filedir):
  2597. # """
  2598. # A helper function that looks up a directory for a file
  2599. # we're trying to find. This only creates the Dir Node
  2600. # if it exists on-disk, since if the directory doesn't
  2601. # exist we know we won't find any files in it... :-)
  2602. # """
  2603. # dir, name = os.path.split(fd)
  2604. # if dir:
  2605. # p = filedir_lookup(p, dir)
  2606. # if not p:
  2607. # return None
  2608. # norm_name = _my_normcase(name)
  2609. # try:
  2610. # node = p.entries[norm_name]
  2611. # except KeyError:
  2612. # return p.dir_on_disk(name)
  2613. # if isinstance(node, Dir):
  2614. # return node
  2615. # if isinstance(node, Entry):
  2616. # node.must_be_same(Dir)
  2617. # return node
  2618. # if isinstance(node, Dir) or isinstance(node, Entry):
  2619. # return node
  2620. # return None
  2621. #paths = [_f for _f in map(filedir_lookup, paths) if _f]
  2622. self.default_filedir = filedir
  2623. paths = [_f for _f in map(self.filedir_lookup, paths) if _f]
  2624. result = None
  2625. for dir in paths:
  2626. if verbose:
  2627. verbose("looking for '%s' in '%s' ...\n" % (filename, dir))
  2628. node, d = dir.srcdir_find_file(filename)
  2629. if node:
  2630. if verbose:
  2631. verbose("... FOUND '%s' in '%s'\n" % (filename, d))
  2632. result = node
  2633. break
  2634. memo_dict[memo_key] = result
  2635. return result
  2636. find_file = FileFinder().find_file
  2637. def invalidate_node_memos(targets):
  2638. """
  2639. Invalidate the memoized values of all Nodes (files or directories)
  2640. that are associated with the given entries. Has been added to
  2641. clear the cache of nodes affected by a direct execution of an
  2642. action (e.g. Delete/Copy/Chmod). Existing Node caches become
  2643. inconsistent if the action is run through Execute(). The argument
  2644. `targets` can be a single Node object or filename, or a sequence
  2645. of Nodes/filenames.
  2646. """
  2647. from traceback import extract_stack
  2648. # First check if the cache really needs to be flushed. Only
  2649. # actions run in the SConscript with Execute() seem to be
  2650. # affected. XXX The way to check if Execute() is in the stacktrace
  2651. # is a very dirty hack and should be replaced by a more sensible
  2652. # solution.
  2653. for f in extract_stack():
  2654. if f[2] == 'Execute' and f[0][-14:] == 'Environment.py':
  2655. break
  2656. else:
  2657. # Dont have to invalidate, so return
  2658. return
  2659. if not SCons.Util.is_List(targets):
  2660. targets = [targets]
  2661. for entry in targets:
  2662. # If the target is a Node object, clear the cache. If it is a
  2663. # filename, look up potentially existing Node object first.
  2664. try:
  2665. entry.clear_memoized_values()
  2666. except AttributeError:
  2667. # Not a Node object, try to look up Node by filename. XXX
  2668. # This creates Node objects even for those filenames which
  2669. # do not correspond to an existing Node object.
  2670. node = get_default_fs().Entry(entry)
  2671. if node:
  2672. node.clear_memoized_values()
  2673. # Local Variables:
  2674. # tab-width:4
  2675. # indent-tabs-mode:nil
  2676. # End:
  2677. # vim: set expandtab tabstop=4 shiftwidth=4: