[elbe-devel] [PATCH 1/1] Add decorator for FinetuningAction registration

dion at linutronix.de dion at linutronix.de
Tue Jun 4 12:23:22 CEST 2019


From: Olivier Dion <dion at linutronix.de>

The decorator takes 1 argument, 'tag', and 1 optional argument,
'register' set to 'True' by default.

The 'tag' argument is a string that represent the xml tag for the
finetuning action.

The 'register' argument can be set to 'False' to ignore the
registration in the decorator.

Example
=======

``````````````````````````````````````````````````````````````````````
@FinetuningAction.register("my-xml-tag")
MyAction(FinetuningAction):
<...>
@FinetuningAction.register("my-xml-tag-not-ready", register=False)
MyActionNotReady(FinetuningAction):
<...>
````````````````````````````````````````````````````````````

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/finetuning.py | 183 ++++++++++---------------------------------------
 1 file changed, 36 insertions(+), 147 deletions(-)

diff --git a/elbepack/finetuning.py b/elbepack/finetuning.py
index 426097aa..eac2eff4 100644
--- a/elbepack/finetuning.py
+++ b/elbepack/finetuning.py
@@ -34,8 +34,12 @@ class FinetuningAction(object):
     actiondict = {}
 
     @classmethod
-    def register(cls, action):
-        cls.actiondict[action.tag] = action
+    def register(cls, tag, register=True):
+        def _register(action):
+            if register is True:
+                cls.actiondict[tag] = action
+            return action
+        return _register
 
     def __new__(cls, node):
         action = cls.actiondict[node.tag]
@@ -51,25 +55,23 @@ class FinetuningAction(object):
         self.execute(log, buildenv, target)
 
 
+ at FinetuningAction.register('image_finetuning', False)
 class ImageFinetuningAction(FinetuningAction):
 
-    tag = 'image_finetuning'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
     def execute(self, _log, _buildenv, _target):
         raise NotImplementedError("<%s> may only be "
-                                  "used in <image-finetuning>" % self.tag)
+                                  "used in <image-finetuning>" % 'image_finetuning')
 
     def execute_img(self, _log, _buildenv, _target, _builddir, _loop_dev):
         raise NotImplementedError('execute_img() not implemented')
 
 
+ at FinetuningAction.register('rm')
 class RmAction(FinetuningAction):
 
-    tag = 'rm'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -88,13 +90,9 @@ class RmAction(FinetuningAction):
             log.do("rm -rvf '%s'" % f)
 
 
-FinetuningAction.register(RmAction)
-
-
+ at FinetuningAction.register('mkdir')
 class MkdirAction(FinetuningAction):
 
-    tag = 'mkdir'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -102,13 +100,9 @@ class MkdirAction(FinetuningAction):
         log.do("mkdir -p " + target.fname(self.node.et.text))
 
 
-FinetuningAction.register(MkdirAction)
-
-
+ at FinetuningAction.register('mknod')
 class MknodAction(FinetuningAction):
 
-    tag = 'mknod'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -120,14 +114,9 @@ class MknodAction(FinetuningAction):
             " " +
             self.node.et.attrib['opts'])
 
-
-FinetuningAction.register(MknodAction)
-
-
+ at FinetuningAction.register('buildenv_mkdir')
 class BuildenvMkdirAction(FinetuningAction):
 
-    tag = 'buildenv_mkdir'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -135,13 +124,9 @@ class BuildenvMkdirAction(FinetuningAction):
         log.do("mkdir -p " + buildenv.rfs.fname(self.node.et.text))
 
 
-FinetuningAction.register(BuildenvMkdirAction)
-
-
+ at FinetuningAction.register('cp')
 class CpAction(FinetuningAction):
 
-    tag = 'cp'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -151,13 +136,9 @@ class CpAction(FinetuningAction):
             log.do("cp -av " + f + " " + target.fname(self.node.et.text))
 
 
-FinetuningAction.register(CpAction)
-
-
+ at FinetuningAction.register('buildenv_cp')
 class BuildenvCpAction(FinetuningAction):
 
-    tag = 'buildenv_cp'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -167,13 +148,9 @@ class BuildenvCpAction(FinetuningAction):
             log.do("cp -av " + f + " " + buildenv.rfs.fname(self.node.et.text))
 
 
-FinetuningAction.register(BuildenvCpAction)
-
-
+ at FinetuningAction.register('b2t_cp')
 class B2TCpAction(FinetuningAction):
 
-    tag = 'b2t_cp'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -183,13 +160,9 @@ class B2TCpAction(FinetuningAction):
             log.do("cp -av " + f + " " + target.fname(self.node.et.text))
 
 
-FinetuningAction.register(B2TCpAction)
-
-
+ at FinetuningAction.register('t2b_cp')
 class T2BCpAction(FinetuningAction):
 
-    tag = 't2b_cp'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -198,14 +171,9 @@ class T2BCpAction(FinetuningAction):
         for f in src:
             log.do("cp -av " + f + " " + buildenv.rfs.fname(self.node.et.text))
 
-
-FinetuningAction.register(T2BCpAction)
-
-
+ at FinetuningAction.register('t2p_mv')
 class T2PMvAction(FinetuningAction):
 
-    tag = 't2p_mv'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -221,13 +189,9 @@ class T2PMvAction(FinetuningAction):
             log.do("mv -v " + f + " " + dest)
 
 
-FinetuningAction.register(T2PMvAction)
-
-
+ at FinetuningAction.register('mv')
 class MvAction(FinetuningAction):
 
-    tag = 'mv'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -237,13 +201,9 @@ class MvAction(FinetuningAction):
             log.do("mv -v " + f + " " + target.fname(self.node.et.text))
 
 
-FinetuningAction.register(MvAction)
-
-
+ at FinetuningAction.register('ln')
 class LnAction(FinetuningAction):
 
-    tag = 'ln'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -254,13 +214,9 @@ class LnAction(FinetuningAction):
                 (self.node.et.attrib['path'], self.node.et.text))
 
 
-FinetuningAction.register(LnAction)
-
-
+ at FinetuningAction.register('buildenv_mv')
 class BuildenvMvAction(FinetuningAction):
 
-    tag = 'buildenv_mv'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -269,14 +225,9 @@ class BuildenvMvAction(FinetuningAction):
         for f in src:
             log.do("mv -v " + f + " " + buildenv.rfs.fname(self.node.et.text))
 
-
-FinetuningAction.register(BuildenvMvAction)
-
-
+ at FinetuningAction.register('adduser')
 class AddUserAction(FinetuningAction):
 
-    tag = 'adduser'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -319,13 +270,9 @@ class AddUserAction(FinetuningAction):
                                self.node.et.text))
 
 
-FinetuningAction.register(AddUserAction)
-
-
+ at FinetuningAction.register('addgroup')
 class AddGroupAction(FinetuningAction):
 
-    tag = 'addgroup'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -343,13 +290,9 @@ class AddGroupAction(FinetuningAction):
                 self.node.et.text))
 
 
-FinetuningAction.register(AddGroupAction)
-
-
+ at FinetuningAction.register('file')
 class AddFileAction(FinetuningAction):
 
-    tag = 'file'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -406,13 +349,10 @@ class AddFileAction(FinetuningAction):
         if mode is not None:
             log.chroot(target.path, 'chmod "%s" "%s"' % (mode, dst))
 
-FinetuningAction.register(AddFileAction)
-
 
+ at FinetuningAction.register('raw_cmd')
 class RawCmdAction(FinetuningAction):
 
-    tag = 'raw_cmd'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -421,13 +361,9 @@ class RawCmdAction(FinetuningAction):
             log.chroot(target.path, self.node.et.text)
 
 
-FinetuningAction.register(RawCmdAction)
-
-
+ at FinetuningAction.register('command')
 class CmdAction(FinetuningAction):
 
-    tag = 'command'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -436,13 +372,9 @@ class CmdAction(FinetuningAction):
             log.chroot(target.path, "/bin/sh", stdin=self.node.et.text)
 
 
-FinetuningAction.register(CmdAction)
-
-
+ at FinetuningAction.register('buildenv_command')
 class BuildenvCmdAction(FinetuningAction):
 
-    tag = 'buildenv_command'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -451,13 +383,9 @@ class BuildenvCmdAction(FinetuningAction):
             log.chroot(buildenv.path, "/bin/sh", stdin=self.node.et.text)
 
 
-FinetuningAction.register(BuildenvCmdAction)
-
-
+ at FinetuningAction.register('purge')
 class PurgeAction(FinetuningAction):
 
-    tag = 'purge'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -466,13 +394,9 @@ class PurgeAction(FinetuningAction):
             log.chroot(target.path, "dpkg --purge " + self.node.et.text)
 
 
-FinetuningAction.register(PurgeAction)
-
-
+ at FinetuningAction.register('updated')
 class UpdatedAction(FinetuningAction):
 
-    tag = 'updated'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -560,13 +484,9 @@ class UpdatedAction(FinetuningAction):
         target.touch_file('/var/cache/elbe/.downgrade_allowed')
 
 
-FinetuningAction.register(UpdatedAction)
-
-
+ at FinetuningAction.register('artifact')
 class ArtifactAction(FinetuningAction):
 
-    tag = 'artifact'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -577,13 +497,9 @@ class ArtifactAction(FinetuningAction):
         target.images.append(self.node.et.text)
 
 
-FinetuningAction.register(ArtifactAction)
-
-
+ at FinetuningAction.register('rm_artifact')
 class RmArtifactAction(FinetuningAction):
 
-    tag = 'rm_artifact'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -595,13 +511,9 @@ class RmArtifactAction(FinetuningAction):
         target.images.remove(self.node.et.text)
 
 
-FinetuningAction.register(ArtifactAction)
-
-
+ at FinetuningAction.register('losetup')
 class LosetupAction(FinetuningAction):
 
-    tag = 'losetup'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -624,13 +536,9 @@ class LosetupAction(FinetuningAction):
             log.do(cmd)
 
 
-FinetuningAction.register(LosetupAction)
-
-
+ at FinetuningAction.register('img_convert')
 class ImgConvertAction(FinetuningAction):
 
-    tag = 'img_convert'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -666,13 +574,9 @@ class ImgConvertAction(FinetuningAction):
             del target.image_packers[src]
 
 
-FinetuningAction.register(ImgConvertAction)
-
-
+ at FinetuningAction.register('set_packer')
 class SetPackerAction(FinetuningAction):
 
-    tag = 'set_packer'
-
     def __init__(self, node):
         FinetuningAction.__init__(self, node)
 
@@ -687,13 +591,9 @@ class SetPackerAction(FinetuningAction):
         target.image_packers[img] = packers[packer]
 
 
-FinetuningAction.register(SetPackerAction)
-
-
+ at FinetuningAction.register('extract_partition')
 class ExtractPartitionAction(ImageFinetuningAction):
 
-    tag = 'extract_partition'
-
     def __init__(self, node):
         ImageFinetuningAction.__init__(self, node)
 
@@ -713,13 +613,9 @@ class ExtractPartitionAction(ImageFinetuningAction):
         target.image_packers[self.node.et.text] = default_packer
 
 
-FinetuningAction.register(ExtractPartitionAction)
-
-
+ at FinetuningAction.register('copy_from_partition')
 class CopyFromPartition(ImageFinetuningAction):
 
-    tag = 'copy_from_partition'
-
     def __init__(self, node):
         ImageFinetuningAction.__init__(self, node)
 
@@ -753,13 +649,9 @@ class CopyFromPartition(ImageFinetuningAction):
             target.images.append(aname)
 
 
-FinetuningAction.register(CopyFromPartition)
-
-
+ at FinetuningAction.register('copy_to_partition')
 class CopyToPartition(ImageFinetuningAction):
 
-    tag = 'copy_to_partition'
-
     def __init__(self, node):
         ImageFinetuningAction.__init__(self, node)
 
@@ -781,9 +673,6 @@ class CopyToPartition(ImageFinetuningAction):
             log.do(cmd)
 
 
-FinetuningAction.register(CopyToPartition)
-
-
 def do_finetuning(xml, log, buildenv, target):
 
     if not xml.has('target/finetuning'):
-- 
2.11.0




More information about the elbe-devel mailing list