fe5ea2fd3d51728c072d9be5fba42774a4bf3fd1
[scons.git] / bin / import-test.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # tree2test.py - turn a directory tree into TestSCons code
6 #
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:
12 #
13 #       test.subdir(['sub1'],
14 #                   ['sub1', 'sub2'])
15 #
16 #       test.write(['sub1', 'file1'], """\
17 #       contents of file1
18 #       """)
19 #
20 #       test.write(['sub1', 'sub2', 'file2'], """\
21 #       contents of file2
22 #       """)
23 #
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.
26 #
27
28 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
29
30 import os.path
31 import sys
32
33 directory = sys.argv[1]
34
35 Top = None
36 TopPath = None
37
38 class Dir:
39     def __init__(self, path):
40         self.path = path
41         self.entries = {}
42     def call_for_each_entry(self, func):
43         entries = self.entries
44         names = entries.keys()
45         names.sort()
46         for name in names:
47             func(name, entries[name])
48
49 def lookup(dirname):
50     global Top, TopPath
51     if not Top:
52         Top = Dir([])
53         TopPath = dirname + os.sep
54         return Top
55     dirname = dirname.replace(TopPath, '')
56     dirs = dirname.split(os.sep)
57     t = Top
58     for d in dirs[:-1]:
59         t = t.entries[d]
60     node = t.entries[dirs[-1]] = Dir(dirs)
61     return node
62
63 def make_nodes(arg, dirname, fnames):
64     dir = lookup(dirname)
65     for f in fnames:
66         dir.entries[f] = None
67
68 def collect_dirs(l, dir):
69     if dir.path:
70         l.append(dir.path)
71     def recurse(n, d):
72         if d:
73             collect_dirs(l, d)
74     dir.call_for_each_entry(recurse)
75
76 def print_files(dir):
77     def print_a_file(n, d):
78         if not d:
79             l = dir.path + [n]
80             sys.stdout.write('\ntest.write(%s, """\\\n' % l)
81             p = os.path.join(directory, *l)
82             sys.stdout.write(open(p, 'r').read())
83             sys.stdout.write('""")\n')
84     dir.call_for_each_entry(print_a_file)
85
86     def recurse(n, d):
87         if d:
88             print_files(d)
89     dir.call_for_each_entry(recurse)
90
91 os.path.walk(directory, make_nodes, None)
92
93 subdir_list = []
94 collect_dirs(subdir_list, Top)
95 subdir_list = [ str(l) for l in subdir_list ]
96 sys.stdout.write('test.subdir(' + ',\n            '.join(subdir_list) + ')\n')
97
98 print_files(Top)
99
100 # Local Variables:
101 # tab-width:4
102 # indent-tabs-mode:nil
103 # End:
104 # vim: set expandtab tabstop=4 shiftwidth=4: