hgext/notify.py
author Vadim Gelfer <vadim.gelfer@gmail.com>
Mon, 08 May 2006 10:59:58 -0700
changeset 2221 05b6c13f43c6
parent 2203 9569eea1707c
child 2224 e8f47dfb70f4
permissions -rw-r--r--
reverse sense of return value from python hooks. old scheme (False/None/0/'' == fail) made coding style unnatural, did not allow use of mercurial commands as hooks. new scheme (False/None/0 == pass) is pythonic, does not require peculiar "return True" at ends of hooks, allows hooks like this: [hooks] # update working dir after push into this repo changegroup.update = python:mercurial.commands.update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     1
# notify.py - email notifications for mercurial
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     2
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     3
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     4
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     5
# This software may be used and distributed according to the terms
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     7
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     8
# hook extension to email notifications to people when changesets are
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     9
# committed to a repo they subscribe to.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    10
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    11
# default mode is to print messages to stdout, for testing and
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    12
# configuring.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    13
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    14
# to use, configure notify extension and enable in hgrc like this:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    15
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    16
#   [extensions]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    17
#   hgext.notify =
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    18
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    19
#   [hooks]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    20
#   # one email for each incoming changeset
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    21
#   incoming.notify = python:hgext.notify.hook
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    22
#   # batch emails when many changesets incoming at one time
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    23
#   changegroup.notify = python:hgext.notify.hook
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    24
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    25
#   [notify]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    26
#   # config items go in here
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    27
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    28
# config items:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    29
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    30
# REQUIRED:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    31
#   config = /path/to/file # file containing subscriptions
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    32
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    33
# OPTIONAL:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    34
#   test = True            # print messages to stdout for testing
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    35
#   strip = 3              # number of slashes to strip for url paths
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    36
#   domain = example.com   # domain to use if committer missing domain
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    37
#   style = ...            # style file to use when formatting email
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    38
#   template = ...         # template to use when formatting email
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    39
#   incoming = ...         # template to use when run as incoming hook
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    40
#   changegroup = ...      # template when run as changegroup hook
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    41
#   maxdiff = 300          # max lines of diffs to include (0=none, -1=all)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    42
#   maxsubject = 67        # truncate subject line longer than this
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    43
#   [email]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    44
#   from = user@host.com   # email address to send as if none given
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    45
#   [web]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    46
#   baseurl = http://hgserver/... # root of hg web site for browsing commits
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    47
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    48
# notify config file has same format as regular hgrc. it has two
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    49
# sections so you can express subscriptions in whatever way is handier
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    50
# for you.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    51
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    52
#   [usersubs]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    53
#   # key is subscriber email, value is ","-separated list of glob patterns
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    54
#   user@host = pattern
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    55
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    56
#   [reposubs]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    57
#   # key is glob pattern, value is ","-separated list of subscriber emails
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    58
#   pattern = user@host
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    59
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    60
# glob patterns are matched against path to repo root.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    61
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    62
# if you like, you can put notify config file in repo that users can
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    63
# push changes to, they can manage their own subscriptions.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    64
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    65
from mercurial.demandload import *
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    66
from mercurial.i18n import gettext as _
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    67
from mercurial.node import *
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    68
demandload(globals(), 'email.Parser mercurial:commands,templater,util')
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    69
demandload(globals(), 'fnmatch socket time')
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    70
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    71
# template for single changeset can include email headers.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    72
single_template = '''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    73
Subject: changeset in {webroot}: {desc|firstline|strip}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    74
From: {author}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    75
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    76
changeset {node|short} in {root}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    77
details: {baseurl}{webroot}?cmd=changeset;node={node|short}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    78
description:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    79
\t{desc|tabindent|strip}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    80
'''.lstrip()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    81
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    82
# template for multiple changesets should not contain email headers,
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    83
# because only first set of headers will be used and result will look
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    84
# strange.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    85
multiple_template = '''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    86
changeset {node|short} in {root}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    87
details: {baseurl}{webroot}?cmd=changeset;node={node|short}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    88
summary: {desc|firstline}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    89
'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    90
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    91
deftemplates = {
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    92
    'changegroup': multiple_template,
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    93
    }
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    94
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    95
class notifier(object):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    96
    '''email notification class.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    97
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
    98
    def __init__(self, ui, repo, hooktype):
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    99
        self.ui = ui
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   100
        self.ui.readconfig(self.ui.config('notify', 'config'))
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   101
        self.repo = repo
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   102
        self.stripcount = int(self.ui.config('notify', 'strip', 0))
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   103
        self.root = self.strip(self.repo.root)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   104
        self.domain = self.ui.config('notify', 'domain')
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   105
        self.sio = templater.stringio()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   106
        self.subs = self.subscribers()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   107
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   108
        mapfile = self.ui.config('notify', 'style')
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   109
        template = (self.ui.config('notify', hooktype) or
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   110
                    self.ui.config('notify', 'template'))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   111
        self.t = templater.changeset_templater(self.ui, self.repo, mapfile,
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   112
                                               self.sio)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   113
        if not mapfile and not template:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   114
            template = deftemplates.get(hooktype) or single_template
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   115
        if template:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   116
            template = templater.parsestring(template, quoted=False)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   117
            self.t.use_template(template)
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   118
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   119
    def strip(self, path):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   120
        '''strip leading slashes from local path, turn into web-safe path.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   121
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   122
        path = util.pconvert(path)
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   123
        count = self.stripcount
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   124
        while path and count >= 0:
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   125
            c = path.find('/')
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   126
            if c == -1:
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   127
                break
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   128
            path = path[c+1:]
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   129
            count -= 1
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   130
        return path
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   131
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   132
    def fixmail(self, addr):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   133
        '''try to clean up email addresses.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   134
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   135
        addr = templater.email(addr.strip())
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   136
        a = addr.find('@localhost')
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   137
        if a != -1:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   138
            addr = addr[:a]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   139
        if '@' not in addr:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   140
            return addr + '@' + self.domain
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   141
        return addr
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   142
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   143
    def subscribers(self):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   144
        '''return list of email addresses of subscribers to this repo.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   145
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   146
        subs = {}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   147
        for user, pats in self.ui.configitems('usersubs'):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   148
            for pat in pats.split(','):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   149
                if fnmatch.fnmatch(self.repo.root, pat.strip()):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   150
                    subs[self.fixmail(user)] = 1
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   151
        for pat, users in self.ui.configitems('reposubs'):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   152
            if fnmatch.fnmatch(self.repo.root, pat):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   153
                for user in users.split(','):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   154
                    subs[self.fixmail(user)] = 1
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   155
        subs = subs.keys()
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   156
        subs.sort()
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   157
        return subs
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   158
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   159
    def url(self, path=None):
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   160
        return self.ui.config('web', 'baseurl') + (path or self.root)
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   161
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   162
    def node(self, node):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   163
        '''format one changeset.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   164
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   165
        self.t.show(changenode=node, changes=self.repo.changelog.read(node),
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   166
                    baseurl=self.ui.config('web', 'baseurl'),
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   167
                    root=self.repo.root,
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   168
                    webroot=self.root)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   169
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   170
    def send(self, node, count):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   171
        '''send message.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   172
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   173
        p = email.Parser.Parser()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   174
        self.sio.seek(0)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   175
        msg = p.parse(self.sio)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   176
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   177
        def fix_subject():
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   178
            '''try to make subject line exist and be useful.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   179
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   180
            subject = msg['Subject']
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   181
            if not subject:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   182
                if count > 1:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   183
                    subject = _('%s: %d new changesets') % (self.root, count)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   184
                else:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   185
                    changes = self.repo.changelog.read(node)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   186
                    s = changes[4].lstrip().split('\n', 1)[0].rstrip()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   187
                    subject = '%s: %s' % (self.root, s)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   188
            maxsubject = int(self.ui.config('notify', 'maxsubject', 67))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   189
            if maxsubject and len(subject) > maxsubject:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   190
                subject = subject[:maxsubject-3] + '...'
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   191
            del msg['Subject']
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   192
            msg['Subject'] = subject
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   193
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   194
        def fix_sender():
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   195
            '''try to make message have proper sender.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   196
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   197
            sender = msg['From']
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   198
            if not sender:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   199
                sender = self.ui.config('email', 'from') or self.ui.username()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   200
            if '@' not in sender or '@localhost' in sender:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   201
                sender = self.fixmail(sender)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   202
            del msg['From']
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   203
            msg['From'] = sender
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   204
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   205
        fix_subject()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   206
        fix_sender()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   207
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   208
        msg['X-Hg-Notification'] = 'changeset ' + short(node)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   209
        if not msg['Message-Id']:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   210
            msg['Message-Id'] = ('<hg.%s.%s.%s@%s>' %
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   211
                                 (short(node), int(time.time()),
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   212
                                  hash(self.repo.root), socket.getfqdn()))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   213
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   214
        msgtext = msg.as_string(0)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   215
        if self.ui.configbool('notify', 'test', True):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   216
            self.ui.write(msgtext)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   217
            if not msgtext.endswith('\n'):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   218
                self.ui.write('\n')
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   219
        else:
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   220
            mail = self.ui.sendmail()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   221
            mail.sendmail(templater.email(msg['From']), self.subs, msgtext)
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   222
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   223
    def diff(self, node):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   224
        maxdiff = int(self.ui.config('notify', 'maxdiff', 300))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   225
        if maxdiff == 0:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   226
            return
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   227
        fp = templater.stringio()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   228
        commands.dodiff(fp, self.ui, self.repo, node,
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   229
                        self.repo.changelog.tip())
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   230
        difflines = fp.getvalue().splitlines(1)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   231
        if maxdiff > 0 and len(difflines) > maxdiff:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   232
            self.sio.write(_('\ndiffs (truncated from %d to %d lines):\n\n') %
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   233
                           (len(difflines), maxdiff))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   234
            difflines = difflines[:maxdiff]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   235
        elif difflines:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   236
            self.sio.write(_('\ndiffs (%d lines):\n\n') % len(difflines))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   237
        self.sio.write(*difflines)
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   238
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   239
def hook(ui, repo, hooktype, node=None, **kwargs):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   240
    '''send email notifications to interested subscribers.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   241
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   242
    if used as changegroup hook, send one email for all changesets in
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   243
    changegroup. else send one email per changeset.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   244
    n = notifier(ui, repo, hooktype)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   245
    if not n.subs: return True
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   246
    node = bin(node)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   247
    if hooktype == 'changegroup':
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   248
        start = repo.changelog.rev(node)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   249
        end = repo.changelog.count()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   250
        count = end - start
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   251
        for rev in xrange(start, end):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   252
            n.node(repo.changelog.node(rev))
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   253
    else:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   254
        count = 1
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   255
        n.node(node)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   256
    n.diff(node)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   257
    n.send(node, count)