summaryrefslogtreecommitdiff
path: root/bin/import-test.py
blob: 4fc4f06f496e6de98c82f5e5492a56372c1e6870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
#
# Copyright (c) 2001 - 2019 The SCons Foundation
#
# tree2test.py - turn a directory tree into TestSCons code
#
# A quick script for importing directory hierarchies containing test
# cases that people supply (typically in a .zip or .tar.gz file) into a
# TestSCons.py script.  No error checking or options yet, it just walks
# the first command-line argument (assumed to be the directory containing
# the test case) and spits out code looking like the following:
#
#       test.subdir(['sub1'],
#                   ['sub1', 'sub2'])
#
#       test.write(['sub1', 'file1'], """\
#       contents of file1
#       """)
#
#       test.write(['sub1', 'sub2', 'file2'], """\
#       contents of file2
#       """)
#
# There's no massaging of contents, so any files that themselves contain
# """ triple-quotes will need to have their contents edited by hand.
#

__revision__ = "bin/import-test.py 103260fce95bf5db1c35fb2371983087d85dd611 2019-07-13 18:25:30 bdbaddog"

import os.path
import sys

directory = sys.argv[1]

Top = None
TopPath = None

class Dir(object):
    def __init__(self, path):
        self.path = path
        self.entries = {}
    def call_for_each_entry(self, func):
        for name in sorted(self.entries.keys()):
            func(name, self.entries[name])

def lookup(dirname):
    global Top, TopPath
    if not Top:
        Top = Dir([])
        TopPath = dirname + os.sep
        return Top
    dirname = dirname.replace(TopPath, '')
    dirs = dirname.split(os.sep)
    t = Top
    for d in dirs[:-1]:
        t = t.entries[d]
    node = t.entries[dirs[-1]] = Dir(dirs)
    return node

def collect_dirs(l, dir):
    if dir.path:
        l.append(dir.path)
    def recurse(n, d):
        if d:
            collect_dirs(l, d)
    dir.call_for_each_entry(recurse)

def print_files(dir):
    def print_a_file(n, d):
        if not d:
            l = dir.path + [n]
            sys.stdout.write('\ntest.write(%s, """\\\n' % l)
            p = os.path.join(directory, *l)
            sys.stdout.write(open(p, 'r').read())
            sys.stdout.write('""")\n')
    dir.call_for_each_entry(print_a_file)

    def recurse(n, d):
        if d:
            print_files(d)
    dir.call_for_each_entry(recurse)

for dirpath, dirnames, filenames in os.walk(directory):
    dir = lookup(dirpath)
    for f in fnames:
        dir.entries[f] = None

subdir_list = []
collect_dirs(subdir_list, Top)
subdir_list = [ str(l) for l in subdir_list ]
sys.stdout.write('test.subdir(' + ',\n            '.join(subdir_list) + ')\n')

print_files(Top)

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: