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