Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 1aebf58

Browse files
committed
hacks to get jar into site-packages and a proper properties file.
1 parent 64b6585 commit 1aebf58

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

jpyutil.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def _add_paths_if_exists(path_list, *paths):
8181
return path_list
8282

8383

84-
def _get_module_path(name, fail=False):
84+
def _get_module_path(name, fail=False, install_path=None):
85+
""" Find the path to the jpy jni modules. """
8586
import imp
8687

8788
module = imp.find_module(name)
@@ -90,6 +91,10 @@ def _get_module_path(name, fail=False):
9091
path = module[1]
9192
if not path and fail:
9293
raise RuntimeError("module '" + name + "' is missing a file path")
94+
95+
if install_path:
96+
return os.path.join(install_path, os.path.split(path)[1])
97+
9398
return path
9499

95100

@@ -104,10 +109,10 @@ def _find_file(search_dirs, *filenames):
104109
return None
105110

106111

107-
def _get_java_api_properties(fail=False):
112+
def _get_java_api_properties(fail=False, path=None):
108113
jpy_config = Properties()
109-
jpy_config.set_property('jpy.jpyLib', _get_module_path('jpy', fail=fail))
110-
jpy_config.set_property('jpy.jdlLib', _get_module_path('jdl', fail=fail))
114+
jpy_config.set_property('jpy.jpyLib', _get_module_path('jpy', fail=fail, install_path=path))
115+
jpy_config.set_property('jpy.jdlLib', _get_module_path('jdl', fail=fail, install_path=path))
111116
jpy_config.set_property('jpy.pythonLib', _find_python_dll_file(fail=fail))
112117
jpy_config.set_property('jpy.pythonPrefix', sys.prefix)
113118
jpy_config.set_property('jpy.pythonExecutable', sys.executable)
@@ -512,6 +517,7 @@ def _execute_python_scripts(scripts, **kwargs):
512517
def write_config_files(out_dir='.',
513518
java_home_dir=None,
514519
jvm_dll_file=None,
520+
install_dir=None,
515521
req_java_api_conf=True,
516522
req_py_api_conf=True):
517523
"""
@@ -520,6 +526,7 @@ def write_config_files(out_dir='.',
520526
:param out_dir: output directory, must exist
521527
:param java_home_dir: optional home directory of the Java JRE or JDK installation
522528
:param jvm_dll_file: optional file to JVM shared library file
529+
:param install_dir: optional path to where to searfh for modules
523530
:param req_java_api_conf: whether to write the jpy configuration file 'jpyconfig.properties' for Java
524531
:param req_py_api_conf: whether to write the jpy configuration file 'jpyconfig.py' for Python
525532
:return: zero on success, otherwise an error code
@@ -561,7 +568,7 @@ def write_config_files(out_dir='.',
561568

562569
try:
563570
java_api_config_file = os.path.join(out_dir, java_api_config_basename)
564-
java_api_properties = _get_java_api_properties(fail=req_java_api_conf)
571+
java_api_properties = _get_java_api_properties(fail=req_java_api_conf, path=install_dir)
565572
java_api_properties.store(java_api_config_file, comments=[
566573
"Created by '%s' tool on %s" % (tool_name, str(datetime.datetime.now())),
567574
"This file is read by the jpy Java API (org.jpy.PyLib class) in order to find shared libraries"])
@@ -596,6 +603,7 @@ def _main():
596603
help="Require that Java API configuration succeeds.")
597604
parser.add_argument("-p", "--req_py", action='store_true', default=False,
598605
help="Require that Python API configuration succeeds.")
606+
parser.add_argument("--install_dir", action='store', default=None, help="Optional. Used during pip install of JPY")
599607
args = parser.parse_args()
600608

601609
log_level = getattr(logging, args.log_level.upper(), None)
@@ -614,7 +622,8 @@ def _main():
614622
java_home_dir=args.java_home,
615623
jvm_dll_file=args.jvm_dll,
616624
req_java_api_conf=args.req_java,
617-
req_py_api_conf=args.req_py)
625+
req_py_api_conf=args.req_py,
626+
install_dir=args.install_dir)
618627
except:
619628
logging.exception("Configuration failed")
620629
retcode = 100

setup.py

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525
from setuptools import setup
2626
from setuptools.command.test import test
27+
from setuptools.command.build_ext import build_ext
2728
from setuptools.command.install import install
29+
from setuptools.command.install_lib import install_lib
2830
from setuptools.extension import Extension
2931
from distutils.cmd import Command
32+
from distutils.util import get_platform
3033
from distutils import log
3134

3235
import jpyutil
@@ -36,10 +39,11 @@
3639
__license__ = jpyutil.__license__
3740
__version__ = jpyutil.__version__
3841

39-
base_dir = os.path.dirname(os.path.abspath(__file__))
42+
base_dir = os.path.dirname(os.path.relpath(__file__))
4043
src_main_c_dir = os.path.join(base_dir, 'src', 'main', 'c')
4144
src_test_py_dir = os.path.join(base_dir, 'src', 'test', 'python')
42-
45+
# src_main_c_dir = 'src/main/c'
46+
# src_test_py_dir = 'src/test/python'
4347

4448
do_maven = False
4549
if '--maven' in sys.argv:
@@ -120,6 +124,7 @@
120124
include_dirs = [src_main_c_dir, os.path.join(jdk_home_dir, 'include')]
121125
library_dirs = [jvm_dll_dir]
122126
libraries = [jpyutil.JVM_LIB_NAME]
127+
123128
define_macros = []
124129
extra_link_args = []
125130
extra_compile_args = []
@@ -137,7 +142,14 @@
137142
library_dirs += [os.path.join(sys.exec_prefix, 'lib')]
138143
extra_link_args += ['-Xlinker', '-rpath', jvm_dll_dir]
139144

145+
146+
140147
# ----------- Functions -------------
148+
def _build_dir():
149+
#return os.path.join(base_dir, 'build/lib.macosx-10.12-x86_64-3.6')
150+
# this is hacky, but use distutils logic to get build dir. see: distutils.command.build
151+
plat = ".%s-%d.%d" % (get_platform(), *sys.version_info[:2])
152+
return os.path.join('build', 'lib' + plat)
141153

142154
def package_maven():
143155
""" Run maven package lifecycle """
@@ -168,18 +180,16 @@ def package_maven():
168180
log.error('Maven did not generate any JAR artifacts')
169181
exit(1)
170182
for jar_file in jar_files:
171-
log.info("Copying " + jar_file + " -> " + lib_dir + "")
172-
shutil.copy(jar_file, lib_dir)
183+
build_dir = _build_dir()
184+
log.info("Copying " + jar_file + " -> " + build_dir + "")
185+
shutil.copy(jar_file, build_dir)
173186

174187

175188
def _read(filename):
176189
""" Helper function for reading in project files """
177190
with open(filename) as file:
178191
return file.read()
179192

180-
def _build_dir():
181-
# TODO: figure out logic for dynamically getting this at runtime
182-
return os.path.join(base_dir, 'build/lib.macosx-10.12-x86_64-3.6')
183193

184194
def test_python_java_rt():
185195
""" Run Python test cases against Java runtime classes. """
@@ -204,32 +214,37 @@ def test_maven():
204214
code = subprocess.call(['mvn', 'test', mvn_args], shell=platform.system() == 'Windows')
205215
return code == 0
206216

207-
def _write_jpy_config(target_dir=None):
217+
def _write_jpy_config(target_dir=None, install_dir=None):
208218
"""
209219
Write out a well-formed jpyconfig.properties file for easier Java
210220
integration in a given location.
211221
"""
212222
if not target_dir:
213223
target_dir = _build_dir()
214-
log.info('Writing jpy configuration to ' + target_dir)
215-
return subprocess.call([sys.executable,
216-
os.path.join(target_dir, 'jpyutil.py'),
217-
'--jvm_dll', jvm_dll_file,
218-
'--java_home', jdk_home_dir,
219-
'--log_level', 'DEBUG',
220-
'--req_java',
221-
'--req_py'])
224+
225+
args = [sys.executable,
226+
os.path.join(target_dir, 'jpyutil.py'),
227+
'--jvm_dll', jvm_dll_file,
228+
'--java_home', jdk_home_dir,
229+
'--log_level', 'DEBUG',
230+
'--req_java',
231+
'--req_py']
232+
if install_dir:
233+
args.append('--install_dir')
234+
args.append(install_dir)
235+
236+
log.info('Writing jpy configuration to %s using install_dir %s' % (target_dir, install_dir))
237+
return subprocess.call(args)
222238

223239
def _copy_jpyutil():
224-
src = os.path.abspath(jpyutil.__file__)
240+
src = os.path.relpath(jpyutil.__file__)
225241
dest = _build_dir()
226242
log.info('Copying %s to %s' % (src, dest))
227243
shutil.copy(src, dest)
228244

229245
def _build_jpy():
230246
package_maven()
231247
_copy_jpyutil()
232-
_write_jpy_config()
233248

234249

235250
def test_suite():
@@ -253,7 +268,7 @@ def test_java(self):
253268
class MavenBuildCommand(Command):
254269
""" Custom JPY Maven builder command """
255270
description = 'run Maven to generate JPY jar'
256-
user_options = []
271+
user_options = [] # do not remove, needs to be stubbed out!
257272

258273
def initialize_options(self):
259274
pass
@@ -264,7 +279,7 @@ def finalize_options(self):
264279
def run(self):
265280
self.announce('Building JPY')
266281
_build_jpy()
267-
282+
268283

269284
class JpyBuildBeforeTest(test):
270285
""" Customization of SetupTools Install command for JPY """
@@ -275,6 +290,13 @@ def run(self):
275290

276291
test.run(self)
277292

293+
class JpyInstallLib(install_lib):
294+
""" Custom install_lib command for getting install_dir """
295+
296+
def run(self):
297+
_write_jpy_config(install_dir=self.install_dir)
298+
install_lib.run(self)
299+
278300

279301
class JpyInstall(install):
280302
""" Custom install command to trigger Maven steps """
@@ -298,7 +320,6 @@ def run(self):
298320
url='https://github.com/bcdev/jpy',
299321
download_url='https://pypi.python.org/pypi/jpy/' + __version__,
300322
py_modules=['jpyutil'],
301-
package_data={'': [jpy_jar_file]},
302323
ext_modules=[Extension('jpy',
303324
sources=sources,
304325
depends=headers,
@@ -322,7 +343,8 @@ def run(self):
322343
cmdclass={
323344
'maven': MavenBuildCommand,
324345
'test': JpyBuildBeforeTest,
325-
'install': JpyInstall
346+
'install': JpyInstall,
347+
'install_lib': JpyInstallLib
326348
},
327349
classifiers=['Development Status :: 4 - Beta',
328350
# Indicate who your project is intended for
@@ -337,4 +359,5 @@ def run(self):
337359
'Programming Language :: Python :: 2.7',
338360
'Programming Language :: Python :: 3',
339361
'Programming Language :: Python :: 3.3',
340-
'Programming Language :: Python :: 3.4'])
362+
'Programming Language :: Python :: 3.4',
363+
'Programming Language :: Python :: 3.5'])

0 commit comments

Comments
 (0)