7d22fc236798 — Tony Tung 8 years ago
extract replaceclass out of manifestdiskcache

Summary: It's an useful function.  Plan to use it in another extension.

Test Plan: python ../../hg-crew/tests/run-tests.py --with-hg ../../hg-crew/hg test-manifestdiskcache.t

Reviewers: #sourcecontrol, lcharignon

Reviewed By: lcharignon

Subscribers: lcharignon, mitrandir

Differential Revision: https://phabricator.fb.com/D2896753

Signature: t1:2896753:1454542605:c660ee3e108b497c4a823bd732f98ebf5e147e02
3 files changed, 43 insertions(+), 36 deletions(-)

A => extutil.py
M manifestdiskcache.py
M tests/test-manifestdiskcache.t
A => extutil.py +36 -0
@@ 0,0 1,36 @@ 
+# extutil.py - useful utility methods for extensions
+#
+# Copyright 2016 Facebook
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+def replaceclass(container, classname):
+    '''Replace a class with another in a module, and interpose it into
+    the hierarchies of all loaded subclasses. This function is
+    intended for use as a decorator.
+
+      import mymodule
+      @replaceclass(mymodule, 'myclass')
+      class mysubclass(mymodule.myclass):
+          def foo(self):
+              f = super(mysubclass, self).foo()
+              return f + ' bar'
+
+    Existing instances of the class being replaced will not have their
+    __class__ modified, so call this function before creating any
+    objects of the target type.
+    '''
+    def wrap(cls):
+        oldcls = getattr(container, classname)
+        for subcls in oldcls.__subclasses__():
+            if subcls is not cls:
+                assert oldcls in subcls.__bases__
+                newbases = [oldbase
+                            for oldbase in subcls.__bases__
+                            if oldbase != oldcls]
+                newbases.append(cls)
+                subcls.__bases__ = tuple(newbases)
+        setattr(container, classname, cls)
+        return cls
+    return wrap

          
M manifestdiskcache.py +2 -30
@@ 44,42 44,14 @@ import sys
 import time
 import traceback
 
+from extutil import replaceclass
+
 CACHE_SUBDIR = 'manifestdiskcache'
 CONFIG_KEY = 'manifestdiskcache'
 HEX_SHA_SIZE_BYTES = 40
 
 testedwith = 'internal'
 
-def replaceclass(container, classname):
-    '''Replace a class with another in a module, and interpose it into
-    the hierarchies of all loaded subclasses. This function is
-    intended for use as a decorator.
-
-      import mymodule
-      @replaceclass(mymodule, 'myclass')
-      class mysubclass(mymodule.myclass):
-          def foo(self):
-              f = super(mysubclass, self).foo()
-              return f + ' bar'
-
-    Existing instances of the class being replaced will not have their
-    __class__ modified, so call this function before creating any
-    objects of the target type.
-    '''
-    def wrap(cls):
-        oldcls = getattr(container, classname)
-        for subcls in oldcls.__subclasses__():
-            if subcls is not cls:
-                assert oldcls in subcls.__bases__
-                newbases = [oldbase
-                            for oldbase in subcls.__bases__
-                            if oldbase != oldcls]
-                newbases.append(cls)
-                subcls.__bases__ = tuple(newbases)
-        setattr(container, classname, cls)
-        return cls
-    return wrap
-
 def extsetup(ui):
     global logging
     logging = ui.configbool(CONFIG_KEY, 'logging', False)

          
M tests/test-manifestdiskcache.t +5 -6
@@ 1,7 1,6 @@ 
 Setup
 
-  $ extpath=`dirname $TESTDIR`
-  $ cp $extpath/manifestdiskcache.py $TESTTMP # use $TESTTMP substitution in message
+  $ export PYTHONPATH=$TESTDIR/..:$PYTHONPATH
 
 Test functionality is present
 

          
@@ 10,7 9,7 @@ Test functionality is present
   $ hg init
   $ cat >> .hg/hgrc << EOF
   > [extensions]
-  > manifestdiskcache=$TESTTMP/manifestdiskcache.py
+  > manifestdiskcache=
   > [manifestdiskcache]
   > logging=True
   > enabled=True

          
@@ 46,7 45,7 @@ Test that we prune the cache.
   $ hg init
   $ cat >> .hg/hgrc << EOF
   > [extensions]
-  > manifestdiskcache=$TESTTMP/manifestdiskcache.py
+  > manifestdiskcache=
   > [manifestdiskcache]
   > logging=True
   > enabled=True

          
@@ 98,7 97,7 @@ Test that a corrupt cache does not inter
   $ hg init
   $ cat >> .hg/hgrc << EOF
   > [extensions]
-  > manifestdiskcache=$TESTTMP/manifestdiskcache.py
+  > manifestdiskcache=
   > [manifestdiskcache]
   > enabled=True
   > EOF

          
@@ 139,7 138,7 @@ Test that we can pin a revision in the c
   OK
   $ cat >> .hg/hgrc << EOF
   > [extensions]
-  > manifestdiskcache=$TESTTMP/manifestdiskcache.py
+  > manifestdiskcache=
   > [manifestdiskcache]
   > cache-size=0
   > runs-between-prunes=1