- Properly removing tmp files

This commit is contained in:
Stepan Oksanichenko 2023-11-10 21:38:01 +02:00
parent ae527a2e01
commit 0a9e5df66c
Signed by: soksanichenko
GPG Key ID: AB9983172AB1E45B

View File

@ -118,15 +118,15 @@ class PackagesGenerator:
self.pkgs = dict() self.pkgs = dict()
self.excluded_packages = excluded_packages self.excluded_packages = excluded_packages
self.included_packages = included_packages self.included_packages = included_packages
self.tmp_files = [] self.tmp_files = [] # type: list[Path]
for arch, arch_list in self.addon_repos.items(): for arch, arch_list in self.addon_repos.items():
self.repo_arches[arch].extend(arch_list) self.repo_arches[arch].extend(arch_list)
self.repo_arches[arch].append(arch) self.repo_arches[arch].append(arch)
def __del__(self): def __del__(self):
for tmp_file in self.tmp_files: for tmp_file in self.tmp_files:
if os.path.exists(tmp_file): if tmp_file.exists():
os.remove(tmp_file) tmp_file.unlink()
@staticmethod @staticmethod
def _get_full_repo_path(repo_info: RepoInfo): def _get_full_repo_path(repo_info: RepoInfo):
@ -149,8 +149,7 @@ class PackagesGenerator:
print(f'Warning message: "{message}"; warning type: "{warning_type}"') print(f'Warning message: "{message}"; warning type: "{warning_type}"')
return True return True
@staticmethod def get_remote_file_content(self, file_url: AnyStr) -> AnyStr:
def get_remote_file_content(file_url: AnyStr) -> AnyStr:
""" """
Get content from a remote file and write it to a temp file Get content from a remote file and write it to a temp file
:param file_url: url of a remote file :param file_url: url of a remote file
@ -163,6 +162,7 @@ class PackagesGenerator:
file_request.raise_for_status() file_request.raise_for_status()
with tempfile.NamedTemporaryFile(delete=False) as file_stream: with tempfile.NamedTemporaryFile(delete=False) as file_stream:
file_stream.write(file_request.content) file_stream.write(file_request.content)
self.tmp_files.append(Path(file_stream.name))
return file_stream.name return file_stream.name
@staticmethod @staticmethod
@ -253,7 +253,6 @@ class PackagesGenerator:
if repo_info.is_remote: if repo_info.is_remote:
repomd_record_file_path = self.get_remote_file_content( repomd_record_file_path = self.get_remote_file_content(
repomd_record_file_path) repomd_record_file_path)
self.tmp_files.append(repomd_record_file_path)
repomd_records_dict[repomd_record.type] = repomd_record_file_path repomd_records_dict[repomd_record.type] = repomd_record_file_path
def _parse_module_repomd_record( def _parse_module_repomd_record(
@ -277,7 +276,6 @@ class PackagesGenerator:
if repo_info.is_remote: if repo_info.is_remote:
repomd_record_file_path = self.get_remote_file_content( repomd_record_file_path = self.get_remote_file_content(
repomd_record_file_path) repomd_record_file_path)
self.tmp_files.append(repomd_record_file_path)
return list(self._parse_modules_file( return list(self._parse_modules_file(
repomd_record_file_path, repomd_record_file_path,
)) ))