From 27b537dd0c8d37df25b735b22ddfa01dbb82d65d Mon Sep 17 00:00:00 2001 From: Angel <1966894-1024mb@users.noreply.gitlab.com> Date: Mon, 8 Jan 2024 20:25:24 -0500 Subject: [PATCH] fix: fix existing icon detection for repo and archive --- fdroidserver/index.py | 45 +++++++++++++++++++++++------------------- fdroidserver/lint.py | 1 - fdroidserver/update.py | 22 ++++++++++----------- tests/test_index.py | 33 +++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 32 deletions(-) diff --git a/fdroidserver/index.py b/fdroidserver/index.py index b527d6da..05295f0e 100644 --- a/fdroidserver/index.py +++ b/fdroidserver/index.py @@ -146,6 +146,7 @@ def make(apps, apks, repodir, archive): signer_fingerprints = load_publish_signer_fingerprints() make_v0(sortedapps, apks, repodir, repodict, requestsdict, signer_fingerprints) + copy_repo_icon(repodir) make_v1(sortedapps, apks, repodir, repodict, requestsdict, signer_fingerprints) make_v2( sortedapps, apks, repodir, repodict, requestsdict, signer_fingerprints, archive @@ -1465,34 +1466,38 @@ def make_v0(apps, apks, repodir, repodict, requestsdict, signer_fingerprints): signindex.config = common.config signindex.sign_jar(signed, use_old_algs=True) - # Copy the repo icon into the repo directory... + +def copy_repo_icon(repodir): icon_dir = os.path.join(repodir, 'icons') repo_icon = os.path.basename( common.config.get('repo_icon', common.default_config['repo_icon']) ) icon_path = os.path.join(icon_dir, repo_icon) if not os.path.exists(icon_path): - logging.warning( - _( - 'repo_icon "{repo_icon}" does not exist in "{icon_dir}", generating placeholder.' - ).format(repo_icon=repo_icon, icon_dir=icon_dir) - ) os.makedirs(icon_dir, exist_ok=True) - try: - import qrcode - - qrcode.make(common.config['repo_url']).save(icon_path) - except Exception as e: - if isinstance(e, ModuleNotFoundError): - logging.error( - _( - 'The "qrcode" Python package is not installed (e.g. apt-get install python3-qrcode)!' - ) - ) - exampleicon = os.path.join( - common.get_examples_dir(), common.default_config['repo_icon'] + if os.path.exists(repo_icon): + shutil.copy(repo_icon, icon_path) + else: + logging.warning( + _( + 'repo_icon "{repo_icon}" does not exist in "{icon_dir}", generating placeholder.' + ).format(repo_icon=repo_icon, icon_dir=icon_dir) ) - shutil.copy(exampleicon, icon_path) + try: + import qrcode + + qrcode.make(common.config['repo_url']).save(icon_path) + except Exception as e: + if isinstance(e, ModuleNotFoundError): + logging.error( + _( + 'The "qrcode" Python package is not installed (e.g. apt-get install python3-qrcode)!' + ) + ) + exampleicon = os.path.join( + common.get_examples_dir(), common.default_config['repo_icon'] + ) + shutil.copy(exampleicon, icon_path) def extract_pubkey(): diff --git a/fdroidserver/lint.py b/fdroidserver/lint.py index 65326e48..66bc6505 100644 --- a/fdroidserver/lint.py +++ b/fdroidserver/lint.py @@ -1691,7 +1691,6 @@ CHECK_CONFIG_KEYS = ( 'apk_signing_key_block_list', 'archive', 'archive_description', - 'archive_icon', 'archive_name', 'archive_older', 'archive_url', diff --git a/fdroidserver/update.py b/fdroidserver/update.py index 1569a640..d37c36b7 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -2796,17 +2796,17 @@ def main(): options.clean = True # check that icons exist now, rather than fail at the end of `fdroid update` - for k in ['repo_icon', 'archive_icon']: - if k in config: - icon_dir = os.path.join(k.split('_')[0], 'icons') - repo_icon = os.path.basename(common.config.get(k, '')) - icon_path = os.path.join(icon_dir, repo_icon) - if not os.path.exists(icon_path): - logging.warning( - _( - '{name} "{repo_icon}" does not exist in "{icon_dir}/"! Check "config.yml".' - ).format(name=k, icon_dir=icon_dir, repo_icon=repo_icon) - ) + icon_key = 'repo_icon' + if icon_key in config: + icon_dir = os.path.join(repodirs[0], 'icons') + repo_icon = os.path.basename(config.get(icon_key, '')) + icon_path = os.path.join(icon_dir, repo_icon) + if not os.path.exists(icon_path): + logging.warning( + _( + '{name} "{repo_icon}" does not exist in "{icon_dir}/"! Check "config.yml".' + ).format(name=icon_key, icon_dir=icon_dir, repo_icon=repo_icon) + ) # if the user asks to create a keystore, do it now, reusing whatever it can if options.create_key: diff --git a/tests/test_index.py b/tests/test_index.py index 49781fa5..899a08cb 100755 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -414,6 +414,8 @@ class IndexTest(unittest.TestCase): requestsdict = {'install': [], 'uninstall': []} common.config['repo_pubkey'] = 'ffffffffffffffffffffffffffffffffff' index.make_v0({}, [], 'repo', repodict, requestsdict, {}) + with self.assertLogs(): + index.copy_repo_icon('repo') self.assertTrue(os.path.isdir(repo_icons_dir)) self.assertTrue( os.path.exists( @@ -422,6 +424,35 @@ class IndexTest(unittest.TestCase): ) self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml'))) + def test_generate_repo_icon(self): + os.chdir(self.testdir) + os.mkdir('repo') + repo_icons_dir = os.path.join('repo', 'icons') + generated_path = os.path.join( + repo_icons_dir, common.default_config['repo_icon'] + ) + self.assertFalse(os.path.isdir(repo_icons_dir)) + self.assertFalse(os.path.exists(generated_path)) + with self.assertLogs(): + index.copy_repo_icon('repo') + self.assertTrue(os.path.isdir(repo_icons_dir)) + self.assertTrue(os.path.exists(generated_path)) + + def test_copy_repo_icon(self): + os.chdir(self.testdir) + os.mkdir('repo') + repo_icons_dir = Path('repo/icons') + self.assertFalse(os.path.isdir(repo_icons_dir)) + common.config['repo_icon'] = 'test_icon.png' + test_value = 'test' + Path(common.config['repo_icon']).write_text(test_value) + copied_path = repo_icons_dir / os.path.basename(common.config['repo_icon']) + self.assertFalse(os.path.isdir(repo_icons_dir)) + self.assertFalse(os.path.exists(copied_path)) + index.copy_repo_icon('repo') + self.assertTrue(os.path.isdir(repo_icons_dir)) + self.assertEqual(test_value, copied_path.read_text()) + def test_make_v0(self): os.chdir(self.testdir) os.mkdir('metadata') @@ -477,6 +508,8 @@ class IndexTest(unittest.TestCase): common.config['repo_pubkey'] = 'ffffffffffffffffffffffffffffffffff' common.config['make_current_version_link'] = True index.make_v0(apps, [apk], 'repo', repodict, requestsdict, {}) + with self.assertLogs(): + index.copy_repo_icon('repo') self.assertTrue(os.path.isdir(repo_icons_dir)) self.assertTrue( os.path.exists(