load_dir.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python
  2. """
  3. Traverses a directory and output the code that would create the same directory
  4. structure during testing. Assumes that the instance of Tester is called 't'.
  5. """
  6. from __future__ import print_function
  7. import sys
  8. import os
  9. import stat
  10. import string
  11. def usage():
  12. print("usage: load_dir.py directory")
  13. def remove_first_component(path):
  14. result = [path]
  15. while 1:
  16. s = os.path.split(result[0])
  17. if not s[0]:
  18. break
  19. result[:1] = list(s)
  20. return os.path.join(*result[1:])
  21. def create_file(arg, dirname, fnames):
  22. for n in fnames:
  23. path = os.path.join(dirname, n)
  24. if not os.path.isdir(path):
  25. print("t.write(\"%s\", \"\"\"" % (remove_first_component(path),),)
  26. f = open(path, "r")
  27. for l in f:
  28. print(l)
  29. print('\n""")\n')
  30. header = """#!/usr/bin/python
  31. # Copyright (C) FILL SOMETHING HERE 2005.
  32. # Distributed under the Boost Software License, Version 1.0. (See
  33. # accompanying file LICENSE.txt or copy at
  34. # https://www.bfgroup.xyz/b2/LICENSE.txt)
  35. import BoostBuild
  36. t = BoostBuild.Tester()
  37. """
  38. footer = """
  39. t.run_build_system()
  40. t.expect_addition("bin/$toolset/debug*/FILL_SOME_HERE.exe")
  41. t.cleanup()
  42. """
  43. def main():
  44. if len(sys.argv) != 2:
  45. usage()
  46. else:
  47. path = sys.argv[1]
  48. if not os.access(path, os.F_OK):
  49. print("Path '%s' does not exist" % (path,))
  50. sys.exit(1)
  51. if not os.path.isdir(path):
  52. print("Path '%s' is not a directory" % (path,))
  53. print(header)
  54. for root, _, files in os.walk(path):
  55. create_file(None, root, files)
  56. print(footer)
  57. if __name__ == '__main__':
  58. main()