Blame SOURCES/0021-Better-error-message-for-dnf-copr-enable.patch

92f559
From 1d097d0e4ecfef78aec5d760278b44d5f3192cdc Mon Sep 17 00:00:00 2001
92f559
From: Silvie Chlupova <sisi.chlupova@gmail.com>
92f559
Date: Mon, 2 Aug 2021 15:04:17 +0200
92f559
Subject: [PATCH] Better error message for dnf copr enable
92f559
92f559
= changelog =
92f559
msg: show better error message if the command dnf copr enable fails
92f559
type: enhancement
92f559
---
92f559
 plugins/copr.py | 63 +++++++++++++++++++++++++++++++------------------
92f559
 1 file changed, 40 insertions(+), 23 deletions(-)
92f559
92f559
diff --git a/plugins/copr.py b/plugins/copr.py
92f559
index 1539c0d..721c010 100644
92f559
--- a/plugins/copr.py
92f559
+++ b/plugins/copr.py
92f559
@@ -27,6 +27,8 @@ import re
92f559
 import shutil
92f559
 import stat
92f559
 import sys
92f559
+import base64
92f559
+import json
92f559
 
92f559
 from dnfpluginscore import _, logger
92f559
 import dnf
92f559
@@ -70,10 +72,10 @@ NO = set([_('no'), _('n'), ''])
92f559
 
92f559
 if PY3:
92f559
     from configparser import ConfigParser, NoOptionError, NoSectionError
92f559
-    from urllib.request import urlopen, HTTPError
92f559
+    from urllib.request import urlopen, HTTPError, URLError
92f559
 else:
92f559
     from ConfigParser import ConfigParser, NoOptionError, NoSectionError
92f559
-    from urllib2 import urlopen, HTTPError
92f559
+    from urllib2 import urlopen, HTTPError, URLError
92f559
 
92f559
 @dnf.plugin.register_command
92f559
 class CoprCommand(dnf.cli.Command):
92f559
@@ -475,28 +477,40 @@ Bugzilla. In case of problems, contact the owner of this repository.
92f559
         api_path = "/coprs/{0}/repo/{1}/dnf.repo?arch={2}".format(project_name, short_chroot, arch)
92f559
 
92f559
         try:
92f559
-            f = self.base.urlopen(self.copr_url + api_path, mode='w+')
92f559
-        except IOError as e:
92f559
+            response = urlopen(self.copr_url + api_path)
92f559
             if os.path.exists(repo_filename):
92f559
                 os.remove(repo_filename)
92f559
-            if '404' in str(e):
92f559
-                try:
92f559
-                    res = urlopen(self.copr_url + "/coprs/" + project_name)
92f559
-                    status_code = res.getcode()
92f559
-                except HTTPError as e:
92f559
-                    status_code = e.getcode()
92f559
-                if str(status_code) != '404':
92f559
-                    raise dnf.exceptions.Error(_("This repository does not have"
92f559
-                                                 " any builds yet so you cannot enable it now."))
92f559
-                else:
92f559
-                    raise dnf.exceptions.Error(_("Such repository does not exist."))
92f559
-            raise
92f559
-
92f559
-        for line in f:
92f559
-            if re.match(r"\[copr:", line):
92f559
-                repo_filename = os.path.join(self.base.conf.get_reposdir,
92f559
-                                             "_" + line[1:-2] + ".repo")
92f559
-            break
92f559
+        except HTTPError as e:
92f559
+            if e.code != 404:
92f559
+                error_msg = _("Request to {0} failed: {1} - {2}").format(self.copr_url + api_path, e.code, str(e))
92f559
+                raise dnf.exceptions.Error(error_msg)
92f559
+            error_msg = _("It wasn't possible to enable this project.\n")
92f559
+            error_data = e.headers.get("Copr-Error-Data")
92f559
+            if error_data:
92f559
+                error_data_decoded = base64.b64decode(error_data).decode('utf-8')
92f559
+                error_data_decoded = json.loads(error_data_decoded)
92f559
+                error_msg += _("Repository '{0}' does not exist in project '{1}'.").format(
92f559
+                    '-'.join(self.chroot_parts), project_name)
92f559
+                if error_data_decoded.get("available chroots"):
92f559
+                    error_msg += _("\nAvailable repositories: ") + ', '.join(
92f559
+                        "'{}'".format(x) for x in error_data_decoded["available chroots"])
92f559
+                    error_msg += _("\n\nIf you want to enable a non-default repository, use the following command:\n"
92f559
+                                   "  'dnf copr enable {0} <repository>'\n"
92f559
+                                   "But note that the installed repo file will likely need a manual "
92f559
+                                   "modification.").format(project_name)
92f559
+                raise dnf.exceptions.Error(error_msg)
92f559
+            else:
92f559
+                error_msg += _("Project {0} does not exist.").format(project_name)
92f559
+                raise dnf.exceptions.Error(error_msg)
92f559
+        except URLError as e:
92f559
+            error_msg = _("Failed to connect to {0}: {1}").format(self.copr_url + api_path, e.reason.strerror)
92f559
+            raise dnf.exceptions.Error(error_msg)
92f559
+
92f559
+        # Try to read the first line, and detect the repo_filename from that (override the repo_filename value).
92f559
+        first_line = response.readline()
92f559
+        line = first_line.decode("utf-8")
92f559
+        if re.match(r"\[copr:", line):
92f559
+            repo_filename = os.path.join(self.base.conf.get_reposdir, "_" + line[1:-2] + ".repo")
92f559
 
92f559
         # if using default hub, remove possible old repofile
92f559
         if self.copr_url == self.default_url:
92f559
@@ -507,7 +521,10 @@ Bugzilla. In case of problems, contact the owner of this repository.
92f559
             if os.path.exists(old_repo_filename):
92f559
                 os.remove(old_repo_filename)
92f559
 
92f559
-        shutil.copy2(f.name, repo_filename)
92f559
+        with open(repo_filename, 'wb') as f:
92f559
+            f.write(first_line)
92f559
+            for line in response.readlines():
92f559
+                f.write(line)
92f559
         os.chmod(repo_filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
92f559
 
92f559
     def _runtime_deps_warning(self, copr_username, copr_projectname):
92f559
-- 
92f559
2.36.1
92f559