From b076e8cba7644eed41a0eec1c69e7816112ae046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20P=C3=B6hn?= Date: Sat, 25 Apr 2020 17:22:48 +0200 Subject: [PATCH] add srclib spec parser --- fdroidserver/common.py | 32 +++++++++++++++++++++++++++++++- fdroidserver/lint.py | 2 +- tests/common.TestCase | 18 +++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index c6ca2fb8..d7a45d2e 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -63,7 +63,7 @@ import fdroidserver.metadata import fdroidserver.lint from fdroidserver import _ from fdroidserver.exception import FDroidException, VCSException, NoSubmodulesException,\ - BuildException, VerificationException + BuildException, VerificationException, MetaDataException from .asynchronousfilereader import AsynchronousFileReader # The path to this fdroidserver distribution @@ -1808,6 +1808,36 @@ def get_app_from_url(url): return app +def parse_srclib_spec(spec): + + if type(spec) != str: + raise MetaDataException(_("can not parse scrlib spec " + "(not a string): '{}'") + .format(spec)) + + tokens = spec.split('@') + if len(tokens) > 2: + raise MetaDataException(_("could not parse srclib spec " + "(too many '@' signs): '{}'") + .format(spec)) + elif len(tokens) < 2: + raise MetaDataException(_("could not parse srclib spec " + "(no ref specified): '{}'") + .format(spec)) + + name = tokens[0] + ref = tokens[1] + number = None + subdir = None + + if ':' in name: + number, name = name.split(':', 1) + if '/' in name: + name, subdir = name.split('/', 1) + + return (name, ref, number, subdir) + + def getsrclib(spec, srclib_dir, subdir=None, basepath=False, raw=False, prepare=True, preponly=False, refresh=True, build=None): diff --git a/fdroidserver/lint.py b/fdroidserver/lint.py index 000d2092..586eefbc 100644 --- a/fdroidserver/lint.py +++ b/fdroidserver/lint.py @@ -616,7 +616,7 @@ def main(): for srclib in build.srclibs: srclibs.add(srclib) for srclib in srclibs: - name, numer, libdir = common.getsrclib(srclib, 'srclibs', prepare=False, refresh=False) + name, ref, number, subdir = common.parse_srclib_spec(srclib) srclibpath = os.path.join('srclibs', name + '.yml') if os.path.isfile(srclibpath): yamllintresult = common.run_yamllint(srclibpath) diff --git a/tests/common.TestCase b/tests/common.TestCase index d5354284..93fbedd3 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -34,7 +34,7 @@ import fdroidserver.signindex import fdroidserver.common import fdroidserver.metadata from testcommon import TmpCwd -from fdroidserver.exception import FDroidException, VCSException +from fdroidserver.exception import FDroidException, VCSException, MetaDataException class CommonTest(unittest.TestCase): @@ -1014,6 +1014,22 @@ class CommonTest(unittest.TestCase): subdir = fdroidserver.common.get_gradle_subdir(build_dir, paths) self.assertEqual(subdirs[f], subdir) + def test_parse_srclib_spec_good(self): + self.assertEqual(fdroidserver.common.parse_srclib_spec('osmand-external-skia@android/oreo'), + ('osmand-external-skia', 'android/oreo', None, None)) + self.assertEqual(fdroidserver.common.parse_srclib_spec('1:appcompat@v7'), + ('appcompat', 'v7', '1', None)) + self.assertEqual(fdroidserver.common.parse_srclib_spec('1:Support/v7/appcompat@android-4.4_r1.1'), + ('Support', 'android-4.4_r1.1', '1', 'v7/appcompat')) + + def test_parse_srclib_spec_bad(self): + with self.assertRaises(MetaDataException): + self.assertEqual(fdroidserver.common.parse_srclib_spec(None)) + with self.assertRaises(MetaDataException): + self.assertEqual(fdroidserver.common.parse_srclib_spec('no-ref')) + with self.assertRaises(MetaDataException): + self.assertEqual(fdroidserver.common.parse_srclib_spec('@multi@at-signs@')) + def test_bad_urls(self): for url in ('asdf', 'file://thing.git',