From 283f10dec1f8b9517c9a59b962860a28898a60d5 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 1 Oct 2020 10:02:05 +0200 Subject: [PATCH] index: generate repo icon if missing, and add tests --- fdroidserver/index.py | 17 +++++++++++++++-- tests/index.TestCase | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/fdroidserver/index.py b/fdroidserver/index.py index 6d247141..d89e5cb8 100644 --- a/fdroidserver/index.py +++ b/fdroidserver/index.py @@ -593,8 +593,21 @@ def make_v0(apps, apks, repodir, repodict, requestsdict, fdroid_signing_key_fing # Copy the repo icon into the repo directory... icon_dir = os.path.join(repodir, 'icons') - iconfilename = os.path.join(icon_dir, os.path.basename(common.config['repo_icon'])) - shutil.copyfile(common.config['repo_icon'], iconfilename) + repo_icon = common.config.get('repo_icon', common.default_config['repo_icon']) + iconfilename = os.path.join(icon_dir, os.path.basename(repo_icon)) + if os.path.exists(repo_icon): + shutil.copyfile(common.config['repo_icon'], iconfilename) + else: + logging.warning(_('repo_icon %s does not exist, generating placeholder.') + % repo_icon) + os.makedirs(os.path.dirname(iconfilename), exist_ok=True) + try: + import qrcode + qrcode.make(common.config['repo_url']).save(iconfilename) + except Exception: + exampleicon = os.path.join(common.get_examples_dir(), + common.default_config['repo_icon']) + shutil.copy(exampleicon, iconfilename) def extract_pubkey(): diff --git a/tests/index.TestCase b/tests/index.TestCase index 5fb56f10..10a5b9a6 100755 --- a/tests/index.TestCase +++ b/tests/index.TestCase @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import datetime import inspect import logging import optparse @@ -29,17 +30,25 @@ from testcommon import TmpCwd GP_FINGERPRINT = 'B7C2EEFD8DAC7806AF67DFCD92EB18126BC08312A7F2D6F3862E46013C7A6135' +class Options: + nosign = True + pretty = False + verbose = False + + class IndexTest(unittest.TestCase): def setUp(self): logging.basicConfig(level=logging.DEBUG) self.basedir = os.path.join(localmodule, 'tests') + os.chmod(os.path.join(self.basedir, 'config.py'), 0o600) self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) if not os.path.exists(self.tmpdir): os.makedirs(self.tmpdir) os.chdir(self.basedir) fdroidserver.common.config = None + fdroidserver.common.options = Options config = fdroidserver.common.read_config(fdroidserver.common.options) config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner') fdroidserver.common.config = config @@ -215,6 +224,29 @@ class IndexTest(unittest.TestCase): self.maxDiff = None self.assertEqual(json.dumps(i, indent=2), json.dumps(o, indent=2)) + def test_make_v0(self): + tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, + dir=self.tmpdir) + os.chdir(tmptestsdir) + os.mkdir('repo') + repo_icons_dir = os.path.join('repo', 'icons') + self.assertFalse(os.path.isdir(repo_icons_dir)) + repodict = { + 'address': 'https://example.com/fdroid/repo', + 'description': 'This is just a test', + 'icon': 'blahblah', + 'name': 'test', + 'timestamp': datetime.datetime.now(), + 'version': 12, + } + requestsdict = {'install': [], 'uninstall': []} + fdroidserver.common.config['repo_pubkey'] = 'ffffffffffffffffffffffffffffffffff' + fdroidserver.index.make_v0({}, [], 'repo', repodict, requestsdict, []) + self.assertTrue(os.path.isdir(repo_icons_dir)) + self.assertTrue(os.path.exists(os.path.join(repo_icons_dir, + fdroidserver.common.default_config['repo_icon']))) + self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml'))) + if __name__ == "__main__": os.chdir(os.path.dirname(__file__)) @@ -222,7 +254,8 @@ if __name__ == "__main__": parser = optparse.OptionParser() parser.add_option("-v", "--verbose", action="store_true", default=False, help="Spew out even more information than normal") - (fdroidserver.common.options, args) = parser.parse_args(['--verbose']) + (options, args) = parser.parse_args() + Options.verbose = options.verbose newSuite = unittest.TestSuite() newSuite.addTest(unittest.makeSuite(IndexTest))