@@ -10,7 +10,7 @@ import sys
1010from argparse import ArgumentParser , ArgumentTypeError
1111from collections import defaultdict
1212from logging import _levelNames as logLevels , exception , warning , info , basicConfig
13- from os import path
13+ from os import path , makedirs
1414from time import time
1515
1616from java2python .compiler import Module , buildAST , transformAST
@@ -48,11 +48,36 @@ def runMain(options):
4848 if options .profile :
4949 import cProfile , pstats
5050 prof = cProfile .Profile ()
51- prof .runcall (runTransform , options )
51+ prof .runcall (runOneOrMany , options )
5252 stats = pstats .Stats (prof , stream = sys .stderr )
5353 stats .strip_dirs ().sort_stats ('cumulative' )
5454 stats .print_stats ().print_callers ()
5555 return 0
56+ else :
57+ return runOneOrMany (options )
58+
59+ def runOneOrMany (options ):
60+ """ Runs our main transformer with each of the input files. """
61+ infile , outfile = options .inputfile , options .outputfile
62+
63+ if infile and not isinstance (infile , file ) and path .isdir (infile ):
64+ if outfile and not isinstance (outfile , file ) and not path .isdir (outfile ):
65+ warning ('Must specify output directory or stdout when using input directory.' )
66+ return 2
67+ def walker (arg , dirname , files ):
68+ for name in [name for name in files if name .endswith ('.java' )]:
69+ fullname = path .join (dirname , name )
70+ options .inputfile = fullname
71+ if outfile and outfile != '-' and not isinstance (outfile , file ):
72+ full = path .abspath (path .join (outfile , fullname ))
73+ head , tail = path .split (full )
74+ tail = path .splitext (tail )[0 ] + '.py'
75+ if not path .exists (head ):
76+ makedirs (head )
77+ options .outputfile = path .join (head , tail )
78+ runTransform (options )
79+ path .walk (infile , walker , None )
80+ return 0
5681 else :
5782 return runTransform (options )
5883
@@ -66,7 +91,7 @@ def runTransform(options):
6691 if options .inputfile and not isinstance (options .inputfile , file ):
6792 filein = options .inputfile
6893 if options .outputfile and not isinstance (options .outputfile , file ):
69- fileout = path . basename ( options .outputfile )
94+ fileout = options .outputfile
7095 elif fileout != filedefault :
7196 fileout = '%s.py' % (path .splitext (filein )[0 ])
7297
0 commit comments