[gimp-web] tools: improve update-mirrors script.



commit 57201fd5d4056605c434d0fcddf755d970d27df2
Author: Jehan <jehan girinstud io>
Date:   Tue Sep 20 21:54:04 2022 +0200

    tools: improve update-mirrors script.
    
    - Makes --keep-oc a boolean option. If is the option is unset, its value
      will depends on --no-download-oc (in particular, when not downloading
      the OpenShift client, let's assume we are keeping the existing one).
    - Better error message when `oc` already exists, by proposing to run
      with --no-download-oc (as alternative to deleting the binary).
    - Log-in message can happen as early as when trying to switch to "gimp"
      project.
    - Handling case when the mirrors.json file is invalid, properly
      displaying the json decoding error by catching the appropriate
      exception.

 tools/downloads/update-mirrors.py | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
---
diff --git a/tools/downloads/update-mirrors.py b/tools/downloads/update-mirrors.py
index 69898dcd..aa674f08 100755
--- a/tools/downloads/update-mirrors.py
+++ b/tools/downloads/update-mirrors.py
@@ -2,6 +2,7 @@
 
 import argparse
 import importlib
+import json
 import os
 import os.path
 import requests
@@ -14,19 +15,25 @@ parser.add_argument('--mirrorsfile', metavar='<file>', default=os.path.dirname(_
                     help='A file with one download mirror per line, either https:// or http://. This script 
will override the existing file.')
 parser.add_argument('--oc-token', metavar='<oc-token>', default=None, required=False,
                     help='Your OpenShift connection token.')
-parser.add_argument('--keep-oc', action='store_true',
-                    help='Do not delete the downloaded oc client.')
+parser.add_argument('--keep-oc', action=argparse.BooleanOptionalAction,
+                    help='Keep or delete the downloaded oc client.')
 parser.add_argument('--no-download-oc', dest='download_oc', action='store_false',
-                    help='Use a previously downloaded oc client.')
+                    help='Use a previously downloaded oc client. It implies --keep-oc unless --no-keep-oc is 
set.')
 
 args = parser.parse_args()
 
 oc = os.path.dirname(__file__) + '/oc'
 oc_tar = oc + '.tar'
 
+if args.keep_oc is None:
+  if args.download_oc:
+    args.keep_oc = False
+  else:
+    args.keep_oc = True
+
 if args.download_oc:
   if os.path.exists(oc):
-    sys.stderr.write('The file "{}" already exists. Please delete it.\n'.format(oc))
+    sys.stderr.write('The file "{}" already exists. Please delete it or run with 
--no-download-oc.\n'.format(oc))
     sys.exit(os.EX_CANTCREAT)
   if os.path.exists(oc_tar):
     sys.stderr.write('The file "{}" already exists. Please delete it.\n'.format(oc_tar))
@@ -64,7 +71,14 @@ print('Switch to GIMP project…')
 try:
   subprocess.check_call(oc + " project gimp", shell=True)
 except subprocess.CalledProcessError:
-  sys.stderr.write('Failed to switch to "gimp" project. Do you have access?\n')
+  sys.stderr.write('\nOpenShift command failed. Are you logged-in?\n')
+  sys.stderr.write('* Go to https://console-openshift-console.apps.openshift4.gnome.org/\n')
+  sys.stderr.write('* Log-in in your browser.\n')
+  sys.stderr.write('* Click your name in top-right of OpenShift page.\n')
+  sys.stderr.write('* Select "Copy login command".\n')
+  sys.stderr.write('* Click "Display Token".\n')
+  sys.stderr.write('* Call me again adding --oc-token <the-copied-token>.\n\n')
+  sys.stderr.write('If you are logged-in already, failure to switch to "gimp" project may indicate an access 
issue.\n')
   sys.exit(os.EX_NOUSER)
 
 sys.stdout.write('Obtaining last pod name…')
@@ -107,10 +121,15 @@ with open(args.mirrorsfile, 'wb') as f:
 print("Mirrors updated in {}.".format(args.mirrorsfile))
 
 mirrorsjson = os.path.dirname(__file__) + '/../../content/downloads/mirrors.json'
+mirrorsjson = os.path.realpath(mirrorsjson)
 print("Now comparing to {} ...".format(mirrorsjson))
 
 cmp = importlib.import_module("cmp-mirrors")
-same = cmp.cmp_mirror_files(args.mirrorsfile, mirrorsjson)
+try:
+  same = cmp.cmp_mirror_files(args.mirrorsfile, mirrorsjson)
+except json.decoder.JSONDecodeError as error:
+  sys.stderr.write("Invalid JSON file '{}': {}\n".format(mirrorsjson, error))
+  sys.exit(os.EX_DATAERR)
 
 if not args.keep_oc:
   os.remove(oc)


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