libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [libcloud] 06/06: Make sure we delete file with local cache if the download is incomplete on script exit.
Date Tue, 22 Sep 2020 10:21:22 GMT
This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit 6025c635ac278d147dd9caf363de90e1abcf2b7c
Author: Tomaz Muraus <tomaz@tomaz.me>
AuthorDate: Tue Sep 22 12:20:08 2020 +0200

    Make sure we delete file with local cache if the download is incomplete
    on script exit.
---
 contrib/scrape-ec2-sizes.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/contrib/scrape-ec2-sizes.py b/contrib/scrape-ec2-sizes.py
index c202903..570a02a 100755
--- a/contrib/scrape-ec2-sizes.py
+++ b/contrib/scrape-ec2-sizes.py
@@ -200,14 +200,22 @@ def download_json():
     if os.path.isfile(FILEPATH):
         return open(FILEPATH, 'r')
 
+    def remove_partial_cached_file():
+        if os.path.isfile(FILEPATH):
+            os.remove(FILEPATH)
+
     # File not cached locally, download data and cache it
     with requests.get(URL, stream=True) as response:
+        atexit.register(remove_partial_cached_file)
+
         with open(FILEPATH, 'wb') as fp:
             # NOTE: We use shutil.copyfileobj with large chunk size instead of
             # response.iter_content with large chunk size since data we
             # download is massive and copyfileobj is more efficient.
             shutil.copyfileobj(response.raw, fp, 10 * 1024 * 1024)
 
+        atexit.unregister(remove_partial_cached_file)
+
     return open(FILEPATH, 'r')
 
 


Mime
View raw message