File: jython_perl

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (190 lines) | stat: -rw-r--r-- 5,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl -w
#
# Used to start the Jython interpreter.
#
# Copyright (C) 2004-2005, Ben Burton <bab@debian.org>
# Released under the GPL.

use strict;
use Text::ParseWords;

# Debian constants.
#
my $jythonHome = '/usr/share/jython';
my $debianJNIDir = '/usr/lib/jni';
my $defaultRuntime = '/usr/bin/java';

# Establish the user's home directory.
#
my $home = $ENV{HOME} || $ENV{LOGDIR};
$home or bail("I cannot determine your home directory.",
	"Please set \$HOME and try again.");

# Decide upon the Java runtime.
#
my $javaRuntime = undef;
if ($ENV{JAVA}) {
	$javaRuntime = $ENV{JAVA};
} elsif (-x $defaultRuntime) {
	$javaRuntime = $defaultRuntime;
} else {
	&warn("The default Java runtime ($defaultRuntime) cannot be executed.",
		"Since every Java runtime in Debian must supply an alternative",
		"for $defaultRuntime, your system is almost certainly misconfigured.");
	if ($ENV{JAVA_HOME}) {
		&warn("Using \$JAVA_HOME/bin/java instead.");
		$javaRuntime = "$ENV{JAVA_HOME}/bin/java";
	} else {
		bail("I cannot find an appropriate Java runtime to use instead.",
			"Please set \$JAVA to the appropriate binary and try again.");
	}
}

# Decide upon and export the classpath.
# Add in $CLASSPATH as well as other useful libraries if they exist.
#
my $classpath = '/usr/share/java/jython.jar';
appendClasspath($ENV{CLASSPATH});
appendJar('/usr/share/java/antlr3-runtime.jar');
appendJar('/usr/share/java/asm3.jar');
appendJar('/usr/share/java/asm3-commons.jar');
appendJar('/usr/share/java/asm3-util.jar');
appendJar('/usr/share/java/jna.jar');
appendJar('/usr/share/java/jna-posix.jar');
appendJar('/usr/share/java/libconstantine-java.jar');
appendJar('/usr/share/java/xercesImpl.jar');
appendJar('/usr/share/java/jline.jar');
appendJar('/usr/share/java/servlet-api-2.5.jar');
appendJar('/usr/share/java/libreadline-java.jar');
appendJar('/usr/share/java/mysql.jar');
appendJar('/usr/share/java/postgresql.jar');

$ENV{CLASSPATH} = $classpath;

# Decide upon the python path.
my $jythonPath = "/usr/lib/site-python";

# Set up the cache directory.
#
my $cacheDir = "$home/.jython-cache";
if (-e $cacheDir and ! -d $cacheDir) {
	# The expected cache directory exists but is not a directory.
	# Use a temporary directory instead.
	$cacheDir = `mktemp -dt jython-cache.XXXXXX` or
		bail("Could not create temporary cache directory.");
	chomp $cacheDir;
}
if (! -e $cacheDir) {
	# Create a new cache directory.
	mkdir $cacheDir or bail("Could not create cache directory $cacheDir.");
}

# We will build up a JNI library path from various places.
#
my $jniPath = '';

# Run through $JAVA_OPTIONS and extract the java arguments.
# Any JNI library path arguments will be removed and put into $jniPath
# instead.
#
my @javaArgs = ();

if ($ENV{JAVA_OPTIONS}) {
	foreach my $arg (shellwords($ENV{JAVA_OPTIONS})) {
		if ($arg =~ /^-Djava\.library\.path=(.+)$/) {
			$jniPath = $jniPath ? "$jniPath:$1" : "$1";
		} else {
			push @javaArgs, $arg;
		}
	}
}

# Run through the command-line options and extract the jython arguments.
# Any -Djava... arguments will be reassigned as java arguments instead
# (unless it's for the JNI library path, in which case it will go to
# $jniPath).
#
my @jythonArgs = ();

foreach my $arg (@ARGV) {
	if ($arg =~ /^-Djava\.library\.path=(.+)$/) {
		$jniPath = $jniPath ? "$jniPath:$1" : "$1";
	} elsif ($arg =~ /^-Djava/) {
		push @javaArgs, $arg;
	} else {
		push @jythonArgs, $arg;
	}
}

# Finish by adding the debian JNI directory to $jniPath if it's not
# already present, since some popular non-free JVMs still don't support
# it (grumble).
#
if ($jniPath !~ /(^|:)$debianJNIDir($|:)/) {
	$jniPath = $jniPath ? "$jniPath:$debianJNIDir" : $debianJNIDir;
}

# Invoke jython!
#
my @fullCommandLine = ( $javaRuntime );
push @fullCommandLine, @javaArgs;
push @fullCommandLine, "-Djava.library.path=$jniPath";
push @fullCommandLine, "-Dpython.home=$jythonHome";
push @fullCommandLine, "-Dpython.path=$jythonPath";
push @fullCommandLine, "-Dpython.executable=$0";
push @fullCommandLine, "-Dpython.cachedir=$cacheDir";
push @fullCommandLine, "-Dpython.console=org.python.util.ReadlineConsole";
push @fullCommandLine, "-Dpython.console.readlinelib=Editline";
$ENV{CALLED_FROM_JYTHONC} and
	push @fullCommandLine, "-Dpython.jythonc.compiler=$ENV{JAVAC}";
push @fullCommandLine, 'org.python.util.jython';
push @fullCommandLine, @jythonArgs;
$ENV{JYTHON_WRAPPER_DEBUG} and print "$ENV{CLASSPATH}\n\n@fullCommandLine\n\n";
exec @fullCommandLine or exit(1);

# All done.
#
exit 0;

# Helper routines:

# Print the given warning messages to stderr, one per line.
# The messages should not contain trailing newlines.
#
sub warn {
	foreach (@_) {
		print STDERR "WARNING: $_\n";
	}
}

# Print the given error messages to stderr, one per line, and exit.
# The messages should not contain trailing newlines.
#
sub bail {
	foreach (@_) {
		print STDERR "ERROR: $_\n";
	}
	exit 1;
}

# Append the given string to the classpath if it's non-empty.
#
sub appendClasspath {
	my $string = shift;
	$string and $classpath = "$classpath:$string";
}

# Append the given jar to the classpath if it exists.
#
sub appendJar {
	my $jar = shift;
	-e $jar and $classpath = "$classpath:$jar";
}

# Append the given directory to the python path if it exists.
#
sub appendPythonDir {
	my $dir = shift;
	-d $dir and $jythonPath = "$jythonPath:$dir";
}