/test/Actions/pre-post.py

https://bitbucket.org/scons/scons/ · Python · 213 lines · 143 code · 39 blank · 31 comment · 0 complexity · 3019afb17eaef6e5a45ebadf546670b0 MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # __COPYRIGHT__
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  17. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. #
  24. # This test exercises the AddPreAction() and AddPostAction() API
  25. # functions, which add pre-build and post-build actions to nodes.
  26. #
  27. __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
  28. import os
  29. import stat
  30. import TestSCons
  31. _exe = TestSCons._exe
  32. _python_ = TestSCons._python_
  33. test = TestSCons.TestSCons()
  34. test.subdir('work1', 'work2', 'work3', 'work4')
  35. test.write(['work1', 'SConstruct'], """
  36. import os.path
  37. import stat
  38. env = Environment(XXX='bar%(_exe)s')
  39. def before(env, target, source):
  40. a=str(target[0])
  41. f=open(a, "wb")
  42. f.write("Foo\\n")
  43. f.close()
  44. os.chmod(a, os.stat(a)[stat.ST_MODE] | stat.S_IXUSR)
  45. f=open("before.txt", "ab")
  46. f.write(os.path.splitext(str(target[0]))[0] + "\\n")
  47. f.close()
  48. def after(env, target, source):
  49. t = str(target[0])
  50. a = "after_" + t
  51. fin = open(t, "rb")
  52. fout = open(a, "wb")
  53. fout.write(fin.read())
  54. fout.close()
  55. fin.close()
  56. os.chmod(a, os.stat(a)[stat.ST_MODE] | stat.S_IXUSR)
  57. foo = env.Program(source='foo.c', target='foo')
  58. AddPreAction(foo, before)
  59. AddPostAction('foo%(_exe)s', after)
  60. bar = env.Program(source='bar.c', target='bar')
  61. env.AddPreAction('$XXX', before)
  62. env.AddPostAction('$XXX', after)
  63. """ % locals())
  64. test.write(['work1', 'foo.c'], r"""
  65. #include <stdio.h>
  66. int main(void)
  67. {
  68. printf("foo.c\n");
  69. return 0;
  70. }
  71. """)
  72. test.write(['work1', 'bar.c'], r"""
  73. #include <stdio.h>
  74. int main(void)
  75. {
  76. printf("bar.c\n");
  77. return 0;
  78. }
  79. """)
  80. test.run(chdir='work1', arguments='.')
  81. test.run(program=test.workpath('work1', 'foo'+ _exe), stdout="foo.c\n")
  82. test.run(program=test.workpath('work1', 'bar'+ _exe), stdout="bar.c\n")
  83. test.must_match(['work1', 'before.txt'], "bar\nfoo\n")
  84. after_foo_exe = test.workpath('work1', 'after_foo' + _exe)
  85. test.run(program=after_foo_exe, stdout="foo.c\n")
  86. after_bar_exe = test.workpath('work1', 'after_bar' + _exe)
  87. test.run(program=after_bar_exe, stdout="bar.c\n")
  88. test.write(['work2', 'SConstruct'], """\
  89. def b(target, source, env):
  90. open(str(target[0]), 'wb').write(env['X'] + '\\n')
  91. env1 = Environment(X='111')
  92. env2 = Environment(X='222')
  93. B = Builder(action = b, env = env1, multi=1)
  94. print("B =", B)
  95. print("B.env =", B.env)
  96. env1.Append(BUILDERS = {'B' : B})
  97. env2.Append(BUILDERS = {'B' : B})
  98. env3 = env1.Clone(X='333')
  99. print("env1 =", env1)
  100. print("env2 =", env2)
  101. print("env3 =", env3)
  102. f1 = env1.B(File('file1.out'), [])
  103. f2 = env2.B('file2.out', [])
  104. f3 = env3.B('file3.out', [])
  105. def do_nothing(env, target, source):
  106. pass
  107. AddPreAction(f2[0], do_nothing)
  108. AddPostAction(f3[0], do_nothing)
  109. print("f1[0].builder =", f1[0].builder)
  110. print("f2[0].builder =", f2[0].builder)
  111. print("f3[0].builder =", f3[0].builder)
  112. print("f1[0].env =", f1[0].env)
  113. print("f2[0].env =", f2[0].env)
  114. print("f3[0].env =", f3[0].env)
  115. """)
  116. test.run(chdir='work2', arguments = '.')
  117. test.must_match(['work2', 'file1.out'], "111\n")
  118. test.must_match(['work2', 'file2.out'], "222\n")
  119. test.must_match(['work2', 'file3.out'], "333\n")
  120. test.write(['work3', 'SConstruct'], """\
  121. def pre(target, source, env):
  122. pass
  123. def post(target, source, env):
  124. pass
  125. def build(target, source, env):
  126. open(str(target[0]), 'wb').write('build()\\n')
  127. env = Environment()
  128. AddPreAction('dir', pre)
  129. AddPostAction('dir', post)
  130. env.Command('dir/file', [], build)
  131. """)
  132. test.run(chdir = 'work3', arguments = 'dir/file', stdout=test.wrap_stdout("""\
  133. pre(["dir"], [])
  134. post(["dir"], [])
  135. build(["%s"], [])
  136. """ % os.path.join('dir', 'file')))
  137. test.must_match(['work3', 'dir', 'file'], "build()\n")
  138. test.write(['work4', 'build.py'], """\
  139. import sys
  140. outfp = open(sys.argv[1], 'wb')
  141. for f in sys.argv[2:]:
  142. outfp.write(open(f, 'rb').read())
  143. outfp.close()
  144. """)
  145. test.write(['work4', 'SConstruct'], """\
  146. def pre_action(target, source, env):
  147. open(str(target[0]), 'ab').write('pre %%s\\n' %% source[0])
  148. def post_action(target, source, env):
  149. open(str(target[0]), 'ab').write('post %%s\\n' %% source[0])
  150. env = Environment()
  151. o = env.Command(['pre-post', 'file.out'],
  152. 'file.in',
  153. r'%(_python_)s build.py ${TARGETS[1]} $SOURCE')
  154. env.AddPreAction(o, pre_action)
  155. env.AddPostAction(o, post_action)
  156. """ % locals())
  157. test.write(['work4', 'file.in'], "file.in\n")
  158. test.run(chdir='work4', arguments='.')
  159. test.must_match(['work4', 'file.out'], "file.in\n")
  160. test.must_match(['work4', 'pre-post'], "pre file.in\npost file.in\n")
  161. test.pass_test()
  162. test.pass_test()
  163. # Local Variables:
  164. # tab-width:4
  165. # indent-tabs-mode:nil
  166. # End:
  167. # vim: set expandtab tabstop=4 shiftwidth=4: