Package: cia-clients
Version: 20081025
Severity: wishlist

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The attached patch updates the copy of bzr-cia included in cia-clients.

- -- System Information:
Debian Release: squeeze/sid
  APT prefers unstable
  APT policy: (650, 'unstable'), (600, 'experimental')
Architecture: i386 (i686)

Kernel: Linux 2.6.29-1-686 (SMP w/2 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages cia-clients depends on:
ii  python                        2.5.4-2    An interactive high-level object-o
ii  python-central                0.6.11     register and build utility for Pyt

cia-clients recommends no packages.

Versions of packages cia-clients suggests:
ii  bzr                         1.14-2       easy to use distributed version co
ii  cvs                         1:1.12.13-12 Concurrent Versions System
ii  darcs                       2.2.0-1      a distributed, interactive, smart 
ii  git-core                    1:1.6.2.4-1  fast, scalable, distributed revisi
ii  mercurial                   1.2-1        scalable distributed version contr
ii  subversion                  1.6.1dfsg-1  Advanced version control system

- -- no debconf information
diff -r d2fd0351c5f4 bzr-cia/__init__.py
--- a/bzr-cia/__init__.py	Sat Oct 25 18:14:34 2008 +0200
+++ b/bzr-cia/__init__.py	Tue May 05 17:53:43 2009 +0200
@@ -1,3 +1,19 @@
+# Copyright (C) 2005-2009 Jelmer Vernooij <jel...@samba.org>
+ 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
 """Post-commit hook to submit the commit to CIA (http://cia.navi.cx/)
 
 Requires bzr 0.15 or higher.
@@ -21,16 +37,11 @@
 
 version_info = (1, 0, 0, 'dev', 0)
 
-import socket
-import sys
-import xmlrpclib
-from xml.sax import saxutils
 
 import bzrlib
 from bzrlib.branch import Branch
 from bzrlib.commands import Command, register_command
 from bzrlib.option import Option
-import bzrlib.errors as errors
 from bzrlib.trace import info, warning
 
 
@@ -53,15 +64,18 @@
         pass
 
 
-def generate_cia_xml(repository, revid, project, revname=None, author=None):
-    revision = repository.get_revision(revid)
-    delta = repository.get_revision_delta(revid)
+def generate_cia_xml(branch, revid, project, revname=None, author=None):
+    from xml.sax import saxutils
+    revision = branch.repository.get_revision(revid)
+    delta = branch.repository.get_revision_delta(revid)
 
     if revname is None:
         revname = revid
 
     if author is None:
-        author = revision.committer
+        authors = revision.get_apparent_authors()
+    else:
+        authors = [author]
 
     files = []
     [files.append(f) for (f,_,_) in delta.added]
@@ -69,6 +83,8 @@
     [files.append(f) for (_,f,_,_,_,_) in delta.renamed]
     [files.append(f) for (f,_,_,_,_) in delta.modified]
 
+    authors_xml = "".join(["      <author>%s</author>\n" % saxutils.escape(author) for author in authors])
+
     return """
 <message>
   <generator> 
@@ -85,17 +101,62 @@
     <commit>
       <revision>%s</revision>
       <files>%s</files>
-      <author>%s</author>
+      %s
       <log>%s</log>
     </commit>
   </body>
 </message>
-""" % (bzrlib.version_string, project, revision.properties['branch-nick'], revision.timestamp, revname,
+""" % (bzrlib.version_string, project, branch.nick, revision.timestamp, revname,
         "\n".join(["<file>%s</file>" % saxutils.escape(f) for f in files]), 
-        saxutils.escape(author), 
+        authors_xml, 
         saxutils.escape(revision.message))
 
 
+def cia_cache_dir():
+    from bzrlib.config import config_dir
+    import os
+    return os.path.join(config_dir(), "cia")
+
+
+def store_failed_message(revid, message):
+    import os
+    cache_dir = cia_cache_dir()
+    if not os.path.isdir(cache_dir):
+        os.mkdir(cache_dir)
+    cache_file = os.path.join(cache_dir, revid)
+    f = open(cache_file, 'w')
+    f.write(message)
+    f.close()
+    return cache_file
+
+
+def cia_connect(config):
+    import xmlrpclib
+    server = config.get_user_option('cia_server')
+    if server is None:
+        server = "http://cia.navi.cx";
+
+    return xmlrpclib.ServerProxy(server)
+
+
+class CIADeliverError(Exception):
+
+    def __init__(self, message):
+        self.message = message
+
+
+def cia_deliver(server, msg):
+    import socket, xmlrpclib
+    try:
+        server.hub.deliver(msg)
+    except xmlrpclib.ProtocolError, e:
+        raise CIADeliverError(e.errmsg)
+    except socket.gaierror, (_, errmsg):
+        raise CIADeliverError(errmsg)
+    except socket.error, (_, errmsg):
+        raise CIADeliverError(errmsg)
+
+
 def cia_submit(branch, revid, revno, dry_run=False):
     config = branch.get_config()
 
@@ -103,10 +164,6 @@
     if project is None:
         return
 
-    server = config.get_user_option('cia_server')
-    if server is None:
-        server = "http://cia.navi.cx";
-
     author = config.get_user_option('cia_user')
     quiet = config.get_user_option('cia_quiet')
 
@@ -116,17 +173,17 @@
     else:
         revspec = revid
 
-    msg = generate_cia_xml(branch.repository, revid, project, revspec, author)
+    msg = generate_cia_xml(branch, revid, project, revspec, author)
 
     if not quiet:
         info("Submitting revision to CIA.")
     if not dry_run:
         try:
-            xmlrpclib.ServerProxy(server).hub.deliver(msg)
-        except xmlrpclib.ProtocolError, e:
-            warning("Unable to submit revision to CIA: %s" % e.errmsg)
-        except socket.gaierror, (_, msg):
-            warning("Unable to submit revision to CIA: %s" % msg)
+            cia_deliver(cia_connect(config), msg)
+        except CIADeliverError, (error, ):
+            warning("Unable to submit revision to CIA: %s" % error)
+            store_failed_message(revid, msg)
+            info("To retry submit later, run 'bzr cia-submit --pending'.")
     else:
         print msg
 
@@ -148,40 +205,63 @@
             print config.get_user_option('cia_project')
 
 
+def submit_pending(config):
+    import os
+    server = cia_connect(config)
+    cache_dir = cia_cache_dir()
+    for revid in os.listdir(cache_dir):
+        path = os.path.join(cache_dir, revid)
+        msg = open(path, 'r').read()
+        try:
+            cia_deliver(server, msg)
+        except CIADeliverError, (error, ):
+            warning("Submitting %s failed: %s" % (revid, error))
+        else:
+            os.remove(path)
+
+
 class cmd_cia_submit(Command):
     """Submit a revision to CIA
 
     """
     takes_args = ['branch?']
     takes_options = ['revision', 
+        Option('pending', 
+            help="Submit all pending CIA submissions."),
         Option('dry-run', 
             help="Show what would be done, but don't actually do anything.")]
 
-    def run(self, branch='.', revision=None,dry_run=False):
-        br = Branch.open(branch)
+    def run(self, branch=None, revision=None, dry_run=False, pending=False):
+        if pending and (branch is not None or revision is not None):
+            raise BzrCommandError("--pending and specifying a branch are mutually exclusive")
+        if pending:
+            from bzrlib.config import GlobalConfig
+            submit_pending(GlobalConfig())
+        else:
+            if branch is not None:
+                branch = "."
+            br = Branch.open(branch)
 
-        if revision is None:
-            revs = [br.last_revision()]
-        elif len(revision) == 1:
-            revs = [revision[0].in_history(br).rev_id]
-        elif len(revision) == 2:
-            if revision[0].spec is None:
-                from_revno = 1
+            if revision is None:
+                revs = [br.last_revision()]
+            elif len(revision) == 1:
+                revs = [revision[0].in_history(br).rev_id]
+            elif len(revision) == 2:
+                if revision[0].spec is None:
+                    from_revno = 1
+                else:
+                    from_revno = revision[0].in_history(br).revno
+
+                if revision[1].spec is None:
+                    to_revno = br.revno()
+                else:
+                    to_revno = revision[1].in_history(br).revno
+                revs = br.revision_history()[from_revno:to_revno]
             else:
-                from_revno = revision[0].in_history(br).revno
+                raise BzrCommandError('bzr submit-cia --revision takes one or two arguments')
 
-            if revision[1].spec is None:
-                to_revno = br.revno()
-            else:
-                to_revno = revision[1].in_history(br).revno
-            revs = br.revision_history()[from_revno:to_revno]
-        else:
-            raise BzrCommandError('bzr submit-cia --revision takes one or two arguments')
-
-        for rev_id in revs:
-            cia_submit(br, rev_id, br.revision_id_to_revno(rev_id), dry_run)
-        
-        return 0
+            for rev_id in revs:
+                cia_submit(br, rev_id, br.revision_id_to_revno(rev_id), dry_run)
 
 
 register_command(cmd_cia_project)
@@ -202,7 +282,7 @@
 
 def test_suite():
     from unittest import TestSuite, TestLoader
-    import tests
+    from bzrlib.plugins.cia import tests
     suite = TestSuite()
     suite.addTest(tests.test_suite())
     return suite
diff -r d2fd0351c5f4 bzr-cia/tests/test_xml.py
--- a/bzr-cia/tests/test_xml.py	Sat Oct 25 18:14:34 2008 +0200
+++ b/bzr-cia/tests/test_xml.py	Tue May 05 17:53:43 2009 +0200
@@ -28,7 +28,7 @@
         revid = tree.commit("bla", timestamp=1180551778, 
                 committer="Jelmer Vernooij <jel...@ganieda.lan.vernstok.nl>")
 
-        xml = generate_cia_xml(tree.branch.repository, revid, "myproject")
+        xml = generate_cia_xml(tree.branch, revid, "myproject")
 
         self.assertEqualDiff("""
 <message>
@@ -57,7 +57,7 @@
         tree = self.make_branch_and_tree("bla")
         revid = tree.commit("bla", timestamp=1180551778)
 
-        xml = generate_cia_xml(tree.branch.repository, revid, "myproject", 
+        xml = generate_cia_xml(tree.branch, revid, "myproject", 
                 author="somebody")
 
         self.assertEqualDiff("""
@@ -87,7 +87,7 @@
         tree = self.make_branch_and_tree("bla")
         revid = tree.commit("bla", timestamp=1180551778)
 
-        xml = generate_cia_xml(tree.branch.repository, revid, "myproject", 
+        xml = generate_cia_xml(tree.branch, revid, "myproject", 
                 revname="42", author="somebody")
 
         self.assertEqualDiff("""

Reply via email to