From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/packagerules/actions/
Date: Thu, 13 Jun 2013 16:34:26 +0000 (UTC) [thread overview]
Message-ID: <1370378970.2d830ed00d38205d1f8ec38345dc04621edfdf29.dywi@gentoo> (raw)
commit: 2d830ed00d38205d1f8ec38345dc04621edfdf29
Author: André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Tue Jun 4 20:49:30 2013 +0000
Commit: André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Tue Jun 4 20:49:30 2013 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=2d830ed0
package rules, actions: modify package information
The "info" modules provides rather abstract classes for modifying the info dict
of a PackageInfo instance, either by simply adding key=value to it (thereby
overwriting the previous value) or by manipulating an existing value with a
regex.
Note: PackageInfo does not support these actions yet (it will ignore them)
---
roverlay/packagerules/actions/info.py | 170 ++++++++++++++++++++++++++++++
roverlay/packagerules/actions/relocate.py | 37 +++++++
2 files changed, 207 insertions(+)
diff --git a/roverlay/packagerules/actions/info.py b/roverlay/packagerules/actions/info.py
new file mode 100644
index 0000000..538fdef
--- /dev/null
+++ b/roverlay/packagerules/actions/info.py
@@ -0,0 +1,170 @@
+# R overlay -- package rule actions, modify package information
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+__all__ = [
+ 'InfoRenameAction', 'LazyInfoRenameAction', 'SuperLazyInfoRenameAction',
+ 'InfoSetToAction',
+]
+
+import re
+
+import roverlay.packagerules.actions.attach
+import roverlay.packagerules.abstract.actions
+
+
+class InfoRenameAction (
+ roverlay.packagerules.abstract.actions.PackageRuleAction
+):
+ """A rename action modifies a package's info using regular expressions."""
+
+ def __init__ ( self, key, regex, subst, priority=1000 ):
+ """Constructor for InfoRenameAction.
+
+ arguments:
+ * key -- info key that should be modified
+ * regex -- regex for matching a part of the original value
+ (using re.search())
+ * subst -- replacement for the matched value part
+ * priority --
+ """
+ super ( InfoRenameAction, self ).__init__ ( priority=priority )
+
+ self.key = key
+ self.regex = (
+ re.compile ( regex ) if isinstance ( regex, str ) else regex
+ )
+ self.subst = subst
+ # --- end of __init__ (...) ---
+
+ def re_sub ( self, value ):
+ # count? flags?
+ return self.regex.sub ( self.subst, value )
+ # --- end of re_sub (...) ---
+
+ def apply_action ( self, p_info ):
+ """Sets
+ p_info [<stored key>] = <original value modified by stored regex,subst>
+
+ arguments:
+ * p_info --
+ """
+ # this only works for keys known _before_ reading desc data
+ p_info.set_direct_unsafe ( self.key, self.re_sub ( p_info [self.key] ) )
+ # --- end of apply_action (...) ---
+
+ def gen_str ( self, level ):
+ # FIXME: that's not always correct!
+ # (could be solved by storing the original regex delimiter)
+ yield (
+ level * ' ' + 'rename ' + self.key
+ + ' s/' + self.regex.pattern + '/' + self.subst + '/' # + flags
+ )
+ # --- end of gen_str (...) ---
+
+# --- end of InfoRenameAction ---
+
+
+class InfoRenameOtherAction ( InfoRenameAction ):
+ """Like InfoRenameAction,
+ but uses a second key for retrieving the original value.
+ """
+
+ def __init__ ( self, key, src_key, dest_key, regex, subst, priority=1000 ):
+ super ( InfoRenameOtherAction, self ).__init__ (
+ key=key, regex=regex, subst=subst, priority=priority
+ )
+ # note that key is only used in gen_str()
+ self.src_key = src_key
+ self.dest_key = dest_key
+ # --- end of __init__ (...) ---
+
+ def apply_action ( self, p_info ):
+ orig_value = p_info [self.src_key]
+ p_info.set_direct_unsafe (
+ self.dest_key, self.re_sub ( p_info [self.src_key] )
+ )
+ # --- end of apply_action (...) ---
+
+# --- end of InfoRenameOtherAction ---
+
+
+class LazyInfoRenameAction (
+ roverlay.packagerules.actions.attach.LazyAction
+):
+ """A lazy variant of InfoRenameAction."""
+ def __init__ ( self, key, regex, subst, priority=1000 ):
+ super ( LazyInfoRenameAction, self ).__init__ (
+ InfoRenameAction ( key=key, regex=regex, subst=subst, priority=None ),
+ priority=priority
+ )
+
+ # or use self._action.key (but self.key should point to the same object)
+ self.key = key
+ # --- end of __init__ (...) ---
+
+ def can_apply_action ( self, p_info ):
+ return p_info.has_key ( self.key )
+ # --- end of can_apply_action (...) ---
+
+# --- end of LazyInfoRenameAction ---
+
+
+class SuperLazyInfoRenameAction (
+ roverlay.packagerules.actions.attach.SuperLazyAction
+):
+ """A super lazy variant of InfoRenameAction."""
+
+ # alternatively use multi-inheritance in [Super]Lazy<modify type>InfoAction
+
+ def __init__ ( self, key, regex, subst, priority=1000 ):
+ super ( SuperLazyInfoRenameAction, self ).__init__ (
+ InfoRenameAction ( key=key, regex=regex, subst=subst, priority=None ),
+ priority=priority
+ )
+ self.key = key
+ # --- end of __init__ (...) ---
+
+ def can_apply_action ( self, p_info ):
+ return p_info.has_key ( self.key )
+ # --- end of can_apply_action (...) ---
+
+# --- end of SuperLazyInfoRenameAction ---
+
+
+class InfoSetToAction (
+ roverlay.packagerules.abstract.actions.PackageRuleAction
+):
+ """A set-to action simply sets a package's info."""
+
+ def __init__ ( self, key, value, priority=1000 ):
+ """Constructor for InfoSetToAction.
+
+ arguments:
+ * key -- info key that should be modified
+ * value -- value that will be stored
+ * priority --
+ """
+ super ( InfoSetToAction, self ).__init__ ( priority=priority )
+ self.key = key
+ self.value = value
+ # --- end of __init__ (...) ---
+
+ def apply_action ( self, p_info ):
+ """Sets p_info [<stored key>] = <stored value>.
+
+ arguments:
+ * p_info --
+ """
+ p_info.set_direct_unsafe ( self.key, self.value )
+ # --- end of apply_action (...) ---
+
+ def gen_str ( self, level ):
+ yield ( level * ' ' + 'set ' + self.key + ' ' + self.value )
+ # --- end of gen_str (...) ---
+
+# --- end of InfoSetToAction ---
+
+# no lazy variants of InfoSetToAction - it should always be applied directly
diff --git a/roverlay/packagerules/actions/relocate.py b/roverlay/packagerules/actions/relocate.py
new file mode 100644
index 0000000..52e3281
--- /dev/null
+++ b/roverlay/packagerules/actions/relocate.py
@@ -0,0 +1,37 @@
+# R overlay -- package rule actions, specific classes for modifing pkg info
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+import os.path
+
+import roverlay.packagerules.actions.info
+
+# FIXME: rename module?
+
+class SrcDestRenameAction (
+ roverlay.packagerules.actions.info.InfoRenameOtherAction
+):
+
+ def __init__ ( self, key, regex, subst, priority=1000 ):
+ super ( SrcDestRenameAction, self ).__init__ (
+ key=key, src_key="package_filename",
+ dest_key="src_uri_dest", regex=regex, subst=subst, priority=priority
+ )
+ # --- end of __init__ (...) ---
+
+ def apply_action ( self, p_info ):
+ # don't modify the ".tar.gz" file extension
+ # TODO: other f-exts will be replaced, this is not critical, because:
+ # * all R packages end with .tar.gz
+ # * fext is an empty str if orig_value does not end with .tar.gz,
+ # so combining fname and fext does not break anything
+ # => worst case is "more accurate regex required" (+overhead here)
+ #
+ fname, fext, DONT_CARE = p_info [self.src_key].partition ( ".tar.gz" )
+
+ p_info.set_direct_unsafe ( self.dest_key, self.re_sub ( fname ) + fext )
+ # --- end of apply_action (...) ---
+
+# --- end of SrcDestRenameAction (...) ---
next reply other threads:[~2013-06-13 16:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-13 16:34 André Erdmann [this message]
-- strict thread matches above, loose matches on Subject: below --
2013-06-13 16:34 [gentoo-commits] proj/R_overlay:master commit in: roverlay/packagerules/actions/ André Erdmann
2013-08-23 13:52 André Erdmann
2013-09-05 16:01 André Erdmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1370378970.2d830ed00d38205d1f8ec38345dc04621edfdf29.dywi@gentoo \
--to=dywi@mailerd.de \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox