From 1ddc115dc05152ca1bb420f06c1da1b36f57a8d7 Mon Sep 17 00:00:00 2001 From: Bruno Bord Date: Fri, 4 Oct 2019 10:38:59 +0200 Subject: [PATCH] Warn user when there's a download error. Expiration date file won't be modified if at least one of the downloads has failed. --- CHANGELOG.md | 1 + download.py | 33 +++++++++++++++++++++++++-------- setup.cfg | 2 ++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8731167..108b0e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Generate a catalog of the expiration dates for files, * Detect when a file has expired and raise a ``UserWarning`` * Add Travis CI badge on README. +* Warn user when there's a download error. Expiration date file won't be modified if at least one of the downloads has failed. ## 0.0.2 (2019-08-23) diff --git a/download.py b/download.py index e2eb010..2fde3f4 100644 --- a/download.py +++ b/download.py @@ -2,10 +2,14 @@ import argparse from datetime import date from datetime import datetime, timedelta -from os.path import join, exists, abspath, dirname +from os.path import join, exists, abspath, dirname, basename import shutil from urllib.request import urlopen from jplephem.spk import DAF, SPK +from colorama import init +from termcolor import colored + +init() JPL = "ftp://ssd.jpl.nasa.gov/pub/eph/planets/bsp" USNO = "http://maia.usno.navy.mil/ser7" @@ -126,9 +130,17 @@ def leap_seconds_expiration(fileobj): def download(url, target): "Download (binary) ``url`` and save it to ``target``." print("URL: {}".format(url)) - with urlopen(url) as response: - with open(target, "wb") as fd: - shutil.copyfileobj(response, fd) + try: + with urlopen(url) as response: + with open(target, "wb") as fd: + shutil.copyfileobj(response, fd) + return True + except Exception as exc: + msg = "*** Error: {} couldn't be downloaded ({})".format( + basename(target), exc + ) + print(colored(msg, 'red')) + return False def get_expiration_date(target, params): @@ -192,6 +204,7 @@ def main(args): target_expiration = abspath( join(__DATA_PATH, '..', 'expiration_data.py') ) + success = True expiration_dates = {} for filename, params in items.items(): @@ -218,16 +231,20 @@ def main(args): if should_i_download: print("Downloading {} ({})".format(filename, reason)) - download(url, target) + success = download(url, target) and success else: print("Skipping {} ({})".format(filename, reason)) + # Generating the expiration date file. - expiration_template = """import datetime + if success: # only in case of a success + expiration_template = """import datetime EXPIRATIONS = {} """ - with open(target_expiration, 'w') as fd: - fd.write(expiration_template.format(expiration_dates)) + with open(target_expiration, 'w') as fd: + fd.write(expiration_template.format(expiration_dates)) + else: + print("** Skipped expiration generation: failed to download.") print("\nDone\n") diff --git a/setup.cfg b/setup.cfg index 823ecbf..b21d3c5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,6 +37,8 @@ dev = jplephem numpy tox + colorama + termcolor tests = pytest skyfield