author | Yuya Nishihara <yuya@tcha.org> |
Sun, 11 Nov 2018 12:33:14 +0900 | |
changeset 40620 | b2e5a554bc7b |
parent 40619 | fbac323eb625 |
child 40622 | d2c997b8001f |
permissions | -rw-r--r-- |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
1 |
# logtoprocess.py - send ui.log() data to a subprocess |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
2 |
# |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
3 |
# Copyright 2016 Facebook, Inc. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
4 |
# |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
31601
8e7feaad2d8d
logtoprocess: use lowercase for docstring title
Jun Wu <quark@fb.com>
parents:
30638
diff
changeset
|
7 |
"""send ui.log() data to a subprocess (EXPERIMENTAL) |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
8 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
9 |
This extension lets you specify a shell command per ui.log() event, |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
10 |
sending all remaining arguments to as environment variables to that command. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
11 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
12 |
Each positional argument to the method results in a `MSG[N]` key in the |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
13 |
environment, starting at 1 (so `MSG1`, `MSG2`, etc.). Each keyword argument |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
14 |
is set as a `OPT_UPPERCASE_KEY` variable (so the key is uppercased, and |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
15 |
prefixed with `OPT_`). The original event name is passed in the `EVENT` |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
16 |
environment variable, and the process ID of mercurial is given in `HGPID`. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
17 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
18 |
So given a call `ui.log('foo', 'bar', 'baz', spam='eggs'), a script configured |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
19 |
for the `foo` event can expect an environment with `MSG1=bar`, `MSG2=baz`, and |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
20 |
`OPT_SPAM=eggs`. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
21 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
22 |
Scripts are configured in the `[logtoprocess]` section, each key an event name. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
23 |
For example:: |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
24 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
25 |
[logtoprocess] |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
26 |
commandexception = echo "$MSG2$MSG3" > /var/log/mercurial_exceptions.log |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
27 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
28 |
would log the warning message and traceback of any failed command dispatch. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
29 |
|
30332
318a24b52eeb
spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents:
29841
diff
changeset
|
30 |
Scripts are run asynchronously as detached daemon processes; mercurial will |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
31 |
not ensure that they exit cleanly. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
32 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
33 |
""" |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
34 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
35 |
from __future__ import absolute_import |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
36 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
37 |
import os |
30638
1c5cbf28f007
py3: replace os.environ with encoding.environ (part 5 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30332
diff
changeset
|
38 |
|
40620
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
39 |
from mercurial import ( |
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
40 |
pycompat, |
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
41 |
) |
39826
c31ce080eb75
py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents:
35063
diff
changeset
|
42 |
from mercurial.utils import ( |
c31ce080eb75
py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents:
35063
diff
changeset
|
43 |
procutil, |
c31ce080eb75
py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents:
35063
diff
changeset
|
44 |
) |
c31ce080eb75
py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents:
35063
diff
changeset
|
45 |
|
29841
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
29463
diff
changeset
|
46 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
47 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
48 |
# be specifying the version(s) of Mercurial they are tested with, or |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
49 |
# leave the attribute unspecified. |
29841
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
29463
diff
changeset
|
50 |
testedwith = 'ships-with-hg-core' |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
51 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
52 |
def uisetup(ui): |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
53 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
54 |
class logtoprocessui(ui.__class__): |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
55 |
def log(self, event, *msg, **opts): |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
56 |
"""Map log events to external commands |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
57 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
58 |
Arguments are passed on as environment variables. |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
59 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
60 |
""" |
29463
4cf0542dcbe7
logtoprocess: do not leak the ui object in uisetup
Jun Wu <quark@fb.com>
parents:
28901
diff
changeset
|
61 |
script = self.config('logtoprocess', event) |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
62 |
if script: |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
63 |
if msg: |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
64 |
# try to format the log message given the remaining |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
65 |
# arguments |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
66 |
try: |
40401
6bd477ee7294
logtoprocess: fix message formatting
Boris Feld <boris.feld@octobus.net>
parents:
39926
diff
changeset
|
67 |
# Format the message as blackbox does |
6bd477ee7294
logtoprocess: fix message formatting
Boris Feld <boris.feld@octobus.net>
parents:
39926
diff
changeset
|
68 |
formatted = msg[0] % msg[1:] |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
69 |
except (TypeError, KeyError): |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
70 |
# Failed to apply the arguments, ignore |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
71 |
formatted = msg[0] |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
72 |
messages = (formatted,) + msg[1:] |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
73 |
else: |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
74 |
messages = msg |
40620
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
75 |
env = { |
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
76 |
b'EVENT': event, |
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
77 |
b'HGPID': os.getpid(), |
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
78 |
} |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
79 |
# positional arguments are listed as MSG[N] keys in the |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
80 |
# environment |
40620
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
81 |
env.update((b'MSG%d' % i, m) for i, m in enumerate(messages, 1)) |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
82 |
# keyword arguments get prefixed with OPT_ and uppercased |
40620
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
83 |
env.update((b'OPT_%s' % key.upper(), value) |
b2e5a554bc7b
logtoprocess: rewrite dict building in py3-compatible way
Yuya Nishihara <yuya@tcha.org>
parents:
40619
diff
changeset
|
84 |
for key, value in pycompat.byteskwargs(opts).items()) |
40619
fbac323eb625
logtoprocess: leverage procutil.shellenviron() to stringify variables (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
40499
diff
changeset
|
85 |
fullenv = procutil.shellenviron(env) |
fbac323eb625
logtoprocess: leverage procutil.shellenviron() to stringify variables (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
40499
diff
changeset
|
86 |
procutil.runbgcommand(script, fullenv, shell=True) |
28901
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
87 |
return super(logtoprocessui, self).log(event, *msg, **opts) |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
88 |
|
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
89 |
# Replace the class for this instance and all clones created from it: |
a368da441b32
logtoprocess: new experimental extension
Martijn Pieters <mjpieters@fb.com>
parents:
diff
changeset
|
90 |
ui.__class__ = logtoprocessui |