#51 Convert to python 3
Merged 4 years ago by tkopecek. Opened 4 years ago by alexi.
alexi/koji-tools top  into  master

Convert to python 3
Alex Iribarren • 4 years ago  
file modified
+18 -15
@@ -1,4 +1,4 @@ 

- #!/usr/bin/env python

+ #!/usr/bin/python3

  

  import koji

  import xml.dom.minidom
@@ -19,17 +19,20 @@ 

  

  def value(v):

      if "string" in v:

-         v = '"%s"' % (v["string"][0],)

+         try:

+             v = '"%s"' % (list(v["string"][0].values())[0][0],)

+         except IndexError:

+             v = '""'

      elif "boolean" in v:

-         v = v["boolean"][0]

+         v = list(v["boolean"][0].values())[0][0]

          if v.lower() in ["", "0", "false", "no"]:

              v = "False"

          elif v.lower() in ["1", "true", "yes"]:

              v = "True"

      elif "int" in v:

-         v = "%d" % (int(v["int"][0]))

+         v = "%d" % (int(list(v["int"][0].values())[0][0]))

      elif "double" in v:

-         v = "%f" % (float(v["double"][0]))

+         v = "%f" % (float(list(v["double"][0].values())[0][0]))

      elif "nil" in v:

          v = "nil"

      elif "array" in v:
@@ -38,7 +41,7 @@ 

          assert len(v["struct"]) == 1

          s = []

          for m in v["struct"][0].get("member", []):

-             s.append((m["name"][0], value(m["value"][0])))

+             s.append((list(m["name"][0].values())[0][0], value(m["value"][0])))

          v = "{%s}" % (", ".join("%s=%s" % (n, v) for n, v in s),)

      else:

          raise Exception("gargh %s" % (pprint.pformat(v)))
@@ -58,8 +61,8 @@ 

  def xmltodict(xmlstring):

      doc = xml.dom.minidom.parseString(xmlstring)

      d = ettodict(doc.documentElement)

-     if d.keys() == ["methodCall"]:

-         return "%s(%s)" % (d["methodCall"]["methodName"][0],

+     if "methodCall" in d:

+         return "%s(%s)" % (list(d["methodCall"]["methodName"][0].values())[0][0],

                             ", ".join(params(d)))

      else:

          return d
@@ -146,24 +149,24 @@ 

  for hn in sorted(hostbyname):

      hi = hostinfo[hn]

      load = sum(float(t["weight"]) for t in taskbyhost[hostbyname[hn]])

-     print "%s  %.1f total %.1f/%.1f %s" % (hn, load, float(hi["task_load"]), float(hi["capacity"]), "R" if hi["ready"] else "-")

+     print("%s  %.1f total %.1f/%.1f %s" % (hn, load, float(hi["task_load"]), float(hi["capacity"]), "R" if hi["ready"] else "-"))

      for t in sorted(taskbyhost[hostbyname[hn]], key=lambda x: -float(x["weight"])):

-         print "  %s %8d %.1f %6s %7s %s" % (

+         print("  %s %8d %.1f %6s %7s %s" % (

              koji.TASK_STATES[t["state"]][:2],

              int(t["id"]),

              float(t["weight"]),

              howlong(t["start_time"]),

              t["arch"],

-             pretty(t["request"]))

-     print ""

+             pretty(t["request"])))

+     print()

  

  states = [koji.TASK_STATES["FREE"]]

  tasks = session.listTasks(opts={"state": states, "channel_id": channel})

  

  tasks_by_arch = count_uniq((t["method"], t["arch"]) for t in tasks)

- print "Tasks available:"

+ print("Tasks available:")

  if tasks_by_arch == []:

-     print "    (none)"

+     print("    (none)")

  else:

      for m, a in sorted(tasks_by_arch):

-         print "    %3d %7s %s" % (tasks_by_arch[m, a], a, m)

+         print("    %3d %7s %s" % (tasks_by_arch[m, a], a, m))

I miss kojitop since our admin machines moved to CentOS 8. This commit ports this tool to python3 so I can enjoy it again.

Commit b700c74 fixes this pull-request

Pull-Request has been merged by tkopecek

4 years ago

Pull-Request has been merged by tkopecek

4 years ago
Metadata