Support suite tags in autogen
[csit.git] / resources / libraries / python / autogen / add_suite_tag.py
1 #!/usr/bin/env python3
2
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 """Script for mass editing suites to add suite tag there."""
17
18 import sys
19
20 from io import open
21 from glob import glob
22
23 from resources.libraries.python.autogen.Regenerator import Regenerator
24
25
26 def edit(text, suite_tag):
27     """Return the edited text.
28
29     :param text: Content of .robot file as read.
30     :param suite_tag: The value of suite tag to insert if not present.
31     :type text: str
32     :type suite_tag: str
33     :returns: New content to rewrite the file with.
34     :rtype: str
35     :raises RuntimeError: If something failed during the editing.
36     """
37     lines_out = list()
38     # Using an iterator to allow several loops in sequence.
39     lines_in = iter(text.splitlines())
40     # Searching where tags begin.
41     while 1:
42         line = next(lines_in)
43         if u"Force Tags" in line:
44             break
45         lines_out.append(line)
46     # The foce tags line has not been written yet.
47     # Search for "empty" line after tags.
48     while 1:
49         line_previous = line
50         lines_out.append(line)
51         line = next(lines_in)
52         if u"|" == line:
53             break
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)
60     for line in lines_in:
61         lines_out.append(line)
62     # Make sure the last line ends properly.
63     lines_out.append(u"")
64     while lines_out[-2] == u"":
65         lines_out.pop()
66     return u"\n".join(lines_out)
67
68
69 def main():
70     """Do it all, return return code.
71
72     :returns: 0 as everything works.
73     :rtype: int
74     """
75     for filename in glob(u"*.robot"):
76         if u"__init__" in filename:
77             continue
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)
89     return 0
90
91
92 if __name__ == u"__main__":
93     sys.exit(main())