Add initial code
[ta/build-tools.git] / dib_elements / myproduct / extra-data.d / collect_ecc.py
1 #!/usr/bin/python
2 # Copyright 2019 Nokia
3 #
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 subprocess
17 import json
18 import sys
19
20 # following libs are found from openssl, libgcrypto, openssh with rpm -qR
21 # in the future, the libs need to be updated with exact version number
22
23 input_capabilities = []
24 output_rpms = []
25 input_packages = [
26                   'openssl',
27                   'openssh',
28                   'p11-kit',
29                   'openssl-libs',
30                   'libgcrypt',
31                   'libgcrypt.so',
32                   'python2-cryptography',
33                   'sshpass',
34                   'm2crypto',
35                   'erlang-crypto',
36                   'openssh-clients',
37                   'python2-passlib',
38                   'python-paramiko',
39                   'python-keyring',
40                   'python2-asn1crypto',
41                   'python2-cryptography',
42                   'python2-pyasn1',
43                   'xstatic-jsencrypt-common',
44                   'krb5-libs'
45                  ]
46 crypto_capabilities = [
47                        'rtld(',
48                        'libcrypt.so'
49                       ]
50
51
52 def output_rpm_command(lib, command):
53     command.append(lib)
54     try:
55         output = subprocess.check_output(command)
56     except:
57         output = ""
58     return output
59
60
61 def build_capability_list_dynamically():
62     map_dict = {}
63     command = ['rpm',
64                '-qa',
65                '--queryformat',
66                '[%{=NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{PROVIDES}\n]']
67     output = subprocess.check_output(command)
68     for item in output.splitlines():
69         items = item.split(' ')
70         if items:
71             if items[0] in map_dict:
72                 capa_list = map_dict[items[0]]
73                 capa_list.append(items[1])
74                 map_dict[items[0]] = capa_list
75             else:
76                 map_dict[items[0]] = [items[1]]
77     for rpms, caps in map_dict.items():
78         for cap in caps:
79             for item in crypto_capabilities:
80                 if cap.startswith(item):
81                     if cap not in input_capabilities:
82                         input_capabilities.append(cap)
83                         command = ['rpm', '-q', '--whatprovides']
84                         output = output_rpm_command(cap, command)
85                         name = output.strip('\n')
86                         result = filter(lambda rpm: rpm['name'] == name, output_rpms)
87                         if not result:
88                             output_rpms.append({'name': name, 'provides': [cap]})
89                         else:
90                             sources_list = result[0]['provides']
91                             if cap not in sources_list:
92                                 sources_list.append(cap)
93
94
95 def capability_dependencies_with_whatrequires():
96     for capability in input_capabilities:
97         command = ['rpm', '-q', '--whatrequires']
98         output = output_rpm_command(capability, command)
99         if output:
100             for item in output.splitlines():
101                 result = filter(lambda rpm: rpm['name'] == item, output_rpms)
102                 if not result:
103                     output_rpms.append({'name': item, 'requires': [capability]})
104                 else:
105                     sources_list = result[0]['requires']
106                     if capability not in sources_list:
107                         sources_list.append(capability)
108
109
110 def package_dependencies_with_provides():
111     for package in input_packages:
112         command = ['rpm', '-q']
113         name = output_rpm_command(package, command)
114         name = name.strip('\n')
115         output_rpms.append({'name': name, 'requires': []})
116         command = ['rpm', '-ql', '--provides']
117         output = output_rpm_command(package, command)
118         for item in output.splitlines():
119             input_capabilities.append(item)
120
121
122 if __name__ == '__main__':
123     build_capability_list_dynamically()
124     package_dependencies_with_provides()
125     capability_dependencies_with_whatrequires()
126     rpms_json = json.dumps(output_rpms, sort_keys=True, indent=4)
127     with open(sys.argv[1], "w") as f:
128         f.write(rpms_json)