X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=roles%2Fchange_kernel_cmdline%2Ffilter_plugins%2Fchange_kernel_cmdline_helpers.py;fp=roles%2Fchange_kernel_cmdline%2Ffilter_plugins%2Fchange_kernel_cmdline_helpers.py;h=0fa9273befaaf6cb8645f2f6fc13981985ca5545;hb=74a49ba6ef2ea715fa492db0bcd85c30398688e8;hp=0000000000000000000000000000000000000000;hpb=a936af362724cca0c5dc2c424902d398f9833410;p=ta%2Finfra-ansible.git diff --git a/roles/change_kernel_cmdline/filter_plugins/change_kernel_cmdline_helpers.py b/roles/change_kernel_cmdline/filter_plugins/change_kernel_cmdline_helpers.py new file mode 100644 index 0000000..0fa9273 --- /dev/null +++ b/roles/change_kernel_cmdline/filter_plugins/change_kernel_cmdline_helpers.py @@ -0,0 +1,64 @@ +#!/usr/bin/python + +# Copyright 2019 Nokia + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import itertools +import re + +def cpulist_to_set(cl): + r = set() + for e in cl.split(','): + if '-' in e: + p = e.split('-') + else: + p = [e, e] + r.update(map(str, range(int(p[0]), int(p[1]) + 1))) + return list(r) + +def set_to_cpulist(cs): + r = [] + for k, g in itertools.groupby(enumerate(sorted(cs, key=int)), lambda (i, v): int(v) - i): + t = list(g) + if len(t) == 1: + r.append(str(t[0][1])) + else: + r.append('-'.join([str(t[0][1]), str(t[-1][1])])) + return ','.join(r) + +def cpulist_combine(conf, name, lst): + r = set() + for s in lst: + if s in conf: + r.update(conf[s]['set']) + if len(r) > 0: + conf[name] = { 'set': sorted(r), 'list': set_to_cpulist(r) } + return conf + +def cmdline_to_list(cmdl): + return re.findall('(?:[^" ]|"[^"]*")+', cmdl) + +def list_to_cmdline(lst): + return ' '.join(lst) + +class FilterModule(object): + def filters(self): + return { + 'cpulist_to_set': cpulist_to_set, + 'set_to_cpulist': set_to_cpulist, + 'cpulist_combine': cpulist_combine, + 'cmdline_to_list': cmdline_to_list, + 'list_to_cmdline': list_to_cmdline + } +