src/mercurialserver/servelog.py
changeset 262 675474f5be32
parent 242 03d8f07230b3
child 295 9741d82e5b1e
equal deleted inserted replaced
260:57dcdb212d00 262:675474f5be32
     6 import mercurial.util
     6 import mercurial.util
     7 import mercurial.node
     7 import mercurial.node
     8 
     8 
     9 import os
     9 import os
    10 import time
    10 import time
       
    11 import fcntl
       
    12 import json
    11 from mercurialserver import ruleset, changes
    13 from mercurialserver import ruleset, changes
    12 
    14 
    13 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    15 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    14     if hooktype == 'changegroup':
    16     if hooktype == 'changegroup':
    15         op = "push"
    17         op = "push"
    16     elif hooktype == 'outgoing':
    18     elif hooktype == 'outgoing':
    17         op = "pull"
    19         op = "pull"
    18     else:
    20     else:
    19         raise mercurial.util.Abort(_('servelog installed as wrong hook type,'
    21         raise mercurial.util.Abort(_('servelog installed as wrong hook type,'
    20             ' must be changegroup or outgoing but is %s') % hooktype)
    22             ' must be changegroup or outgoing but is %s') % hooktype)
    21     t = time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime())
    23     log = open(repo.join("mercurial-server.log"), "a+")
    22     user = ruleset.rules.get('user')
       
    23     # FIXME: lock it
       
    24     log = open(repo.join("serve-log"), "a+")
       
    25     try:
    24     try:
    26         for ctx in changes.changes(repo, node):
    25         fcntl.flock(log.fileno(), fcntl.LOCK_EX)
    27             log.write("%s %s key=%s changeset=%s\n" % 
    26         log.seek(0, os.SEEK_END)
    28                 (t, op, user, mercurial.node.hex(ctx.node())))
    27         # YAML log file format
       
    28         log.write("- {0}\n".format(json.dumps(dict(
       
    29             timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
       
    30             op=op,
       
    31             key=ruleset.rules.get('user'),
       
    32             ssh_connection=os.environ['SSH_CONNECTION'],
       
    33             nodes=[mercurial.node.hex(ctx.node())
       
    34                 for ctx in changes.changes(repo, node)],
       
    35          ))))
    29     finally:
    36     finally:
    30         log.close()
    37         log.close()