Skip to content

Commit 294a578

Browse files
authored
Verify and redownload corrupted .nupkg files (#552)
* Initial solution * Solve review comments * Resolve review comments
1 parent a1376ec commit 294a578

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

lean/components/cloud/module_manager.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,27 @@ def _download_file(self, product_id: int, organization_id: str, package: NuGetPa
116116
:param organization_id: the id of the organization that has a license for the module
117117
:param package: the NuGet package to download
118118
"""
119-
package_file = Path(MODULES_DIRECTORY) / package.get_file_name()
120119

120+
package_file = Path(MODULES_DIRECTORY) / package.get_file_name()
121+
121122
if package_file.is_file():
122-
if product_id not in self._installed_packages:
123-
self._installed_packages[product_id] = []
124-
self._installed_packages[product_id].append(package)
125-
self._logger.debug(f"ModuleManager._download_file(): {package_file} already exists locally")
126-
return
123+
from zipfile import ZipFile, BadZipFile
124+
from contextlib import suppress
125+
with suppress(BadZipFile, IOError):
126+
with ZipFile(package_file, 'r') as zip_ref:
127+
# Verify the integrity of the file
128+
if zip_ref.testzip() is None:
129+
self._logger.debug(f"{package_file.name} exists and passed the integrity check.")
130+
if product_id not in self._installed_packages:
131+
self._installed_packages[product_id] = []
132+
self._installed_packages[product_id].append(package)
133+
self._logger.debug(f"ModuleManager._download_file(): {package_file} already exists locally")
134+
return
135+
136+
self._logger.info(f"{package_file.name} exists but is corrupted. Downloading again...")
137+
138+
from os import remove
139+
remove(package_file)
127140

128141
self._logger.info(f"Downloading '{package_file.name}'")
129142

0 commit comments

Comments
 (0)