1
2
3
4
5
6
7
8 import os, shlex
9 import platform
10 import subprocess
11 import exceptions
12 import logging
13
14
15 DEFAULT_DEBUG = "-Xrunjdwp:server=y,transport=dt_socket,address=8787,suspend=n"
16
18 try:
19 p = subprocess.Popen([command[0],"-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
20 std = p.communicate()
21 rc = p.wait()
22 if rc == 0:
23 return
24 except:
25 pass
26
27 raise exceptions.Exception("Java could not be found. (Executable=%s)" % command[0])
28
30 if os.environ.has_key(key):
31 env[key] = os.environ[key]
32
33 -def cmd(args,\
34 java = "java",\
35 xargs = None,\
36 chdir = None,\
37 debug = None,\
38 debug_string = DEFAULT_DEBUG):
39 """
40 Defines the command to be used by run or popen.
41 """
42
43 if isinstance(java,str):
44 command = [java]
45 else:
46 command = list(java)
47
48 if isinstance(xargs,str):
49 xargs = shlex.split(xargs)
50
51
52
53 command += [ "-Dlog4j.configuration=%s" % os.path.join("etc", "log4j.xml") ]
54
55
56 if xargs != None:
57 command += xargs
58
59
60 if debug == None:
61 if os.environ.has_key("DEBUG"):
62 command += ["-Xdebug",debug_string]
63 else:
64 if debug:
65 command += ["-Xdebug",debug_string]
66
67
68 if os.environ.has_key("JAVA_OPTS"):
69 command += shlex.split(os.environ["JAVA_OPTS"])
70
71
72 command += [ "-Djava.awt.headless=true" ]
73
74
75 command += args
76
77 return command
78
79 -def run(args,\
80 use_exec = False,\
81 java = "java",\
82 xargs = None,\
83 chdir = None,\
84 debug = None,\
85 debug_string = DEFAULT_DEBUG):
86 """
87 Execute a Java process, either via subprocess waiting for the process to finish and
88 returning the output or if use_exec is True, via os.execvpe with the current environment.
89
90 -X style arguments for the Java process can be set either via the xargs argument
91 or if unset, the JAVA_OPTS environment variable will be checked. Note: shlex.split()
92 is called on the JAVA_OPTS value and so bash-style escaping can be used to protect
93 whitespaces.
94
95 Debugging can more simply be turned on by passing True for the debug argument.
96 If more control over the debugging configuration is needed, pass debug_string.
97 """
98 command = cmd(args, java, xargs, chdir, debug, debug_string)
99 check_java(command)
100 if use_exec:
101 env = os.environ
102 if chdir:
103 os.chdir(chdir)
104 if platform.system() == "Windows":
105 command = [ "\"%s\"" % i for i in command ]
106 os.execvpe(command[0], command, env)
107 else:
108 os.execvpe(command[0], command, env)
109 else:
110 p = popen(args, java, xargs, chdir, debug, debug_string)
111 output = p.communicate()[0]
112 return output
113
114 -def popen(args,\
115 java = "java",\
116 xargs = None,\
117 chdir = None,\
118 debug = None,\
119 debug_string = DEFAULT_DEBUG,\
120 stdout = subprocess.PIPE,\
121 stderr = subprocess.PIPE):
122 """
123 Creates a subprocess.Popen object and returns it. Uses cmd() internally to create
124 the Java command to be executed. This is the same logic as run(use_exec=False) but
125 the Popen is returned rather than the stdout.
126 """
127 command = cmd(args, java, xargs, chdir, debug, debug_string)
128 check_java(command)
129 if not chdir:
130 chdir = os.getcwd()
131 return subprocess.Popen(command, stdout=stdout, stderr=stderr, cwd=chdir, env = os.environ)
132