Automate generation of docker builder images.
[ci-management.git] / docker / scripts / dbld_csit_find_ansible_packages.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 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         cays = CsitAnsibleYamlStruct(**csit_ansible_yaml)
43         packages = [pkg for pkg in cays.packages_base if type(pkg) is str]
44         if arch in [*cays.packages_by_arch]:
45             packages += [pkg for pkg in cays.packages_by_arch[arch]
46                          if type(pkg) is str]
47         if distro in [*cays.packages_by_distro]:
48             packages += [pkg for pkg in cays.packages_by_distro[distro]
49                          if type(pkg) is str]
50         return packages
51
52 def is_csit_ansible_yaml_file(filename: str):
53      (root,ext) = os.path.splitext(filename)
54      if ext == '.yaml' \
55         and filename.find('csit/') >= 0 \
56         and filename.find('/ansible/') > 0 \
57         and os.path.isfile(filename):
58          return True
59      else:
60          return False
61
62 def main(args: List[str]) -> None:
63     if len(args) < 1:
64         log.warning('Must have at least 1 file name')
65         return
66     pkg_list = []
67     distro = 'ubuntu'
68     arch = 'x86_64'
69
70     for arg in args:
71         if arg.lower() == '--ubuntu':
72             distro = 'ubuntu'
73         elif arg.lower() == '--centos':
74             distro = 'centos'
75         elif arg.lower() == '--x86_64':
76             arch = 'x86_64'
77         elif arg.lower() == '--aarch64':
78             arch = 'aarch64'
79         elif is_csit_ansible_yaml_file(arg):
80            pkg_list += packages_in_csit_ansible_yaml_file(arg, distro, arch)
81         else:
82             log.warning(f'Invalid CSIT Ansible YAML file: {arg}')
83     pkg_list = list(set(pkg_list))
84     pkg_list.sort()
85     print(" ".join(pkg_list))
86
87 if __name__ == "__main__":
88     main(sys.argv[1:])