PageRenderTime 65ms CodeModel.GetById 19ms 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

Large files files are truncated, but you can click here to view the full file

  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. """

Large files files are truncated, but you can click here to view the full file