changeset 34300:e6723c939344

blackbox: move _openlogfile to a separate method This removes several `stat` syscalls for accessing `_bbvfs` and makes `ui` object cleaner. Differential Revision: https://phab.mercurial-scm.org/D769
author Jun Wu <quark@fb.com>
date Thu, 21 Sep 2017 11:10:20 -0700
parents b1d4ac068961
children 3704d3f21136
files hgext/blackbox.py
diffstat 1 files changed, 35 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/blackbox.py	Thu Sep 21 11:03:37 2017 -0700
+++ b/hgext/blackbox.py	Thu Sep 21 11:10:20 2017 -0700
@@ -73,6 +73,40 @@
 
 lastui = None
 
+def _openlogfile(ui, vfs):
+    def rotate(oldpath, newpath):
+        try:
+            vfs.unlink(newpath)
+        except OSError as err:
+            if err.errno != errno.ENOENT:
+                ui.debug("warning: cannot remove '%s': %s\n" %
+                         (newpath, err.strerror))
+        try:
+            if newpath:
+                vfs.rename(oldpath, newpath)
+        except OSError as err:
+            if err.errno != errno.ENOENT:
+                ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
+                         (newpath, oldpath, err.strerror))
+
+    maxsize = ui.configbytes('blackbox', 'maxsize')
+    name = 'blackbox.log'
+    if maxsize > 0:
+        try:
+            st = vfs.stat(name)
+        except OSError:
+            pass
+        else:
+            if st.st_size >= maxsize:
+                path = vfs.join(name)
+                maxfiles = ui.configint('blackbox', 'maxfiles', 7)
+                for i in xrange(maxfiles - 1, 1, -1):
+                    rotate(oldpath='%s.%d' % (path, i - 1),
+                           newpath='%s.%d' % (path, i))
+                rotate(oldpath=path,
+                       newpath=maxfiles > 0 and path + '.1')
+    return vfs(name, 'a')
+
 def wrapui(ui):
     class blackboxui(ui.__class__):
         @property
@@ -89,40 +123,6 @@
         def track(self):
             return self.configlist('blackbox', 'track', ['*'])
 
-        def _openlogfile(self):
-            def rotate(oldpath, newpath):
-                try:
-                    self._bbvfs.unlink(newpath)
-                except OSError as err:
-                    if err.errno != errno.ENOENT:
-                        self.debug("warning: cannot remove '%s': %s\n" %
-                                   (newpath, err.strerror))
-                try:
-                    if newpath:
-                        self._bbvfs.rename(oldpath, newpath)
-                except OSError as err:
-                    if err.errno != errno.ENOENT:
-                        self.debug("warning: cannot rename '%s' to '%s': %s\n" %
-                                   (newpath, oldpath, err.strerror))
-
-            maxsize = self.configbytes('blackbox', 'maxsize')
-            name = 'blackbox.log'
-            if maxsize > 0:
-                try:
-                    st = self._bbvfs.stat(name)
-                except OSError:
-                    pass
-                else:
-                    if st.st_size >= maxsize:
-                        path = self._bbvfs.join(name)
-                        maxfiles = self.configint('blackbox', 'maxfiles', 7)
-                        for i in xrange(maxfiles - 1, 1, -1):
-                            rotate(oldpath='%s.%d' % (path, i - 1),
-                                   newpath='%s.%d' % (path, i))
-                        rotate(oldpath=path,
-                               newpath=maxfiles > 0 and path + '.1')
-            return self._bbvfs(name, 'a')
-
         def log(self, event, *msg, **opts):
             global lastui
             super(blackboxui, self).log(event, *msg, **opts)
@@ -172,7 +172,7 @@
             try:
                 fmt = '%s %s @%s%s (%s)%s> %s'
                 args = (date, user, rev, changed, pid, src, formattedmsg)
-                with ui._openlogfile() as fp:
+                with _openlogfile(ui, vfs) as fp:
                     fp.write(fmt % args)
             except (IOError, OSError) as err:
                 self.debug('warning: cannot write to blackbox.log: %s\n' %