Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Hadoop uses a java implementation of Python: Jython.
4This provides provides helper around that.
5"""
7import os
8import sys
9import urllib
10import urllib.request
11from pyquickhelper.loghelper import run_cmd, noLOG
13JYTHON_VERSION = "2.7.1-rc2"
16def download_java_standalone(version=JYTHON_VERSION):
17 """
18 download the standalone jython
19 if it does not exists, we should version ``JYTHON_VERSION``
20 by default in order to fit the cluster's version
22 @param version ``JYTHON_VERSION`` or ...
23 @return path to it
24 """
25 dest = "jython-standalone-%s.jar" % version
26 url = "https://search.maven.org/remotecontent?filepath=org/python/jython-standalone/{1}/{0}".format(
27 dest, version)
28 this = os.path.abspath(os.path.dirname(__file__))
29 final = os.path.join(this, dest)
30 if os.path.exists(final):
31 return final
33 try:
34 u = urllib.request.urlopen(url)
35 except urllib.error.HTTPError as e:
36 raise Exception(
37 "Unable to download file from '{0}'.".format(url)) from e
38 alls = u.read()
39 u.close()
41 with open(final, "wb") as f:
42 f.write(alls)
44 return final
47def get_java_path():
48 """
49 returns the java path
51 :raises FileNotFoundError: if java is not found
52 """
53 if "JAVA_HOME" in os.environ:
54 java = os.environ["JAVA_HOME"]
55 else:
56 if sys.platform.startswith("win"):
57 location = r'C:\Program Files\Java'
58 if not os.path.exists(location):
59 raise FileNotFoundError(
60 "path {0} does not exists, you need to install java.\nGo to " +
61 "http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html")
62 pa = os.listdir(location)
63 if len(pa) == 0:
64 raise FileNotFoundError(
65 "path {0} does not exists, you need to install java.\nGo to " +
66 "http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html")
67 pa = [os.path.join(location, _) for _ in pa]
68 for p in pa:
69 if os.path.isdir(p) and os.path.exists(p):
70 return p
71 raise FileNotFoundError(("path '{0}' does not exists, you need to install java.\nGo to \n" +
72 "http://www.oracle.com/technetwork/java/javase/downloads" +
73 "/jre8-downloads-2133155.html").format(location))
74 else:
75 java = ""
76 return java
79def get_java_cmd():
80 """
81 return the java path
83 @return java cmd
84 """
85 if sys.platform.startswith("win"):
86 java = get_java_path()
87 cmd = os.path.join(java, 'bin', 'java.exe')
88 if not os.path.exists(cmd):
89 raise FileNotFoundError(cmd)
90 return '"{0}"'.format(cmd)
91 else:
92 return "java"
95def is_java_installed(fLOG=noLOG):
96 """
97 Checks if :epkg:`java` is installed.
99 @return boolean
100 """
101 if sys.platform.startswith("win"):
102 cmd = get_java_cmd() + " -showversion"
103 out, err = run_cmd(cmd, wait=True, log_error=False)
104 fLOG("OUT:\n", out)
105 fLOG("ERR:\n", err)
106 return "Java(TM)" in err
107 else:
108 cmd = get_java_cmd() + " -showversion"
109 out, err = run_cmd(cmd, wait=True, log_error=False)
110 fLOG("OUT:\n", out)
111 fLOG("ERR:\n", err)
112 return "OpenJDK Runtime Environment" in err
115def get_jython_jar():
116 """
117 This function assumes a file ``jython-standalone-x.x.x.jar``
118 is present in this directory, the function returns the file.
120 @return absolute path
121 """
122 this = os.path.abspath(os.path.dirname(__file__))
123 files = [os.path.join(this, _) for _ in os.listdir(this)]
124 files = [_ for _ in files if "jython-standalone" in _]
125 if len(files) == 0:
126 raise FileNotFoundError("no jython-standalone*.jar found in " + this)
127 if len(files) != 1:
128 raise FileNotFoundError(
129 "more than one jython-standalone*.jar found in " +
130 this +
131 "\n:" +
132 "\n".join(files))
133 return files[0]
136def run_jython(pyfile,
137 argv=None,
138 jython_path=None,
139 sin=None,
140 timeout=None,
141 fLOG=noLOG):
142 """
143 runs a jython script and returns the standard output and error
145 @param pyfile jython file
146 @param argv arguments to sned to the command line
147 @param jython_path path to jython standalone
148 @param sin data to send to the standard input
149 @param timeout timeout
150 @param fLOG logging function
151 @return out, err
153 If *jython_path* is None, the function looks into this directory.
154 """
155 if jython_path is None:
156 jython_path = get_jython_jar()
158 def clean(i, p):
159 "local function"
160 if i == 0:
161 return p
162 if '"' in p:
163 p = p.replace('"', '\\"')
164 if " " in p:
165 p = '"{0}"'.format(p)
166 return p
168 cmd = [get_java_cmd(), "-jar", jython_path, pyfile]
169 if argv is not None:
170 cmd.extend(argv)
171 cmd = " ".join(clean(i, _) for i, _ in enumerate(cmd))
172 out, err = run_cmd(
173 cmd, wait=True, sin=sin, communicate=True, timeout=timeout, shell=False)
174 return out, err