Patchwork [7,of,8] blackbox: properly replace ui class

login
register
mail settings
Submitter timeless@mozdev.org
Date Feb. 24, 2016, 8:10 p.m.
Message ID <c50b512bb9eb82a3a77b.1456344651@waste.org>
Download mbox | patch
Permalink /patch/13372/
State Accepted
Headers show

Comments

timeless@mozdev.org - Feb. 24, 2016, 8:10 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1454475280 0
#      Wed Feb 03 04:54:40 2016 +0000
# Node ID c50b512bb9eb82a3a77b3c95f6335a100fb11ce9
# Parent  cc84ed9b974032db3b779206b93fb6864e4673ee
blackbox: properly replace ui class

Without this, anyone creating a ui object using: uimod.ui()
skips the blackbox.

Also, anyone doing ui.copy() skipped the blackbox.

Unfortunately, the ui object lifestyle is a bit messy,
the first one that's created is never actually initialized
with subclasses, instead pieces of the subclass are adopted
into the primal ui object. In order to handle this, a
_partialinit method will be called to ensure that the
blackboxui is properly initialized.
Augie Fackler - Feb. 25, 2016, 6:09 p.m.
On Wed, Feb 24, 2016 at 02:10:51PM -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1454475280 0
> #      Wed Feb 03 04:54:40 2016 +0000
> # Node ID c50b512bb9eb82a3a77b3c95f6335a100fb11ce9
> # Parent  cc84ed9b974032db3b779206b93fb6864e4673ee
> blackbox: properly replace ui class

This one looks good and is queued. I had a question on patch 8.

>
> Without this, anyone creating a ui object using: uimod.ui()
> skips the blackbox.
>
> Also, anyone doing ui.copy() skipped the blackbox.
>
> Unfortunately, the ui object lifestyle is a bit messy,
> the first one that's created is never actually initialized
> with subclasses, instead pieces of the subclass are adopted
> into the primal ui object. In order to handle this, a
> _partialinit method will be called to ensure that the
> blackboxui is properly initialized.
>
> diff --git a/hgext/blackbox.py b/hgext/blackbox.py
> --- a/hgext/blackbox.py
> +++ b/hgext/blackbox.py
> @@ -44,6 +44,7 @@
>
>  from mercurial import (
>      cmdutil,
> +    ui as uimod,
>      util,
>  )
>
> @@ -79,6 +80,26 @@
>
>  def wrapui(ui):
>      class blackboxui(ui.__class__):
> +        def __init__(self, src=None):
> +            super(blackboxui, self).__init__(src)
> +            if src is None:
> +                self._partialinit()
> +            else:
> +                self._bbfp = src._bbfp
> +                self._bbrepo = src._bbrepo
> +                self._bbvfs = src._bbvfs
> +
> +        def _partialinit(self):
> +            if util.safehasattr(self, '_bbvfs'):
> +                return
> +            self._bbfp = None
> +            self._bbrepo = None
> +            self._bbvfs = None
> +
> +        def copy(self):
> +            self._partialinit()
> +            return self.__class__(self)
> +
>          @util.propertycache
>          def track(self):
>              return self.configlist('blackbox', 'track', ['*'])
> @@ -122,13 +143,14 @@
>          def log(self, event, *msg, **opts):
>              global lastui
>              super(blackboxui, self).log(event, *msg, **opts)
> +            self._partialinit()
>
>              if not '*' in self.track and not event in self.track:
>                  return
>
> -            if util.safehasattr(self, '_bbfp'):
> +            if self._bbfp:
>                  ui = self
> -            elif util.safehasattr(self, '_bbvfs'):
> +            elif self._bbvfs:
>                  try:
>                      self._bbfp = self._openlogfile()
>                  except (IOError, OSError) as err:
> @@ -143,15 +165,14 @@
>                  # was seen.
>                  ui = lastui
>
> -            if (util.safehasattr(ui, '_bbfp') and
> -                ui._bbfp is not None):
> +            if ui and ui._bbfp:
>                  date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
>                  user = util.getuser()
>                  pid = str(util.getpid())
>                  formattedmsg = msg[0] % msg[1:]
>                  rev = '(unknown)'
>                  changed = ''
> -                if util.safehasattr(ui, '_bbrepo'):
> +                if ui._bbrepo:
>                      ctx = ui._bbrepo[None]
>                      if ctx.rev() is not None:
>                          rev = hexfn(ctx.node())
> @@ -169,14 +190,16 @@
>                  except IOError as err:
>                      self.debug('warning: cannot write to blackbox.log: %s\n' %
>                                 err.strerror)
> -                if not lastui or util.safehasattr(ui, '_bbrepo'):
> +                if not lastui or ui._bbrepo:
>                      lastui = ui
>
>          def setrepo(self, repo):
> +            self._bbfp = None
> +            self._bbrepo = repo
>              self._bbvfs = repo.vfs
> -            self._bbrepo = repo
>
>      ui.__class__ = blackboxui
> +    uimod.ui = blackboxui
>
>  def uisetup(ui):
>      wrapui(ui)
> diff --git a/tests/test-blackbox.t b/tests/test-blackbox.t
> --- a/tests/test-blackbox.t
> +++ b/tests/test-blackbox.t
> @@ -178,8 +178,15 @@
>
>    result: None
>    $ hg blackbox
> -  1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)> blackbox
> -  1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)> blackbox exited 0 after * seconds (glob)
> +  1970/01/01 00:00:00 bob @0e46349438790c460c5c9f7546bfcd39b267bbd2 (5000)> commit -m commit2 -d 2000-01-02 foo
> +  1970/01/01 00:00:00 bob @0e46349438790c460c5c9f7546bfcd39b267bbd2 (5000)> updated served branch cache in * seconds (glob)
> +  1970/01/01 00:00:00 bob @0e46349438790c460c5c9f7546bfcd39b267bbd2 (5000)> wrote served branch cache with 1 labels and 1 nodes
> +  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> commit -m commit2 -d 2000-01-02 foo exited 0 after * seconds (glob)
> +  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r 0
> +  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> writing .hg/cache/tags2-visible with 0 tags
> +  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r 0 exited 0 after * seconds (glob)
> +  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r tip
> +  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r tip exited 0 after * seconds (glob)
>    1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> blackbox
>
>  cleanup
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -44,6 +44,7 @@ 
 
 from mercurial import (
     cmdutil,
+    ui as uimod,
     util,
 )
 
@@ -79,6 +80,26 @@ 
 
 def wrapui(ui):
     class blackboxui(ui.__class__):
+        def __init__(self, src=None):
+            super(blackboxui, self).__init__(src)
+            if src is None:
+                self._partialinit()
+            else:
+                self._bbfp = src._bbfp
+                self._bbrepo = src._bbrepo
+                self._bbvfs = src._bbvfs
+
+        def _partialinit(self):
+            if util.safehasattr(self, '_bbvfs'):
+                return
+            self._bbfp = None
+            self._bbrepo = None
+            self._bbvfs = None
+
+        def copy(self):
+            self._partialinit()
+            return self.__class__(self)
+
         @util.propertycache
         def track(self):
             return self.configlist('blackbox', 'track', ['*'])
@@ -122,13 +143,14 @@ 
         def log(self, event, *msg, **opts):
             global lastui
             super(blackboxui, self).log(event, *msg, **opts)
+            self._partialinit()
 
             if not '*' in self.track and not event in self.track:
                 return
 
-            if util.safehasattr(self, '_bbfp'):
+            if self._bbfp:
                 ui = self
-            elif util.safehasattr(self, '_bbvfs'):
+            elif self._bbvfs:
                 try:
                     self._bbfp = self._openlogfile()
                 except (IOError, OSError) as err:
@@ -143,15 +165,14 @@ 
                 # was seen.
                 ui = lastui
 
-            if (util.safehasattr(ui, '_bbfp') and
-                ui._bbfp is not None):
+            if ui and ui._bbfp:
                 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
                 user = util.getuser()
                 pid = str(util.getpid())
                 formattedmsg = msg[0] % msg[1:]
                 rev = '(unknown)'
                 changed = ''
-                if util.safehasattr(ui, '_bbrepo'):
+                if ui._bbrepo:
                     ctx = ui._bbrepo[None]
                     if ctx.rev() is not None:
                         rev = hexfn(ctx.node())
@@ -169,14 +190,16 @@ 
                 except IOError as err:
                     self.debug('warning: cannot write to blackbox.log: %s\n' %
                                err.strerror)
-                if not lastui or util.safehasattr(ui, '_bbrepo'):
+                if not lastui or ui._bbrepo:
                     lastui = ui
 
         def setrepo(self, repo):
+            self._bbfp = None
+            self._bbrepo = repo
             self._bbvfs = repo.vfs
-            self._bbrepo = repo
 
     ui.__class__ = blackboxui
+    uimod.ui = blackboxui
 
 def uisetup(ui):
     wrapui(ui)
diff --git a/tests/test-blackbox.t b/tests/test-blackbox.t
--- a/tests/test-blackbox.t
+++ b/tests/test-blackbox.t
@@ -178,8 +178,15 @@ 
   
   result: None
   $ hg blackbox
-  1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)> blackbox
-  1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)> blackbox exited 0 after * seconds (glob)
+  1970/01/01 00:00:00 bob @0e46349438790c460c5c9f7546bfcd39b267bbd2 (5000)> commit -m commit2 -d 2000-01-02 foo
+  1970/01/01 00:00:00 bob @0e46349438790c460c5c9f7546bfcd39b267bbd2 (5000)> updated served branch cache in * seconds (glob)
+  1970/01/01 00:00:00 bob @0e46349438790c460c5c9f7546bfcd39b267bbd2 (5000)> wrote served branch cache with 1 labels and 1 nodes
+  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> commit -m commit2 -d 2000-01-02 foo exited 0 after * seconds (glob)
+  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r 0
+  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> writing .hg/cache/tags2-visible with 0 tags
+  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r 0 exited 0 after * seconds (glob)
+  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r tip
+  1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> log -r tip exited 0 after * seconds (glob)
   1970/01/01 00:00:00 bob @45589e459b2edfbf3dbde7e01f611d2c1e7453d7 (5000)> blackbox
 
 cleanup