hgext/hgcia.py
author Adrian Buehlmann <adrian@cadifra.com>
Mon, 08 Jun 2009 20:02:44 +0200
changeset 8795 51c29aec0b75
parent 8778 c5f36402daad
child 8894 868670dbc237
permissions -rw-r--r--
mq: eliminate warning on qpush with empty series Warning was "DeprecationWarning: use lock.release instead of del lock"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     1
# Copyright (C) 2007-8 Brendan Cully <brendan@kublai.com>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     2
# Published under the GNU GPL
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     3
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     4
"""CIA notification
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     5
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     6
This is meant to be run as a changegroup or incoming hook.
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     7
To configure it, set the following options in your hgrc:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     8
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     9
[cia]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    10
# your registered CIA user name
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    11
user = foo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    12
# the name of the project in CIA
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    13
project = foo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    14
# the module (subproject) (optional)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    15
#module = foo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    16
# Append a diffstat to the log message (optional)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    17
#diffstat = False
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    18
# Template to use for log messages (optional)
8541
06ace50443f6 hgcia, convert: escape backslashes in docstrings
Martin Geisler <mg@lazybytes.net>
parents: 7762
diff changeset
    19
#template = {desc}\\n{baseurl}/rev/{node}-- {diffstat}
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    20
# Style to use (optional)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    21
#style = foo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    22
# The URL of the CIA notification service (optional)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    23
# You can use mailto: URLs to send by email, eg
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    24
# mailto:cia@cia.vc
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    25
# Make sure to set email.from if you do this.
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    26
#url = http://cia.vc/
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    27
# print message instead of sending it (optional)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    28
#test = False
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    29
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    30
[hooks]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    31
# one of these:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    32
changegroup.cia = python:hgcia.hook
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    33
#incoming.cia = python:hgcia.hook
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    34
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    35
[web]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    36
# If you want hyperlinks (optional)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    37
baseurl = http://server/path/to/repo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    38
"""
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    39
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    40
from mercurial.i18n import _
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    41
from mercurial.node import *
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    42
from mercurial import cmdutil, patch, templater, util, mail
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    43
import email.Parser
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    44
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    45
import xmlrpclib
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    46
from xml.sax import saxutils
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    47
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    48
socket_timeout = 30 # seconds
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    49
try:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    50
    # set a timeout for the socket so you don't have to wait so looooong
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    51
    # when cia.vc is having problems. requires python >= 2.3:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    52
    import socket
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    53
    socket.setdefaulttimeout(socket_timeout)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    54
except:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    55
    pass
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    56
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    57
HGCIA_VERSION = '0.1'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    58
HGCIA_URL = 'http://hg.kublai.com/mercurial/hgcia'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    59
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    60
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    61
class ciamsg(object):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    62
    """ A CIA message """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    63
    def __init__(self, cia, ctx):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    64
        self.cia = cia
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    65
        self.ctx = ctx
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    66
        self.url = self.cia.url
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    67
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    68
    def fileelem(self, path, uri, action):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    69
        if uri:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    70
            uri = ' uri=%s' % saxutils.quoteattr(uri)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    71
        return '<file%s action=%s>%s</file>' % (
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    72
            uri, saxutils.quoteattr(action), saxutils.escape(path))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    73
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    74
    def fileelems(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    75
        n = self.ctx.node()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    76
        f = self.cia.repo.status(self.ctx.parents()[0].node(), n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    77
        url = self.url or ''
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    78
        elems = []
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    79
        for path in f[0]:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    80
            uri = '%s/diff/%s/%s' % (url, short(n), path)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    81
            elems.append(self.fileelem(path, url and uri, 'modify'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    82
        for path in f[1]:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    83
            # TODO: copy/rename ?
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    84
            uri = '%s/file/%s/%s' % (url, short(n), path)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    85
            elems.append(self.fileelem(path, url and uri, 'add'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    86
        for path in f[2]:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    87
            elems.append(self.fileelem(path, '', 'remove'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    88
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    89
        return '\n'.join(elems)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    90
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    91
    def sourceelem(self, project, module=None, branch=None):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    92
        msg = ['<source>', '<project>%s</project>' % saxutils.escape(project)]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    93
        if module:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    94
            msg.append('<module>%s</module>' % saxutils.escape(module))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    95
        if branch:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    96
            msg.append('<branch>%s</branch>' % saxutils.escape(branch))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    97
        msg.append('</source>')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    98
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    99
        return '\n'.join(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   100
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   101
    def diffstat(self):
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8541
diff changeset
   102
        class patchbuf(object):
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   103
            def __init__(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   104
                self.lines = []
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   105
                # diffstat is stupid
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   106
                self.name = 'cia'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   107
            def write(self, data):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   108
                self.lines.append(data)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   109
            def close(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   110
                pass
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   111
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   112
        n = self.ctx.node()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   113
        pbuf = patchbuf()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   114
        patch.export(self.cia.repo, [n], fp=pbuf)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   115
        return patch.diffstat(pbuf.lines) or ''
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   116
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   117
    def logmsg(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   118
        diffstat = self.cia.diffstat and self.diffstat() or ''
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   119
        self.cia.ui.pushbuffer()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   120
        self.cia.templater.show(self.ctx, changes=self.ctx.changeset(),
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   121
                                url=self.cia.url, diffstat=diffstat)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   122
        return self.cia.ui.popbuffer()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   123
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   124
    def xml(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   125
        n = short(self.ctx.node())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   126
        src = self.sourceelem(self.cia.project, module=self.cia.module,
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   127
                              branch=self.ctx.branch())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   128
        # unix timestamp
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   129
        dt = self.ctx.date()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   130
        timestamp = dt[0]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   131
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   132
        author = saxutils.escape(self.ctx.user())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   133
        rev = '%d:%s' % (self.ctx.rev(), n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   134
        log = saxutils.escape(self.logmsg())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   135
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   136
        url = self.url and '<url>%s/rev/%s</url>' % (saxutils.escape(self.url),
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   137
                                                     n) or ''
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   138
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   139
        msg = """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   140
<message>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   141
  <generator>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   142
    <name>Mercurial (hgcia)</name>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   143
    <version>%s</version>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   144
    <url>%s</url>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   145
    <user>%s</user>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   146
  </generator>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   147
  %s
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   148
  <body>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   149
    <commit>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   150
      <author>%s</author>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   151
      <version>%s</version>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   152
      <log>%s</log>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   153
      %s
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   154
      <files>%s</files>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   155
    </commit>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   156
  </body>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   157
  <timestamp>%d</timestamp>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   158
</message>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   159
""" % \
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   160
            (HGCIA_VERSION, saxutils.escape(HGCIA_URL),
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
            saxutils.escape(self.cia.user), src, author, rev, log, url,
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   162
            self.fileelems(), timestamp)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   163
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   164
        return msg
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   165
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   166
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   167
class hgcia(object):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   168
    """ CIA notification class """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   169
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   170
    deftemplate = '{desc}'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   171
    dstemplate = '{desc}\n-- \n{diffstat}'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   172
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   173
    def __init__(self, ui, repo):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   174
        self.ui = ui
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   175
        self.repo = repo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   176
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   177
        self.ciaurl = self.ui.config('cia', 'url', 'http://cia.vc')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   178
        self.user = self.ui.config('cia', 'user')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   179
        self.project = self.ui.config('cia', 'project')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   180
        self.module = self.ui.config('cia', 'module')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   181
        self.diffstat = self.ui.configbool('cia', 'diffstat')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   182
        self.emailfrom = self.ui.config('email', 'from')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
        self.dryrun = self.ui.configbool('cia', 'test')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   184
        self.url = self.ui.config('web', 'baseurl')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   185
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   186
        style = self.ui.config('cia', 'style')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   187
        template = self.ui.config('cia', 'template')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   188
        if not template:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   189
            template = self.diffstat and self.dstemplate or self.deftemplate
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   190
        template = templater.parsestring(template, quoted=False)
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7438
diff changeset
   191
        t = cmdutil.changeset_templater(self.ui, self.repo, False, None,
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7438
diff changeset
   192
                                        style, False)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   193
        t.use_template(template)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   194
        self.templater = t
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   195
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   196
    def sendrpc(self, msg):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   197
        srv = xmlrpclib.Server(self.ciaurl)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   198
        srv.hub.deliver(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   199
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   200
    def sendemail(self, address, data):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   201
        p = email.Parser.Parser()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   202
        msg = p.parsestr(data)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   203
        msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   204
        msg['To'] = address
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   205
        msg['From'] = self.emailfrom
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   206
        msg['Subject'] = 'DeliverXML'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   207
        msg['Content-type'] = 'text/xml'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   208
        msgtext = msg.as_string(0)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   209
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   210
        self.ui.status(_('hgcia: sending update to %s\n') % address)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   211
        mail.sendmail(self.ui, util.email(self.emailfrom),
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   212
                      [address], msgtext)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   213
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   214
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   215
def hook(ui, repo, hooktype, node=None, url=None, **kwargs):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   216
    """ send CIA notification """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   217
    def sendmsg(cia, ctx):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   218
        msg = ciamsg(cia, ctx).xml()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   219
        if cia.dryrun:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   220
            ui.write(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   221
        elif cia.ciaurl.startswith('mailto:'):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   222
            if not cia.emailfrom:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   223
                raise util.Abort(_('email.from must be defined when '
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   224
                                   'sending by email'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   225
            cia.sendemail(cia.ciaurl[7:], msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   226
        else:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   227
            cia.sendrpc(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   228
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   229
    n = bin(node)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   230
    cia = hgcia(ui, repo)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   231
    if not cia.user:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   232
        ui.debug(_('cia: no user specified'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   233
        return
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   234
    if not cia.project:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   235
        ui.debug(_('cia: no project specified'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   236
        return
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   237
    if hooktype == 'changegroup':
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   238
        start = repo.changelog.rev(n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   239
        end = len(repo.changelog)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   240
        for rev in xrange(start, end):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   241
            n = repo.changelog.node(rev)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   242
            ctx = repo.changectx(n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   243
            sendmsg(cia, ctx)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   244
    else:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   245
        ctx = repo.changectx(n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   246
        sendmsg(cia, ctx)