Latex_multibib / bin /

#!/usr/bin/env python
# - 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
# 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__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os.path
import sys

directory = sys.argv[1]

Top = None
TopPath = None

class Dir:
    def __init__(self, path):
        self.path = path
        self.entries = {}
    def call_for_each_entry(self, func):
        entries = self.entries
        names = entries.keys()
        for name in names:
            func(name, 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 make_nodes(arg, dirname, fnames):
    dir = lookup(dirname)
    for f in fnames:
        dir.entries[f] = None

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

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())

    def recurse(n, d):
        if d:

os.path.walk(directory, make_nodes, 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')

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.