From e0df6d24795237dbe586627d13ad2c9ef404f614 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 20 Oct 2017 11:35:48 +0200 Subject: [PATCH] choose the most recent available version of Java This came about testing on OSX, where there are often multiple versions of the JDK installed. This was choosing the oldest version. It should choose the most recent version. --- .travis.yml | 2 ++ fdroidserver/common.py | 59 ++++++++++++++++++++++++++---------------- tests/common.TestCase | 39 ++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 27df250a..8af32606 100644 --- a/.travis.yml +++ b/.travis.yml @@ -75,6 +75,8 @@ install: sudo pip3 install --quiet --editable . ; sudo rm -rf fdroidserver.egg-info; + ls -l /System/Library/Java/JavaVirtualMachines || true; + ls -l /Library/Java/JavaVirtualMachines || true; echo $PATH; echo $JAVA_HOME; jenv versions; diff --git a/fdroidserver/common.py b/fdroidserver/common.py index c6fbf0d2..7e76513d 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -132,6 +132,41 @@ def setup_global_opts(parser): help=_("Restrict output to warnings and errors")) +def _add_java_paths_to_config(pathlist, thisconfig): + def path_version_key(s): + versionlist = [] + for u in re.split('[^0-9]+', s): + try: + versionlist.append(int(u)) + except ValueError: + pass + return versionlist + + for d in sorted(pathlist, key=path_version_key): + if os.path.islink(d): + continue + j = os.path.basename(d) + # the last one found will be the canonical one, so order appropriately + for regex in [ + r'^1\.([6-9])\.0\.jdk$', # OSX + r'^jdk1\.([6-9])\.0_[0-9]+.jdk$', # OSX and Oracle tarball + r'^jdk1\.([6-9])\.0_[0-9]+$', # Oracle Windows + r'^jdk([6-9])-openjdk$', # Arch + r'^java-([6-9])-openjdk$', # Arch + r'^java-([6-9])-jdk$', # Arch (oracle) + r'^java-1\.([6-9])\.0-.*$', # RedHat + r'^java-([6-9])-oracle$', # Debian WebUpd8 + r'^jdk-([6-9])-oracle-.*$', # Debian make-jpkg + r'^java-([6-9])-openjdk-[^c][^o][^m].*$', # Debian + ]: + m = re.match(regex, j) + if not m: + continue + for p in [d, os.path.join(d, 'Contents', 'Home')]: + if os.path.exists(os.path.join(p, 'bin', 'javac')): + thisconfig['java_paths'][m.group(1)] = p + + def fill_config_defaults(thisconfig): for k, v in default_config.items(): if k not in thisconfig: @@ -167,29 +202,7 @@ def fill_config_defaults(thisconfig): pathlist.append(os.getenv('JAVA_HOME')) if os.getenv('PROGRAMFILES') is not None: pathlist += glob.glob(os.path.join(os.getenv('PROGRAMFILES'), 'Java', 'jdk1.[6-9].*')) - for d in sorted(pathlist): - if os.path.islink(d): - continue - j = os.path.basename(d) - # the last one found will be the canonical one, so order appropriately - for regex in [ - r'^1\.([6-9])\.0\.jdk$', # OSX - r'^jdk1\.([6-9])\.0_[0-9]+.jdk$', # OSX and Oracle tarball - r'^jdk1\.([6-9])\.0_[0-9]+$', # Oracle Windows - r'^jdk([6-9])-openjdk$', # Arch - r'^java-([6-9])-openjdk$', # Arch - r'^java-([6-9])-jdk$', # Arch (oracle) - r'^java-1\.([6-9])\.0-.*$', # RedHat - r'^java-([6-9])-oracle$', # Debian WebUpd8 - r'^jdk-([6-9])-oracle-.*$', # Debian make-jpkg - r'^java-([6-9])-openjdk-[^c][^o][^m].*$', # Debian - ]: - m = re.match(regex, j) - if not m: - continue - for p in [d, os.path.join(d, 'Contents', 'Home')]: - if os.path.exists(os.path.join(p, 'bin', 'javac')): - thisconfig['java_paths'][m.group(1)] = p + _add_java_paths_to_config(pathlist, thisconfig) for java_version in ('7', '8', '9'): if java_version not in thisconfig['java_paths']: diff --git a/tests/common.TestCase b/tests/common.TestCase index 3a10b7bb..2c261267 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -92,6 +92,41 @@ class CommonTest(unittest.TestCase): else: print('no build-tools found: ' + build_tools) + def test_find_java_root_path(self): + tmptestsdir = tempfile.mkdtemp(prefix='test_find_java_root_path', dir=self.tmpdir) + os.chdir(tmptestsdir) + + all_pathlists = [ + ([ # Debian + '/usr/lib/jvm/java-1.5.0-gcj-5-amd64', + '/usr/lib/jvm/java-8-openjdk-amd64', + '/usr/lib/jvm/java-1.8.0-openjdk-amd64', + ], '/usr/lib/jvm/java-8-openjdk-amd64'), + ([ # OSX + '/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk', + '/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk', + '/System/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk', + ], '/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk'), + ] + + for pathlist, choice in all_pathlists: + # strip leading / to make relative paths to test without root + pathlist = [p[1:] for p in pathlist] + + # create test file used in common._add_java_paths_to_config() + for p in pathlist: + if p.startswith('/System') or p.startswith('/Library'): + basedir = os.path.join(p, 'Contents', 'Home', 'bin') + else: + basedir = os.path.join(p, 'bin') + os.makedirs(basedir) + open(os.path.join(basedir, 'javac'), 'w').close() + + config = dict() + config['java_paths'] = dict() + fdroidserver.common._add_java_paths_to_config(pathlist, config) + self.assertEqual(config['java_paths']['8'], choice[1:]) + def testIsApkDebuggable(self): config = dict() fdroidserver.common.fill_config_defaults(config) @@ -177,7 +212,7 @@ class CommonTest(unittest.TestCase): def test_prepare_sources_refresh(self): packageName = 'org.fdroid.ci.test.app' - testdir = tempfile.mkdtemp(prefix='test_verify_apks', dir=self.tmpdir) + testdir = tempfile.mkdtemp(prefix='test_prepare_sources_refresh', dir=self.tmpdir) print('testdir', testdir) os.chdir(testdir) os.mkdir('build') @@ -462,4 +497,4 @@ if __name__ == "__main__": newSuite = unittest.TestSuite() newSuite.addTest(unittest.makeSuite(CommonTest)) - unittest.main() + unittest.main(failfast=False)