[PATCH 04/21] add-committer-certs: improve performance

Felipe Contreras felipe.contreras at gmail.com
Sun May 13 10:14:12 EDT 2012


Before: 524.70s user 121.86s system 87% cpu 12:17.71 total
After: 0.73s user 0.11s system 100% cpu 0.833 total

No functional changes.

Let's not make our life more difficult, shall we?

Signed-off-by: Felipe Contreras <felipe.contreras at gmail.com>
---
 add-committer-certs.py |   94 ++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 52 deletions(-)

diff --git a/add-committer-certs.py b/add-committer-certs.py
index 26dbd0a..3ef63ac 100755
--- a/add-committer-certs.py
+++ b/add-committer-certs.py
@@ -3,6 +3,7 @@
 
 import os
 import sys
+import subprocess
 
 sys.stdout.write("Adding committer certs based on changelog cert signers...")
 sys.stdout.flush()
@@ -32,58 +33,47 @@ with open("authormap", "r") as f:
         if author and name:
             author_map[author] = name
 
-keys = {}
-cmd = mtn + "automate keys"
-with os.popen(cmd, "r") as pipe:
-    key_hash = None
-    for l in pipe.readlines():
-        if l.startswith("           hash ["):
-            key_hash = l[17:-2]
-        elif l.startswith("            hash ["):
-            key_hash = l[18:-2]
-        elif l.startswith('     given_name "'):
-            keys[key_hash] = l[17:-2]
-        elif l.startswith('      given_name "'):
-            keys[key_hash] = l[18:-2]
+# find missing authors
+cmd = "{mtn} db execute 'select lower(hex(r.id)),sum(c.name==\"author\") from revisions r join revision_certs c on r.id=c.revision_id group by r.id'"
+pipe = subprocess.Popen(cmd.format(mtn=mtn), shell=True, stdout=subprocess.PIPE)
+for l in pipe.stdout.readlines():
+    tmp = l.rstrip().split(' | ')
+    if len(tmp) != 2:
+        continue
+    revision, authors = tmp
+    if int(authors) == 0:
+        print "{revision} has no author.".format(revision=revision)
 
-# Add a "committer" cert for each revision based on the signer of the changelog
-# cert.  This will be picked up by our patched `hg convert`.
-cmd = "{mtn} automate roots ; {mtn} automate descendents `{mtn} automate roots`".format(mtn=mtn)
-with os.popen(cmd, "r") as pipe:
-    for l in pipe.readlines():
-        revision = l.strip()
-        cmd = "{mtn} automate certs {revision}".format(mtn=mtn, revision=revision)
-        with os.popen(cmd, "r") as pipe2:
-            key_hash = None
-            signer = None
-            found_author = False
-            author = None
-            for l in pipe2.readlines():
-                if l.startswith("      key ["):
-                    key_hash = l[11:-2]
-                elif l == '     name "changelog"\n':
-                    signer = keys[key_hash]
-                elif l == '     name "author"\n':
-                    found_author = True
-                elif found_author and l.startswith('    value "'):
-                    found_author = False
-                    author = l[11:-2]
-            if signer and not signer.startswith("tailor@"):
-                if not author:
-                    print "{revision} has no author.".format(revision=revision)
-                    continue
-                try:
-                    signer = author_map[signer]
-                except KeyError:
-                    print "Missing signer: {signer}".format(signer=signer)
-                try:
-                    author = author_map[author]
-                except KeyError:
-                    print "Missing author: {author}".format(author=author)
-                if signer != author:
-                    cmd = "{mtn} cert {revision} committer {signer}".format(
-                        mtn=mtn, revision=revision,
-                        signer=shellquote(signer))
-                    os.system(cmd)
+revisions = {}
+
+# find missing committers
+cmd = "{mtn} db execute 'select lower(hex(cc.revision_id)),k.name,ca.value from revision_certs cc, revision_certs ca " \
+        "join public_keys k on k.id = cc.keypair_id where cc.revision_id=ca.revision_id and cc.name=\"changelog\" and ca.name=\"author\"'"
+pipe = subprocess.Popen(cmd.format(mtn=mtn), shell=True, stdout=subprocess.PIPE)
+for l in pipe.stdout.readlines():
+    tmp = l.rstrip().split(' | ')
+    if len(tmp) != 3:
+        continue
+    revision, signer, author = tmp
+    if signer and not signer.startswith("tailor@"):
+        try:
+            signer = author_map[signer]
+        except KeyError:
+            print "Missing signer: {signer}".format(signer=signer)
+        try:
+            author = author_map[author]
+        except KeyError:
+            print "Missing author: {author}".format(author=author)
+        if signer != author:
+            revisions[revision] = signer
+        else:
+            revisions.pop(revision, None)
+
+for revision, signer in revisions.items():
+    cmd = "{mtn} cert {revision} committer {signer}".format(
+        mtn=mtn,
+        revision=revision,
+        signer=shellquote(signer))
+    os.system(cmd)
 
 print "DONE"
-- 
1.7.10.1




More information about the Devel mailing list