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