##// END OF EJS Templates
blackbox: fix a failing pyflakes test
Bryan O'Sullivan -
r18675:f816aa37 default
parent child Browse files
Show More
@@ -1,105 +1,105
1 # worker.py - master-slave parallelism support
1 # worker.py - master-slave parallelism support
2 #
2 #
3 # Copyright 2013 Facebook, Inc.
3 # Copyright 2013 Facebook, Inc.
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """log repository events to a blackbox for debugging
8 """log repository events to a blackbox for debugging
9
9
10 Logs event information to .hg/blackbox.log to help debug and diagnose problems.
10 Logs event information to .hg/blackbox.log to help debug and diagnose problems.
11 The events that get logged can be configured via the blackbox.track config key.
11 The events that get logged can be configured via the blackbox.track config key.
12 Examples:
12 Examples:
13
13
14 [blackbox]
14 [blackbox]
15 track = *
15 track = *
16
16
17 [blackbox]
17 [blackbox]
18 track = command, commandfinish, commandexception, exthook, pythonhook
18 track = command, commandfinish, commandexception, exthook, pythonhook
19
19
20 [blackbox]
20 [blackbox]
21 track = incoming
21 track = incoming
22
22
23 """
23 """
24
24
25 from mercurial import util, cmdutil
25 from mercurial import util, cmdutil
26 from mercurial.i18n import _
26 from mercurial.i18n import _
27 import os, getpass, re, string
27 import os, getpass, re
28
28
29 cmdtable = {}
29 cmdtable = {}
30 command = cmdutil.command(cmdtable)
30 command = cmdutil.command(cmdtable)
31 testedwith = 'internal'
31 testedwith = 'internal'
32 lastblackbox = None
32 lastblackbox = None
33
33
34 def wrapui(ui):
34 def wrapui(ui):
35 class blackboxui(ui.__class__):
35 class blackboxui(ui.__class__):
36 @util.propertycache
36 @util.propertycache
37 def track(self):
37 def track(self):
38 return ui.configlist('blackbox', 'track', ['*'])
38 return ui.configlist('blackbox', 'track', ['*'])
39
39
40 def log(self, event, *msg, **opts):
40 def log(self, event, *msg, **opts):
41 global lastblackbox
41 global lastblackbox
42 super(blackboxui, self).log(event, *msg, **opts)
42 super(blackboxui, self).log(event, *msg, **opts)
43
43
44 if not '*' in self.track and not event in self.track:
44 if not '*' in self.track and not event in self.track:
45 return
45 return
46
46
47 if util.safehasattr(self, '_blackbox'):
47 if util.safehasattr(self, '_blackbox'):
48 blackbox = self._blackbox
48 blackbox = self._blackbox
49 else:
49 else:
50 # certain ui instances exist outside the context of
50 # certain ui instances exist outside the context of
51 # a repo, so just default to the last blackbox that
51 # a repo, so just default to the last blackbox that
52 # was seen.
52 # was seen.
53 blackbox = lastblackbox
53 blackbox = lastblackbox
54
54
55 if blackbox:
55 if blackbox:
56 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
56 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
57 user = getpass.getuser()
57 user = getpass.getuser()
58 formattedmsg = msg[0] % msg[1:]
58 formattedmsg = msg[0] % msg[1:]
59 blackbox.write('%s %s> %s' % (date, user, formattedmsg))
59 blackbox.write('%s %s> %s' % (date, user, formattedmsg))
60 lastblackbox = blackbox
60 lastblackbox = blackbox
61
61
62 def setrepo(self, repo):
62 def setrepo(self, repo):
63 self._blackbox = repo.opener('blackbox.log', 'a')
63 self._blackbox = repo.opener('blackbox.log', 'a')
64
64
65 ui.__class__ = blackboxui
65 ui.__class__ = blackboxui
66
66
67 def uisetup(ui):
67 def uisetup(ui):
68 wrapui(ui)
68 wrapui(ui)
69
69
70 def reposetup(ui, repo):
70 def reposetup(ui, repo):
71 # During 'hg pull' a httppeer repo is created to represent the remote repo.
71 # During 'hg pull' a httppeer repo is created to represent the remote repo.
72 # It doesn't have a .hg directory to put a blackbox in, so we don't do
72 # It doesn't have a .hg directory to put a blackbox in, so we don't do
73 # the blackbox setup for it.
73 # the blackbox setup for it.
74 if not repo.local():
74 if not repo.local():
75 return
75 return
76
76
77 ui.setrepo(repo)
77 ui.setrepo(repo)
78
78
79 @command('^blackbox',
79 @command('^blackbox',
80 [('l', 'limit', 10, _('the number of events to show')),
80 [('l', 'limit', 10, _('the number of events to show')),
81 ],
81 ],
82 _('hg blackbox [OPTION]...'))
82 _('hg blackbox [OPTION]...'))
83 def blackbox(ui, repo, *revs, **opts):
83 def blackbox(ui, repo, *revs, **opts):
84 '''view the recent repository events
84 '''view the recent repository events
85 '''
85 '''
86
86
87 if not os.path.exists(repo.join('blackbox.log')):
87 if not os.path.exists(repo.join('blackbox.log')):
88 return
88 return
89
89
90 limit = opts.get('limit')
90 limit = opts.get('limit')
91 blackbox = repo.opener('blackbox.log', 'r')
91 blackbox = repo.opener('blackbox.log', 'r')
92 lines = blackbox.read().split('\n')
92 lines = blackbox.read().split('\n')
93
93
94 count = 0
94 count = 0
95 output = []
95 output = []
96 for line in reversed(lines):
96 for line in reversed(lines):
97 if count >= limit:
97 if count >= limit:
98 break
98 break
99
99
100 # count the commands by matching lines like: 2013/01/23 19:13:36 root>
100 # count the commands by matching lines like: 2013/01/23 19:13:36 root>
101 if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
101 if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
102 count += 1
102 count += 1
103 output.append(line)
103 output.append(line)
104
104
105 ui.status('\n'.join(reversed(output)))
105 ui.status('\n'.join(reversed(output)))
General Comments 0
You need to be logged in to leave comments. Login now