Using upsert_file to handle Zim file download.

This commit is contained in:
Roland 2025-02-18 12:19:16 -07:00
parent 15f6e7ed5c
commit c82baa1ad8
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 11 additions and 10 deletions

View file

@ -7,7 +7,7 @@ from wrolpi.downloader import Downloader, Download, DownloadResult
__all__ = ['KiwixCatalogDownloader', 'KiwixZimDownloader', 'kiwix_zim_downloader', 'kiwix_catalog_downloader']
from wrolpi.files.lib import refresh_files
from wrolpi.files.lib import refresh_files, upsert_file
from wrolpi.vars import PYTEST
logger = logger.getChild(__name__)
@ -79,17 +79,16 @@ class KiwixZimDownloader(Downloader):
# Notify the maintainer if outdated Zim files are lying around.
lib.flag_outdated_zim_files()
return_code = await lib.check_zim(output_path)
if return_code == 127:
try:
return_code = await lib.check_zim(output_path)
if return_code > 0:
# zimcheck ran, but the file is invalid.
return DownloadResult(success=False, error=f'Zim file is invalid')
except FileNotFoundError:
logger.warning(f'Not validating {output_path} because zimcheck is not installed')
elif return_code > 0:
return DownloadResult(success=False, error=f'Zim file is invalid')
# Add the new Zim to the FileGroups, model it.
if PYTEST:
await refresh_files([output_path, ])
else:
background_task(refresh_files([output_path, ]))
await upsert_file(output_path)
# Restart Kiwix serve, so it finds the new zim file.
return_code = await lib.restart_kiwix()

View file

@ -472,7 +472,9 @@ async def check_zim(path: pathlib.Path) -> int:
@warning: Only performs the checksum check!
"""
cmd = ['zimcheck', '-C', path.absolute()]
cmd = ('zimcheck', '-C', path.absolute())
cmd_str = " ".join(str(i) for i in cmd)
logger.debug(f'check_zim: {cmd_str}')
result = await run_command(cmd)
logger.debug(f'zimcheck returned {result.return_code}')
if result.return_code != 0 and result.stderr: