diff options
author | 2014-07-17 22:06:48 +0200 | |
---|---|---|
committer | 2014-07-17 22:06:48 +0200 | |
commit | 12380781282c45e9bb6ed6d56af08b2382738841 (patch) | |
tree | 475d2dc461dc0f20696fdf98e30d68600b7a4590 /roverlay | |
parent | overlay/abccontrol: convert_str() (diff) | |
download | R_overlay-12380781282c45e9bb6ed6d56af08b2382738841.tar.gz R_overlay-12380781282c45e9bb6ed6d56af08b2382738841.tar.bz2 R_overlay-12380781282c45e9bb6ed6d56af08b2382738841.zip |
add-policy rule gen: BitmaskMapCreator
creates a "bitmask" -> "acceptor chain" map, which means that the addition
control pkgrule generator is mostly done now, except for some cleanup.
All steps have been implemented, what remains to be done is integrating it
in the package rule system and roverlay.defaultscript (+argparser etc.).
Diffstat (limited to 'roverlay')
-rw-r--r-- | roverlay/packagerules/generators/addition_control.py | 289 |
1 files changed, 225 insertions, 64 deletions
diff --git a/roverlay/packagerules/generators/addition_control.py b/roverlay/packagerules/generators/addition_control.py index 0021817..1591ecd 100644 --- a/roverlay/packagerules/generators/addition_control.py +++ b/roverlay/packagerules/generators/addition_control.py @@ -5,6 +5,7 @@ # either version 2 of the License, or (at your option) any later version. from __future__ import absolute_import +from __future__ import print_function import abc @@ -13,12 +14,16 @@ import fnmatch import re + import roverlay.packagerules.generators.abstract.addition_control import roverlay.packagerules.abstract.acceptors import roverlay.packagerules.acceptors.stringmatch import roverlay.packagerules.acceptors.util +import roverlay.overlay.abccontrol +from roverlay.overlay.abccontrol import AdditionControlResult + from roverlay.packagerules.abstract.acceptors import Acceptor_AND @@ -30,7 +35,7 @@ from roverlay.packagerules.acceptors.util import ( get_category, get_ebuild_name, get_ebuild_version_tuple, ) -#import roverlay.util.fileio +import roverlay.util.fileio import roverlay.util.namespace import roverlay.util.portage_regex.default @@ -43,12 +48,6 @@ from roverlay.util.portage_regex.wildcard import ( - - - - - - class TokenValueError ( ValueError ): pass @@ -59,7 +58,18 @@ class TokenItemNotSupported ( TokenValueError ): pass +def _read_list_file ( + filepath, + _read_text_file=roverlay.util.fileio.read_text_file +): + def strip_line ( s ): + return s.strip() + if filepath: + for line in _read_text_file ( filepath, preparse=strip_line ): + # skip line if <> + yield line +# --- end of _read_list_file (...) --- class AdditionControlPackageRuleGenerator ( @@ -90,6 +100,13 @@ class AdditionControlPackageRuleGenerator ( # CategoryToken = collections.namedtuple ( 'CategoryToken', 'name' ) + DEFAULT_CATEGORY_TOKEN = CategoryToken ( + TokenItemTuple ( + TOKEN_ITEM_IS_STR, + roverlay.packagerules.acceptors.util.DEFAULT_CATEGORY_REPLACEMENT + ) + ) + def create_token_item_from_str ( self, s ): get_obj = self.namespace.get_object_v @@ -162,12 +179,16 @@ class AdditionControlPackageRuleGenerator ( # --- end of _create_package_token (...) --- def _create_category_token ( self, category_str ): - category_token = self.namespace.get_object_v ( - self.__class__.CategoryToken, - ( - self.create_token_item_from_str ( category_str ), + if category_str == self.default_category: + # validate_token() not necessary (assumption) + return self.DEFAULT_CATEGORY_TOKEN + else: + category_token = self.namespace.get_object_v ( + self.__class__.CategoryToken, + ( + self.create_token_item_from_str ( category_str ), + ) ) - ) return self.validate_token ( category_token ) # --- end of _create_category_token (...) --- @@ -253,7 +274,9 @@ class AdditionControlPackageRuleGenerator ( def __init__ ( self, default_category ): super ( AdditionControlPackageRuleGenerator, self ).__init__() - self.namespace = roverlay.util.namespace.SimpleNamespace() + self.namespace = roverlay.util.namespace.SimpleNamespace() + self.default_category = default_category + # --- end of __init__ (...) --- def clear_object_cache ( self ): if self.namespace: @@ -290,9 +313,7 @@ class AdditionControlPackageRuleGenerator ( # --- end of token_item_to_acceptor (...) --- def category_token_to_acceptor ( self, category_token, priority ): - if category_token is True: - raise Exception ( "C-TRUE" ) - + assert category_token and category_token is not True return self.token_item_to_acceptor ( category_token.name, get_category, priority @@ -300,40 +321,176 @@ class AdditionControlPackageRuleGenerator ( # --- end of category_token_to_acceptor (...) --- def package_token_to_acceptor ( self, package_token, priority ): - if package_token is True: - raise Exception ( "P-TRUE" ) + assert package_token and package_token is not True + + relevant_items = [ + item_and_getter for item_and_getter in zip ( + package_token, + ( + get_ebuild_name, + get_ebuild_version_tuple + ) + ) if item_and_getter[0] and item_and_getter[0] is not True + ] + + if not relevant_items: + raise TokenValueError ( package_token ) + + elif len(relevant_items) == 1: + return self.token_item_to_acceptor ( + relevant_items[0][0], relevant_items[0][1], priority + ) else: - relevant_items = [ - item_and_getter for item_and_getter in zip ( - package_token, - ( - get_ebuild_name, - get_ebuild_version_tuple - ) - ) if item_and_getter[0] and item_and_getter[0] is not True + sub_acceptors = [ + self.token_item_to_acceptor ( item, getter, 0 ) + for item, getter in relevant_items ] - if not relevant_items: - raise TokenValueError ( package_token ) + combined_acceptor = Acceptor_AND ( priority=priority ) + for sub_acceptor in sub_acceptors: + combined_acceptor.add_acceptor ( sub_acceptor ) - elif len(relevant_items) == 1: - return self.token_item_to_acceptor ( - relevant_items[0][0], relevant_items[0][1], priority - ) + return combined_acceptor + # --- end of package_token_to_acceptor (...) --- + +# --- end of AdditionControlPackageRuleGenerator --- + + +class BitmaskMapCreator ( object ): + """creates a "bitmask" => "acceptor chain" map""" + + def __init__ ( self, rule_generator ): + super ( BitmaskMapCreator, self ).__init__() + self.rule_generator = rule_generator + self.data = rule_generator.create_new_bitmask_map() + + def get_bitmask ( self ): + return self.data + + def get_bitmask_copy ( self ): + return self.data.copy() + + def _insert_package ( self, bitmask_arg, package_str, package_regex ): + category_token, package_token = self.rule_generator.create_token ( + package_str, with_category=True, package_regex=package_regex + ) + + if isinstance ( bitmask_arg, str ): + bitmask_int = AdditionControlResult.convert_str ( bitmask_arg ) + else: + bitmask_int = int ( bitmask_arg ) + + + try: + cat_entry = self.data [category_token] + except KeyError: + self.data [category_token] = { package_token: bitmask_int } + else: + try: + pkg_entry = cat_entry [package_token] + except KeyError: + cat_entry [package_token] = bitmask_int else: - sub_acceptors = [ - self.token_item_to_acceptor ( item, getter, 0 ) - for item, getter in relevant_items - ] + pkg_entry |= bitmask_int + # -- end try <package entry exists> + # -- end try <category entry exists> + # --- end of _insert_package (...) --- - combined_acceptor = Acceptor_AND ( priority=priority ) - for sub_acceptor in sub_acceptors: - combined_acceptor.add_acceptor ( sub_acceptor ) + def _split_bitmask_line ( self, line, default_bitmask ): + # str<[bitmask,]arg> => ( bitmask||default_bitmask, arg ) + args = line.split ( None, 1 ) + + if len(args) == 2: + # convert bitmask str now + return ( AdditionControlResult.convert_str(args[0]), args[1] ) + + elif default_bitmask or ( + default_bitmask == 0 and default_bitmask is not False + # ? or default_bitmask is 0 + ): + return ( default_bitmask, args[0] ) + + else: + raise ValueError ( line ) + # --- end of _split_bitmask_line (...) --- + + def _insert_packages_v ( + self, bitmask, arglist, package_regex, extended_format + ): + insert_package = self._insert_package + + if extended_format: + split_bitmask_line = self._split_bitmask_line + + for arg in arglist: + call_args = split_bitmask_line ( arg, bitmask ) + insert_package ( call_args[0], call_args[1], package_regex ) + + else: + for arg in arglist: + insert_package ( bitmask, arg, package_regex ) + # --- end of _insert_packages_v (...) --- + + def insert_packages_v ( self, bitmask, packages, extended_format=False ): + self._insert_packages_v ( + bitmask, packages, RE_WILDCARD_PACKAGE, extended_format + ) + # --- end of insert_packages_v (...) --- + + def insert_package ( self, bitmask, package, *args, **kwargs ): + self.insert_packages_v ( bitmask, ( package, ), *args, **kwargs ) + + def insert_packages ( self, bitmask, *packages, **kwargs ): + self.insert_packages_v ( bitmask, packages, **kwargs ) + + def insert_ebuild_files_v ( + self, bitmask, ebuild_files, extended_format=False + ): + self._insert_packages_v ( + bitmask, ebuild_files, RE_PACKAGE_EBUILD_FILE, extended_format + ) + # --- end of insert_ebuild_files_v (...) --- + + def insert_ebuild_file ( self, bitmask, ebuild_file, *args, **kwargs ): + self.insert_ebuild_files_v ( + bitmask, ( ebuild_file, ), *args, **kwargs + ) + + def insert_ebuild_files ( self, bitmask, *ebuild_files, **kwargs ): + self.insert_ebuild_files_v ( bitmask, ebuild_files, **kwargs ) + + + def feed ( self, + bitmask, package_list=None, ebuild_file_list=None, + extended_format=False + ): + if package_list: + self.insert_packages_v ( bitmask, package_list, extended_format ) + + + if ebuild_file_list: + self.insert_ebuild_files_v ( + bitmask, ebuild_file_list, extended_format + ) + + # --- end of feed (...) --- + + def feed_from_file ( + self, bitmask, package_list_file=None, ebuild_list_file=None, **kw + ): + # or ebuild_file_list_file + + self.feed ( + bitmask, + _read_list_file ( package_list_file ), + _read_list_file ( ebuild_list_file ), + **kw + ) + # --- end of feed_from_file (...) --- + +# --- end of BitmaskMapCreator --- - return combined_acceptor - # --- end of package_token_to_acceptor (...) --- -# --- end of AdditionControlPackageRuleGenerator --- @@ -341,29 +498,27 @@ class AdditionControlPackageRuleGenerator ( def temporary_demo_func(): ARES = AdditionControlResult rule_generator = AdditionControlPackageRuleGenerator("sci-R") - CTOKEN = rule_generator.create_category_token - PTOKEN = rule_generator.create_package_token + bmap_creator = BitmaskMapCreator(rule_generator) + P = bmap_creator.insert_package add_control_rule = None bitmask_acceptor_chain_map = None - acceptor_chain_bitmask_map = { - CTOKEN ( "sys-*" ): { - PTOKEN ( "a*-2" ): ARES.PKG_FORCE_DENY, - }, - CTOKEN ( "*" ): { - PTOKEN ( "?*" ): ARES.PKG_REVBUMP_ON_COLLISION, - PTOKEN ( 'p0' ): ARES.PKG_FORCE_DENY|ARES.PKG_FORCE_REPLACE, - }, - CTOKEN ( 'c' ): { - PTOKEN ( "***?****" ): ARES.PKG_FORCE_DENY, - }, - CTOKEN ( 'd' ): { - PTOKEN ( "*" ): ARES.PKG_REPLACE_ONLY, - }, - CTOKEN ( 'f' ): { - PTOKEN ( 'p1' ): ARES.PKG_REPLACE_ONLY, - PTOKEN ( 'p1-5.0' ): ARES.PKG_REPLACE_ONLY, - }, - } + # ref + acceptor_chain_bitmask_map = bmap_creator.data + + bmap_creator.feed_from_file ( + "force-replace", + "/tmp/ML" + ) + + + P ( "force-deny", "sys-*/a*-2", True ) + P ( ARES.PKG_REVBUMP_ON_COLLISION, "*/?*" ) + P ( "force-deny,force-replace", "*/p0" ) + P ( ARES.PKG_FORCE_DENY, "c/***?****" ) + P ( "replace-only", "d/*" ) + P ( "replace-only", "f/p1" ) + P ( ARES.PKG_REPLACE_ONLY, "f/p1-5.0" ) + P ( ARES.PKG_FORCE_REPLACE, "sci-R/*" ) print ( "** initial acceptor_chain -> raw_bitmask map" ) @@ -402,6 +557,13 @@ def temporary_demo_func(): print ( "** content of the rule generator\'s namespace" ) print ( rule_generator.namespace._objects ) + for cls in rule_generator.namespace._objects: + if type(cls) != type(object): + continue + + + if issubclass ( cls, roverlay.packagerules.abstract.acceptors._AcceptorCompound ): + raise Exception(cls) print ( "** content of the rule generator\'s namespace after clear-cache" ) rule_generator.clear_object_cache() @@ -413,7 +575,6 @@ def temporary_demo_func(): if __name__ == '__main__': import sys import os - from roverlay.overlay.abccontrol import AdditionControlResult try: temporary_demo_func() |