[gimp-web/wip/Jehan/issue-236-new-sponsor-page: 17/25] tools: return error code for gimp-check-mirrors.py.




commit 935523dcc8095effa24bb3c04d7deac1023c405b
Author: Jehan <jehan girinstud io>
Date:   Mon Sep 6 21:37:30 2021 +0200

    tools: return error code for gimp-check-mirrors.py.
    
    This would allow for the script to be used in an automated workflow with
    other scripts. Positive errors are common standard errors (for instance
    if input file cannot be read).
    Negative errors are the number of mirror discrepancies (outdated mirror,
    or wrong checksum in particular).
    
    Also adding an exception handling for timeout (it was being recognized
    as a OSError, with a "none" string, which is not so useful).
    
    Finally only check the original file checksum in the --verify-checksum
    case.

 tools/downloads/gimp-check-mirrors.py | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
---
diff --git a/tools/downloads/gimp-check-mirrors.py b/tools/downloads/gimp-check-mirrors.py
index 158aa702..208d3f2e 100755
--- a/tools/downloads/gimp-check-mirrors.py
+++ b/tools/downloads/gimp-check-mirrors.py
@@ -8,6 +8,7 @@ import requests
 from urllib.error import HTTPError, URLError
 from urllib.request import urlretrieve, urlopen
 import concurrent.futures
+import sys
 
 # argparse for mirrorsfile and uri
 parser = argparse.ArgumentParser(description='Check if GIMP download mirrors have a file from 
download.gimp.org.')
@@ -31,9 +32,12 @@ def load_url(url, timeout):
     with requests.head(url, timeout=timeout) as conn:
         return conn
 
-with urlopen('https://download.gimp.org/pub/gimp/' + dgo_uri_local, timeout = 5.0) as remote_file:
-    origin_sum = hashlib.sha256()
-    origin_sum.update(remote_file.read())
+if args.verify_checksum:
+    with urlopen('https://download.gimp.org/pub/gimp/' + dgo_uri_local, timeout = 5.0) as remote_file:
+        origin_sum = hashlib.sha256()
+        origin_sum.update(remote_file.read())
+
+error_count = 0
 
 # read mirrors file
 # fileinput.
@@ -54,11 +58,28 @@ with fileinput.input(files=(args.mirrorsfile), mode='r') as f:
                   checksum_text = ' (checksum OK)'
                 else:
                   checksum_text = ' (checksum KO)'
+                  error_count += 1
 
             print(str(response.status_code) + ' : ' + mirror_uri + checksum_text)
+            if response.status_code != 200:
+                error_count += 1
         except HTTPError as error:
+            error_count += 1
             print(str(error.code) + ' : ' + mirror_uri)
         except URLError as error:
+            error_count += 1
             print(str(error.reason) + ' : ' + mirror_uri)
+        except requests.exceptions.ConnectTimeout as error:
+            error_count += 1
+            print('Timed out: ' + mirror_uri)
         except OSError as error:
+            error_count += 1
             print(str(error.strerror) + ' : ' + mirror_uri)
+    if error_count == 0:
+        sys.exit(os.EX_OK)
+    else:
+        sys.stderr.write("{} errors reported.\n".format(error_count))
+        # Return negative error count as information error code.
+        sys.exit(- error_count)
+
+sys.exit(os.EX_DATAERR)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]