#9 Comment/karma submit: just use bodhi-client CLI called by os.system
Closed 4 years ago by kparal. Opened 4 years ago by frantisekz.
frantisekz/fedora-easy-karma master  into  master

file modified
+29 -19
@@ -30,12 +30,14 @@ 

  import re

  

  from optparse import OptionParser

+ from shlex import quote

  from textwrap import wrap

  

  # extra python modules

  # Used to catch fedora.client.AuthError and fedora.client.ServerError

  # exceptions

  import fedora

+ import bodhi

  

  # fedora_cert is optional. It is only used to get the real fas_username, which

  # is also supplied as a command line option and eventually in a config file.
@@ -190,8 +192,6 @@ 

  

                  # the format of the user has changed, add a data member

                  comment["username"] = comment["user"]["name"]

-                 if comment["anonymous"]:

-                     comment["username"] += " (unauthenticated)"

  

                  comments.append(

                      "%(indent)s%(username)s - %(timestamp)s "
@@ -222,6 +222,7 @@ 

              values["karma_status"] = "%d/%s" % (values["karma"],

                                                  values["stable_karma"])

  

+         values["submitter"] = values["user"]["name"]

          return format_string % values

  

      @staticmethod
@@ -653,7 +654,7 @@ 

  

              # Ignore own updates

              if self.options.ignore_own and \

-                     update["submitter"] == self.options.fas_username:

+                     update["user"]["name"] == self.options.fas_username:

                  continue

  

              if update not in processed_updates and \
@@ -773,7 +774,7 @@ 

  

      def already_commented(self, update, user):

          for comment in update["comments"]:

-             if not comment["anonymous"] and comment["user"]["name"] == user:

+             if comment["user"]["name"] == user:

                  return True

          return False

  
@@ -828,22 +829,31 @@ 

                      pass

  

      def send_comment(self, bc, update, comment, karma):

-         orig_retries = bc.retries

-         bc.retries = 1

-         for retry in range(0, self.options.retries + 1):

+         # Prepare command for bodhi CLI

+         command = "bodhi updates comment --karma {} --user {} {} {}".format(

+             karma, self.options.fas_username, update["updateid"], quote(comment)

+         )

+         res = os.system(command)

+         if res != 0:

+             # If return code isn't 0, something went wrong, try to explain or workaround some cases

+             if res == 1:

+                 return (False, 'Failed to connect to bodhi, is your internet connection working?')

+             # Try to workaround https://github.com/fedora-infra/bodhi/issues/3298

+             homedir = os.path.expanduser("~")

+             if not homedir:

+                 # Bail out if we weren't able to get homedir

+                 return (False, 'Failed to get homedir')

+             self.warning("Working around https://github.com/fedora-infra/bodhi/issues/3298 ...")

              try:

-                 res = bc.comment(update["title"], comment, karma=karma)

-                 bc.retries = orig_retries

-                 return (True, res)

-             except fedora.client.AuthError as e:

-                 self.warning("Authentication error")

-                 bc.password = getpass.getpass('FAS password for %s: ' %

-                                               self.options.fas_username)

-             except fedora.client.ServerError as e:

-                 self.warning("Server error: %s" % str(e))

- 

-         bc.retries = orig_retries

-         return (False, 'too many errors')

+                 os.remove(homedir + '/.fedora/openidbaseclient-sessions.cache')

+             except FileNotFoundError:

+                 return (False, 'Failed to remove ~/.fedora/openidbaseclient-sessions.cache')

+             res = os.system(command)

+             if res != 0:

+                 return (False, 'Failed to submit comment and karma')

+             return (True, res)

+ 

+         return (True, res)

  

  

  if __name__ == "__main__":

This might be useful as a downstream patch for fedora-easy-karma, but more testing is welcome (or rather needed).

Attempts to fix: https://github.com/fedora-infra/bodhi/issues/3298

Replaces bc.comment call with os.system("bodhi updates comment ") , removes ~/.fedora/openidbaseclient-sessions.cache if submit failed and tries again.
Looks like it fixes issues with f-e-k for me. Ugly hack that seems to get the work done :)

Includes: https://pagure.io/fedora-easy-karma/pull-request/8
Obsoletes: https://pagure.io/fedora-easy-karma/pull-request/7

rebased onto 07fd234

4 years ago

rebased onto 6e5a6ae

4 years ago

so looking at https://github.com/fedora-infra/bodhi/issues/3298 , @kparal said back in July that clearing a cache file helped for him - is that not working for everyone?

My experience was that I had to clear cache every few days or weeks, the error seemed to pop up at random intervals. This PR does just that, if a submission error occurs, it removes auth tokens and tries again.

The karma patch by frantisekz works for me and I can use FEK again.

This is now rebased as #11. Sorry for yet another PR, but I figured it will be easier for us to collaborate on it when it is a local branch.

Pull-Request has been closed by kparal

4 years ago
Metadata