src/mercurialserver/servelog.py
author Paul Crowley <paul@lshift.net>
Fri, 17 Dec 2010 14:39:25 +0000
changeset 262 675474f5be32
parent 242 03d8f07230b3
child 295 9741d82e5b1e
permissions -rw-r--r--
New log file format based on JSON/YAML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
242
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 78
diff changeset
     1
"""
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 78
diff changeset
     2
Hook to log changesets pushed and pulled
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 78
diff changeset
     3
"""
54
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     4
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     5
from mercurial.i18n import _
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     6
import mercurial.util
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     7
import mercurial.node
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     8
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
     9
import os
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    10
import time
262
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    11
import fcntl
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    12
import json
78
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 67
diff changeset
    13
from mercurialserver import ruleset, changes
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 67
diff changeset
    14
54
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    15
def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
55
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    16
    if hooktype == 'changegroup':
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    17
        op = "push"
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    18
    elif hooktype == 'outgoing':
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    19
        op = "pull"
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    20
    else:
59
f96de2d99f00 give logging module a more specific name
Paul Crowley <paul@lshift.net>
parents: 56
diff changeset
    21
        raise mercurial.util.Abort(_('servelog installed as wrong hook type,'
55
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    22
            ' must be changegroup or outgoing but is %s') % hooktype)
262
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    23
    log = open(repo.join("mercurial-server.log"), "a+")
54
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    24
    try:
262
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    25
        fcntl.flock(log.fileno(), fcntl.LOCK_EX)
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    26
        log.seek(0, os.SEEK_END)
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    27
        # YAML log file format
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    28
        log.write("- {0}\n".format(json.dumps(dict(
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    29
            timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    30
            op=op,
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    31
            key=ruleset.rules.get('user'),
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    32
            ssh_connection=os.environ['SSH_CONNECTION'],
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    33
            nodes=[mercurial.node.hex(ctx.node())
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    34
                for ctx in changes.changes(repo, node)],
675474f5be32 New log file format based on JSON/YAML
Paul Crowley <paul@lshift.net>
parents: 242
diff changeset
    35
         ))))
54
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    36
    finally:
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    37
        log.close()