Node.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2005-2018 (ita)
  4. """
  5. Node: filesystem structure
  6. #. Each file/folder is represented by exactly one node.
  7. #. Some potential class properties are stored on :py:class:`waflib.Build.BuildContext` : nodes to depend on, etc.
  8. Unused class members can increase the `.wafpickle` file size sensibly.
  9. #. Node objects should never be created directly, use
  10. the methods :py:func:`Node.make_node` or :py:func:`Node.find_node` for the low-level operations
  11. #. The methods :py:func:`Node.find_resource`, :py:func:`Node.find_dir` :py:func:`Node.find_or_declare` must be
  12. used when a build context is present
  13. #. Each instance of :py:class:`waflib.Context.Context` has a unique :py:class:`Node` subclass required for serialization.
  14. (:py:class:`waflib.Node.Nod3`, see the :py:class:`waflib.Context.Context` initializer). A reference to the context
  15. owning a node is held as *self.ctx*
  16. """
  17. import os, re, sys, shutil
  18. from waflib import Utils, Errors
  19. exclude_regs = '''
  20. **/*~
  21. **/#*#
  22. **/.#*
  23. **/%*%
  24. **/._*
  25. **/*.swp
  26. **/CVS
  27. **/CVS/**
  28. **/.cvsignore
  29. **/SCCS
  30. **/SCCS/**
  31. **/vssver.scc
  32. **/.svn
  33. **/.svn/**
  34. **/BitKeeper
  35. **/.git
  36. **/.git/**
  37. **/.gitignore
  38. **/.bzr
  39. **/.bzrignore
  40. **/.bzr/**
  41. **/.hg
  42. **/.hg/**
  43. **/_MTN
  44. **/_MTN/**
  45. **/.arch-ids
  46. **/{arch}
  47. **/_darcs
  48. **/_darcs/**
  49. **/.intlcache
  50. **/.DS_Store'''
  51. """
  52. Ant patterns for files and folders to exclude while doing the
  53. recursive traversal in :py:meth:`waflib.Node.Node.ant_glob`
  54. """
  55. def ant_matcher(s, ignorecase):
  56. reflags = re.I if ignorecase else 0
  57. ret = []
  58. for x in Utils.to_list(s):
  59. x = x.replace('\\', '/').replace('//', '/')
  60. if x.endswith('/'):
  61. x += '**'
  62. accu = []
  63. for k in x.split('/'):
  64. if k == '**':
  65. accu.append(k)
  66. else:
  67. k = k.replace('.', '[.]').replace('*','.*').replace('?', '.').replace('+', '\\+')
  68. k = '^%s$' % k
  69. try:
  70. exp = re.compile(k, flags=reflags)
  71. except Exception as e:
  72. raise Errors.WafError('Invalid pattern: %s' % k, e)
  73. else:
  74. accu.append(exp)
  75. ret.append(accu)
  76. return ret
  77. def ant_sub_filter(name, nn):
  78. ret = []
  79. for lst in nn:
  80. if not lst:
  81. pass
  82. elif lst[0] == '**':
  83. ret.append(lst)
  84. if len(lst) > 1:
  85. if lst[1].match(name):
  86. ret.append(lst[2:])
  87. else:
  88. ret.append([])
  89. elif lst[0].match(name):
  90. ret.append(lst[1:])
  91. return ret
  92. def ant_sub_matcher(name, pats):
  93. nacc = ant_sub_filter(name, pats[0])
  94. nrej = ant_sub_filter(name, pats[1])
  95. if [] in nrej:
  96. nacc = []
  97. return [nacc, nrej]
  98. class Node(object):
  99. """
  100. This class is organized in two parts:
  101. * The basic methods meant for filesystem access (compute paths, create folders, etc)
  102. * The methods bound to a :py:class:`waflib.Build.BuildContext` (require ``bld.srcnode`` and ``bld.bldnode``)
  103. """
  104. dict_class = dict
  105. """
  106. Subclasses can provide a dict class to enable case insensitivity for example.
  107. """
  108. __slots__ = ('name', 'parent', 'children', 'cache_abspath', 'cache_isdir')
  109. def __init__(self, name, parent):
  110. """
  111. .. note:: Use :py:func:`Node.make_node` or :py:func:`Node.find_node` instead of calling this constructor
  112. """
  113. self.name = name
  114. self.parent = parent
  115. if parent:
  116. if name in parent.children:
  117. raise Errors.WafError('node %s exists in the parent files %r already' % (name, parent))
  118. parent.children[name] = self
  119. def __setstate__(self, data):
  120. "Deserializes node information, used for persistence"
  121. self.name = data[0]
  122. self.parent = data[1]
  123. if data[2] is not None:
  124. # Issue 1480
  125. self.children = self.dict_class(data[2])
  126. def __getstate__(self):
  127. "Serializes node information, used for persistence"
  128. return (self.name, self.parent, getattr(self, 'children', None))
  129. def __str__(self):
  130. """
  131. String representation (abspath), for debugging purposes
  132. :rtype: string
  133. """
  134. return self.abspath()
  135. def __repr__(self):
  136. """
  137. String representation (abspath), for debugging purposes
  138. :rtype: string
  139. """
  140. return self.abspath()
  141. def __copy__(self):
  142. """
  143. Provided to prevent nodes from being copied
  144. :raises: :py:class:`waflib.Errors.WafError`
  145. """
  146. raise Errors.WafError('nodes are not supposed to be copied')
  147. def read(self, flags='r', encoding='latin-1'):
  148. """
  149. Reads and returns the contents of the file represented by this node, see :py:func:`waflib.Utils.readf`::
  150. def build(bld):
  151. bld.path.find_node('wscript').read()
  152. :param flags: Open mode
  153. :type flags: string
  154. :param encoding: encoding value for Python3
  155. :type encoding: string
  156. :rtype: string or bytes
  157. :return: File contents
  158. """
  159. return Utils.readf(self.abspath(), flags, encoding)
  160. def write(self, data, flags='w', encoding='latin-1'):
  161. """
  162. Writes data to the file represented by this node, see :py:func:`waflib.Utils.writef`::
  163. def build(bld):
  164. bld.path.make_node('foo.txt').write('Hello, world!')
  165. :param data: data to write
  166. :type data: string
  167. :param flags: Write mode
  168. :type flags: string
  169. :param encoding: encoding value for Python3
  170. :type encoding: string
  171. """
  172. Utils.writef(self.abspath(), data, flags, encoding)
  173. def read_json(self, convert=True, encoding='utf-8'):
  174. """
  175. Reads and parses the contents of this node as JSON (Python ≥ 2.6)::
  176. def build(bld):
  177. bld.path.find_node('abc.json').read_json()
  178. Note that this by default automatically decodes unicode strings on Python2, unlike what the Python JSON module does.
  179. :type convert: boolean
  180. :param convert: Prevents decoding of unicode strings on Python2
  181. :type encoding: string
  182. :param encoding: The encoding of the file to read. This default to UTF8 as per the JSON standard
  183. :rtype: object
  184. :return: Parsed file contents
  185. """
  186. import json # Python 2.6 and up
  187. object_pairs_hook = None
  188. if convert and sys.hexversion < 0x3000000:
  189. try:
  190. _type = unicode
  191. except NameError:
  192. _type = str
  193. def convert(value):
  194. if isinstance(value, list):
  195. return [convert(element) for element in value]
  196. elif isinstance(value, _type):
  197. return str(value)
  198. else:
  199. return value
  200. def object_pairs(pairs):
  201. return dict((str(pair[0]), convert(pair[1])) for pair in pairs)
  202. object_pairs_hook = object_pairs
  203. return json.loads(self.read(encoding=encoding), object_pairs_hook=object_pairs_hook)
  204. def write_json(self, data, pretty=True):
  205. """
  206. Writes a python object as JSON to disk (Python ≥ 2.6) as UTF-8 data (JSON standard)::
  207. def build(bld):
  208. bld.path.find_node('xyz.json').write_json(199)
  209. :type data: object
  210. :param data: The data to write to disk
  211. :type pretty: boolean
  212. :param pretty: Determines if the JSON will be nicely space separated
  213. """
  214. import json # Python 2.6 and up
  215. indent = 2
  216. separators = (',', ': ')
  217. sort_keys = pretty
  218. newline = os.linesep
  219. if not pretty:
  220. indent = None
  221. separators = (',', ':')
  222. newline = ''
  223. output = json.dumps(data, indent=indent, separators=separators, sort_keys=sort_keys) + newline
  224. self.write(output, encoding='utf-8')
  225. def exists(self):
  226. """
  227. Returns whether the Node is present on the filesystem
  228. :rtype: bool
  229. """
  230. return os.path.exists(self.abspath())
  231. def isdir(self):
  232. """
  233. Returns whether the Node represents a folder
  234. :rtype: bool
  235. """
  236. return os.path.isdir(self.abspath())
  237. def chmod(self, val):
  238. """
  239. Changes the file/dir permissions::
  240. def build(bld):
  241. bld.path.chmod(493) # 0755
  242. """
  243. os.chmod(self.abspath(), val)
  244. def delete(self, evict=True):
  245. """
  246. Removes the file/folder from the filesystem (equivalent to `rm -rf`), and remove this object from the Node tree.
  247. Do not use this object after calling this method.
  248. """
  249. try:
  250. try:
  251. if os.path.isdir(self.abspath()):
  252. shutil.rmtree(self.abspath())
  253. else:
  254. os.remove(self.abspath())
  255. except OSError:
  256. if os.path.exists(self.abspath()):
  257. raise
  258. finally:
  259. if evict:
  260. self.evict()
  261. def evict(self):
  262. """
  263. Removes this node from the Node tree
  264. """
  265. del self.parent.children[self.name]
  266. def suffix(self):
  267. """
  268. Returns the file rightmost extension, for example `a.b.c.d → .d`
  269. :rtype: string
  270. """
  271. k = max(0, self.name.rfind('.'))
  272. return self.name[k:]
  273. def height(self):
  274. """
  275. Returns the depth in the folder hierarchy from the filesystem root or from all the file drives
  276. :returns: filesystem depth
  277. :rtype: integer
  278. """
  279. d = self
  280. val = -1
  281. while d:
  282. d = d.parent
  283. val += 1
  284. return val
  285. def listdir(self):
  286. """
  287. Lists the folder contents
  288. :returns: list of file/folder names ordered alphabetically
  289. :rtype: list of string
  290. """
  291. lst = Utils.listdir(self.abspath())
  292. lst.sort()
  293. return lst
  294. def mkdir(self):
  295. """
  296. Creates a folder represented by this node. Intermediate folders are created as needed.
  297. :raises: :py:class:`waflib.Errors.WafError` when the folder is missing
  298. """
  299. if self.isdir():
  300. return
  301. try:
  302. self.parent.mkdir()
  303. except OSError:
  304. pass
  305. if self.name:
  306. try:
  307. os.makedirs(self.abspath())
  308. except OSError:
  309. pass
  310. if not self.isdir():
  311. raise Errors.WafError('Could not create the directory %r' % self)
  312. try:
  313. self.children
  314. except AttributeError:
  315. self.children = self.dict_class()
  316. def find_node(self, lst):
  317. """
  318. Finds a node on the file system (files or folders), and creates the corresponding Node objects if it exists
  319. :param lst: relative path
  320. :type lst: string or list of string
  321. :returns: The corresponding Node object or None if no entry was found on the filesystem
  322. :rtype: :py:class:´waflib.Node.Node´
  323. """
  324. if isinstance(lst, str):
  325. lst = [x for x in Utils.split_path(lst) if x and x != '.']
  326. if lst and lst[0].startswith('\\\\') and not self.parent:
  327. node = self.ctx.root.make_node(lst[0])
  328. node.cache_isdir = True
  329. return node.find_node(lst[1:])
  330. cur = self
  331. for x in lst:
  332. if x == '..':
  333. cur = cur.parent or cur
  334. continue
  335. try:
  336. ch = cur.children
  337. except AttributeError:
  338. cur.children = self.dict_class()
  339. else:
  340. try:
  341. cur = ch[x]
  342. continue
  343. except KeyError:
  344. pass
  345. # optimistic: create the node first then look if it was correct to do so
  346. cur = self.__class__(x, cur)
  347. if not cur.exists():
  348. cur.evict()
  349. return None
  350. if not cur.exists():
  351. cur.evict()
  352. return None
  353. return cur
  354. def make_node(self, lst):
  355. """
  356. Returns or creates a Node object corresponding to the input path without considering the filesystem.
  357. :param lst: relative path
  358. :type lst: string or list of string
  359. :rtype: :py:class:´waflib.Node.Node´
  360. """
  361. if isinstance(lst, str):
  362. lst = [x for x in Utils.split_path(lst) if x and x != '.']
  363. cur = self
  364. for x in lst:
  365. if x == '..':
  366. cur = cur.parent or cur
  367. continue
  368. try:
  369. cur = cur.children[x]
  370. except AttributeError:
  371. cur.children = self.dict_class()
  372. except KeyError:
  373. pass
  374. else:
  375. continue
  376. cur = self.__class__(x, cur)
  377. return cur
  378. def search_node(self, lst):
  379. """
  380. Returns a Node previously defined in the data structure. The filesystem is not considered.
  381. :param lst: relative path
  382. :type lst: string or list of string
  383. :rtype: :py:class:´waflib.Node.Node´ or None if there is no entry in the Node datastructure
  384. """
  385. if isinstance(lst, str):
  386. lst = [x for x in Utils.split_path(lst) if x and x != '.']
  387. cur = self
  388. for x in lst:
  389. if x == '..':
  390. cur = cur.parent or cur
  391. else:
  392. try:
  393. cur = cur.children[x]
  394. except (AttributeError, KeyError):
  395. return None
  396. return cur
  397. def path_from(self, node):
  398. """
  399. Path of this node seen from the other::
  400. def build(bld):
  401. n1 = bld.path.find_node('foo/bar/xyz.txt')
  402. n2 = bld.path.find_node('foo/stuff/')
  403. n1.path_from(n2) # '../bar/xyz.txt'
  404. :param node: path to use as a reference
  405. :type node: :py:class:`waflib.Node.Node`
  406. :returns: a relative path or an absolute one if that is better
  407. :rtype: string
  408. """
  409. c1 = self
  410. c2 = node
  411. c1h = c1.height()
  412. c2h = c2.height()
  413. lst = []
  414. up = 0
  415. while c1h > c2h:
  416. lst.append(c1.name)
  417. c1 = c1.parent
  418. c1h -= 1
  419. while c2h > c1h:
  420. up += 1
  421. c2 = c2.parent
  422. c2h -= 1
  423. while not c1 is c2:
  424. lst.append(c1.name)
  425. up += 1
  426. c1 = c1.parent
  427. c2 = c2.parent
  428. if c1.parent:
  429. lst.extend(['..'] * up)
  430. lst.reverse()
  431. return os.sep.join(lst) or '.'
  432. else:
  433. return self.abspath()
  434. def abspath(self):
  435. """
  436. Returns the absolute path. A cache is kept in the context as ``cache_node_abspath``
  437. :rtype: string
  438. """
  439. try:
  440. return self.cache_abspath
  441. except AttributeError:
  442. pass
  443. # think twice before touching this (performance + complexity + correctness)
  444. if not self.parent:
  445. val = os.sep
  446. elif not self.parent.name:
  447. val = os.sep + self.name
  448. else:
  449. val = self.parent.abspath() + os.sep + self.name
  450. self.cache_abspath = val
  451. return val
  452. if Utils.is_win32:
  453. def abspath(self):
  454. try:
  455. return self.cache_abspath
  456. except AttributeError:
  457. pass
  458. if not self.parent:
  459. val = ''
  460. elif not self.parent.name:
  461. val = self.name + os.sep
  462. else:
  463. val = self.parent.abspath().rstrip(os.sep) + os.sep + self.name
  464. self.cache_abspath = val
  465. return val
  466. def relpath(self):
  467. """
  468. Returns the relative path. This is used in place of abspath() to keep paths short
  469. for environments like cygwin where path lengths to file operations are severely limited
  470. (for example, when cross-compiling for arm-none-eabi on cygwin)
  471. :rtype: string
  472. """
  473. return os.path.relpath(self.abspath())
  474. def is_child_of(self, node):
  475. """
  476. Returns whether the object belongs to a subtree of the input node::
  477. def build(bld):
  478. node = bld.path.find_node('wscript')
  479. node.is_child_of(bld.path) # True
  480. :param node: path to use as a reference
  481. :type node: :py:class:`waflib.Node.Node`
  482. :rtype: bool
  483. """
  484. p = self
  485. diff = self.height() - node.height()
  486. while diff > 0:
  487. diff -= 1
  488. p = p.parent
  489. return p is node
  490. def ant_iter(self, accept=None, maxdepth=25, pats=[], dir=False, src=True, remove=True, quiet=False):
  491. """
  492. Recursive method used by :py:meth:`waflib.Node.ant_glob`.
  493. :param accept: function used for accepting/rejecting a node, returns the patterns that can be still accepted in recursion
  494. :type accept: function
  495. :param maxdepth: maximum depth in the filesystem (25)
  496. :type maxdepth: int
  497. :param pats: list of patterns to accept and list of patterns to exclude
  498. :type pats: tuple
  499. :param dir: return folders too (False by default)
  500. :type dir: bool
  501. :param src: return files (True by default)
  502. :type src: bool
  503. :param remove: remove files/folders that do not exist (True by default)
  504. :type remove: bool
  505. :param quiet: disable build directory traversal warnings (verbose mode)
  506. :type quiet: bool
  507. :returns: A generator object to iterate from
  508. :rtype: iterator
  509. """
  510. dircont = self.listdir()
  511. dircont.sort()
  512. try:
  513. lst = set(self.children.keys())
  514. except AttributeError:
  515. self.children = self.dict_class()
  516. else:
  517. if remove:
  518. for x in lst - set(dircont):
  519. self.children[x].evict()
  520. for name in dircont:
  521. npats = accept(name, pats)
  522. if npats and npats[0]:
  523. accepted = [] in npats[0]
  524. node = self.make_node([name])
  525. isdir = node.isdir()
  526. if accepted:
  527. if isdir:
  528. if dir:
  529. yield node
  530. elif src:
  531. yield node
  532. if isdir:
  533. node.cache_isdir = True
  534. if maxdepth:
  535. for k in node.ant_iter(accept=accept, maxdepth=maxdepth - 1, pats=npats, dir=dir, src=src, remove=remove, quiet=quiet):
  536. yield k
  537. def ant_glob(self, *k, **kw):
  538. """
  539. Finds files across folders and returns Node objects:
  540. * ``**/*`` find all files recursively
  541. * ``**/*.class`` find all files ending by .class
  542. * ``..`` find files having two dot characters
  543. For example::
  544. def configure(cfg):
  545. # find all .cpp files
  546. cfg.path.ant_glob('**/*.cpp')
  547. # find particular files from the root filesystem (can be slow)
  548. cfg.root.ant_glob('etc/*.txt')
  549. # simple exclusion rule example
  550. cfg.path.ant_glob('*.c*', excl=['*.c'], src=True, dir=False)
  551. For more information about the patterns, consult http://ant.apache.org/manual/dirtasks.html
  552. Please remember that the '..' sequence does not represent the parent directory::
  553. def configure(cfg):
  554. cfg.path.ant_glob('../*.h') # incorrect
  555. cfg.path.parent.ant_glob('*.h') # correct
  556. The Node structure is itself a filesystem cache, so certain precautions must
  557. be taken while matching files in the build or installation phases.
  558. Nodes objects that do have a corresponding file or folder are garbage-collected by default.
  559. This garbage collection is usually required to prevent returning files that do not
  560. exist anymore. Yet, this may also remove Node objects of files that are yet-to-be built.
  561. This typically happens when trying to match files in the build directory,
  562. but there are also cases when files are created in the source directory.
  563. Run ``waf -v`` to display any warnings, and try consider passing ``remove=False``
  564. when matching files in the build directory.
  565. Since ant_glob can traverse both source and build folders, it is a best practice
  566. to call this method only from the most specific build node::
  567. def build(bld):
  568. # traverses the build directory, may need ``remove=False``:
  569. bld.path.ant_glob('project/dir/**/*.h')
  570. # better, no accidental build directory traversal:
  571. bld.path.find_node('project/dir').ant_glob('**/*.h') # best
  572. In addition, files and folders are listed immediately. When matching files in the
  573. build folders, consider passing ``generator=True`` so that the generator object
  574. returned can defer computation to a later stage. For example::
  575. def build(bld):
  576. bld(rule='tar xvf ${SRC}', source='arch.tar')
  577. bld.add_group()
  578. gen = bld.bldnode.ant_glob("*.h", generator=True, remove=True)
  579. # files will be listed only after the arch.tar is unpacked
  580. bld(rule='ls ${SRC}', source=gen, name='XYZ')
  581. :param incl: ant patterns or list of patterns to include
  582. :type incl: string or list of strings
  583. :param excl: ant patterns or list of patterns to exclude
  584. :type excl: string or list of strings
  585. :param dir: return folders too (False by default)
  586. :type dir: bool
  587. :param src: return files (True by default)
  588. :type src: bool
  589. :param maxdepth: maximum depth of recursion
  590. :type maxdepth: int
  591. :param ignorecase: ignore case while matching (False by default)
  592. :type ignorecase: bool
  593. :param generator: Whether to evaluate the Nodes lazily
  594. :type generator: bool
  595. :param remove: remove files/folders that do not exist (True by default)
  596. :type remove: bool
  597. :param quiet: disable build directory traversal warnings (verbose mode)
  598. :type quiet: bool
  599. :returns: The corresponding Node objects as a list or as a generator object (generator=True)
  600. :rtype: by default, list of :py:class:`waflib.Node.Node` instances
  601. """
  602. src = kw.get('src', True)
  603. dir = kw.get('dir')
  604. excl = kw.get('excl', exclude_regs)
  605. incl = k and k[0] or kw.get('incl', '**')
  606. remove = kw.get('remove', True)
  607. maxdepth = kw.get('maxdepth', 25)
  608. ignorecase = kw.get('ignorecase', False)
  609. quiet = kw.get('quiet', False)
  610. pats = (ant_matcher(incl, ignorecase), ant_matcher(excl, ignorecase))
  611. if kw.get('generator'):
  612. return Utils.lazy_generator(self.ant_iter, (ant_sub_matcher, maxdepth, pats, dir, src, remove, quiet))
  613. it = self.ant_iter(ant_sub_matcher, maxdepth, pats, dir, src, remove, quiet)
  614. if kw.get('flat'):
  615. # returns relative paths as a space-delimited string
  616. # prefer Node objects whenever possible
  617. return ' '.join(x.path_from(self) for x in it)
  618. return list(it)
  619. # ----------------------------------------------------------------------------
  620. # the methods below require the source/build folders (bld.srcnode/bld.bldnode)
  621. def is_src(self):
  622. """
  623. Returns True if the node is below the source directory. Note that ``!is_src() ≠ is_bld()``
  624. :rtype: bool
  625. """
  626. cur = self
  627. x = self.ctx.srcnode
  628. y = self.ctx.bldnode
  629. while cur.parent:
  630. if cur is y:
  631. return False
  632. if cur is x:
  633. return True
  634. cur = cur.parent
  635. return False
  636. def is_bld(self):
  637. """
  638. Returns True if the node is below the build directory. Note that ``!is_bld() ≠ is_src()``
  639. :rtype: bool
  640. """
  641. cur = self
  642. y = self.ctx.bldnode
  643. while cur.parent:
  644. if cur is y:
  645. return True
  646. cur = cur.parent
  647. return False
  648. def get_src(self):
  649. """
  650. Returns the corresponding Node object in the source directory (or self if already
  651. under the source directory). Use this method only if the purpose is to create
  652. a Node object (this is common with folders but not with files, see ticket 1937)
  653. :rtype: :py:class:`waflib.Node.Node`
  654. """
  655. cur = self
  656. x = self.ctx.srcnode
  657. y = self.ctx.bldnode
  658. lst = []
  659. while cur.parent:
  660. if cur is y:
  661. lst.reverse()
  662. return x.make_node(lst)
  663. if cur is x:
  664. return self
  665. lst.append(cur.name)
  666. cur = cur.parent
  667. return self
  668. def get_bld(self):
  669. """
  670. Return the corresponding Node object in the build directory (or self if already
  671. under the build directory). Use this method only if the purpose is to create
  672. a Node object (this is common with folders but not with files, see ticket 1937)
  673. :rtype: :py:class:`waflib.Node.Node`
  674. """
  675. cur = self
  676. x = self.ctx.srcnode
  677. y = self.ctx.bldnode
  678. lst = []
  679. while cur.parent:
  680. if cur is y:
  681. return self
  682. if cur is x:
  683. lst.reverse()
  684. return self.ctx.bldnode.make_node(lst)
  685. lst.append(cur.name)
  686. cur = cur.parent
  687. # the file is external to the current project, make a fake root in the current build directory
  688. lst.reverse()
  689. if lst and Utils.is_win32 and len(lst[0]) == 2 and lst[0].endswith(':'):
  690. lst[0] = lst[0][0]
  691. return self.ctx.bldnode.make_node(['__root__'] + lst)
  692. def find_resource(self, lst):
  693. """
  694. Use this method in the build phase to find source files corresponding to the relative path given.
  695. First it looks up the Node data structure to find any declared Node object in the build directory.
  696. If None is found, it then considers the filesystem in the source directory.
  697. :param lst: relative path
  698. :type lst: string or list of string
  699. :returns: the corresponding Node object or None
  700. :rtype: :py:class:`waflib.Node.Node`
  701. """
  702. if isinstance(lst, str):
  703. lst = [x for x in Utils.split_path(lst) if x and x != '.']
  704. node = self.get_bld().search_node(lst)
  705. if not node:
  706. node = self.get_src().find_node(lst)
  707. if node and node.isdir():
  708. return None
  709. return node
  710. def find_or_declare(self, lst):
  711. """
  712. Use this method in the build phase to declare output files which
  713. are meant to be written in the build directory.
  714. This method creates the Node object and its parent folder
  715. as needed.
  716. :param lst: relative path
  717. :type lst: string or list of string
  718. """
  719. if isinstance(lst, str) and os.path.isabs(lst):
  720. node = self.ctx.root.make_node(lst)
  721. else:
  722. node = self.get_bld().make_node(lst)
  723. node.parent.mkdir()
  724. return node
  725. def find_dir(self, lst):
  726. """
  727. Searches for a folder on the filesystem (see :py:meth:`waflib.Node.Node.find_node`)
  728. :param lst: relative path
  729. :type lst: string or list of string
  730. :returns: The corresponding Node object or None if there is no such folder
  731. :rtype: :py:class:`waflib.Node.Node`
  732. """
  733. if isinstance(lst, str):
  734. lst = [x for x in Utils.split_path(lst) if x and x != '.']
  735. node = self.find_node(lst)
  736. if node and not node.isdir():
  737. return None
  738. return node
  739. # helpers for building things
  740. def change_ext(self, ext, ext_in=None):
  741. """
  742. Declares a build node with a distinct extension; this is uses :py:meth:`waflib.Node.Node.find_or_declare`
  743. :return: A build node of the same path, but with a different extension
  744. :rtype: :py:class:`waflib.Node.Node`
  745. """
  746. name = self.name
  747. if ext_in is None:
  748. k = name.rfind('.')
  749. if k >= 0:
  750. name = name[:k] + ext
  751. else:
  752. name = name + ext
  753. else:
  754. name = name[:- len(ext_in)] + ext
  755. return self.parent.find_or_declare([name])
  756. def bldpath(self):
  757. """
  758. Returns the relative path seen from the build directory ``src/foo.cpp``
  759. :rtype: string
  760. """
  761. return self.path_from(self.ctx.bldnode)
  762. def srcpath(self):
  763. """
  764. Returns the relative path seen from the source directory ``../src/foo.cpp``
  765. :rtype: string
  766. """
  767. return self.path_from(self.ctx.srcnode)
  768. def relpath(self):
  769. """
  770. If a file in the build directory, returns :py:meth:`waflib.Node.Node.bldpath`,
  771. else returns :py:meth:`waflib.Node.Node.srcpath`
  772. :rtype: string
  773. """
  774. cur = self
  775. x = self.ctx.bldnode
  776. while cur.parent:
  777. if cur is x:
  778. return self.bldpath()
  779. cur = cur.parent
  780. return self.srcpath()
  781. def bld_dir(self):
  782. """
  783. Equivalent to self.parent.bldpath()
  784. :rtype: string
  785. """
  786. return self.parent.bldpath()
  787. def h_file(self):
  788. """
  789. See :py:func:`waflib.Utils.h_file`
  790. :return: a hash representing the file contents
  791. :rtype: string or bytes
  792. """
  793. return Utils.h_file(self.abspath())
  794. def get_bld_sig(self):
  795. """
  796. Returns a signature (see :py:meth:`waflib.Node.Node.h_file`) for the purpose
  797. of build dependency calculation. This method uses a per-context cache.
  798. :return: a hash representing the object contents
  799. :rtype: string or bytes
  800. """
  801. # previous behaviour can be set by returning self.ctx.node_sigs[self] when a build node
  802. try:
  803. cache = self.ctx.cache_sig
  804. except AttributeError:
  805. cache = self.ctx.cache_sig = {}
  806. try:
  807. ret = cache[self]
  808. except KeyError:
  809. p = self.abspath()
  810. try:
  811. ret = cache[self] = self.h_file()
  812. except EnvironmentError:
  813. if self.isdir():
  814. # allow folders as build nodes, do not use the creation time
  815. st = os.stat(p)
  816. ret = cache[self] = Utils.h_list([p, st.st_ino, st.st_mode])
  817. return ret
  818. raise
  819. return ret
  820. pickle_lock = Utils.threading.Lock()
  821. """Lock mandatory for thread-safe node serialization"""
  822. class Nod3(Node):
  823. """Mandatory subclass for thread-safe node serialization"""
  824. pass # do not remove