Patchwork D769: blackbox: move _openlogfile to a separate method

login
register
mail settings
Submitter phabricator
Date Sept. 22, 2017, 12:50 p.m.
Message ID <d2d5fe7307d373cbe48424a3089cf594@localhost.localdomain>
Download mbox | patch
Permalink /patch/24113/
State Not Applicable
Headers show

Comments

phabricator - Sept. 22, 2017, 12:50 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGe6723c939344: blackbox: move _openlogfile to a separate method (authored by quark, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D769?vs=1971&id=2008

REVISION DETAIL
  https://phab.mercurial-scm.org/D769

AFFECTED FILES
  hgext/blackbox.py

CHANGE DETAILS




To: quark, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -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' %