Add stable/2402 branch and remove stable/2306 branch to docker executor image scripts
[ci-management.git] / docker / scripts / dbld_csit_find_ansible_packages.py
1 #! /usr/bin/env python3
2
3 # Copyright (c) 2021 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 import os
17 import pprint
18 import sys
19 from typing import List
20 import yaml
21 import logging
22
23 logging.basicConfig(format='%(message)s')
24 log = logging.getLogger(__name__)
25
26 def print_yaml_struct(yaml_struct, depth=0):
27     indent = " " * depth
28     for k,v in sorted(yaml_struct.items(), key=lambda x: x[0]):
29         if isinstance(v, dict):
30             log.warning(f"{indent}{k}")
31             print_yaml_struct(v, depth+1)
32         else:
33             log.warning(f"{indent}{k} {v}")
34
35 class CsitAnsibleYamlStruct:
36     def __init__(self, **entries):
37         self.__dict__.update(entries)
38
39 def packages_in_csit_ansible_yaml_file(yamlfile: str, distro, arch) -> list:
40     with open(yamlfile) as yf:
41         csit_ansible_yaml = yaml.safe_load(yf)
42         if csit_ansible_yaml is None:
43             return ""
44         cays = CsitAnsibleYamlStruct(**csit_ansible_yaml)
45         try:
46             packages = [pkg for pkg in cays.packages_base if type(pkg) is str]
47         except AttributeError:
48             return ""
49         if arch in [*cays.packages_by_arch]:
50             packages += [pkg for pkg in cays.packages_by_arch[arch]
51                          if type(pkg) is str]
52         if distro in [*cays.packages_by_distro]:
53             packages += [pkg for pkg in cays.packages_by_distro[distro]
54                          if type(pkg) is str]
55         return packages
56
57 def is_csit_ansible_yaml_file(filename: str):
58      (root,ext) = os.path.splitext(filename)
59      if ext == '.yaml' \
60         and filename.find('csit/') >= 0 \
61         and filename.find('ansible/') > 0 \
62         and os.path.isfile(filename):
63          return True
64      else:
65          return False
66
67 def main(args: List[str]) -> None:
68     if len(args) < 1:
69         log.warning('Must have at least 1 file name')
70         return
71     pkg_list = []
72     distro = 'ubuntu'
73     arch = 'x86_64'
74
75     for arg in args:
76         if arg.lower() == '--ubuntu':
77             distro = 'ubuntu'
78         elif arg.lower() == '--x86_64':
79             arch = 'x86_64'
80         elif arg.lower() == '--aarch64':
81             arch = 'aarch64'
82         elif is_csit_ansible_yaml_file(arg):
83            pkg_list += packages_in_csit_ansible_yaml_file(arg, distro, arch)
84         else:
85             log.warning(f'Invalid CSIT Ansible YAML file: {arg}')
86     pkg_list = list(set(pkg_list))
87     pkg_list.sort()
88     print(" ".join(pkg_list))
89
90 if __name__ == "__main__":
91     main(sys.argv[1:])