src/mercurialserver/servelog.py
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 31 Oct 2018 13:21:37 -0700
changeset 377 18869fcfbc92
parent 375 a41e4382ea6e
permissions -rw-r--r--
Merge a41e4382ea6e
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
296
6feeef02c057 Prefer import json over import simplejson
Paul Crowley <paul@lshift.net>
parents: 295
diff changeset
    12
311
3cbde66305e4 Fix white space
Paul Crowley <paul@lshift.net>
parents: 307
diff changeset
    13
try:
296
6feeef02c057 Prefer import json over import simplejson
Paul Crowley <paul@lshift.net>
parents: 295
diff changeset
    14
    import json
312
4e65f8242c0b Oops, test for the right attribute of json
Paul Crowley <paul@lshift.net>
parents: 311
diff changeset
    15
    json.dumps
311
3cbde66305e4 Fix white space
Paul Crowley <paul@lshift.net>
parents: 307
diff changeset
    16
except ImportError:
296
6feeef02c057 Prefer import json over import simplejson
Paul Crowley <paul@lshift.net>
parents: 295
diff changeset
    17
    import simplejson as json
6feeef02c057 Prefer import json over import simplejson
Paul Crowley <paul@lshift.net>
parents: 295
diff changeset
    18
78
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 67
diff changeset
    19
from mercurialserver import ruleset, changes
2a3407a14654 Replaced env vars with Python globals
Paul Crowley <paul@lshift.net>
parents: 67
diff changeset
    20
373
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    21
def writelog(repo, op, **kw):
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    22
    """Write a log entry in the mercurial-server log file as a JSON
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    23
    formatted dict.
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    24
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    25
    op is the operation (typically 'push', 'pull' or 'publish') 
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    26
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    27
    any element in the kw argument is also added in the log entry
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    28
    dictionnary.
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    29
    """
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    30
    logentry = kw.copy()
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    31
    logentry.update(dict(
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    32
        timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()),
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    33
        op=op,
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    34
        key=ruleset.rules.get('user'),
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    35
        ssh_connection=os.environ['SSH_CONNECTION'],))
375
a41e4382ea6e Use repo vfs to join the server log
Cédric Krier <ced@b2ck.com>
parents: 373
diff changeset
    36
    with open(repo.vfs.join("mercurial-server.log"), "a+") as log:
373
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    37
        fcntl.flock(log.fileno(), fcntl.LOCK_EX)
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    38
        log.seek(0, os.SEEK_END)
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    39
        # YAML log file format
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    40
        log.write("- %s\n" % json.dumps(data))
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    41
    
54
e2ca551c2822 Add push logging
Paul Crowley <paul@lshift.net>
parents:
diff changeset
    42
def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
55
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    43
    if hooktype == 'changegroup':
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    44
        op = "push"
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    45
    elif hooktype == 'outgoing':
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    46
        op = "pull"
2661b21688d4 Log pulls too
Paul Crowley <paul@lshift.net>
parents: 54
diff changeset
    47
    else:
373
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    48
        raise mercurial.util.Abort(_('servelog.hook installed as wrong hook type,'
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    49
                                     ' must be changegroup or outgoing but is %s') % hooktype)
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    50
    writelog(repo, op, 
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    51
             nodes=[mercurial.node.hex(ctx.node())
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    52
                    for ctx in changes.changes(repo, node)])
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    53
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    54
def phaseshook(ui, repo, hooktype, node=None, source=None, **kwargs):
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    55
    if hooktype == 'pushkey':
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    56
        if kwargs.get('namespace') == 'phases':
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    57
            writelog(repo, 'publish', nodes=[kwargs['key']])
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    58
    else:
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    59
        raise mercurial.util.Abort(_('servelog.phaseshook installed as wrong hook type,'
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    60
                                     ' must be pushkey but is %s') % hooktype)
a286d6c6b19c Log phase transitions (draft->public)
David Douard <david.douard@logilab.fr>
parents: 312
diff changeset
    61