Skip to content

Commit

Permalink
Merge pull request #133 from altendky/fix_windows_install
Browse files Browse the repository at this point in the history
Fix WindowsError being accessed before assignment
  • Loading branch information
LeeKamentsky committed Apr 2, 2018
2 parents e9fe3dc + 3afc553 commit a9c3fe0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ install:
- cd $TRAVIS_REPO_SLUG
- conda create --use-local -n testenv python=$PYTHON_VERSION
- source activate testenv
- conda install numpy
- conda install cython
- python setup.py develop
# Install sphinx and friends to build documentation.
# (LEEK) travis install of sphinx on Python 3.5 crashed
Expand Down
107 changes: 63 additions & 44 deletions javabridge/locate.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def is_mingw():
(sys.version_info.major == 3)))
is_mingw = is_mingw()

if is_win:
if sys.version_info.major == 2:
import _winreg as winreg
from exceptions import WindowsError
else:
import winreg

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -121,28 +127,36 @@ def get_out(cmd):
jdk_dir = os.path.abspath(jdk_dir)
return jdk_dir
elif is_win:
if sys.version_info.major == 2:
import _winreg as winreg
else:
import winreg
java_key_path = 'SOFTWARE\\JavaSoft\\Java Runtime Environment'
looking_for = java_key_path
try:
kjava = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, java_key_path)
looking_for = java_key_path + "\\CurrentVersion"
kjava_values = dict([winreg.EnumValue(kjava, i)[:2]
for i in range(winreg.QueryInfoKey(kjava)[1])])
current_version = kjava_values['CurrentVersion']
looking_for = java_key_path + '\\' + current_version
kjava_current = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
looking_for)
kjava_current_values = dict([winreg.EnumValue(kjava_current, i)[:2]
for i in range(winreg.QueryInfoKey(kjava_current)[1])])
return kjava_current_values['JavaHome']
except:
logger.error("Failed to find registry entry: %s\n" %looking_for,
exc_info=True)
return None
# Registry keys changed in 1.9
# https://docs.oracle.com/javase/9/migrate/toc.htm#GUID-EEED398E-AE37-4D12-AB10-49F82F720027
java_key_paths = (
'SOFTWARE\\JavaSoft\\JRE',
'SOFTWARE\\JavaSoft\\Java Runtime Environment',
)
for java_key_path in java_key_paths:
looking_for = java_key_path
try:
kjava = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, java_key_path)
looking_for = java_key_path + "\\CurrentVersion"
kjava_values = dict([winreg.EnumValue(kjava, i)[:2]
for i in range(winreg.QueryInfoKey(kjava)[1])])
current_version = kjava_values['CurrentVersion']
looking_for = java_key_path + '\\' + current_version
kjava_current = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
looking_for)
kjava_current_values = dict([winreg.EnumValue(kjava_current, i)[:2]
for i in range(winreg.QueryInfoKey(kjava_current)[1])])
return kjava_current_values['JavaHome']
except WindowsError as e:
if e.errno == 2:
continue
else:
raise

raise RuntimeError(
"Failed to find the Java Runtime Environment. "
"Please download and install the Oracle JRE 1.6 or later"
)


def find_jdk():
Expand All @@ -152,28 +166,33 @@ def find_jdk():
if is_mac:
return find_javahome()
if is_win:
if sys.version_info.major == 2:
import _winreg as winreg
from exceptions import WindowsError
else:
import winreg
try:
jdk_key_path = 'SOFTWARE\\JavaSoft\\Java Development Kit'
kjdk = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, jdk_key_path)
kjdk_values = dict([winreg.EnumValue(kjdk, i)[:2]
for i in range(winreg.QueryInfoKey(kjdk)[1])])
current_version = kjdk_values['CurrentVersion']
kjdk_current = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
jdk_key_path + '\\' + current_version)
kjdk_current_values = dict([winreg.EnumValue(kjdk_current, i)[:2]
for i in range(winreg.QueryInfoKey(kjdk_current)[1])])
return kjdk_current_values['JavaHome']
except WindowsError as e:
if e.errno == 2:
raise RuntimeError(
"Failed to find the Java Development Kit. Please download and install the Oracle JDK 1.6 or later")
else:
raise
# Registry keys changed in 1.9
# https://docs.oracle.com/javase/9/migrate/toc.htm#GUID-EEED398E-AE37-4D12-AB10-49F82F720027
jdk_key_paths = (
'SOFTWARE\\JavaSoft\\JDK',
'SOFTWARE\\JavaSoft\\Java Development Kit',
)
for jdk_key_path in jdk_key_paths:
try:
kjdk = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, jdk_key_path)
kjdk_values = dict([winreg.EnumValue(kjdk, i)[:2]
for i in range(winreg.QueryInfoKey(kjdk)[1])])
current_version = kjdk_values['CurrentVersion']
kjdk_current = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
jdk_key_path + '\\' + current_version)
kjdk_current_values = dict([winreg.EnumValue(kjdk_current, i)[:2]
for i in range(winreg.QueryInfoKey(kjdk_current)[1])])
return kjdk_current_values['JavaHome']
except WindowsError as e:
if e.errno == 2:
continue
else:
raise

raise RuntimeError(
"Failed to find the Java Development Kit. "
"Please download and install the Oracle JDK 1.6 or later"
)

def find_javac_cmd():
"""Find the javac executable"""
Expand Down
12 changes: 9 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ def build_cython():
pyx for pyx, c in zip(pyx_filenames, c_filenames)
if os.path.exists(pyx) and needs_compilation(c, pyx)]
if len(nc_pyx_filenames) > 0:
cmd = ['cython'] + nc_pyx_filenames
subprocess.check_call(cmd)
cython_cmd = [sys.executable, '-m', 'cython']
cmd = cython_cmd + nc_pyx_filenames
env = dict(os.environ)
env['PYTHONPATH'] = os.pathsep.join(sys.path)
try:
subprocess.check_call(cmd, env=env)
except FileNotFoundError:
raise RuntimeError("Failed to find Cython: {}".format(cython_cmd))

def get_jvm_include_dirs():
'''Return a sequence of paths to include directories for JVM defs'''
Expand Down Expand Up @@ -386,7 +392,7 @@ def get_version():
'Programming Language :: Python :: 3'
],
license='BSD License',
setup_requires=['numpy'],
setup_requires=['cython', 'numpy'],
install_requires=['numpy'],
tests_require="nose",
entry_points={'nose.plugins.0.10': [
Expand Down

0 comments on commit a9c3fe0

Please sign in to comment.