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

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