update: handle large, corrupt, or inaccessible fastlane/triple-t files

```
Traceback (most recent call last):
  File "../fdroid", line 22, in <module>
    fdroidserver.__main__.main()
  File "/var/lib/jenkins/userContent/reproducible/reproducible_fdroid_build_apps/fdroidserver/__main__.py", line 227, in main
    raise e
  File "/var/lib/jenkins/userContent/reproducible/reproducible_fdroid_build_apps/fdroidserver/__main__.py", line 208, in main
    mod.main()
  File "/var/lib/jenkins/userContent/reproducible/reproducible_fdroid_build_apps/fdroidserver/update.py", line 2340, in main
    repoapps = prepare_apps(apps, apks, repodirs[0])
  File "/var/lib/jenkins/userContent/reproducible/reproducible_fdroid_build_apps/fdroidserver/update.py", line 2176, in prepare_apps
    copy_triple_t_store_metadata(apps_with_packages)
  File "/var/lib/jenkins/userContent/reproducible/reproducible_fdroid_build_apps/fdroidserver/update.py", line 1076, in copy_triple_t_store_metadata
    _set_author_entry(app, 'authorWebSite', os.path.join(root, f))
  File "/var/lib/jenkins/userContent/reproducible/reproducible_fdroid_build_apps/fdroidserver/update.py", line 784, in _set_author_entry
    with open(f, errors='replace') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'build/player.efis.cfd/pfd/src/main/play/contact-website.txt'
```
This commit is contained in:
Hans-Christoph Steiner 2021-02-11 17:29:48 +01:00
parent 14e9bffedb
commit 525dcb8f98
2 changed files with 100 additions and 14 deletions

View File

@ -768,23 +768,41 @@ def _get_localized_dict(app, locale):
def _set_localized_text_entry(app, locale, key, f):
limit = config['char_limits'][key]
localized = _get_localized_dict(app, locale)
with open(f, errors='replace') as fp:
text = fp.read()[:limit]
if len(text) > 0:
if key in ('name', 'summary', 'video'): # hardcoded as a single line
localized[key] = text.strip('\n')
else:
localized[key] = text
"""Read a fastlane/triple-t metadata file and add an entry to the app
This reads more than the limit, in case there is leading or
trailing whitespace to be stripped
"""
try:
limit = config['char_limits'][key]
localized = _get_localized_dict(app, locale)
with open(f, errors='replace') as fp:
text = fp.read(limit * 2)
if len(text) > 0:
if key in ('name', 'summary', 'video'): # hardcoded as a single line
localized[key] = text.strip('\n')[:limit]
else:
localized[key] = text[:limit]
except Exception as e:
logging.error(_('{path}: {error}').format(path=f, error=str(e)))
def _set_author_entry(app, key, f):
limit = config['char_limits']['author']
with open(f, errors='replace') as fp:
text = fp.read()[:limit]
if len(text) > 0:
app[key] = text.strip()
"""read a fastlane/triple-t author file and add the entry to the app
This reads more than the limit, in case there is leading or
trailing whitespace to be stripped
"""
try:
limit = config['char_limits']['author']
with open(f, errors='replace') as fp:
text = fp.read(limit * 2)
if len(text) > 0:
app[key] = text.strip()[:limit]
except Exception as e:
logging.error(_('{path}: {error}').format(path=f, error=str(e)))
def _strip_and_copy_image(in_file, outpath):

View File

@ -11,6 +11,7 @@ import optparse
import os
import random
import shutil
import string
import subprocess
import sys
import tempfile
@ -1309,6 +1310,73 @@ class UpdateTest(unittest.TestCase):
self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(' WhyIncludeWhitespace '))
self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(['first', 'second']))
def test_set_localized_text_entry(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name,
dir=self.tmpdir)
os.chdir(tmptestsdir)
config = dict()
fdroidserver.common.fill_config_defaults(config)
fdroidserver.update.config = config
fdroidserver.update.options = fdroidserver.common.options
files = {
'full-description.txt': 'description',
'short-description.txt': 'summary',
'title.txt': 'name',
'video-url.txt': 'video',
}
for f, key in files.items():
limit = config['char_limits'][key]
with open(f, 'w') as fp:
fp.write(''.join(random.choice(string.ascii_letters) for i in range(limit + 100)))
locale = 'ru_US'
app = dict()
fdroidserver.update._set_localized_text_entry(app, locale, key, f)
self.assertEqual(limit, len(app['localized'][locale][key]))
f = 'badlink-' + f
os.symlink('/path/to/nowhere', f)
app = dict()
fdroidserver.update._set_localized_text_entry(app, locale, key, f)
self.assertIsNone(app['localized'].get(locale, {}).get(key))
def test_set_author_entry(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name,
dir=self.tmpdir)
os.chdir(tmptestsdir)
config = dict()
fdroidserver.common.fill_config_defaults(config)
fdroidserver.update.config = config
fdroidserver.update.options = fdroidserver.common.options
f = 'contact-website.txt'
key = 'author'
url = 'https://f-droid.org/'
limit = config['char_limits']['author']
with open(f, 'w') as fp:
fp.write(url)
fp.write('\n')
app = dict()
fdroidserver.update._set_author_entry(app, key, f)
self.assertEqual(url, app[key])
f = 'limits.txt'
key = 'author'
limit = config['char_limits']['author']
for key in ('authorEmail', 'authorPhone', 'authorWebSite'):
with open(f, 'w') as fp:
fp.write(''.join(random.choice(string.ascii_letters) for i in range(limit + 100)))
app = dict()
fdroidserver.update._set_author_entry(app, key, f)
self.assertEqual(limit, len(app[key]))
f = 'badlink.txt'
os.symlink('/path/to/nowhere', f)
app = dict()
fdroidserver.update._set_author_entry(app, key, f)
self.assertIsNone(app.get(key))
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))