From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=BDupka?= Subject: [virt-test][PATCH 4/7] virt: Adds named variants to Cartesian config. Date: Fri, 29 Mar 2013 18:14:07 +0100 Message-ID: <1364577250-2637-5-git-send-email-jzupka@redhat.com> References: <1364577250-2637-1-git-send-email-jzupka@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE To: virt-test-devel@redhat.com, kvm@vger.kernel.org, kvm-autotest@redhat.com, lmr@redhat.com, ldoktor@redhat.com, jzupka@redhat.com, ehabkost@redhat.com, pbonzini@redhat.com Return-path: Received: from mx1.redhat.com ([209.132.183.28]:35272 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756013Ab3C2ROZ (ORCPT ); Fri, 29 Mar 2013 13:14:25 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r2THEPD0016528 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 29 Mar 2013 13:14:25 -0400 In-Reply-To: <1364577250-2637-1-git-send-email-jzupka@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: ******** example: variants name=3Dtests: - wait: run =3D "wait" variants: - long: time =3D short_time - short: long time =3D logn_time - test2: run =3D "test1" variants name=3Dvirt_system: - linux: - windows_XP: variants name=3Dhost_os: - linux: image =3D linux - windows_XP: image =3D windows tests>wait.short: shutdown =3D destroy only host_os>linux ******** output: dict 1: host_os>linux.virt_system>linux.tests>wait.long dep =3D [] host_os =3D linux image =3D linux name =3D host_os>linux.virt_system>linux.tests>wait.long run =3D wait shortname =3D host_os>linux.virt_system>linux.tests>wait.long tests =3D wait time =3D short_time virt_system =3D linux dict 2: host_os>linux.virt_system>linux.tests>wait.short dep =3D ['host_os>linux.virt_system>linux.tests>wait.long'] host_os =3D linux image =3D linux name =3D host_os>linux.virt_system>linux.tests>wait.short run =3D wait shortname =3D host_os>linux.virt_system>linux.tests>wait.short shutdown =3D destroy tests =3D wait time =3D logn_time virt_system =3D linux dict 3: host_os>linux.virt_system>linux.tests>test2 dep =3D [] host_os =3D linux image =3D linux name =3D host_os>linux.virt_system>linux.tests>test2 run =3D test1 shortname =3D host_os>linux.virt_system>linux.tests>test2 tests =3D test2 virt_system =3D linux dict 4: host_os>linux.virt_system>windows_XP.tests>wait.long dep =3D [] host_os =3D linux image =3D linux name =3D host_os>linux.virt_system>windows_XP.tests>wait.long run =3D wait shortname =3D host_os>linux.virt_system>windows_XP.tests>wait.long tests =3D wait time =3D short_time virt_system =3D windows_XP dict 5: host_os>linux.virt_system>windows_XP.tests>wait.short dep =3D ['host_os>linux.virt_system>windows_XP.tests>wait.long'] host_os =3D linux image =3D linux name =3D host_os>linux.virt_system>windows_XP.tests>wait.short run =3D wait shortname =3D host_os>linux.virt_system>windows_XP.tests>wait.short shutdown =3D destroy tests =3D wait time =3D logn_time virt_system =3D windows_XP dict 6: host_os>linux.virt_system>windows_XP.tests>test2 dep =3D [] host_os =3D linux image =3D linux name =3D host_os>linux.virt_system>windows_XP.tests>test2 run =3D test1 shortname =3D host_os>linux.virt_system>windows_XP.tests>test2 tests =3D test2 virt_system =3D windows_XP =46or filtering of named variants is used character ">" because there w= as problem with conflict with =3D in expression key =3D value. The char ">= " could be changed to something better but it should be different from "=3D= " for optimization of speed. Additionally named variant adds keys to final dictionary in case of example is it (virt_system =3D linux). It should reduce size of config = file. Keys defined in config and keys defined by named variants are in same name space. Signed-off-by: Ji=C5=99=C3=AD =C5=BDupka --- virttest/cartesian_config.py | 138 +++++++++++++++++++++++++++++++++++= +++----- 1 file changed, 124 insertions(+), 14 deletions(-) diff --git a/virttest/cartesian_config.py b/virttest/cartesian_config.p= y index ef91051..04ed2b5 100755 --- a/virttest/cartesian_config.py +++ b/virttest/cartesian_config.py @@ -145,6 +145,74 @@ class MissingIncludeError: num_failed_cases =3D 5 =20 =20 +class Label(object): + __slots__ =3D ["name", "var_name", "long_name", "hash_val", "hash_= var"] + + def __init__(self, name, next_name=3DNone): + if next_name is None: + self.name =3D name + self.var_name =3D None + else: + self.name =3D next_name + self.var_name =3D name + + if self.var_name is None: + self.long_name =3D "%s" % (self.name) + else: + self.long_name =3D "%s>%s" % (self.var_name, self.name) + + self.hash_val =3D self.hash_name() + self.hash_var =3D None + if self.var_name: + self.hash_var =3D self.hash_variant() + + + def __str__(self): + return self.long_name + + + def __repr__(self): + return self.long_name + + + def __eq__(self, o): + """ + The comparison is asymmetric due to optimization. + """ + if o.var_name: + if self.long_name =3D=3D o.long_name: + return True + else: + if self.name =3D=3D o.name: + return True + return False + + + def __ne__(self, o): + """ + The comparison is asymmetric due to optimization. + """ + if o.var_name: + if self.long_name !=3D o.long_name: + return True + else: + if self.name !=3D o.name: + return True + return False + + + def __hash__(self): + return self.hash_val + + + def hash_name(self): + return sum([i + 1 * ord(x) for i, x in enumerate(self.name)]) + + + def hash_variant(self): + return sum([i + 1 * ord(x) for i, x in enumerate(str(self))]) + + class Node(object): __slots__ =3D ["name", "dep", "content", "children", "labels", "append_to_shortname", "failed_cases", "default"] @@ -212,18 +280,19 @@ class Filter(object): def __init__(self, s): self.filter =3D [] for char in s: - if not (char.isalnum() or char.isspace() or char in ".,_-"= ): + if not (char.isalnum() or char.isspace() or char in ".,_->= "): raise ParserError("Illegal characters in filter") for word in s.replace(",", " ").split(): #= OR word =3D [block.split(".") for block in word.split("..")] = # AND - for word in s.replace(",", " ").split(): - word =3D [block.split(".") for block in word.split("..")] - for block in word: + words =3D [] for block in word: #= . + b =3D [] for elem in block: if not elem: raise ParserError("Syntax error") - self.filter +=3D [word] + b.append(Label(*elem.split(">"))) + words.append(b) + self.filter +=3D [words] =20 =20 def match(self, ctx, ctx_set): @@ -506,15 +575,16 @@ class Parser(object): # node.dump(0) # Update dep for d in node.dep: - dep =3D dep + [".".join(ctx + [d])] + dep =3D dep + [".".join([str(label) for label in ctx + [d]= ])] # Update ctx ctx =3D ctx + node.name ctx_set =3D set(ctx) labels =3D node.labels # Get the current name - name =3D ".".join(ctx) + name =3D ".".join([str(label) for label in ctx]) if node.name: self._debug("checking out %r", name) + # Check previously failed filters for i, failed_case in enumerate(node.failed_cases): if not might_pass(*failed_case): @@ -534,9 +604,11 @@ class Parser(object): add_failed_case() self._debug("Failed_cases %s", node.failed_cases) return + # Update shortname if node.append_to_shortname: shortname =3D shortname + node.name + # Recurse into children count =3D 0 for n in node.children: @@ -546,7 +618,8 @@ class Parser(object): # Reached leaf? if not node.children: self._debug(" reached leaf, returning it") - d =3D {"name": name, "dep": dep, "shortname": ".".join(sho= rtname)} + d =3D {"name": name, "dep": dep, + "shortname": ".".join([str(sn) for sn in shortname])} for _, _, op in new_content: op.apply_to_dict(d) yield d @@ -583,7 +656,7 @@ class Parser(object): print s % args =20 =20 - def _parse_variants(self, cr, node, prev_indent=3D-1): + def _parse_variants(self, cr, node, prev_indent=3D-1, var_name=3DN= one): """ Read and parse lines from a FileReader object until a line wit= h an indent level lower than or equal to prev_indent is encountered= =2E @@ -591,6 +664,7 @@ class Parser(object): @param cr: A FileReader/StrReader object. @param node: A node to operate on. @param prev_indent: The indent level of the "parent" block. + @param var_name: Variants name @return: A node object. """ node4 =3D Node() @@ -620,9 +694,15 @@ class Parser(object): node2.labels =3D node.labels =20 node3 =3D self._parse(cr, node2, prev_indent=3Dindent) - node3.name =3D name.lstrip("@").split(".") - node3.dep =3D dep.replace(",", " ").split() - node3.append_to_shortname =3D not name.startswith("@") + node3.name =3D [Label(var_name, n) for n in name.lstrip("@= ").split(".")] + node3.dep =3D [Label(var_name, d) for d in dep.replace(","= , " ").split()] + + if var_name: + l =3D "%s =3D %s" % (var_name, name) + op_match =3D _ops_exp.search(l) + node3.content +=3D [(cr.filename, linenum, Op(l, op_ma= tch))] + + is_default =3D name.startswith("@") =20 node4.children +=3D [node3] node4.labels.update(node3.labels) @@ -649,14 +729,44 @@ class Parser(object): words =3D line.split(None, 1) =20 # Parse 'variants' - if line =3D=3D "variants:": + if line.startswith("variants"): # 'variants' is not allowed inside a conditional block if (isinstance(node, Condition) or isinstance(node, NegativeCondition)): raise ParserError("'variants' is not allowed insid= e a " "conditional block", None, cr.filename, linenum) - node =3D self._parse_variants(cr, node, prev_indent=3D= indent) + name =3D None + if not words[0] in ["variants", "variants:"]: + raise ParserError("Illegal characters in variants"= , + line, cr.filename, linenum) + if words[0] =3D=3D "variants": + try: + name, _ =3D words[1].split(":") # split name = and comment + except ValueError: + raise ParserError("Missing : in variants expre= ssion", + line, cr.filename, linen= um) + for char in name: + if not (char.isalnum() or char.isspace() or + char in "._-=3D,"): + raise ParserError("Illegal characters in v= ariants", + line, cr.filename, linen= um) + var_name =3D None + if name: + block =3D name.split(",") + for b in block: + oper =3D b.split("=3D") + if oper[0] =3D=3D "name": + if len(oper) =3D=3D 1: + raise ParserError("Missing name of var= iants", + line, cr.filename, l= inenum) + var_name =3D oper[1].strip() + else: + raise ParserError("Ilegal variants param", + line, cr.filename, line= num) + node =3D self._parse_variants(cr, node, prev_indent=3D= indent, + var_name=3Dvar_name) + var_name=3Dname) continue =20 # Parse 'include' statements --=20 1.8.1.4