Skip to content

Commit bfc8dec

Browse files
author
Troy Melhase
committed
Adds support for translating entire directories. See issue natural#1.
1 parent f62a9e7 commit bfc8dec

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

bin/j2py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import sys
1010
from argparse import ArgumentParser, ArgumentTypeError
1111
from collections import defaultdict
1212
from logging import _levelNames as logLevels, exception, warning, info, basicConfig
13-
from os import path
13+
from os import path, makedirs
1414
from time import time
1515

1616
from 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

doc/usage.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ page.
3333

3434
Read from the given file. Specify `-` for `stdin`. If not
3535
given the command will read from `stdin`.
36+
37+
If `[INPUT]` is a directory, the script will walk the directory looking for
38+
files named `.java`, and transform each one. If `[OUTPUT]` is also a
39+
directory, the directory structure of `[INPUT]` will be recreated below it.
40+
41+
If `[INPUT]` is a directory, `[OUTPUT]` must also be a directory, or it may
42+
be `-` or unspecified.
3643

3744
* `[OUTPUT]`
3845

0 commit comments

Comments
 (0)