src/mercurialserver/servelog.py
author Dale Wijnand <dale.wijnand@gmail.com>
Thu, 20 Jan 2011 21:18:29 +0100
changeset 295 9741d82e5b1e
parent 262 675474f5be32
child 296 6feeef02c057
permissions -rw-r--r--
servelog: use simplejson and the % operator for python versions < 2.6 Both the json module and str format operator are new in version 2.6, so replace them with the simplejson module and the % operator, respectively.

"""
Hook to log changesets pushed and pulled
"""

from mercurial.i18n import _
import mercurial.util
import mercurial.node

import os
import time
import fcntl
try: import simplejson as json
except ImportError: import json
from mercurialserver import ruleset, changes

def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    if hooktype == 'changegroup':
        op = "push"
    elif hooktype == 'outgoing':
        op = "pull"
    else:
        raise mercurial.util.Abort(_('servelog installed as wrong hook type,'
            ' must be changegroup or outgoing but is %s') % hooktype)
    log = open(repo.join("mercurial-server.log"), "a+")
    try:
        fcntl.flock(log.fileno(), fcntl.LOCK_EX)
        log.seek(0, os.SEEK_END)
        # YAML log file format
        log.write("- %s\n" % json.dumps(dict(
            timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
            op=op,
            key=ruleset.rules.get('user'),
            ssh_connection=os.environ['SSH_CONNECTION'],
            nodes=[mercurial.node.hex(ctx.node())
                for ctx in changes.changes(repo, node)],
         )))
    finally:
        log.close()