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