src/mercurialserver/servelog.py
branchdebian
changeset 313 f597eb3b5aaf
parent 312 4e65f8242c0b
child 373 a286d6c6b19c
equal deleted inserted replaced
294:31d5c6236f71 313:f597eb3b5aaf
     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 
       
    13 try:
       
    14     import json
       
    15     json.dumps
       
    16 except ImportError:
       
    17     import simplejson as json
       
    18 
    13 from mercurialserver import ruleset, changes
    19 from mercurialserver import ruleset, changes
    14 
    20 
    15 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    21 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    16     if hooktype == 'changegroup':
    22     if hooktype == 'changegroup':
    17         op = "push"
    23         op = "push"
    23     log = open(repo.join("mercurial-server.log"), "a+")
    29     log = open(repo.join("mercurial-server.log"), "a+")
    24     try:
    30     try:
    25         fcntl.flock(log.fileno(), fcntl.LOCK_EX)
    31         fcntl.flock(log.fileno(), fcntl.LOCK_EX)
    26         log.seek(0, os.SEEK_END)
    32         log.seek(0, os.SEEK_END)
    27         # YAML log file format
    33         # YAML log file format
    28         log.write("- {0}\n".format(json.dumps(dict(
    34         log.write("- %s\n" % json.dumps(dict(
    29             timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
    35             timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
    30             op=op,
    36             op=op,
    31             key=ruleset.rules.get('user'),
    37             key=ruleset.rules.get('user'),
    32             ssh_connection=os.environ['SSH_CONNECTION'],
    38             ssh_connection=os.environ['SSH_CONNECTION'],
    33             nodes=[mercurial.node.hex(ctx.node())
    39             nodes=[mercurial.node.hex(ctx.node())
    34                 for ctx in changes.changes(repo, node)],
    40                 for ctx in changes.changes(repo, node)],
    35          ))))
    41          )))
    36     finally:
    42     finally:
    37         log.close()
    43         log.close()