16 except ImportError: |
16 except ImportError: |
17 import simplejson as json |
17 import simplejson as json |
18 |
18 |
19 from mercurialserver import ruleset, changes |
19 from mercurialserver import ruleset, changes |
20 |
20 |
|
21 def writelog(repo, op, **kw): |
|
22 """Write a log entry in the mercurial-server log file as a JSON |
|
23 formatted dict. |
|
24 |
|
25 op is the operation (typically 'push', 'pull' or 'publish') |
|
26 |
|
27 any element in the kw argument is also added in the log entry |
|
28 dictionnary. |
|
29 """ |
|
30 logentry = kw.copy() |
|
31 logentry.update(dict( |
|
32 timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()), |
|
33 op=op, |
|
34 key=ruleset.rules.get('user'), |
|
35 ssh_connection=os.environ['SSH_CONNECTION'],)) |
|
36 with open(repo.join("mercurial-server.log"), "a+") as log: |
|
37 fcntl.flock(log.fileno(), fcntl.LOCK_EX) |
|
38 log.seek(0, os.SEEK_END) |
|
39 # YAML log file format |
|
40 log.write("- %s\n" % json.dumps(data)) |
|
41 |
21 def hook(ui, repo, hooktype, node=None, source=None, **kwargs): |
42 def hook(ui, repo, hooktype, node=None, source=None, **kwargs): |
22 if hooktype == 'changegroup': |
43 if hooktype == 'changegroup': |
23 op = "push" |
44 op = "push" |
24 elif hooktype == 'outgoing': |
45 elif hooktype == 'outgoing': |
25 op = "pull" |
46 op = "pull" |
26 else: |
47 else: |
27 raise mercurial.util.Abort(_('servelog installed as wrong hook type,' |
48 raise mercurial.util.Abort(_('servelog.hook installed as wrong hook type,' |
28 ' must be changegroup or outgoing but is %s') % hooktype) |
49 ' must be changegroup or outgoing but is %s') % hooktype) |
29 log = open(repo.join("mercurial-server.log"), "a+") |
50 writelog(repo, op, |
30 try: |
51 nodes=[mercurial.node.hex(ctx.node()) |
31 fcntl.flock(log.fileno(), fcntl.LOCK_EX) |
52 for ctx in changes.changes(repo, node)]) |
32 log.seek(0, os.SEEK_END) |
53 |
33 # YAML log file format |
54 def phaseshook(ui, repo, hooktype, node=None, source=None, **kwargs): |
34 log.write("- %s\n" % json.dumps(dict( |
55 if hooktype == 'pushkey': |
35 timestamp=time.strftime("%Y-%m-%d_%H:%M:%S Z", time.gmtime()), |
56 if kwargs.get('namespace') == 'phases': |
36 op=op, |
57 writelog(repo, 'publish', nodes=[kwargs['key']]) |
37 key=ruleset.rules.get('user'), |
58 else: |
38 ssh_connection=os.environ['SSH_CONNECTION'], |
59 raise mercurial.util.Abort(_('servelog.phaseshook installed as wrong hook type,' |
39 nodes=[mercurial.node.hex(ctx.node()) |
60 ' must be pushkey but is %s') % hooktype) |
40 for ctx in changes.changes(repo, node)], |
61 |
41 ))) |
|
42 finally: |
|
43 log.close() |
|