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

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