3 # Copyright (c) 2020 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """Script for mass editing suites to add suite tag there."""
24 def edit(text, suite_tag):
25 """Return the edited text.
27 :param text: Content of .robot file as read.
28 :param suite_tag: The value of suite tag to insert if not present.
31 :returns: New content to rewrite the file with.
33 :raises RuntimeError: If something failed during the editing.
36 # Using an iterator to allow several loops in sequence.
37 lines_in = iter(text.splitlines())
38 # Searching where tags begin.
41 if u"Force Tags" in line:
43 lines_out.append(line)
44 # The foce tags line has not been written yet.
45 # Search for "empty" line after tags.
48 lines_out.append(line)
52 # All tags are written, we remember the last one.
53 line_suite = u"| ... | " + suite_tag
54 if line_suite != line_previous:
55 lines_out.append(line_suite)
56 # Write the empty line and copy the rest.
57 lines_out.append(line)
59 lines_out.append(line)
60 # Make sure the last line ends properly.
62 while lines_out[-2] == u"":
64 return u"\n".join(lines_out)
68 """Do it all, return return code.
70 :returns: 0 as everything works.
73 for filename in glob(u"*.robot"):
74 if u"__init__" in filename:
76 with open(filename, u"rt") as file_in:
77 text_in = file_in.read()
78 dash_split = filename.split(u"-", 1)
79 if len(dash_split[0]) <= 4:
80 # It was something like "2n1l", we need one more split.
81 dash_split = dash_split[1].split(u"-", 1)
82 suite_id = dash_split[1].split(u".", 1)[0]
83 suite_tag = suite_id.rsplit(u"-", 1)[0]
84 text_out = edit(text_in, suite_tag)
85 with open(filename, u"wt") as file_out:
86 file_out.write(text_out)
90 if __name__ == u"__main__":