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

916 lines
34 KiB

6 years ago
  1. """SCons.Subst
  2. SCons string substitution.
  3. """
  4. #
  5. # Copyright (c) 2001 - 2017 The SCons Foundation
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. __revision__ = "src/engine/SCons/Subst.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  26. import collections
  27. import re
  28. import SCons.Errors
  29. from SCons.Util import is_String, is_Sequence
  30. # Indexed by the SUBST_* constants below.
  31. _strconv = [SCons.Util.to_String_for_subst,
  32. SCons.Util.to_String_for_subst,
  33. SCons.Util.to_String_for_signature]
  34. AllowableExceptions = (IndexError, NameError)
  35. def SetAllowableExceptions(*excepts):
  36. global AllowableExceptions
  37. AllowableExceptions = [_f for _f in excepts if _f]
  38. def raise_exception(exception, target, s):
  39. name = exception.__class__.__name__
  40. msg = "%s `%s' trying to evaluate `%s'" % (name, exception, s)
  41. if target:
  42. raise SCons.Errors.BuildError(target[0], msg)
  43. else:
  44. raise SCons.Errors.UserError(msg)
  45. class Literal(object):
  46. """A wrapper for a string. If you use this object wrapped
  47. around a string, then it will be interpreted as literal.
  48. When passed to the command interpreter, all special
  49. characters will be escaped."""
  50. def __init__(self, lstr):
  51. self.lstr = lstr
  52. def __str__(self):
  53. return self.lstr
  54. def escape(self, escape_func):
  55. return escape_func(self.lstr)
  56. def for_signature(self):
  57. return self.lstr
  58. def is_literal(self):
  59. return 1
  60. def __eq__(self, other):
  61. if not isinstance(other, Literal):
  62. return False
  63. return self.lstr == other.lstr
  64. def __neq__(self, other):
  65. return not self.__eq__(other)
  66. class SpecialAttrWrapper(object):
  67. """This is a wrapper for what we call a 'Node special attribute.'
  68. This is any of the attributes of a Node that we can reference from
  69. Environment variable substitution, such as $TARGET.abspath or
  70. $SOURCES[1].filebase. We implement the same methods as Literal
  71. so we can handle special characters, plus a for_signature method,
  72. such that we can return some canonical string during signature
  73. calculation to avoid unnecessary rebuilds."""
  74. def __init__(self, lstr, for_signature=None):
  75. """The for_signature parameter, if supplied, will be the
  76. canonical string we return from for_signature(). Else
  77. we will simply return lstr."""
  78. self.lstr = lstr
  79. if for_signature:
  80. self.forsig = for_signature
  81. else:
  82. self.forsig = lstr
  83. def __str__(self):
  84. return self.lstr
  85. def escape(self, escape_func):
  86. return escape_func(self.lstr)
  87. def for_signature(self):
  88. return self.forsig
  89. def is_literal(self):
  90. return 1
  91. def quote_spaces(arg):
  92. """Generic function for putting double quotes around any string that
  93. has white space in it."""
  94. if ' ' in arg or '\t' in arg:
  95. return '"%s"' % arg
  96. else:
  97. return str(arg)
  98. class CmdStringHolder(collections.UserString):
  99. """This is a special class used to hold strings generated by
  100. scons_subst() and scons_subst_list(). It defines a special method
  101. escape(). When passed a function with an escape algorithm for a
  102. particular platform, it will return the contained string with the
  103. proper escape sequences inserted.
  104. """
  105. def __init__(self, cmd, literal=None):
  106. collections.UserString.__init__(self, cmd)
  107. self.literal = literal
  108. def is_literal(self):
  109. return self.literal
  110. def escape(self, escape_func, quote_func=quote_spaces):
  111. """Escape the string with the supplied function. The
  112. function is expected to take an arbitrary string, then
  113. return it with all special characters escaped and ready
  114. for passing to the command interpreter.
  115. After calling this function, the next call to str() will
  116. return the escaped string.
  117. """
  118. if self.is_literal():
  119. return escape_func(self.data)
  120. elif ' ' in self.data or '\t' in self.data:
  121. return quote_func(self.data)
  122. else:
  123. return self.data
  124. def escape_list(mylist, escape_func):
  125. """Escape a list of arguments by running the specified escape_func
  126. on every object in the list that has an escape() method."""
  127. def escape(obj, escape_func=escape_func):
  128. try:
  129. e = obj.escape
  130. except AttributeError:
  131. return obj
  132. else:
  133. return e(escape_func)
  134. return list(map(escape, mylist))
  135. class NLWrapper(object):
  136. """A wrapper class that delays turning a list of sources or targets
  137. into a NodeList until it's needed. The specified function supplied
  138. when the object is initialized is responsible for turning raw nodes
  139. into proxies that implement the special attributes like .abspath,
  140. .source, etc. This way, we avoid creating those proxies just
  141. "in case" someone is going to use $TARGET or the like, and only
  142. go through the trouble if we really have to.
  143. In practice, this might be a wash performance-wise, but it's a little
  144. cleaner conceptually...
  145. """
  146. def __init__(self, list, func):
  147. self.list = list
  148. self.func = func
  149. def _return_nodelist(self):
  150. return self.nodelist
  151. def _gen_nodelist(self):
  152. mylist = self.list
  153. if mylist is None:
  154. mylist = []
  155. elif not is_Sequence(mylist):
  156. mylist = [mylist]
  157. # The map(self.func) call is what actually turns
  158. # a list into appropriate proxies.
  159. self.nodelist = SCons.Util.NodeList(list(map(self.func, mylist)))
  160. self._create_nodelist = self._return_nodelist
  161. return self.nodelist
  162. _create_nodelist = _gen_nodelist
  163. class Targets_or_Sources(collections.UserList):
  164. """A class that implements $TARGETS or $SOURCES expansions by in turn
  165. wrapping a NLWrapper. This class handles the different methods used
  166. to access the list, calling the NLWrapper to create proxies on demand.
  167. Note that we subclass collections.UserList purely so that the
  168. is_Sequence() function will identify an object of this class as
  169. a list during variable expansion. We're not really using any
  170. collections.UserList methods in practice.
  171. """
  172. def __init__(self, nl):
  173. self.nl = nl
  174. def __getattr__(self, attr):
  175. nl = self.nl._create_nodelist()
  176. return getattr(nl, attr)
  177. def __getitem__(self, i):
  178. nl = self.nl._create_nodelist()
  179. return nl[i]
  180. def __getslice__(self, i, j):
  181. nl = self.nl._create_nodelist()
  182. i = max(i, 0); j = max(j, 0)
  183. return nl[i:j]
  184. def __str__(self):
  185. nl = self.nl._create_nodelist()
  186. return str(nl)
  187. def __repr__(self):
  188. nl = self.nl._create_nodelist()
  189. return repr(nl)
  190. class Target_or_Source(object):
  191. """A class that implements $TARGET or $SOURCE expansions by in turn
  192. wrapping a NLWrapper. This class handles the different methods used
  193. to access an individual proxy Node, calling the NLWrapper to create
  194. a proxy on demand.
  195. """
  196. def __init__(self, nl):
  197. self.nl = nl
  198. def __getattr__(self, attr):
  199. nl = self.nl._create_nodelist()
  200. try:
  201. nl0 = nl[0]
  202. except IndexError:
  203. # If there is nothing in the list, then we have no attributes to
  204. # pass through, so raise AttributeError for everything.
  205. raise AttributeError("NodeList has no attribute: %s" % attr)
  206. return getattr(nl0, attr)
  207. def __str__(self):
  208. nl = self.nl._create_nodelist()
  209. if nl:
  210. return str(nl[0])
  211. return ''
  212. def __repr__(self):
  213. nl = self.nl._create_nodelist()
  214. if nl:
  215. return repr(nl[0])
  216. return ''
  217. class NullNodeList(SCons.Util.NullSeq):
  218. def __call__(self, *args, **kwargs): return ''
  219. def __str__(self): return ''
  220. NullNodesList = NullNodeList()
  221. def subst_dict(target, source):
  222. """Create a dictionary for substitution of special
  223. construction variables.
  224. This translates the following special arguments:
  225. target - the target (object or array of objects),
  226. used to generate the TARGET and TARGETS
  227. construction variables
  228. source - the source (object or array of objects),
  229. used to generate the SOURCES and SOURCE
  230. construction variables
  231. """
  232. dict = {}
  233. if target:
  234. def get_tgt_subst_proxy(thing):
  235. try:
  236. subst_proxy = thing.get_subst_proxy()
  237. except AttributeError:
  238. subst_proxy = thing # probably a string, just return it
  239. return subst_proxy
  240. tnl = NLWrapper(target, get_tgt_subst_proxy)
  241. dict['TARGETS'] = Targets_or_Sources(tnl)
  242. dict['TARGET'] = Target_or_Source(tnl)
  243. # This is a total cheat, but hopefully this dictionary goes
  244. # away soon anyway. We just let these expand to $TARGETS
  245. # because that's "good enough" for the use of ToolSurrogates
  246. # (see test/ToolSurrogate.py) to generate documentation.
  247. dict['CHANGED_TARGETS'] = '$TARGETS'
  248. dict['UNCHANGED_TARGETS'] = '$TARGETS'
  249. else:
  250. dict['TARGETS'] = NullNodesList
  251. dict['TARGET'] = NullNodesList
  252. if source:
  253. def get_src_subst_proxy(node):
  254. try:
  255. rfile = node.rfile
  256. except AttributeError:
  257. pass
  258. else:
  259. node = rfile()
  260. try:
  261. return node.get_subst_proxy()
  262. except AttributeError:
  263. return node # probably a String, just return it
  264. snl = NLWrapper(source, get_src_subst_proxy)
  265. dict['SOURCES'] = Targets_or_Sources(snl)
  266. dict['SOURCE'] = Target_or_Source(snl)
  267. # This is a total cheat, but hopefully this dictionary goes
  268. # away soon anyway. We just let these expand to $TARGETS
  269. # because that's "good enough" for the use of ToolSurrogates
  270. # (see test/ToolSurrogate.py) to generate documentation.
  271. dict['CHANGED_SOURCES'] = '$SOURCES'
  272. dict['UNCHANGED_SOURCES'] = '$SOURCES'
  273. else:
  274. dict['SOURCES'] = NullNodesList
  275. dict['SOURCE'] = NullNodesList
  276. return dict
  277. # Constants for the "mode" parameter to scons_subst_list() and
  278. # scons_subst(). SUBST_RAW gives the raw command line. SUBST_CMD
  279. # gives a command line suitable for passing to a shell. SUBST_SIG
  280. # gives a command line appropriate for calculating the signature
  281. # of a command line...if this changes, we should rebuild.
  282. SUBST_CMD = 0
  283. SUBST_RAW = 1
  284. SUBST_SIG = 2
  285. _rm = re.compile(r'\$[()]')
  286. _rm_split = re.compile(r'(\$[()])')
  287. # Indexed by the SUBST_* constants above.
  288. _regex_remove = [ _rm, None, _rm_split ]
  289. def _rm_list(list):
  290. return [l for l in list if not l in ('$(', '$)')]
  291. def _remove_list(list):
  292. result = []
  293. depth = 0
  294. for l in list:
  295. if l == '$(':
  296. depth += 1
  297. elif l == '$)':
  298. depth -= 1
  299. if depth < 0:
  300. break
  301. elif depth == 0:
  302. result.append(l)
  303. if depth != 0:
  304. return None
  305. return result
  306. # Indexed by the SUBST_* constants above.
  307. _list_remove = [ _rm_list, None, _remove_list ]
  308. # Regular expressions for splitting strings and handling substitutions,
  309. # for use by the scons_subst() and scons_subst_list() functions:
  310. #
  311. # The first expression compiled matches all of the $-introduced tokens
  312. # that we need to process in some way, and is used for substitutions.
  313. # The expressions it matches are:
  314. #
  315. # "$$"
  316. # "$("
  317. # "$)"
  318. # "$variable" [must begin with alphabetic or underscore]
  319. # "${any stuff}"
  320. #
  321. # The second expression compiled is used for splitting strings into tokens
  322. # to be processed, and it matches all of the tokens listed above, plus
  323. # the following that affect how arguments do or don't get joined together:
  324. #
  325. # " " [white space]
  326. # "non-white-space" [without any dollar signs]
  327. # "$" [single dollar sign]
  328. #
  329. _dollar_exps_str = r'\$[\$\(\)]|\$[_a-zA-Z][\.\w]*|\${[^}]*}'
  330. _dollar_exps = re.compile(r'(%s)' % _dollar_exps_str)
  331. _separate_args = re.compile(r'(%s|\s+|[^\s\$]+|\$)' % _dollar_exps_str)
  332. # This regular expression is used to replace strings of multiple white
  333. # space characters in the string result from the scons_subst() function.
  334. _space_sep = re.compile(r'[\t ]+(?![^{]*})')
  335. def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={}, lvars={}, conv=None):
  336. """Expand a string or list containing construction variable
  337. substitutions.
  338. This is the work-horse function for substitutions in file names
  339. and the like. The companion scons_subst_list() function (below)
  340. handles separating command lines into lists of arguments, so see
  341. that function if that's what you're looking for.
  342. """
  343. if isinstance(strSubst, str) and strSubst.find('$') < 0:
  344. return strSubst
  345. class StringSubber(object):
  346. """A class to construct the results of a scons_subst() call.
  347. This binds a specific construction environment, mode, target and
  348. source with two methods (substitute() and expand()) that handle
  349. the expansion.
  350. """
  351. def __init__(self, env, mode, conv, gvars):
  352. self.env = env
  353. self.mode = mode
  354. self.conv = conv
  355. self.gvars = gvars
  356. def expand(self, s, lvars):
  357. """Expand a single "token" as necessary, returning an
  358. appropriate string containing the expansion.
  359. This handles expanding different types of things (strings,
  360. lists, callables) appropriately. It calls the wrapper
  361. substitute() method to re-expand things as necessary, so that
  362. the results of expansions of side-by-side strings still get
  363. re-evaluated separately, not smushed together.
  364. """
  365. if is_String(s):
  366. try:
  367. s0, s1 = s[:2]
  368. except (IndexError, ValueError):
  369. return s
  370. if s0 != '$':
  371. return s
  372. if s1 == '$':
  373. return '$'
  374. elif s1 in '()':
  375. return s
  376. else:
  377. key = s[1:]
  378. if key[0] == '{' or '.' in key:
  379. if key[0] == '{':
  380. key = key[1:-1]
  381. try:
  382. s = eval(key, self.gvars, lvars)
  383. except KeyboardInterrupt:
  384. raise
  385. except Exception as e:
  386. if e.__class__ in AllowableExceptions:
  387. return ''
  388. raise_exception(e, lvars['TARGETS'], s)
  389. else:
  390. if key in lvars:
  391. s = lvars[key]
  392. elif key in self.gvars:
  393. s = self.gvars[key]
  394. elif not NameError in AllowableExceptions:
  395. raise_exception(NameError(key), lvars['TARGETS'], s)
  396. else:
  397. return ''
  398. # Before re-expanding the result, handle
  399. # recursive expansion by copying the local
  400. # variable dictionary and overwriting a null
  401. # string for the value of the variable name
  402. # we just expanded.
  403. #
  404. # This could potentially be optimized by only
  405. # copying lvars when s contains more expansions,
  406. # but lvars is usually supposed to be pretty
  407. # small, and deeply nested variable expansions
  408. # are probably more the exception than the norm,
  409. # so it should be tolerable for now.
  410. lv = lvars.copy()
  411. var = key.split('.')[0]
  412. lv[var] = ''
  413. return self.substitute(s, lv)
  414. elif is_Sequence(s):
  415. def func(l, conv=self.conv, substitute=self.substitute, lvars=lvars):
  416. return conv(substitute(l, lvars))
  417. return list(map(func, s))
  418. elif callable(s):
  419. try:
  420. s = s(target=lvars['TARGETS'],
  421. source=lvars['SOURCES'],
  422. env=self.env,
  423. for_signature=(self.mode != SUBST_CMD))
  424. except TypeError:
  425. # This probably indicates that it's a callable
  426. # object that doesn't match our calling arguments
  427. # (like an Action).
  428. if self.mode == SUBST_RAW:
  429. return s
  430. s = self.conv(s)
  431. return self.substitute(s, lvars)
  432. elif s is None:
  433. return ''
  434. else:
  435. return s
  436. def substitute(self, args, lvars):
  437. """Substitute expansions in an argument or list of arguments.
  438. This serves as a wrapper for splitting up a string into
  439. separate tokens.
  440. """
  441. if is_String(args) and not isinstance(args, CmdStringHolder):
  442. args = str(args) # In case it's a UserString.
  443. try:
  444. def sub_match(match):
  445. return self.conv(self.expand(match.group(1), lvars))
  446. result = _dollar_exps.sub(sub_match, args)
  447. except TypeError:
  448. # If the internal conversion routine doesn't return
  449. # strings (it could be overridden to return Nodes, for
  450. # example), then the 1.5.2 re module will throw this
  451. # exception. Back off to a slower, general-purpose
  452. # algorithm that works for all data types.
  453. args = _separate_args.findall(args)
  454. result = []
  455. for a in args:
  456. result.append(self.conv(self.expand(a, lvars)))
  457. if len(result) == 1:
  458. result = result[0]
  459. else:
  460. result = ''.join(map(str, result))
  461. return result
  462. else:
  463. return self.expand(args, lvars)
  464. if conv is None:
  465. conv = _strconv[mode]
  466. # Doing this every time is a bit of a waste, since the Executor
  467. # has typically already populated the OverrideEnvironment with
  468. # $TARGET/$SOURCE variables. We're keeping this (for now), though,
  469. # because it supports existing behavior that allows us to call
  470. # an Action directly with an arbitrary target+source pair, which
  471. # we use in Tool/tex.py to handle calling $BIBTEX when necessary.
  472. # If we dropped that behavior (or found another way to cover it),
  473. # we could get rid of this call completely and just rely on the
  474. # Executor setting the variables.
  475. if 'TARGET' not in lvars:
  476. d = subst_dict(target, source)
  477. if d:
  478. lvars = lvars.copy()
  479. lvars.update(d)
  480. # We're (most likely) going to eval() things. If Python doesn't
  481. # find a __builtins__ value in the global dictionary used for eval(),
  482. # it copies the current global values for you. Avoid this by
  483. # setting it explicitly and then deleting, so we don't pollute the
  484. # construction environment Dictionary(ies) that are typically used
  485. # for expansion.
  486. gvars['__builtins__'] = __builtins__
  487. ss = StringSubber(env, mode, conv, gvars)
  488. result = ss.substitute(strSubst, lvars)
  489. try:
  490. del gvars['__builtins__']
  491. except KeyError:
  492. pass
  493. res = result
  494. if is_String(result):
  495. # Remove $(-$) pairs and any stuff in between,
  496. # if that's appropriate.
  497. remove = _regex_remove[mode]
  498. if remove:
  499. if mode == SUBST_SIG:
  500. result = _list_remove[mode](remove.split(result))
  501. if result is None:
  502. raise SCons.Errors.UserError("Unbalanced $(/$) in: " + res)
  503. result = ' '.join(result)
  504. else:
  505. result = remove.sub('', result)
  506. if mode != SUBST_RAW:
  507. # Compress strings of white space characters into
  508. # a single space.
  509. result = _space_sep.sub(' ', result).strip()
  510. elif is_Sequence(result):
  511. remove = _list_remove[mode]
  512. if remove:
  513. result = remove(result)
  514. if result is None:
  515. raise SCons.Errors.UserError("Unbalanced $(/$) in: " + str(res))
  516. return result
  517. def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={}, lvars={}, conv=None):
  518. """Substitute construction variables in a string (or list or other
  519. object) and separate the arguments into a command list.
  520. The companion scons_subst() function (above) handles basic
  521. substitutions within strings, so see that function instead
  522. if that's what you're looking for.
  523. """
  524. class ListSubber(collections.UserList):
  525. """A class to construct the results of a scons_subst_list() call.
  526. Like StringSubber, this class binds a specific construction
  527. environment, mode, target and source with two methods
  528. (substitute() and expand()) that handle the expansion.
  529. In addition, however, this class is used to track the state of
  530. the result(s) we're gathering so we can do the appropriate thing
  531. whenever we have to append another word to the result--start a new
  532. line, start a new word, append to the current word, etc. We do
  533. this by setting the "append" attribute to the right method so
  534. that our wrapper methods only need ever call ListSubber.append(),
  535. and the rest of the object takes care of doing the right thing
  536. internally.
  537. """
  538. def __init__(self, env, mode, conv, gvars):
  539. collections.UserList.__init__(self, [])
  540. self.env = env
  541. self.mode = mode
  542. self.conv = conv
  543. self.gvars = gvars
  544. if self.mode == SUBST_RAW:
  545. self.add_strip = lambda x: self.append(x)
  546. else:
  547. self.add_strip = lambda x: None
  548. self.in_strip = None
  549. self.next_line()
  550. def expand(self, s, lvars, within_list):
  551. """Expand a single "token" as necessary, appending the
  552. expansion to the current result.
  553. This handles expanding different types of things (strings,
  554. lists, callables) appropriately. It calls the wrapper
  555. substitute() method to re-expand things as necessary, so that
  556. the results of expansions of side-by-side strings still get
  557. re-evaluated separately, not smushed together.
  558. """
  559. if is_String(s):
  560. try:
  561. s0, s1 = s[:2]
  562. except (IndexError, ValueError):
  563. self.append(s)
  564. return
  565. if s0 != '$':
  566. self.append(s)
  567. return
  568. if s1 == '$':
  569. self.append('$')
  570. elif s1 == '(':
  571. self.open_strip('$(')
  572. elif s1 == ')':
  573. self.close_strip('$)')
  574. else:
  575. key = s[1:]
  576. if key[0] == '{' or key.find('.') >= 0:
  577. if key[0] == '{':
  578. key = key[1:-1]
  579. try:
  580. s = eval(key, self.gvars, lvars)
  581. except KeyboardInterrupt:
  582. raise
  583. except Exception as e:
  584. if e.__class__ in AllowableExceptions:
  585. return
  586. raise_exception(e, lvars['TARGETS'], s)
  587. else:
  588. if key in lvars:
  589. s = lvars[key]
  590. elif key in self.gvars:
  591. s = self.gvars[key]
  592. elif not NameError in AllowableExceptions:
  593. raise_exception(NameError(), lvars['TARGETS'], s)
  594. else:
  595. return
  596. # Before re-expanding the result, handle
  597. # recursive expansion by copying the local
  598. # variable dictionary and overwriting a null
  599. # string for the value of the variable name
  600. # we just expanded.
  601. lv = lvars.copy()
  602. var = key.split('.')[0]
  603. lv[var] = ''
  604. self.substitute(s, lv, 0)
  605. self.this_word()
  606. elif is_Sequence(s):
  607. for a in s:
  608. self.substitute(a, lvars, 1)
  609. self.next_word()
  610. elif callable(s):
  611. try:
  612. s = s(target=lvars['TARGETS'],
  613. source=lvars['SOURCES'],
  614. env=self.env,
  615. for_signature=(self.mode != SUBST_CMD))
  616. except TypeError:
  617. # This probably indicates that it's a callable
  618. # object that doesn't match our calling arguments
  619. # (like an Action).
  620. if self.mode == SUBST_RAW:
  621. self.append(s)
  622. return
  623. s = self.conv(s)
  624. self.substitute(s, lvars, within_list)
  625. elif s is None:
  626. self.this_word()
  627. else:
  628. self.append(s)
  629. def substitute(self, args, lvars, within_list):
  630. """Substitute expansions in an argument or list of arguments.
  631. This serves as a wrapper for splitting up a string into
  632. separate tokens.
  633. """
  634. if is_String(args) and not isinstance(args, CmdStringHolder):
  635. args = str(args) # In case it's a UserString.
  636. args = _separate_args.findall(args)
  637. for a in args:
  638. if a[0] in ' \t\n\r\f\v':
  639. if '\n' in a:
  640. self.next_line()
  641. elif within_list:
  642. self.append(a)
  643. else:
  644. self.next_word()
  645. else:
  646. self.expand(a, lvars, within_list)
  647. else:
  648. self.expand(args, lvars, within_list)
  649. def next_line(self):
  650. """Arrange for the next word to start a new line. This
  651. is like starting a new word, except that we have to append
  652. another line to the result."""
  653. collections.UserList.append(self, [])
  654. self.next_word()
  655. def this_word(self):
  656. """Arrange for the next word to append to the end of the
  657. current last word in the result."""
  658. self.append = self.add_to_current_word
  659. def next_word(self):
  660. """Arrange for the next word to start a new word."""
  661. self.append = self.add_new_word
  662. def add_to_current_word(self, x):
  663. """Append the string x to the end of the current last word
  664. in the result. If that is not possible, then just add
  665. it as a new word. Make sure the entire concatenated string
  666. inherits the object attributes of x (in particular, the
  667. escape function) by wrapping it as CmdStringHolder."""
  668. if not self.in_strip or self.mode != SUBST_SIG:
  669. try:
  670. current_word = self[-1][-1]
  671. except IndexError:
  672. self.add_new_word(x)
  673. else:
  674. # All right, this is a hack and it should probably
  675. # be refactored out of existence in the future.
  676. # The issue is that we want to smoosh words together
  677. # and make one file name that gets escaped if
  678. # we're expanding something like foo$EXTENSION,
  679. # but we don't want to smoosh them together if
  680. # it's something like >$TARGET, because then we'll
  681. # treat the '>' like it's part of the file name.
  682. # So for now, just hard-code looking for the special
  683. # command-line redirection characters...
  684. try:
  685. last_char = str(current_word)[-1]
  686. except IndexError:
  687. last_char = '\0'
  688. if last_char in '<>|':
  689. self.add_new_word(x)
  690. else:
  691. y = current_word + x
  692. # We used to treat a word appended to a literal
  693. # as a literal itself, but this caused problems
  694. # with interpreting quotes around space-separated
  695. # targets on command lines. Removing this makes
  696. # none of the "substantive" end-to-end tests fail,
  697. # so we'll take this out but leave it commented
  698. # for now in case there's a problem not covered
  699. # by the test cases and we need to resurrect this.
  700. #literal1 = self.literal(self[-1][-1])
  701. #literal2 = self.literal(x)
  702. y = self.conv(y)
  703. if is_String(y):
  704. #y = CmdStringHolder(y, literal1 or literal2)
  705. y = CmdStringHolder(y, None)
  706. self[-1][-1] = y
  707. def add_new_word(self, x):
  708. if not self.in_strip or self.mode != SUBST_SIG:
  709. literal = self.literal(x)
  710. x = self.conv(x)
  711. if is_String(x):
  712. x = CmdStringHolder(x, literal)
  713. self[-1].append(x)
  714. self.append = self.add_to_current_word
  715. def literal(self, x):
  716. try:
  717. l = x.is_literal
  718. except AttributeError:
  719. return None
  720. else:
  721. return l()
  722. def open_strip(self, x):
  723. """Handle the "open strip" $( token."""
  724. self.add_strip(x)
  725. self.in_strip = 1
  726. def close_strip(self, x):
  727. """Handle the "close strip" $) token."""
  728. self.add_strip(x)
  729. self.in_strip = None
  730. if conv is None:
  731. conv = _strconv[mode]
  732. # Doing this every time is a bit of a waste, since the Executor
  733. # has typically already populated the OverrideEnvironment with
  734. # $TARGET/$SOURCE variables. We're keeping this (for now), though,
  735. # because it supports existing behavior that allows us to call
  736. # an Action directly with an arbitrary target+source pair, which
  737. # we use in Tool/tex.py to handle calling $BIBTEX when necessary.
  738. # If we dropped that behavior (or found another way to cover it),
  739. # we could get rid of this call completely and just rely on the
  740. # Executor setting the variables.
  741. if 'TARGET' not in lvars:
  742. d = subst_dict(target, source)
  743. if d:
  744. lvars = lvars.copy()
  745. lvars.update(d)
  746. # We're (most likely) going to eval() things. If Python doesn't
  747. # find a __builtins__ value in the global dictionary used for eval(),
  748. # it copies the current global values for you. Avoid this by
  749. # setting it explicitly and then deleting, so we don't pollute the
  750. # construction environment Dictionary(ies) that are typically used
  751. # for expansion.
  752. gvars['__builtins__'] = __builtins__
  753. ls = ListSubber(env, mode, conv, gvars)
  754. ls.substitute(strSubst, lvars, 0)
  755. try:
  756. del gvars['__builtins__']
  757. except KeyError:
  758. pass
  759. return ls.data
  760. def scons_subst_once(strSubst, env, key):
  761. """Perform single (non-recursive) substitution of a single
  762. construction variable keyword.
  763. This is used when setting a variable when copying or overriding values
  764. in an Environment. We want to capture (expand) the old value before
  765. we override it, so people can do things like:
  766. env2 = env.Clone(CCFLAGS = '$CCFLAGS -g')
  767. We do this with some straightforward, brute-force code here...
  768. """
  769. if isinstance(strSubst, str) and strSubst.find('$') < 0:
  770. return strSubst
  771. matchlist = ['$' + key, '${' + key + '}']
  772. val = env.get(key, '')
  773. def sub_match(match, val=val, matchlist=matchlist):
  774. a = match.group(1)
  775. if a in matchlist:
  776. a = val
  777. if is_Sequence(a):
  778. return ' '.join(map(str, a))
  779. else:
  780. return str(a)
  781. if is_Sequence(strSubst):
  782. result = []
  783. for arg in strSubst:
  784. if is_String(arg):
  785. if arg in matchlist:
  786. arg = val
  787. if is_Sequence(arg):
  788. result.extend(arg)
  789. else:
  790. result.append(arg)
  791. else:
  792. result.append(_dollar_exps.sub(sub_match, arg))
  793. else:
  794. result.append(arg)
  795. return result
  796. elif is_String(strSubst):
  797. return _dollar_exps.sub(sub_match, strSubst)
  798. else:
  799. return strSubst
  800. # Local Variables:
  801. # tab-width:4
  802. # indent-tabs-mode:nil
  803. # End:
  804. # vim: set expandtab tabstop=4 shiftwidth=4: