PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/couchjs/scons/scons-local-2.0.1/SCons/Script/SConscript.py

http://github.com/cloudant/bigcouch
Python | 640 lines | 465 code | 42 blank | 133 comment | 64 complexity | d847664e95df8908de8bbeb10637bdc6 MD5 | raw file
Possible License(s): Apache-2.0
  1. """SCons.Script.SConscript
  2. This module defines the Python API provided to SConscript and SConstruct
  3. files.
  4. """
  5. #
  6. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included
  17. # in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  20. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  21. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. from __future__ import division
  27. __revision__ = "src/engine/SCons/Script/SConscript.py 5134 2010/08/16 23:02:40 bdeegan"
  28. import SCons
  29. import SCons.Action
  30. import SCons.Builder
  31. import SCons.Defaults
  32. import SCons.Environment
  33. import SCons.Errors
  34. import SCons.Node
  35. import SCons.Node.Alias
  36. import SCons.Node.FS
  37. import SCons.Platform
  38. import SCons.SConf
  39. import SCons.Script.Main
  40. import SCons.Tool
  41. import SCons.Util
  42. import collections
  43. import os
  44. import os.path
  45. import re
  46. import sys
  47. import traceback
  48. # The following variables used to live in this module. Some
  49. # SConscript files out there may have referred to them directly as
  50. # SCons.Script.SConscript.*. This is now supported by some special
  51. # handling towards the bottom of the SConscript.__init__.py module.
  52. #Arguments = {}
  53. #ArgList = []
  54. #BuildTargets = TargetList()
  55. #CommandLineTargets = []
  56. #DefaultTargets = []
  57. class SConscriptReturn(Exception):
  58. pass
  59. launch_dir = os.path.abspath(os.curdir)
  60. GlobalDict = None
  61. # global exports set by Export():
  62. global_exports = {}
  63. # chdir flag
  64. sconscript_chdir = 1
  65. def get_calling_namespaces():
  66. """Return the locals and globals for the function that called
  67. into this module in the current call stack."""
  68. try: 1//0
  69. except ZeroDivisionError:
  70. # Don't start iterating with the current stack-frame to
  71. # prevent creating reference cycles (f_back is safe).
  72. frame = sys.exc_info()[2].tb_frame.f_back
  73. # Find the first frame that *isn't* from this file. This means
  74. # that we expect all of the SCons frames that implement an Export()
  75. # or SConscript() call to be in this file, so that we can identify
  76. # the first non-Script.SConscript frame as the user's local calling
  77. # environment, and the locals and globals dictionaries from that
  78. # frame as the calling namespaces. See the comment below preceding
  79. # the DefaultEnvironmentCall block for even more explanation.
  80. while frame.f_globals.get("__name__") == __name__:
  81. frame = frame.f_back
  82. return frame.f_locals, frame.f_globals
  83. def compute_exports(exports):
  84. """Compute a dictionary of exports given one of the parameters
  85. to the Export() function or the exports argument to SConscript()."""
  86. loc, glob = get_calling_namespaces()
  87. retval = {}
  88. try:
  89. for export in exports:
  90. if SCons.Util.is_Dict(export):
  91. retval.update(export)
  92. else:
  93. try:
  94. retval[export] = loc[export]
  95. except KeyError:
  96. retval[export] = glob[export]
  97. except KeyError, x:
  98. raise SCons.Errors.UserError("Export of non-existent variable '%s'"%x)
  99. return retval
  100. class Frame(object):
  101. """A frame on the SConstruct/SConscript call stack"""
  102. def __init__(self, fs, exports, sconscript):
  103. self.globals = BuildDefaultGlobals()
  104. self.retval = None
  105. self.prev_dir = fs.getcwd()
  106. self.exports = compute_exports(exports) # exports from the calling SConscript
  107. # make sure the sconscript attr is a Node.
  108. if isinstance(sconscript, SCons.Node.Node):
  109. self.sconscript = sconscript
  110. elif sconscript == '-':
  111. self.sconscript = None
  112. else:
  113. self.sconscript = fs.File(str(sconscript))
  114. # the SConstruct/SConscript call stack:
  115. call_stack = []
  116. # For documentation on the methods in this file, see the scons man-page
  117. def Return(*vars, **kw):
  118. retval = []
  119. try:
  120. fvars = SCons.Util.flatten(vars)
  121. for var in fvars:
  122. for v in var.split():
  123. retval.append(call_stack[-1].globals[v])
  124. except KeyError, x:
  125. raise SCons.Errors.UserError("Return of non-existent variable '%s'"%x)
  126. if len(retval) == 1:
  127. call_stack[-1].retval = retval[0]
  128. else:
  129. call_stack[-1].retval = tuple(retval)
  130. stop = kw.get('stop', True)
  131. if stop:
  132. raise SConscriptReturn
  133. stack_bottom = '% Stack boTTom %' # hard to define a variable w/this name :)
  134. def _SConscript(fs, *files, **kw):
  135. top = fs.Top
  136. sd = fs.SConstruct_dir.rdir()
  137. exports = kw.get('exports', [])
  138. # evaluate each SConscript file
  139. results = []
  140. for fn in files:
  141. call_stack.append(Frame(fs, exports, fn))
  142. old_sys_path = sys.path
  143. try:
  144. SCons.Script.sconscript_reading = SCons.Script.sconscript_reading + 1
  145. if fn == "-":
  146. exec sys.stdin in call_stack[-1].globals
  147. else:
  148. if isinstance(fn, SCons.Node.Node):
  149. f = fn
  150. else:
  151. f = fs.File(str(fn))
  152. _file_ = None
  153. # Change directory to the top of the source
  154. # tree to make sure the os's cwd and the cwd of
  155. # fs match so we can open the SConscript.
  156. fs.chdir(top, change_os_dir=1)
  157. if f.rexists():
  158. actual = f.rfile()
  159. _file_ = open(actual.get_abspath(), "r")
  160. elif f.srcnode().rexists():
  161. actual = f.srcnode().rfile()
  162. _file_ = open(actual.get_abspath(), "r")
  163. elif f.has_src_builder():
  164. # The SConscript file apparently exists in a source
  165. # code management system. Build it, but then clear
  166. # the builder so that it doesn't get built *again*
  167. # during the actual build phase.
  168. f.build()
  169. f.built()
  170. f.builder_set(None)
  171. if f.exists():
  172. _file_ = open(f.get_abspath(), "r")
  173. if _file_:
  174. # Chdir to the SConscript directory. Use a path
  175. # name relative to the SConstruct file so that if
  176. # we're using the -f option, we're essentially
  177. # creating a parallel SConscript directory structure
  178. # in our local directory tree.
  179. #
  180. # XXX This is broken for multiple-repository cases
  181. # where the SConstruct and SConscript files might be
  182. # in different Repositories. For now, cross that
  183. # bridge when someone comes to it.
  184. try:
  185. src_dir = kw['src_dir']
  186. except KeyError:
  187. ldir = fs.Dir(f.dir.get_path(sd))
  188. else:
  189. ldir = fs.Dir(src_dir)
  190. if not ldir.is_under(f.dir):
  191. # They specified a source directory, but
  192. # it's above the SConscript directory.
  193. # Do the sensible thing and just use the
  194. # SConcript directory.
  195. ldir = fs.Dir(f.dir.get_path(sd))
  196. try:
  197. fs.chdir(ldir, change_os_dir=sconscript_chdir)
  198. except OSError:
  199. # There was no local directory, so we should be
  200. # able to chdir to the Repository directory.
  201. # Note that we do this directly, not through
  202. # fs.chdir(), because we still need to
  203. # interpret the stuff within the SConscript file
  204. # relative to where we are logically.
  205. fs.chdir(ldir, change_os_dir=0)
  206. os.chdir(actual.dir.get_abspath())
  207. # Append the SConscript directory to the beginning
  208. # of sys.path so Python modules in the SConscript
  209. # directory can be easily imported.
  210. sys.path = [ f.dir.get_abspath() ] + sys.path
  211. # This is the magic line that actually reads up
  212. # and executes the stuff in the SConscript file.
  213. # The locals for this frame contain the special
  214. # bottom-of-the-stack marker so that any
  215. # exceptions that occur when processing this
  216. # SConscript can base the printed frames at this
  217. # level and not show SCons internals as well.
  218. call_stack[-1].globals.update({stack_bottom:1})
  219. old_file = call_stack[-1].globals.get('__file__')
  220. try:
  221. del call_stack[-1].globals['__file__']
  222. except KeyError:
  223. pass
  224. try:
  225. try:
  226. exec _file_ in call_stack[-1].globals
  227. except SConscriptReturn:
  228. pass
  229. finally:
  230. if old_file is not None:
  231. call_stack[-1].globals.update({__file__:old_file})
  232. else:
  233. SCons.Warnings.warn(SCons.Warnings.MissingSConscriptWarning,
  234. "Ignoring missing SConscript '%s'" % f.path)
  235. finally:
  236. SCons.Script.sconscript_reading = SCons.Script.sconscript_reading - 1
  237. sys.path = old_sys_path
  238. frame = call_stack.pop()
  239. try:
  240. fs.chdir(frame.prev_dir, change_os_dir=sconscript_chdir)
  241. except OSError:
  242. # There was no local directory, so chdir to the
  243. # Repository directory. Like above, we do this
  244. # directly.
  245. fs.chdir(frame.prev_dir, change_os_dir=0)
  246. rdir = frame.prev_dir.rdir()
  247. rdir._create() # Make sure there's a directory there.
  248. try:
  249. os.chdir(rdir.get_abspath())
  250. except OSError, e:
  251. # We still couldn't chdir there, so raise the error,
  252. # but only if actions are being executed.
  253. #
  254. # If the -n option was used, the directory would *not*
  255. # have been created and we should just carry on and
  256. # let things muddle through. This isn't guaranteed
  257. # to work if the SConscript files are reading things
  258. # from disk (for example), but it should work well
  259. # enough for most configurations.
  260. if SCons.Action.execute_actions:
  261. raise e
  262. results.append(frame.retval)
  263. # if we only have one script, don't return a tuple
  264. if len(results) == 1:
  265. return results[0]
  266. else:
  267. return tuple(results)
  268. def SConscript_exception(file=sys.stderr):
  269. """Print an exception stack trace just for the SConscript file(s).
  270. This will show users who have Python errors where the problem is,
  271. without cluttering the output with all of the internal calls leading
  272. up to where we exec the SConscript."""
  273. exc_type, exc_value, exc_tb = sys.exc_info()
  274. tb = exc_tb
  275. while tb and stack_bottom not in tb.tb_frame.f_locals:
  276. tb = tb.tb_next
  277. if not tb:
  278. # We did not find our exec statement, so this was actually a bug
  279. # in SCons itself. Show the whole stack.
  280. tb = exc_tb
  281. stack = traceback.extract_tb(tb)
  282. try:
  283. type = exc_type.__name__
  284. except AttributeError:
  285. type = str(exc_type)
  286. if type[:11] == "exceptions.":
  287. type = type[11:]
  288. file.write('%s: %s:\n' % (type, exc_value))
  289. for fname, line, func, text in stack:
  290. file.write(' File "%s", line %d:\n' % (fname, line))
  291. file.write(' %s\n' % text)
  292. def annotate(node):
  293. """Annotate a node with the stack frame describing the
  294. SConscript file and line number that created it."""
  295. tb = sys.exc_info()[2]
  296. while tb and stack_bottom not in tb.tb_frame.f_locals:
  297. tb = tb.tb_next
  298. if not tb:
  299. # We did not find any exec of an SConscript file: what?!
  300. raise SCons.Errors.InternalError("could not find SConscript stack frame")
  301. node.creator = traceback.extract_stack(tb)[0]
  302. # The following line would cause each Node to be annotated using the
  303. # above function. Unfortunately, this is a *huge* performance hit, so
  304. # leave this disabled until we find a more efficient mechanism.
  305. #SCons.Node.Annotate = annotate
  306. class SConsEnvironment(SCons.Environment.Base):
  307. """An Environment subclass that contains all of the methods that
  308. are particular to the wrapper SCons interface and which aren't
  309. (or shouldn't be) part of the build engine itself.
  310. Note that not all of the methods of this class have corresponding
  311. global functions, there are some private methods.
  312. """
  313. #
  314. # Private methods of an SConsEnvironment.
  315. #
  316. def _exceeds_version(self, major, minor, v_major, v_minor):
  317. """Return 1 if 'major' and 'minor' are greater than the version
  318. in 'v_major' and 'v_minor', and 0 otherwise."""
  319. return (major > v_major or (major == v_major and minor > v_minor))
  320. def _get_major_minor_revision(self, version_string):
  321. """Split a version string into major, minor and (optionally)
  322. revision parts.
  323. This is complicated by the fact that a version string can be
  324. something like 3.2b1."""
  325. version = version_string.split(' ')[0].split('.')
  326. v_major = int(version[0])
  327. v_minor = int(re.match('\d+', version[1]).group())
  328. if len(version) >= 3:
  329. v_revision = int(re.match('\d+', version[2]).group())
  330. else:
  331. v_revision = 0
  332. return v_major, v_minor, v_revision
  333. def _get_SConscript_filenames(self, ls, kw):
  334. """
  335. Convert the parameters passed to SConscript() calls into a list
  336. of files and export variables. If the parameters are invalid,
  337. throws SCons.Errors.UserError. Returns a tuple (l, e) where l
  338. is a list of SConscript filenames and e is a list of exports.
  339. """
  340. exports = []
  341. if len(ls) == 0:
  342. try:
  343. dirs = kw["dirs"]
  344. except KeyError:
  345. raise SCons.Errors.UserError("Invalid SConscript usage - no parameters")
  346. if not SCons.Util.is_List(dirs):
  347. dirs = [ dirs ]
  348. dirs = list(map(str, dirs))
  349. name = kw.get('name', 'SConscript')
  350. files = [os.path.join(n, name) for n in dirs]
  351. elif len(ls) == 1:
  352. files = ls[0]
  353. elif len(ls) == 2:
  354. files = ls[0]
  355. exports = self.Split(ls[1])
  356. else:
  357. raise SCons.Errors.UserError("Invalid SConscript() usage - too many arguments")
  358. if not SCons.Util.is_List(files):
  359. files = [ files ]
  360. if kw.get('exports'):
  361. exports.extend(self.Split(kw['exports']))
  362. variant_dir = kw.get('variant_dir') or kw.get('build_dir')
  363. if variant_dir:
  364. if len(files) != 1:
  365. raise SCons.Errors.UserError("Invalid SConscript() usage - can only specify one SConscript with a variant_dir")
  366. duplicate = kw.get('duplicate', 1)
  367. src_dir = kw.get('src_dir')
  368. if not src_dir:
  369. src_dir, fname = os.path.split(str(files[0]))
  370. files = [os.path.join(str(variant_dir), fname)]
  371. else:
  372. if not isinstance(src_dir, SCons.Node.Node):
  373. src_dir = self.fs.Dir(src_dir)
  374. fn = files[0]
  375. if not isinstance(fn, SCons.Node.Node):
  376. fn = self.fs.File(fn)
  377. if fn.is_under(src_dir):
  378. # Get path relative to the source directory.
  379. fname = fn.get_path(src_dir)
  380. files = [os.path.join(str(variant_dir), fname)]
  381. else:
  382. files = [fn.abspath]
  383. kw['src_dir'] = variant_dir
  384. self.fs.VariantDir(variant_dir, src_dir, duplicate)
  385. return (files, exports)
  386. #
  387. # Public methods of an SConsEnvironment. These get
  388. # entry points in the global name space so they can be called
  389. # as global functions.
  390. #
  391. def Configure(self, *args, **kw):
  392. if not SCons.Script.sconscript_reading:
  393. raise SCons.Errors.UserError("Calling Configure from Builders is not supported.")
  394. kw['_depth'] = kw.get('_depth', 0) + 1
  395. return SCons.Environment.Base.Configure(self, *args, **kw)
  396. def Default(self, *targets):
  397. SCons.Script._Set_Default_Targets(self, targets)
  398. def EnsureSConsVersion(self, major, minor, revision=0):
  399. """Exit abnormally if the SCons version is not late enough."""
  400. scons_ver = self._get_major_minor_revision(SCons.__version__)
  401. if scons_ver < (major, minor, revision):
  402. if revision:
  403. scons_ver_string = '%d.%d.%d' % (major, minor, revision)
  404. else:
  405. scons_ver_string = '%d.%d' % (major, minor)
  406. print "SCons %s or greater required, but you have SCons %s" % \
  407. (scons_ver_string, SCons.__version__)
  408. sys.exit(2)
  409. def EnsurePythonVersion(self, major, minor):
  410. """Exit abnormally if the Python version is not late enough."""
  411. try:
  412. v_major, v_minor, v_micro, release, serial = sys.version_info
  413. python_ver = (v_major, v_minor)
  414. except AttributeError:
  415. python_ver = self._get_major_minor_revision(sys.version)[:2]
  416. if python_ver < (major, minor):
  417. v = sys.version.split(" ", 1)[0]
  418. print "Python %d.%d or greater required, but you have Python %s" %(major,minor,v)
  419. sys.exit(2)
  420. def Exit(self, value=0):
  421. sys.exit(value)
  422. def Export(self, *vars, **kw):
  423. for var in vars:
  424. global_exports.update(compute_exports(self.Split(var)))
  425. global_exports.update(kw)
  426. def GetLaunchDir(self):
  427. global launch_dir
  428. return launch_dir
  429. def GetOption(self, name):
  430. name = self.subst(name)
  431. return SCons.Script.Main.GetOption(name)
  432. def Help(self, text):
  433. text = self.subst(text, raw=1)
  434. SCons.Script.HelpFunction(text)
  435. def Import(self, *vars):
  436. try:
  437. frame = call_stack[-1]
  438. globals = frame.globals
  439. exports = frame.exports
  440. for var in vars:
  441. var = self.Split(var)
  442. for v in var:
  443. if v == '*':
  444. globals.update(global_exports)
  445. globals.update(exports)
  446. else:
  447. if v in exports:
  448. globals[v] = exports[v]
  449. else:
  450. globals[v] = global_exports[v]
  451. except KeyError,x:
  452. raise SCons.Errors.UserError("Import of non-existent variable '%s'"%x)
  453. def SConscript(self, *ls, **kw):
  454. if 'build_dir' in kw:
  455. msg = """The build_dir keyword has been deprecated; use the variant_dir keyword instead."""
  456. SCons.Warnings.warn(SCons.Warnings.DeprecatedBuildDirWarning, msg)
  457. def subst_element(x, subst=self.subst):
  458. if SCons.Util.is_List(x):
  459. x = list(map(subst, x))
  460. else:
  461. x = subst(x)
  462. return x
  463. ls = list(map(subst_element, ls))
  464. subst_kw = {}
  465. for key, val in kw.items():
  466. if SCons.Util.is_String(val):
  467. val = self.subst(val)
  468. elif SCons.Util.is_List(val):
  469. result = []
  470. for v in val:
  471. if SCons.Util.is_String(v):
  472. v = self.subst(v)
  473. result.append(v)
  474. val = result
  475. subst_kw[key] = val
  476. files, exports = self._get_SConscript_filenames(ls, subst_kw)
  477. subst_kw['exports'] = exports
  478. return _SConscript(self.fs, *files, **subst_kw)
  479. def SConscriptChdir(self, flag):
  480. global sconscript_chdir
  481. sconscript_chdir = flag
  482. def SetOption(self, name, value):
  483. name = self.subst(name)
  484. SCons.Script.Main.SetOption(name, value)
  485. #
  486. #
  487. #
  488. SCons.Environment.Environment = SConsEnvironment
  489. def Configure(*args, **kw):
  490. if not SCons.Script.sconscript_reading:
  491. raise SCons.Errors.UserError("Calling Configure from Builders is not supported.")
  492. kw['_depth'] = 1
  493. return SCons.SConf.SConf(*args, **kw)
  494. # It's very important that the DefaultEnvironmentCall() class stay in this
  495. # file, with the get_calling_namespaces() function, the compute_exports()
  496. # function, the Frame class and the SConsEnvironment.Export() method.
  497. # These things make up the calling stack leading up to the actual global
  498. # Export() or SConscript() call that the user issued. We want to allow
  499. # users to export local variables that they define, like so:
  500. #
  501. # def func():
  502. # x = 1
  503. # Export('x')
  504. #
  505. # To support this, the get_calling_namespaces() function assumes that
  506. # the *first* stack frame that's not from this file is the local frame
  507. # for the Export() or SConscript() call.
  508. _DefaultEnvironmentProxy = None
  509. def get_DefaultEnvironmentProxy():
  510. global _DefaultEnvironmentProxy
  511. if not _DefaultEnvironmentProxy:
  512. default_env = SCons.Defaults.DefaultEnvironment()
  513. _DefaultEnvironmentProxy = SCons.Environment.NoSubstitutionProxy(default_env)
  514. return _DefaultEnvironmentProxy
  515. class DefaultEnvironmentCall(object):
  516. """A class that implements "global function" calls of
  517. Environment methods by fetching the specified method from the
  518. DefaultEnvironment's class. Note that this uses an intermediate
  519. proxy class instead of calling the DefaultEnvironment method
  520. directly so that the proxy can override the subst() method and
  521. thereby prevent expansion of construction variables (since from
  522. the user's point of view this was called as a global function,
  523. with no associated construction environment)."""
  524. def __init__(self, method_name, subst=0):
  525. self.method_name = method_name
  526. if subst:
  527. self.factory = SCons.Defaults.DefaultEnvironment
  528. else:
  529. self.factory = get_DefaultEnvironmentProxy
  530. def __call__(self, *args, **kw):
  531. env = self.factory()
  532. method = getattr(env, self.method_name)
  533. return method(*args, **kw)
  534. def BuildDefaultGlobals():
  535. """
  536. Create a dictionary containing all the default globals for
  537. SConstruct and SConscript files.
  538. """
  539. global GlobalDict
  540. if GlobalDict is None:
  541. GlobalDict = {}
  542. import SCons.Script
  543. d = SCons.Script.__dict__
  544. def not_a_module(m, d=d, mtype=type(SCons.Script)):
  545. return not isinstance(d[m], mtype)
  546. for m in filter(not_a_module, dir(SCons.Script)):
  547. GlobalDict[m] = d[m]
  548. return GlobalDict.copy()
  549. # Local Variables:
  550. # tab-width:4
  551. # indent-tabs-mode:nil
  552. # End:
  553. # vim: set expandtab tabstop=4 shiftwidth=4: