PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/dependencies/boost-1.46.0/tools/build/v2/test/BoostBuild.py

https://bitbucket.org/gorkasg/defendersofmankind
Python | 921 lines | 883 code | 24 blank | 14 comment | 22 complexity | 1d892fbcf57eaffa939d6abbcd3953c7 MD5 | raw file
Possible License(s): GPL-3.0, ISC, MIT, LGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. # Copyright 2002-2005 Vladimir Prus.
  2. # Copyright 2002-2003 Dave Abrahams.
  3. # Copyright 2006 Rene Rivera.
  4. # Distributed under the Boost Software License, Version 1.0.
  5. # (See accompanying file LICENSE_1_0.txt or copy at
  6. # http://www.boost.org/LICENSE_1_0.txt)
  7. import TestCmd
  8. import copy
  9. import fnmatch
  10. import glob
  11. import math
  12. import os
  13. import re
  14. import shutil
  15. import string
  16. import StringIO
  17. import sys
  18. import tempfile
  19. import time
  20. import traceback
  21. import tree
  22. import types
  23. from xml.sax.saxutils import escape
  24. annotations = []
  25. def print_annotation(name, value, xml):
  26. """Writes some named bits of information about test run.
  27. """
  28. if xml:
  29. print escape(name) + " {{{"
  30. print escape(value)
  31. print "}}}"
  32. else:
  33. print name + " {{{"
  34. print value
  35. print "}}}"
  36. def flush_annotations(xml=0):
  37. global annotations
  38. for ann in annotations:
  39. print_annotation(ann[0], ann[1], xml)
  40. annotations = []
  41. def clear_annotations():
  42. global annotations
  43. annotations = []
  44. defer_annotations = 0
  45. def set_defer_annotations(n):
  46. global defer_annotations
  47. defer_annotations = n
  48. def annotation(name, value):
  49. """Records an annotation about the test run.
  50. """
  51. annotations.append((name, value))
  52. if not defer_annotations:
  53. flush_annotations()
  54. def get_toolset():
  55. toolset = None;
  56. for arg in sys.argv[1:]:
  57. if not arg.startswith('-'):
  58. toolset = arg
  59. return toolset or 'gcc'
  60. # Detect the host OS.
  61. windows = False
  62. if os.environ.get('OS', '').lower().startswith('windows') or \
  63. os.__dict__.has_key('uname') and \
  64. os.uname()[0].lower().startswith('cygwin'):
  65. windows = True
  66. suffixes = {}
  67. # Prepare the map of suffixes
  68. def prepare_suffix_map(toolset):
  69. global windows
  70. global suffixes
  71. suffixes = {'.exe': '', '.dll': '.so', '.lib': '.a', '.obj': '.o'}
  72. suffixes['.implib'] = '.no_implib_files_on_this_platform'
  73. if windows:
  74. suffixes = {}
  75. if toolset in ["gcc"]:
  76. suffixes['.lib'] = '.a' # static libs have '.a' suffix with mingw...
  77. suffixes['.obj'] = '.o'
  78. suffixes['.implib'] = '.lib'
  79. if os.__dict__.has_key('uname') and (os.uname()[0] == 'Darwin'):
  80. suffixes['.dll'] = '.dylib'
  81. def re_remove(sequence, regex):
  82. me = re.compile(regex)
  83. result = filter(lambda x: me.match(x), sequence)
  84. if 0 == len(result):
  85. raise ValueError()
  86. for r in result:
  87. sequence.remove(r)
  88. def glob_remove(sequence, pattern):
  89. result = fnmatch.filter(sequence, pattern)
  90. if 0 == len(result):
  91. raise ValueError()
  92. for r in result:
  93. sequence.remove(r)
  94. # Configuration stating whether Boost Build is expected to automatically prepend
  95. # prefixes to built library targets.
  96. lib_prefix = True
  97. dll_prefix = True
  98. if windows:
  99. dll_prefix = False
  100. #
  101. # FIXME: this is copy-pasted from TestSCons.py
  102. # Should be moved to TestCmd.py?
  103. #
  104. if os.name == 'posix':
  105. def _failed(self, status=0):
  106. if self.status is None:
  107. return None
  108. return _status(self) != status
  109. def _status(self):
  110. if os.WIFEXITED(self.status):
  111. return os.WEXITSTATUS(self.status)
  112. else:
  113. return -1
  114. elif os.name == 'nt':
  115. def _failed(self, status=0):
  116. return not self.status is None and self.status != status
  117. def _status(self):
  118. return self.status
  119. class Tester(TestCmd.TestCmd):
  120. """Main tester class for Boost Build.
  121. Optional arguments:
  122. `arguments` - Arguments passed to the run executable.
  123. `executable` - Name of the executable to invoke.
  124. `match` - Function to use for compating actual and
  125. expected file contents.
  126. `boost_build_path` - Boost build path to be passed to the run
  127. executable.
  128. `translate_suffixes` - Whether to update suffixes on the the file
  129. names passed from the test script so they
  130. match those actually created by the current
  131. toolset. For example, static library files
  132. are specified by using the .lib suffix but
  133. when the 'gcc' toolset is used it actually
  134. creates them using the .a suffix.
  135. `pass_toolset` - Whether the test system should pass the
  136. specified toolset to the run executable.
  137. `use_test_config` - Whether the test system should tell the run
  138. executable to read in the test_config.jam
  139. configuration file.
  140. `ignore_toolset_requirements` - Whether the test system should tell the run
  141. executable to ignore toolset requirements.
  142. `workdir` - indicates an absolute directory where the
  143. test will be run from.
  144. Optional arguments inherited from the base class:
  145. `description` - Test description string displayed in case of
  146. a failed test.
  147. `subdir' - List of subdirectories to automatically
  148. create under the working directory. Each
  149. subdirectory needs to be specified
  150. separately parent coming before its child.
  151. `verbose` - Flag that may be used to enable more verbose
  152. test system output. Note that it does not
  153. also enable more verbose build system
  154. output like the --verbose command line
  155. option does.
  156. """
  157. def __init__(self, arguments="", executable="bjam",
  158. match=TestCmd.match_exact, boost_build_path=None,
  159. translate_suffixes=True, pass_toolset=True, use_test_config=True,
  160. ignore_toolset_requirements=True, workdir="", **keywords):
  161. self.original_workdir = os.getcwd()
  162. if workdir != '' and not os.path.isabs(workdir):
  163. raise "Parameter workdir <"+workdir+"> must point to an absolute directory: "
  164. self.last_build_time_start = 0
  165. self.last_build_time_finish = 0
  166. self.translate_suffixes = translate_suffixes
  167. self.use_test_config = use_test_config
  168. self.toolset = get_toolset()
  169. self.pass_toolset = pass_toolset
  170. self.ignore_toolset_requirements = ignore_toolset_requirements
  171. prepare_suffix_map(pass_toolset and self.toolset or 'gcc')
  172. if not '--default-bjam' in sys.argv:
  173. jam_build_dir = ""
  174. if os.name == 'nt':
  175. jam_build_dir = "bin.ntx86"
  176. elif (os.name == 'posix') and os.__dict__.has_key('uname'):
  177. if os.uname()[0].lower().startswith('cygwin'):
  178. jam_build_dir = "bin.cygwinx86"
  179. if 'TMP' in os.environ and os.environ['TMP'].find('~') != -1:
  180. print 'Setting $TMP to /tmp to get around problem with short path names'
  181. os.environ['TMP'] = '/tmp'
  182. elif os.uname()[0] == 'Linux':
  183. cpu = os.uname()[4]
  184. if re.match("i.86", cpu):
  185. jam_build_dir = "bin.linuxx86";
  186. else:
  187. jam_build_dir = "bin.linux" + os.uname()[4]
  188. elif os.uname()[0] == 'SunOS':
  189. jam_build_dir = "bin.solaris"
  190. elif os.uname()[0] == 'Darwin':
  191. if os.uname()[4] == 'i386':
  192. jam_build_dir = "bin.macosxx86"
  193. else:
  194. jam_build_dir = "bin.macosxppc"
  195. elif os.uname()[0] == "AIX":
  196. jam_build_dir = "bin.aix"
  197. elif os.uname()[0] == "IRIX64":
  198. jam_build_dir = "bin.irix"
  199. elif os.uname()[0] == "FreeBSD":
  200. jam_build_dir = "bin.freebsd"
  201. elif os.uname()[0] == "OSF1":
  202. jam_build_dir = "bin.osf"
  203. else:
  204. raise "Don't know directory where Jam is built for this system: " + os.name + "/" + os.uname()[0]
  205. else:
  206. raise "Don't know directory where Jam is built for this system: " + os.name
  207. # Find where jam_src is located. Try for the debug version if it is
  208. # lying around.
  209. dirs = [os.path.join('../engine/src', jam_build_dir + '.debug'),
  210. os.path.join('../engine/src', jam_build_dir),
  211. ]
  212. for d in dirs:
  213. if os.path.exists(d):
  214. jam_build_dir = d
  215. break
  216. else:
  217. print "Cannot find built Boost.Jam"
  218. sys.exit(1)
  219. verbosity = ['-d0', '--quiet']
  220. if '--verbose' in sys.argv:
  221. keywords['verbose'] = True
  222. verbosity = ['-d+2']
  223. if boost_build_path is None:
  224. boost_build_path = self.original_workdir + "/.."
  225. program_list = []
  226. if '--default-bjam' in sys.argv:
  227. program_list.append(executable)
  228. inpath_bjam = True
  229. else:
  230. program_list.append(os.path.join(jam_build_dir, executable))
  231. inpath_bjam = None
  232. program_list.append('-sBOOST_BUILD_PATH="' + boost_build_path + '"')
  233. if verbosity:
  234. program_list += verbosity
  235. if arguments:
  236. program_list += arguments.split(" ")
  237. TestCmd.TestCmd.__init__(
  238. self
  239. , program=program_list
  240. , match=match
  241. , workdir=workdir
  242. , inpath=inpath_bjam
  243. , **keywords)
  244. os.chdir(self.workdir)
  245. def cleanup(self):
  246. try:
  247. TestCmd.TestCmd.cleanup(self)
  248. os.chdir(self.original_workdir)
  249. except AttributeError:
  250. # When this is called during TestCmd.TestCmd.__del__ we can have
  251. # both 'TestCmd' and 'os' unavailable in our scope. Do nothing in
  252. # this case.
  253. pass
  254. #
  255. # Methods that change the working directory's content.
  256. #
  257. def set_tree(self, tree_location):
  258. # It is not possible to remove the current directory.
  259. d = os.getcwd()
  260. os.chdir(os.path.dirname(self.workdir))
  261. shutil.rmtree(self.workdir, ignore_errors=False)
  262. if not os.path.isabs(tree_location):
  263. tree_location = os.path.join(self.original_workdir, tree_location)
  264. shutil.copytree(tree_location, self.workdir)
  265. os.chdir(d)
  266. def make_writable(unused, dir, entries):
  267. for e in entries:
  268. name = os.path.join(dir, e)
  269. os.chmod(name, os.stat(name)[0] | 0222)
  270. os.path.walk(".", make_writable, None)
  271. def write(self, file, content):
  272. self.wait_for_time_change_since_last_build()
  273. nfile = self.native_file_name(file)
  274. try:
  275. os.makedirs(os.path.dirname(nfile))
  276. except Exception, e:
  277. pass
  278. open(nfile, "wb").write(content)
  279. def rename(self, old, new):
  280. try:
  281. os.makedirs(os.path.dirname(new))
  282. except:
  283. pass
  284. try:
  285. os.remove(new)
  286. except:
  287. pass
  288. os.rename(old, new)
  289. self.touch(new);
  290. def copy(self, src, dst):
  291. self.wait_for_time_change_since_last_build()
  292. try:
  293. self.write(dst, self.read(src, 1))
  294. except:
  295. self.fail_test(1)
  296. def copy_preserving_timestamp(self, src, dst):
  297. src_name = self.native_file_name(src)
  298. dst_name = self.native_file_name(dst)
  299. stats = os.stat(src_name)
  300. self.write(dst, self.read(src, 1))
  301. os.utime(dst_name, (stats.st_atime, stats.st_mtime))
  302. def touch(self, names):
  303. self.wait_for_time_change_since_last_build()
  304. for name in self.adjust_names(names):
  305. os.utime(self.native_file_name(name), None)
  306. def rm(self, names):
  307. self.wait_for_time_change_since_last_build()
  308. if not type(names) == types.ListType:
  309. names = [names]
  310. # Avoid attempts to remove the current directory.
  311. os.chdir(self.original_workdir)
  312. for name in names:
  313. n = self.native_file_name(name)
  314. n = glob.glob(n)
  315. if n: n = n[0]
  316. if not n:
  317. n = self.glob_file(string.replace(name, "$toolset", self.toolset+"*"))
  318. if n:
  319. if os.path.isdir(n):
  320. shutil.rmtree(n, ignore_errors=False)
  321. else:
  322. os.unlink(n)
  323. # Create working dir root again in case we removed it.
  324. if not os.path.exists(self.workdir):
  325. os.mkdir(self.workdir)
  326. os.chdir(self.workdir)
  327. def expand_toolset(self, name):
  328. """Expands $toolset in the given file to tested toolset.
  329. """
  330. content = self.read(name)
  331. content = string.replace(content, "$toolset", self.toolset)
  332. self.write(name, content)
  333. def dump_stdio(self):
  334. annotation("STDOUT", self.stdout())
  335. annotation("STDERR", self.stderr())
  336. #
  337. # FIXME: Large portion copied from TestSCons.py, should be moved?
  338. #
  339. def run_build_system(self, extra_args="", subdir="", stdout=None, stderr="",
  340. status=0, match=None, pass_toolset=None, use_test_config=None,
  341. ignore_toolset_requirements=None, expected_duration=None, **kw):
  342. self.last_build_time_start = time.time()
  343. try:
  344. if os.path.isabs(subdir):
  345. if stderr:
  346. print "You must pass a relative directory to subdir <"+subdir+">."
  347. status = 1
  348. return
  349. self.previous_tree = tree.build_tree(self.workdir)
  350. if match is None:
  351. match = self.match
  352. if pass_toolset is None:
  353. pass_toolset = self.pass_toolset
  354. if use_test_config is None:
  355. use_test_config = self.use_test_config
  356. if ignore_toolset_requirements is None:
  357. ignore_toolset_requirements = self.ignore_toolset_requirements
  358. try:
  359. kw['program'] = []
  360. kw['program'] += self.program
  361. if extra_args:
  362. kw['program'] += extra_args.split(" ")
  363. if pass_toolset:
  364. kw['program'].append("toolset=" + self.toolset)
  365. if use_test_config:
  366. kw['program'].append('--test-config="%s"'
  367. % os.path.join(self.original_workdir, "test-config.jam"))
  368. if ignore_toolset_requirements:
  369. kw['program'].append("--ignore-toolset-requirements")
  370. if "--python" in sys.argv:
  371. kw['program'].append("--python")
  372. kw['chdir'] = subdir
  373. self.last_program_invocation = kw['program']
  374. apply(TestCmd.TestCmd.run, [self], kw)
  375. except:
  376. self.dump_stdio()
  377. raise
  378. finally:
  379. self.last_build_time_finish = time.time()
  380. if (status != None) and _failed(self, status):
  381. expect = ''
  382. if status != 0:
  383. expect = " (expected %d)" % status
  384. annotation("failure", '"%s" returned %d%s'
  385. % (kw['program'], _status(self), expect))
  386. annotation("reason", "unexpected status returned by bjam")
  387. self.fail_test(1)
  388. if not (stdout is None) and not match(self.stdout(), stdout):
  389. annotation("failure", "Unexpected stdout")
  390. annotation("Expected STDOUT", stdout)
  391. annotation("Actual STDOUT", self.stdout())
  392. stderr = self.stderr()
  393. if stderr:
  394. annotation("STDERR", stderr)
  395. self.maybe_do_diff(self.stdout(), stdout)
  396. self.fail_test(1, dump_stdio=False)
  397. # Intel tends to produce some messages to stderr which make tests fail.
  398. intel_workaround = re.compile("^xi(link|lib): executing.*\n", re.M)
  399. actual_stderr = re.sub(intel_workaround, "", self.stderr())
  400. if not (stderr is None) and not match(actual_stderr, stderr):
  401. annotation("failure", "Unexpected stderr")
  402. annotation("Expected STDERR", stderr)
  403. annotation("Actual STDERR", self.stderr())
  404. annotation("STDOUT", self.stdout())
  405. self.maybe_do_diff(actual_stderr, stderr)
  406. self.fail_test(1, dump_stdio=False)
  407. if not expected_duration is None:
  408. actual_duration = self.last_build_time_finish - self.last_build_time_start
  409. if (actual_duration > expected_duration):
  410. print "Test run lasted %f seconds while it was expected to " \
  411. "finish in under %f seconds." % (actual_duration,
  412. expected_duration)
  413. self.fail_test(1, dump_stdio=False)
  414. self.tree = tree.build_tree(self.workdir)
  415. self.difference = tree.trees_difference(self.previous_tree, self.tree)
  416. self.difference.ignore_directories()
  417. self.unexpected_difference = copy.deepcopy(self.difference)
  418. def glob_file(self, name):
  419. result = None
  420. if hasattr(self, 'difference'):
  421. for f in self.difference.added_files+self.difference.modified_files+self.difference.touched_files:
  422. if fnmatch.fnmatch(f, name):
  423. result = self.native_file_name(f)
  424. break
  425. if not result:
  426. result = glob.glob(self.native_file_name(name))
  427. if result:
  428. result = result[0]
  429. return result
  430. def read(self, name, binary=False):
  431. try:
  432. if self.toolset:
  433. name = string.replace(name, "$toolset", self.toolset+"*")
  434. name = self.glob_file(name)
  435. openMode = "r"
  436. if binary:
  437. openMode += "b"
  438. else:
  439. openMode += "U"
  440. return open(name, openMode).read()
  441. except:
  442. annotation("failure", "Could not open '%s'" % name)
  443. self.fail_test(1)
  444. return ''
  445. def read_and_strip(self, name):
  446. if not self.glob_file(name):
  447. return ''
  448. f = open(self.glob_file(name), "rb")
  449. lines = f.readlines()
  450. result = string.join(map(string.rstrip, lines), "\n")
  451. if lines and lines[-1][-1] == '\n':
  452. return result + '\n'
  453. else:
  454. return result
  455. def fail_test(self, condition, dump_stdio=True, *args):
  456. if not condition:
  457. return
  458. if hasattr(self, 'difference'):
  459. f = StringIO.StringIO()
  460. self.difference.pprint(f)
  461. annotation("changes caused by the last build command", f.getvalue())
  462. if dump_stdio:
  463. self.dump_stdio()
  464. if '--preserve' in sys.argv:
  465. print
  466. print "*** Copying the state of working dir into 'failed_test' ***"
  467. print
  468. path = os.path.join(self.original_workdir, "failed_test")
  469. if os.path.isdir(path):
  470. shutil.rmtree(path, ignore_errors=False)
  471. elif os.path.exists(path):
  472. raise "Path " + path + " already exists and is not a directory";
  473. shutil.copytree(self.workdir, path)
  474. print "The failed command was:"
  475. print ' '.join(self.last_program_invocation)
  476. at = TestCmd.caller(traceback.extract_stack(), 0)
  477. annotation("stacktrace", at)
  478. sys.exit(1)
  479. # A number of methods below check expectations with actual difference
  480. # between directory trees before and after a build. All the 'expect*'
  481. # methods require exact names to be passed. All the 'ignore*' methods allow
  482. # wildcards.
  483. # All names can be lists, which are taken to be directory components.
  484. def expect_addition(self, names):
  485. for name in self.adjust_names(names):
  486. try:
  487. glob_remove(self.unexpected_difference.added_files, name)
  488. except:
  489. annotation("failure", "File %s not added as expected" % name)
  490. self.fail_test(1)
  491. def ignore_addition(self, wildcard):
  492. self.ignore_elements(self.unexpected_difference.added_files, wildcard)
  493. def expect_removal(self, names):
  494. for name in self.adjust_names(names):
  495. try:
  496. glob_remove(self.unexpected_difference.removed_files, name)
  497. except:
  498. annotation("failure", "File %s not removed as expected" % name)
  499. self.fail_test(1)
  500. def ignore_removal(self, wildcard):
  501. self.ignore_elements(self.unexpected_difference.removed_files, wildcard)
  502. def expect_modification(self, names):
  503. for name in self.adjust_names(names):
  504. try:
  505. glob_remove(self.unexpected_difference.modified_files, name)
  506. except:
  507. annotation("failure", "File %s not modified as expected" % name)
  508. self.fail_test(1)
  509. def ignore_modification(self, wildcard):
  510. self.ignore_elements(self.unexpected_difference.modified_files, \
  511. wildcard)
  512. def expect_touch(self, names):
  513. d = self.unexpected_difference
  514. for name in self.adjust_names(names):
  515. # We need to check both touched and modified files. The reason is
  516. # that:
  517. # (1) Windows binaries such as obj, exe or dll files have slight
  518. # differences even with identical inputs due to Windows PE
  519. # format headers containing an internal timestamp.
  520. # (2) Intel's compiler for Linux has the same behaviour.
  521. filesets = [d.modified_files, d.touched_files]
  522. while filesets:
  523. try:
  524. glob_remove(filesets[-1], name)
  525. break
  526. except ValueError:
  527. filesets.pop()
  528. if not filesets:
  529. annotation("failure", "File %s not touched as expected" % name)
  530. self.fail_test(1)
  531. def ignore_touch(self, wildcard):
  532. self.ignore_elements(self.unexpected_difference.touched_files, wildcard)
  533. def ignore(self, wildcard):
  534. self.ignore_elements(self.unexpected_difference.added_files, wildcard)
  535. self.ignore_elements(self.unexpected_difference.removed_files, wildcard)
  536. self.ignore_elements(self.unexpected_difference.modified_files, wildcard)
  537. self.ignore_elements(self.unexpected_difference.touched_files, wildcard)
  538. def expect_nothing(self, names):
  539. for name in self.adjust_names(names):
  540. if name in self.difference.added_files:
  541. annotation("failure",
  542. "File %s added, but no action was expected" % name)
  543. self.fail_test(1)
  544. if name in self.difference.removed_files:
  545. annotation("failure",
  546. "File %s removed, but no action was expected" % name)
  547. self.fail_test(1)
  548. pass
  549. if name in self.difference.modified_files:
  550. annotation("failure",
  551. "File %s modified, but no action was expected" % name)
  552. self.fail_test(1)
  553. if name in self.difference.touched_files:
  554. annotation("failure",
  555. "File %s touched, but no action was expected" % name)
  556. self.fail_test(1)
  557. def expect_nothing_more(self):
  558. # Not totally sure about this change, but I do not see a good
  559. # alternative.
  560. if windows:
  561. self.ignore('*.ilk') # MSVC incremental linking files.
  562. self.ignore('*.pdb') # MSVC program database files.
  563. self.ignore('*.rsp') # Response files.
  564. self.ignore('*.tds') # Borland debug symbols.
  565. self.ignore('*.manifest') # MSVC DLL manifests.
  566. # Debug builds of bjam built with gcc produce this profiling data.
  567. self.ignore('gmon.out')
  568. self.ignore('*/gmon.out')
  569. self.ignore("bin/config.log")
  570. self.ignore("*.pyc")
  571. if not self.unexpected_difference.empty():
  572. annotation('failure', 'Unexpected changes found')
  573. output = StringIO.StringIO()
  574. self.unexpected_difference.pprint(output)
  575. annotation("unexpected changes", output.getvalue())
  576. self.fail_test(1)
  577. def __expect_line(self, content, expected, expected_to_exist):
  578. expected = expected.strip()
  579. lines = content.splitlines()
  580. found = False
  581. for line in lines:
  582. line = line.strip()
  583. if fnmatch.fnmatch(line, expected):
  584. found = True
  585. break
  586. if expected_to_exist and not found:
  587. annotation("failure",
  588. "Did not find expected line:\n%s\nin output:\n%s" %
  589. (expected, content))
  590. self.fail_test(1)
  591. if not expected_to_exist and found:
  592. annotation("failure",
  593. "Found an unexpected line:\n%s\nin output:\n%s" %
  594. (expected, content))
  595. self.fail_test(1)
  596. def expect_output_line(self, line, expected_to_exist=True):
  597. self.__expect_line(self.stdout(), line, expected_to_exist)
  598. def expect_content_line(self, name, line, expected_to_exist=True):
  599. content = self.__read_file(name)
  600. self.__expect_line(content, line, expected_to_exist)
  601. def __read_file(self, name, exact=False):
  602. name = self.adjust_names(name)[0]
  603. result = ""
  604. try:
  605. if exact:
  606. result = self.read(name)
  607. else:
  608. result = string.replace(self.read_and_strip(name), "\\", "/")
  609. except (IOError, IndexError):
  610. print "Note: could not open file", name
  611. self.fail_test(1)
  612. return result
  613. def expect_content(self, name, content, exact=False):
  614. actual = self.__read_file(name, exact)
  615. content = string.replace(content, "$toolset", self.toolset+"*")
  616. matched = False
  617. if exact:
  618. matched = fnmatch.fnmatch(actual, content)
  619. else:
  620. def sorted_(x):
  621. x.sort()
  622. return x
  623. actual_ = map(lambda x: sorted_(x.split()), actual.splitlines())
  624. content_ = map(lambda x: sorted_(x.split()), content.splitlines())
  625. if len(actual_) == len(content_):
  626. matched = map(
  627. lambda x, y: map(lambda n, p: fnmatch.fnmatch(n, p), x, y),
  628. actual_, content_)
  629. matched = reduce(
  630. lambda x, y: x and reduce(
  631. lambda a, b: a and b,
  632. y),
  633. matched)
  634. if not matched:
  635. print "Expected:\n"
  636. print content
  637. print "Got:\n"
  638. print actual
  639. self.fail_test(1)
  640. def maybe_do_diff(self, actual, expected):
  641. if os.environ.has_key("DO_DIFF") and os.environ["DO_DIFF"] != '':
  642. e = tempfile.mktemp("expected")
  643. a = tempfile.mktemp("actual")
  644. open(e, "w").write(expected)
  645. open(a, "w").write(actual)
  646. print "DIFFERENCE"
  647. if os.system("diff -u " + e + " " + a):
  648. print "Unable to compute difference: diff -u %s %s" % (e, a)
  649. os.unlink(e)
  650. os.unlink(a)
  651. else:
  652. print "Set environmental variable 'DO_DIFF' to examine difference."
  653. # Helpers.
  654. def mul(self, *arguments):
  655. if len(arguments) == 0:
  656. return None
  657. here = arguments[0]
  658. if type(here) == type(''):
  659. here = [here]
  660. if len(arguments) > 1:
  661. there = apply(self.mul, arguments[1:])
  662. result = []
  663. for i in here:
  664. for j in there:
  665. result.append(i + j)
  666. return result
  667. return here
  668. # Internal methods.
  669. def ignore_elements(self, list, wildcard):
  670. """Removes in-place, element of 'list' that match the given wildcard.
  671. """
  672. list[:] = filter(lambda x, w=wildcard: not fnmatch.fnmatch(x, w), list)
  673. def adjust_lib_name(self, name):
  674. global lib_prefix
  675. result = name
  676. pos = string.rfind(name, ".")
  677. if pos != -1:
  678. suffix = name[pos:]
  679. if suffix == ".lib":
  680. (head, tail) = os.path.split(name)
  681. if lib_prefix:
  682. tail = "lib" + tail
  683. result = os.path.join(head, tail)
  684. elif suffix == ".dll":
  685. (head, tail) = os.path.split(name)
  686. if dll_prefix:
  687. tail = "lib" + tail
  688. result = os.path.join(head, tail)
  689. # If we want to use this name in a Jamfile, we better convert \ to /, as
  690. # otherwise we would have to quote \.
  691. result = string.replace(result, "\\", "/")
  692. return result
  693. def adjust_suffix(self, name):
  694. if not self.translate_suffixes:
  695. return name
  696. pos = string.rfind(name, ".")
  697. if pos != -1:
  698. suffix = name[pos:]
  699. name = name[:pos]
  700. if suffixes.has_key(suffix):
  701. suffix = suffixes[suffix]
  702. else:
  703. suffix = ''
  704. return name + suffix
  705. # Acceps either a string or a list of strings and returns a list of strings.
  706. # Adjusts suffixes on all names.
  707. def adjust_names(self, names):
  708. if type(names) == types.StringType:
  709. names = [names]
  710. r = map(self.adjust_lib_name, names)
  711. r = map(self.adjust_suffix, r)
  712. r = map(lambda x, t=self.toolset: string.replace(x, "$toolset", t+"*"), r)
  713. return r
  714. def native_file_name(self, name):
  715. name = self.adjust_names(name)[0]
  716. elements = string.split(name, "/")
  717. return os.path.normpath(apply(os.path.join, [self.workdir]+elements))
  718. # Wait while time is no longer equal to the time last "run_build_system"
  719. # call finished. Used to avoid subsequent builds treating existing files as
  720. # 'current'.
  721. def wait_for_time_change_since_last_build(self):
  722. while 1:
  723. # In fact, I'm not sure why "+ 2" as opposed to "+ 1" is needed but
  724. # empirically, "+ 1" sometimes causes 'touch' and other functions
  725. # not to bump the file time enough for a rebuild to happen.
  726. if math.floor(time.time()) < math.floor(self.last_build_time_finish) + 2:
  727. time.sleep(0.1)
  728. else:
  729. break
  730. class List:
  731. def __init__(self, s=""):
  732. elements = []
  733. if isinstance(s, type("")):
  734. # Have to handle espaced spaces correctly.
  735. s = string.replace(s, "\ ", '\001')
  736. elements = string.split(s)
  737. else:
  738. elements = s;
  739. self.l = []
  740. for e in elements:
  741. self.l.append(string.replace(e, '\001', ' '))
  742. def __len__(self):
  743. return len(self.l)
  744. def __getitem__(self, key):
  745. return self.l[key]
  746. def __setitem__(self, key, value):
  747. self.l[key] = value
  748. def __delitem__(self, key):
  749. del self.l[key]
  750. def __str__(self):
  751. return str(self.l)
  752. def __repr__(self):
  753. return (self.__module__ + '.List('
  754. + repr(string.join(self.l, ' '))
  755. + ')')
  756. def __mul__(self, other):
  757. result = List()
  758. if not isinstance(other, List):
  759. other = List(other)
  760. for f in self:
  761. for s in other:
  762. result.l.append(f + s)
  763. return result
  764. def __rmul__(self, other):
  765. if not isinstance(other, List):
  766. other = List(other)
  767. return List.__mul__(other, self)
  768. def __add__(self, other):
  769. result = List()
  770. result.l = self.l[:] + other.l[:]
  771. return result
  772. # Quickie tests. Should use doctest instead.
  773. if __name__ == '__main__':
  774. assert str(List("foo bar") * "/baz") == "['foo/baz', 'bar/baz']"
  775. assert repr("foo/" * List("bar baz")) == "__main__.List('foo/bar foo/baz')"
  776. print 'tests passed'