Skip to content

Commit

Permalink
Add cleanup process for temporary files created
Browse files Browse the repository at this point in the history
Signed-off-by: Linsho Kaku <[email protected]>
  • Loading branch information
linshokaku committed Nov 16, 2023
1 parent 4834155 commit 6af1a04
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions oras/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import copy
import os
import urllib
from contextlib import contextmanager, nullcontext
from dataclasses import asdict, dataclass
from http.cookiejar import DefaultCookiePolicy
from typing import Callable, List, Optional, Tuple, Union
from tempfile import TemporaryDirectory
from typing import Callable, Generator, List, Optional, Tuple, Union

import jsonschema
import requests
Expand All @@ -25,6 +27,14 @@
container_type = Union[str, oras.container.Container]


@contextmanager
def temporary_empty_config() -> Generator[str, None, None]:
with TemporaryDirectory() as tmpdir:
config_file = oras.utils.get_tmpfile(tmpdir=tmpdir, suffix=".json")
oras.utils.write_file(config_file, "{}")
yield config_file


@dataclass
class Subject:
mediaType: str
Expand Down Expand Up @@ -747,11 +757,10 @@ def push(self, *args, **kwargs) -> requests.Response:

# Config is just another layer blob!
logger.debug(f"Preparing config {conf}")
if config_file is None:
config_file = oras.utils.get_tmpfile(suffix=".json")
with open(config_file, "w") as f:
f.write("{}")
response = self.upload_blob(config_file, container, conf)
with temporary_empty_config() if config_file is None else nullcontext(
config_file
) as config_file:
response = self.upload_blob(config_file, container, conf)

self._check_200_response(response)

Expand Down

0 comments on commit 6af1a04

Please sign in to comment.