public static void main(String[] args) {
// Parse the command line options
CommandLineOptions opts = new CommandLineOptions();
if (!opts.parse(args)) {
if (opts.version) {
System.err.println(InteractiveConsole.getDefaultBanner());
System.exit(0);
}
System.err.println(usage);
int exitcode = opts.help ? 0 : -1;
System.exit(exitcode);
}
// Setup the basic python system state from these options
PySystemState.initialize(System.getProperties(),
opts.properties, opts.argv);
if (opts.notice) {
System.err.println(InteractiveConsole.getDefaultBanner());
}
// Now create an interpreter
InteractiveConsole interp = null;
try {
interp = (InteractiveConsole) Class.forName(
PySystemState.registry.getProperty("python.console",
"org.python.util.InteractiveConsole")).newInstance();
} catch (Exception e) {
interp = new InteractiveConsole();
}
//System.err.println("interp");
PyModule mod = imp.addModule("__main__");
interp.setLocals(mod.__dict__);
//System.err.println("imp");
if (Options.importSite) {
try {
imp.load("site");
} catch (PyException pye) {
if (!Py.matchException(pye, Py.ImportError)) {
System.err.println("error importing site");
Py.printException(pye);
System.exit(-1);
}
}
}
if (opts.command != null) {
try {
interp.exec(opts.command);
} catch (Throwable t) {
Py.printException(t);
}
}
// was there a filename on the command line?
if (opts.filename != null) {
String path = new java.io.File(opts.filename).getParent();
if (path == null)
path = "";
Py.getSystemState().path.insert(0, new PyString(path));
if (opts.jar) {
runJar(opts.filename);
} else if (opts.filename.equals("-")) {
try {
interp.execfile(System.in, "< stdin >");
} catch (Throwable t) {
Py.printException(t);
}
} else {
try {
interp.execfile(opts.filename);
} catch (Throwable t) {
Py.printException(t);
}
}
}
else {
// if there was no file name on the command line, then "" is
// the first element on sys.path. This is here because if
// there /was/ a filename on the c.l., and say the -i option
// was given, sys.path[0] will have gotten filled in with the
// dir of the argument filename.
Py.getSystemState().path.insert(0, new PyString(""));
}
if (opts.interactive) {
try {
interp.interact(null);
} catch (Throwable t) {
Py.printException(t);
}
}
}
|