Small docstring update for vars.Add
[scons.git] / bin / import-test.py
blobd9790175087749699d3e9b315db2c04bffb5ea38
1 #!/usr/bin/env python
3 # __COPYRIGHT__
5 # tree2test.py - turn a directory tree into TestSCons code
7 # A quick script for importing directory hierarchies containing test
8 # cases that people supply (typically in a .zip or .tar.gz file) into a
9 # TestSCons.py script. No error checking or options yet, it just walks
10 # the first command-line argument (assumed to be the directory containing
11 # the test case) and spits out code looking like the following:
13 # test.subdir(['sub1'],
14 # ['sub1', 'sub2'])
16 # test.write(['sub1', 'file1'], """\
17 # contents of file1
18 # """)
20 # test.write(['sub1', 'sub2', 'file2'], """\
21 # contents of file2
22 # """)
24 # There's no massaging of contents, so any files that themselves contain
25 # """ triple-quotes will need to have their contents edited by hand.
28 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
30 import os.path
31 import sys
33 directory = sys.argv[1]
35 Top = None
36 TopPath = None
38 class Dir:
39 def __init__(self, path):
40 self.path = path
41 self.entries = {}
42 def call_for_each_entry(self, func):
43 for name in sorted(self.entries.keys()):
44 func(name, self.entries[name])
46 def lookup(dirname):
47 global Top, TopPath
48 if not Top:
49 Top = Dir([])
50 TopPath = dirname + os.sep
51 return Top
52 dirname = dirname.replace(TopPath, '')
53 dirs = dirname.split(os.sep)
54 t = Top
55 for d in dirs[:-1]:
56 t = t.entries[d]
57 node = t.entries[dirs[-1]] = Dir(dirs)
58 return node
60 def collect_dirs(l, dir):
61 if dir.path:
62 l.append(dir.path)
63 def recurse(n, d):
64 if d:
65 collect_dirs(l, d)
66 dir.call_for_each_entry(recurse)
68 def print_files(dir):
69 def print_a_file(n, d):
70 if not d:
71 l = dir.path + [n]
72 sys.stdout.write('\ntest.write(%s, """\\\n' % l)
73 p = os.path.join(directory, *l)
74 sys.stdout.write(open(p, 'r').read())
75 sys.stdout.write('""")\n')
76 dir.call_for_each_entry(print_a_file)
78 def recurse(n, d):
79 if d:
80 print_files(d)
81 dir.call_for_each_entry(recurse)
83 for dirpath, dirnames, filenames in os.walk(directory):
84 dir = lookup(dirpath)
85 for f in filenames:
86 dir.entries[f] = None
88 subdir_list = []
89 collect_dirs(subdir_list, Top)
90 subdir_list = [ str(l) for l in subdir_list ]
91 sys.stdout.write('test.subdir(' + ',\n '.join(subdir_list) + ')\n')
93 print_files(Top)
95 # Local Variables:
96 # tab-width:4
97 # indent-tabs-mode:nil
98 # End:
99 # vim: set expandtab tabstop=4 shiftwidth=4: