X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2Fautogen%2Fadd_suite_tag.py;fp=resources%2Flibraries%2Fpython%2Fautogen%2Fadd_suite_tag.py;h=3e07316b64d4739f1051198cb6bfca529c718191;hb=e916ab4db7dec2cc0bb21dcc31460f819d68b0d5;hp=0000000000000000000000000000000000000000;hpb=b55e324d526e5b05baef015c3614b9743c955992;p=csit.git diff --git a/resources/libraries/python/autogen/add_suite_tag.py b/resources/libraries/python/autogen/add_suite_tag.py new file mode 100755 index 0000000000..3e07316b64 --- /dev/null +++ b/resources/libraries/python/autogen/add_suite_tag.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2019 Cisco and/or its affiliates. +# 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. + +"""Script for mass editing suites to add suite tag there.""" + +import sys + +from io import open +from glob import glob + +from resources.libraries.python.autogen.Regenerator import Regenerator + + +def edit(text, suite_tag): + """Return the edited text. + + :param text: Content of .robot file as read. + :param suite_tag: The value of suite tag to insert if not present. + :type text: str + :type suite_tag: str + :returns: New content to rewrite the file with. + :rtype: str + :raises RuntimeError: If something failed during the editing. + """ + lines_out = list() + # Using an iterator to allow several loops in sequence. + lines_in = iter(text.splitlines()) + # Searching where tags begin. + while 1: + line = next(lines_in) + if u"Force Tags" in line: + break + lines_out.append(line) + # The foce tags line has not been written yet. + # Search for "empty" line after tags. + while 1: + line_previous = line + lines_out.append(line) + line = next(lines_in) + if u"|" == line: + break + # All tags are written, we remember the last one. + line_suite = u"| ... | " + suite_tag + if line_suite != line_previous: + lines_out.append(line_suite) + # Write the empty line and copy the rest. + lines_out.append(line) + for line in lines_in: + lines_out.append(line) + # Make sure the last line ends properly. + lines_out.append(u"") + while lines_out[-2] == u"": + lines_out.pop() + return u"\n".join(lines_out) + + +def main(): + """Do it all, return return code. + + :returns: 0 as everything works. + :rtype: int + """ + for filename in glob(u"*.robot"): + if u"__init__" in filename: + continue + with open(filename, u"rt") as file_in: + text_in = file_in.read() + dash_split = filename.split(u"-", 1) + if len(dash_split[0]) <= 4: + # It was something like "2n1l", we need one more split. + dash_split = dash_split[1].split(u"-", 1) + suite_id = dash_split[1].split(u".", 1)[0] + suite_tag = suite_id.rsplit(u"-", 1)[0] + text_out = edit(text_in, suite_tag) + with open(filename, u"wt") as file_out: + file_out.write(text_out) + return 0 + + +if __name__ == u"__main__": + sys.exit(main())