/usr/share/pyshared/vamos/selection.py is in undertaker 1.3b-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #
# vamos - common auxiliary functionality
#
# Copyright (C) 2011 Christian Dietrich <christian.dietrich@informatik.uni-erlangen.de>
# Copyright (C) 2011 Reinhard Tartler <tartler@informatik.uni-erlangen.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import copy
class Selection:
"""A selection is a selection of symbols (derived from kconfig
features). """
def __init__(self, other_selection = None):
"""A selection can be empty, or can be copied from another
selection. So this is somewhat like a copy constructor in c++.
other_selection == None: empty selection
other_selection == [(),()]: selection from tuple
other_selection == Selection(..): copy selection from other selection"""
# expr == (SYMBOL || SYMBOL) && (SYMBOL || SYMBOL)
# inner sets: alternatives
# outer list: conjugations
self.expr = [set([])]
# All mentioned symbols in this selection
self.symbols = set([])
if other_selection:
# Copy the other expression
if type(other_selection) == list:
self.expr = copy.deepcopy(other_selection)
else:
self.expr = copy.deepcopy(other_selection.expr)
for or_expr in self.expr:
for symbol in or_expr:
self.symbols.add(symbol)
def push_down(self):
"""Add a new level of or_expressions to the conjugation"""
self.expr.append(set([]))
def add_alternative(self, alternative):
"""Add an alternative to the current "top of stack" or expression"""
self.expr[-1].add(alternative)
self.symbols.add(alternative)
def features(self):
"""From a list of symbols determine the features and their selection:
CONFIG_BAR -> CONFIG_BAR=y
CONFIG_FOO_MODULE -> CONFIG_FOO=m"""
features = {}
for or_expr in self.expr:
if len(or_expr) == 0:
continue
symbol = list(or_expr)[0]
value = "y"
if symbol.endswith("_MODULE"):
symbol = symbol[:-len("_MODULE")]
value = "m"
features[symbol] = value
return features
def feature_in_selection(self, feature):
""" Tests if feature ({,_MODULE}) is already in this selection"""
if feature.endswith("_MODULE"):
feature = feature[:-len("_MODULE")]
return feature in self.symbols or \
feature + "_MODULE" in self.symbols
def __str__(self):
"""Format the selection to a (normalized) propositional formula
(return-type: string)"""
or_exprs = []
for or_expr in self.expr:
if len(or_expr) == 0:
continue
elif len(or_expr) == 1:
or_exprs.append(list(or_expr)[0])
else:
or_exprs.append("(" + " || ".join(sorted(list(or_expr))) + ")")
if len(or_exprs) > 0:
return " && ".join(sorted(or_exprs))
else:
return ""
__repr__ = __str__
def better_than(self, other_selection):
"""One Selection is better than another if it is a subset of
the other (and if it is shorter)"""
if len(self.expr) < len(other_selection.expr):
# When our expression is shorter we can test if our
# expression is a subset of the other expression
# This means all our expressions are within the other
# expression
for or_expr in self.expr:
within = False
for or_expr_1 in other_selection.expr:
if or_expr_1 == or_expr:
within = True
if within == False:
return False
return True
else:
return False
def merge(self, other):
"""Expressions can be merged into one selection if they differ
only in or_expression"""
if len(self.expr) != len(other.expr):
return None
new_expr = []
differences = 0
different_or_expr = set()
for or_expr in self.expr:
within = False
for or_expr_1 in other.expr:
if or_expr_1 == or_expr:
new_expr.append(or_expr)
within = True
if not within:
differences += 1
different_or_expr = different_or_expr.union(or_expr)
if differences > 1:
return None
for or_expr in other.expr:
within = False
for or_expr_1 in self.expr:
if or_expr_1 == or_expr:
within = True
if not within:
different_or_expr = different_or_expr.union(or_expr)
new_expr.append(different_or_expr)
return Selection(new_expr)
|