3 # Copyright (c) 2019 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."""
23 from resources.libraries.python.autogen.Regenerator import Regenerator
26 def edit(text, suite_tag):
27 """Return the edited text.
29 :param text: Content of .robot file as read.
30 :param suite_tag: The value of suite tag to insert if not present.
33 :returns: New content to rewrite the file with.
35 :raises RuntimeError: If something failed during the editing.
38 # Using an iterator to allow several loops in sequence.
39 lines_in = iter(text.splitlines())
40 # Searching where tags begin.
43 if u"Force Tags" in line:
45 lines_out.append(line)
46 # The foce tags line has not been written yet.
47 # Search for "empty" line after tags.
50 lines_out.append(line)
54 # All tags are written, we remember the last one.
55 line_suite = u"| ... | " + suite_tag
56 if line_suite != line_previous:
57 lines_out.append(line_suite)
58 # Write the empty line and copy the rest.
59 lines_out.append(line)
61 lines_out.append(line)
62 # Make sure the last line ends properly.
64 while lines_out[-2] == u"":
66 return u"\n".join(lines_out)
70 """Do it all, return return code.
72 :returns: 0 as everything works.
75 for filename in glob(u"*.robot"):
76 if u"__init__" in filename:
78 with open(filename, u"rt") as file_in:
79 text_in = file_in.read()
80 dash_split = filename.split(u"-", 1)
81 if len(dash_split[0]) <= 4:
82 # It was something like "2n1l", we need one more split.
83 dash_split = dash_split[1].split(u"-", 1)
84 suite_id = dash_split[1].split(u".", 1)[0]
85 suite_tag = suite_id.rsplit(u"-", 1)[0]
86 text_out = edit(text_in, suite_tag)
87 with open(filename, u"wt") as file_out:
88 file_out.write(text_out)
92 if __name__ == u"__main__":