Debian Codegraph @

setup.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # !/usr/bin/env python3
2
3 # Copyright 2014-2020 Brockmann Consult GmbH
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import glob
18 import os
19 import os.path
20 import platform
21 import shutil
22 import subprocess
23 import sys
24 import unittest
25 from distutils import log
26 from distutils.cmd import Command
27 from distutils.util import get_platform
28 from setuptools import setup
29 from setuptools.command.install import install
30 from setuptools.command.install_lib import install_lib
31 from setuptools.command.test import test
32 from setuptools.extension import Extension
33
34 import jpyutil
35
36 __author__ = jpyutil.__author__
37 __copyright__ = jpyutil.__copyright__
38 __license__ = jpyutil.__license__
39 __version__ = jpyutil.__version__
40
41 base_dir = os.path.dirname(os.path.relpath(__file__))
42 src_main_c_dir = os.path.join(base_dir, 'src', 'main', 'c')
43 src_test_py_dir = os.path.join(base_dir, 'src', 'test', 'python')
44
45 is_ci = os.environ.get('CI') == 'true'
46
47 sources = [
48 os.path.join(src_main_c_dir, 'jpy_module.c'),
49 os.path.join(src_main_c_dir, 'jpy_diag.c'),
50 os.path.join(src_main_c_dir, 'jpy_verboseexcept.c'),
51 os.path.join(src_main_c_dir, 'jpy_conv.c'),
52 os.path.join(src_main_c_dir, 'jpy_compat.c'),
53 os.path.join(src_main_c_dir, 'jpy_jtype.c'),
54 os.path.join(src_main_c_dir, 'jpy_jarray.c'),
55 os.path.join(src_main_c_dir, 'jpy_jobj.c'),
56 os.path.join(src_main_c_dir, 'jpy_jmethod.c'),
57 os.path.join(src_main_c_dir, 'jpy_jfield.c'),
58 os.path.join(src_main_c_dir, 'jni/org_jpy_PyLib.c'),
59 ]
60
61 headers = [
62 os.path.join(src_main_c_dir, 'jpy_module.h'),
63 os.path.join(src_main_c_dir, 'jpy_diag.h'),
64 os.path.join(src_main_c_dir, 'jpy_conv.h'),
65 os.path.join(src_main_c_dir, 'jpy_compat.h'),
66 os.path.join(src_main_c_dir, 'jpy_jtype.h'),
67 os.path.join(src_main_c_dir, 'jpy_jarray.h'),
68 os.path.join(src_main_c_dir, 'jpy_jobj.h'),
69 os.path.join(src_main_c_dir, 'jpy_jmethod.h'),
70 os.path.join(src_main_c_dir, 'jpy_jfield.h'),
71 os.path.join(src_main_c_dir, 'jni/org_jpy_PyLib.h'),
72 ]
73
74 # Python unit tests that just use Java runtime classes (rt.jar)
75 python_java_rt_tests = [
76 os.path.join(src_test_py_dir, 'jpy_rt_test.py'),
77 os.path.join(src_test_py_dir, 'jpy_mt_test.py'),
78 os.path.join(src_test_py_dir, 'jpy_diag_test.py'),
79 # os.path.join(src_test_py_dir, 'jpy_perf_test.py'),
80 ]
81
82 # Python unit tests that require target/test-classes or target/classes
83 # available on the classpath
84 python_java_jpy_tests = [
85 os.path.join(src_test_py_dir, 'jpy_array_test.py'),
86 os.path.join(src_test_py_dir, 'jpy_field_test.py'),
87 os.path.join(src_test_py_dir, 'jpy_retval_test.py'),
88 os.path.join(src_test_py_dir, 'jpy_exception_test.py'),
89 os.path.join(src_test_py_dir, 'jpy_py_exception_chain_test.py'),
90 os.path.join(src_test_py_dir, 'jpy_overload_test.py'),
91 os.path.join(src_test_py_dir, 'jpy_typeconv_test.py'),
92 os.path.join(src_test_py_dir, 'jpy_typeconv_test_pyobj.py'),
93 os.path.join(src_test_py_dir, 'jpy_typeres_test.py'),
94 os.path.join(src_test_py_dir, 'jpy_modretparam_test.py'),
95 os.path.join(src_test_py_dir, 'jpy_translation_test.py'),
96 os.path.join(src_test_py_dir, 'jpy_gettype_test.py'),
97 os.path.join(src_test_py_dir, 'jpy_reentrant_test.py'),
98 os.path.join(src_test_py_dir, 'jpy_java_embeddable_test.py'),
99 os.path.join(src_test_py_dir, 'jpy_obj_test.py'),
100 os.path.join(src_test_py_dir, 'jpy_eval_exec_test.py'),
101 os.path.join(src_test_py_dir, 'jpy_mt_eval_exec_test.py'),
102 os.path.join(src_test_py_dir, 'jpy_reachability_fence_test.py'),
103 os.path.join(src_test_py_dir, 'jpy_cleanup_thread_test.py'),
104 ]
105
106 # e.g. jdk_home_dir = '/home/marta/jdk1.7.0_15'
107 jdk_home_dir = jpyutil.find_jdk_home_dir()
108 if jdk_home_dir is None:
109 log.error('Error: environment variable "JAVA_HOME" must be set to a JDK (>= v1.7) installation directory')
110 exit(1)
111
112 log.info('Building a %s-bit library for a %s system with JDK at %s' % (
113 '64' if jpyutil.PYTHON_64BIT else '32', platform.system(), jdk_home_dir))
114
115 jvm_dll_file = jpyutil.find_jvm_dll_file(jdk_home_dir)
116 if not jvm_dll_file:
117 log.error('Error: Cannot find any JVM shared library')
118 exit(1)
119
120 lib_dir = os.path.join(base_dir, 'lib')
121 jpy_jar_file = os.path.join(lib_dir, 'jpy.jar')
122 jvm_dll_dir = os.path.dirname(jvm_dll_file)
123
124 include_dirs = [src_main_c_dir, os.path.join(jdk_home_dir, 'include')]
125 library_dirs = [jvm_dll_dir]
126 libraries = [jpyutil.JVM_LIB_NAME]
127
128 define_macros = []
129 extra_link_args = []
130 extra_compile_args = []
131
132 if platform.system() == 'Windows':
133 define_macros += [('WIN32', '1')]
134 include_dirs += [os.path.join(jdk_home_dir, 'include', 'win32')]
135 library_dirs += [os.path.join(jdk_home_dir, 'lib')]
136 elif platform.system() == 'Linux':
137 include_dirs += [os.path.join(jdk_home_dir, 'include', 'linux')]
138 libraries += ['dl']
139 extra_link_args += ['-Xlinker', '-rpath', jvm_dll_dir]
140 elif platform.system() == 'Darwin':
141 include_dirs += [os.path.join(jdk_home_dir, 'include', 'darwin')]
142 if is_ci:
143 # This has the effect of removing the non-portable LC_LOAD_DYLIB and LC_RPATH directives for libjvm.dylib from the .so files.
144 # See https://github.com/jpy-consortium/jpy/issues/79 for details.
145 libraries = None
146 library_dirs = None
147 else:
148 # Remove local build workaround for non-portable macOS wheels
149 # TODO: https://github.com/jpy-consortium/jpy/issues/80
150 library_dirs += [os.path.join(sys.exec_prefix, 'lib')]
151 extra_link_args += ['-Xlinker', '-rpath', jvm_dll_dir]
152
153 # ----------- Functions -------------
154 def _build_dir():
155 # this is hacky, but use distutils logic to get build dir. see: distutils.command.build
156 plat = '.%s-%d.%d' % (get_platform(), sys.version_info.major, sys.version_info.minor)
157 log.info('Platform specifier: "%s"' % plat)
158 path = os.path.join('build', 'lib' + plat)
159 log.info('Build directory path: "%s"' % path)
160 return path
161
162
163 def package_maven():
164 """ Run maven package lifecycle """
165 if not os.getenv('JAVA_HOME'):
166 # make sure Maven uses the same JDK which we have used to compile
167 # and link the C-code
168 os.environ['JAVA_HOME'] = jdk_home_dir
169
170 mvn_goal = 'package'
171 log.info("Executing Maven goal '" + mvn_goal + "'")
172 code = subprocess.call(['mvn', 'clean', mvn_goal, '-DskipTests', '-B'],
173 shell=platform.system() == 'Windows')
174 if code:
175 exit(code)
176
177 # Copy JAR results to lib/*.jar
178
179 if not os.path.exists(lib_dir):
180 os.mkdir(lib_dir)
181 target_dir = os.path.join(base_dir, 'target')
182 jar_files = glob.glob(os.path.join(target_dir, '*.jar'))
183 jar_files = [f for f in jar_files
184 if not (f.endswith('-sources.jar')
185 or f.endswith('-javadoc.jar'))]
186 if not jar_files:
187 log.error('Maven did not generate any JAR artifacts')
188 exit(1)
189 for jar_file in jar_files:
190 build_dir = _build_dir()
191 if os.path.exists(build_dir):
192 log.info('Build directory "%s" exists.' % build_dir)
193 else:
194 log.info('Creating missing build directory "%s".' % build_dir)
195 os.makedirs(build_dir)
196 log.info("Copying " + jar_file + " -> " + build_dir + "")
197 shutil.copy(jar_file, build_dir)
198
199
200 def _read(filename):
201 """ Helper function for reading in project files """
202 with open(filename, encoding='UTF-8') as file:
203 return file.read()
204
205
206 def test_python_java_rt():
207 """ Run Python test cases against Java runtime classes. """
208 sub_env = {'PYTHONPATH': _build_dir()}
209 import os
210 sub_env.update(dict(os.environ))
211
212 log.info('Executing Python unit tests (against Java runtime classes)...')
213 return jpyutil._execute_python_scripts(python_java_rt_tests, env=sub_env)
214
215
216 def test_python_java_classes():
217 """ Run Python tests against JPY test classes """
218 sub_env = {'PYTHONPATH': _build_dir()}
219 import os
220 sub_env.update(dict(os.environ))
221
222 log.info('Executing Python unit tests (against JPY test classes)...')
223 return jpyutil._execute_python_scripts(python_java_jpy_tests, env=sub_env)
224
225
226 def test_maven():
227 jpy_config = os.path.join(_build_dir(), 'jpyconfig.properties')
228 # jpy.stopIsNoOp=true prevents Py_Finalize from being called between test methods/classes.
229 # Multiple start/stop cycles in the same JVM crash CPython (issue #70); with this flag,
230 # stopPython() is a no-op and startPython() remains idempotent.
231 mvn_args = ('-DargLine=-Xmx512m -Djpy.config=' + jpy_config + ' -Djpy.debug=true'
232 + ' -Djpy.stopIsNoOp=true')
233 log.info("Executing Maven goal 'test' with arg line " + repr(mvn_args))
234 # PYTHONHOME must point to the base Python installation (not the venv prefix) so that the
235 # embedded interpreter can find the standard library. sys.base_prefix is correct for both
236 # plain and virtual-environment Python installations.
237 env = dict(os.environ)
238 env['PYTHONHOME'] = sys.base_prefix
239 code = subprocess.call(['mvn', 'test', mvn_args], shell=platform.system() == 'Windows', env=env)
240 return code == 0
241
242
243 def _write_jpy_config(target_dir=None, install_dir=None):
244 """
245 Write out a well-formed jpyconfig.properties file for easier Java
246 integration in a given location.
247 """
248 if not target_dir:
249 target_dir = _build_dir()
250
251 args = [sys.executable,
252 os.path.join(target_dir, 'jpyutil.py'),
253 '--jvm_dll', jvm_dll_file,
254 '--java_home', jdk_home_dir,
255 '--log_level', 'DEBUG',
256 '--req_java',
257 '--req_py']
258 if install_dir:
259 args.append('--install_dir')
260 args.append(install_dir)
261
262 log.info('Writing jpy configuration to %s using install_dir %s' % (target_dir, install_dir))
263 # Make the compiled jpy/jdl extensions discoverable to the jpyutil.py subprocess so that
264 # importlib.util.find_spec('jpy') resolves to the freshly-built .so in the build directory.
265 env = dict(os.environ)
266 existing = env.get('PYTHONPATH', '')
267 env['PYTHONPATH'] = (target_dir + os.pathsep + existing) if existing else target_dir
268 return subprocess.call(args, env=env)
269
270
271 def _copy_jpyutil():
272 src = os.path.relpath(jpyutil.__file__)
273 dest = _build_dir()
274 log.info('Copying %s to %s' % (src, dest))
275 shutil.copy(src, dest)
276
277
278 def _build_jpy():
279 package_maven()
280 _copy_jpyutil()
281
282
283 def test_suite():
284 suite = unittest.TestSuite()
285
286 def test_python_with_java_runtime(self):
287 assert 0 == test_python_java_rt()
288
289 def test_python_with_java_classes(self):
290 assert 0 == test_python_java_classes()
291
292 def test_java(self):
293 assert test_maven()
294
295 suite.addTest(test_python_with_java_runtime)
296 suite.addTest(test_python_with_java_classes)
297 suite.addTest(test_java)
298
299 return suite
300
301
302 class MavenBuildCommand(Command):
303 """ Custom JPY Maven builder command """
304 description = 'run Maven to generate JPY jar'
305 user_options = [] # do not remove, needs to be stubbed out!
306
307 def initialize_options(self):
308 pass
309
310 def finalize_options(self):
311 pass
312
313 def run(self):
314 self.announce('Building JPY')
315 _build_jpy()
316
317
318 class JpyBuildBeforeTest(test):
319 """ Customization of SetupTools Install command for JPY """
320
321 def run(self):
322 self.run_command('build')
323 self.run_command('maven')
324 _write_jpy_config()
325 test.run(self)
326
327
328 class JpyInstallLib(install_lib):
329 """ Custom install_lib command for getting install_dir """
330
331 def run(self):
332 if not is_ci:
333 _write_jpy_config(install_dir=self.install_dir)
334 install_lib.run(self)
335
336
337 class JpyInstall(install):
338 """ Custom install command to trigger Maven steps """
339
340 def run(self):
341 self.run_command('build')
342 self.run_command('maven')
343 install.run(self)
344
345
346 setup(name='jpy',
347 description='Bi-directional Python-Java bridge',
348 long_description=_read('README.md') + '\n\n' + _read('CHANGES.md'),
349 version=__version__,
350 long_description_content_type='text/markdown',
351 platforms='Windows, Linux, Darwin',
352 author=__author__,
353 author_email='[email protected]',
354 maintainer='Brockmann Consult GmbH',
355 maintainer_email='[email protected]',
356 license=__license__,
357 url='https://github.com/jpy-consortium/jpy',
358 download_url='https://pypi.python.org/pypi/jpy/' + __version__,
359 py_modules=['jpyutil'],
360 ext_modules=[Extension('jpy',
361 sources=sources,
362 depends=headers,
363 include_dirs=include_dirs,
364 library_dirs=library_dirs,
365 libraries=libraries,
366 extra_link_args=extra_link_args,
367 extra_compile_args=extra_compile_args,
368 define_macros=define_macros),
369 Extension('jdl',
370 sources=[os.path.join(src_main_c_dir, 'jni/org_jpy_DL.c')],
371 depends=[os.path.join(src_main_c_dir, 'jni/org_jpy_DL.h')],
372 include_dirs=include_dirs,
373 library_dirs=library_dirs,
374 libraries=libraries,
375 extra_link_args=extra_link_args,
376 extra_compile_args=extra_compile_args,
377 define_macros=define_macros),
378 ],
379 test_suite='setup.test_suite',
380 cmdclass={
381 'maven': MavenBuildCommand,
382 'test': JpyBuildBeforeTest,
383 'install': JpyInstall,
384 'install_lib': JpyInstallLib
385 },
386 python_requires='>=3.9',
387 classifiers=['Development Status :: 5 - Production/Stable',
388 # Indicate who your project is intended for
389 'Intended Audience :: Developers',
390
391 # Pick your license as you wish (should match "license" above)
392 'License :: OSI Approved :: Apache Software License',
393
394 'Programming Language :: Python :: 3.9',
395 'Programming Language :: Python :: 3.10',
396 'Programming Language :: Python :: 3.11',
397 'Programming Language :: Python :: 3.12',
398 'Programming Language :: Python :: 3.13',
399 'Programming Language :: Python :: 3.14',
400 ])