src/mercurialserver/servelog.py
changeset 295 9741d82e5b1e
parent 262 675474f5be32
child 296 6feeef02c057
equal deleted inserted replaced
292:022d2e6bcdde 295:9741d82e5b1e
     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
    11 import fcntl
    12 import json
    12 try: import simplejson as json
       
    13 except ImportError: import json
    13 from mercurialserver import ruleset, changes
    14 from mercurialserver import ruleset, changes
    14 
    15 
    15 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    16 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    16     if hooktype == 'changegroup':
    17     if hooktype == 'changegroup':
    17         op = "push"
    18         op = "push"
    23     log = open(repo.join("mercurial-server.log"), "a+")
    24     log = open(repo.join("mercurial-server.log"), "a+")
    24     try:
    25     try:
    25         fcntl.flock(log.fileno(), fcntl.LOCK_EX)
    26         fcntl.flock(log.fileno(), fcntl.LOCK_EX)
    26         log.seek(0, os.SEEK_END)
    27         log.seek(0, os.SEEK_END)
    27         # YAML log file format
    28         # YAML log file format
    28         log.write("- {0}\n".format(json.dumps(dict(
    29         log.write("- %s\n" % json.dumps(dict(
    29             timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
    30             timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
    30             op=op,
    31             op=op,
    31             key=ruleset.rules.get('user'),
    32             key=ruleset.rules.get('user'),
    32             ssh_connection=os.environ['SSH_CONNECTION'],
    33             ssh_connection=os.environ['SSH_CONNECTION'],
    33             nodes=[mercurial.node.hex(ctx.node())
    34             nodes=[mercurial.node.hex(ctx.node())
    34                 for ctx in changes.changes(repo, node)],
    35                 for ctx in changes.changes(repo, node)],
    35          ))))
    36          )))
    36     finally:
    37     finally:
    37         log.close()
    38         log.close()