#!/usr/bin/env python## __COPYRIGHT__## 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__="__FILE__ __REVISION__ __DATE__ __DEVELOPER__"importos.pathimportsysdirectory=sys.argv[1]Top=NoneTopPath=NoneclassDir:def__init__(self,path):self.path=pathself.entries={}defcall_for_each_entry(self,func):entries=self.entriesnames=entries.keys()names.sort()fornameinnames:func(name,entries[name])deflookup(dirname):globalTop,TopPathifnotTop:Top=Dir([])TopPath=dirname+os.sepreturnTopdirname=dirname.replace(TopPath,'')dirs=dirname.split(os.sep)t=Topfordindirs[:-1]:t=t.entries[d]node=t.entries[dirs[-1]]=Dir(dirs)returnnodedefmake_nodes(arg,dirname,fnames):dir=lookup(dirname)forfinfnames:dir.entries[f]=Nonedefcollect_dirs(l,dir):ifdir.path:l.append(dir.path)defrecurse(n,d):ifd:collect_dirs(l,d)dir.call_for_each_entry(recurse)defprint_files(dir):defprint_a_file(n,d):ifnotd: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)defrecurse(n,d):ifd:print_files(d)dir.call_for_each_entry(recurse)os.path.walk(directory,make_nodes,None)subdir_list=[]collect_dirs(subdir_list,Top)subdir_list=[str(l)forlinsubdir_list]sys.stdout.write('test.subdir('+',\n '.join(subdir_list)+')\n')print_files(Top)