1+ import psutil
2+ import sys
3+
4+ from .jgo import jgo_main as main
5+
6+ def add_jvm_args_as_necessary (argv , gc_option = '-XX:+UseConcMarkSweepGC' ):
7+ """
8+
9+ Extend existing ``argv`` with reasonable default values for garbage collection and max heap size.
10+ If ``-Xmx`` is not specified in ``argv``, set max heap size to half the system's memory.
11+
12+ :param argv: arugment vector
13+ :param gc_option: Use this garbage collector settings, if any.
14+ :return: ``argv`` with
15+ """
16+ if gc_option and not gc_option in argv :
17+ argv += [gc_option ]
18+
19+ for arg in argv :
20+ if '-Xmx' in arg :
21+ return argv
22+
23+ total_memory = psutil .virtual_memory ().total
24+ exponent = 3 if total_memory > 2 * 1024 ** 3 else 2
25+ memory_unit = 'G' if exponent == 3 else 'M'
26+ max_heap_size = '{}{}' .format (max (total_memory // (1024 ** exponent ) // 2 , 1 ), memory_unit )
27+
28+ argv = ['-Xmx{}' .format (max_heap_size )] + argv
29+
30+ return argv
31+
32+ def maven_scijava_repository ():
33+ return 'https://maven.imagej.net/content/groups/public'
34+
35+
36+ def main_from_endpoint (
37+ primary_endpoint ,
38+ argv = sys .argv [1 :],
39+ repositories = {'imagej.public' : maven_scijava_repository ()},
40+ primary_endpoint_version = None ,
41+ primary_endpoint_main_class = None ,
42+ secondary_endpoints = ()):
43+ """
44+ Convenience method to populate appropriate argv for jgo. This is useful to distribute Java programs as Python modules.
45+
46+ For example, to run paintera with slf4j logging bindings, call
47+ ``
48+ main_from_endpoint(
49+ 'org.janelia.saalfeldlab:paintera',
50+ primary_endpoint_version='0.8.1',
51+ secondary_endpoints=('org.slf4j:slf4j-simple:1.7.25',),
52+ )
53+ ``
54+
55+ :param primary_endpoint: The primary endpoint of the Java program you want to run.
56+ :param repositories: Any maven repository that holds the required jars. Defaults to {'imagej.public': maven_scijava_repository()}.
57+ :param primary_endpoint_version: Will be appended to ``primary_endpoint`` if it does not evaluate to ``False``
58+ :param primary_endpoint_main_class: Will be appended to ``primary_endpoitn`` if it does not evaluate to ``False``.
59+ :param secondary_endpoints: Any other endpoints that should be added.
60+ :return: ``None``
61+ """
62+ double_dash_index = [i for (i , arg ) in enumerate (argv ) if arg == '--' ][0 ] if '--' in argv else - 1
63+ jgo_and_jvm_argv = ([] if double_dash_index < 0 else argv [:double_dash_index ]) + ['--ignore-jgorc' ]
64+ repository_strings = ['-r' ] + ['{}={}' .format (k , v ) for (k , v ) in repositories .items ()]
65+ primary_endpoint = primary_endpoint + ':{}' .format (primary_endpoint_version ) if primary_endpoint_version else primary_endpoint
66+ primary_endpoint = primary_endpoint + ':{}' .format (primary_endpoint_main_class ) if primary_endpoint_main_class else primary_endpoint
67+ endpoint = ['+' .join ((primary_endpoint ,) + secondary_endpoints )]
68+ paintera_argv = argv if double_dash_index < 0 else argv [double_dash_index + 1 :]
69+ argv = add_jvm_args_as_necessary (jgo_and_jvm_argv ) + repository_strings + endpoint + paintera_argv
70+
71+ main (argv = argv )
0 commit comments