Add initial code
[ta/build-tools.git] / tools / script / process_rpmdata.py
1 #!/usr/bin/env 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 argparse
17 import sys
18
19 from tools.convert import to_json, CsvConverter
20 from tools.log import set_logging
21 from tools.io import write_to, read_json
22
23
24 class RpmDataProcesser(object):
25     def __init__(self, rpmdata):
26         self.components = self._get_components(rpmdata)
27
28     @staticmethod
29     def _get_components(rpmdata):
30         components = []
31         bom_field_map = {'name': 'Name',
32                          'version': 'Version',
33                          'foss': 'FOSS',
34                          'source-url': 'Source URL',
35                          'crypto-capable': 'Crypto capable'}
36         for rpm in rpmdata:
37             components.append(unicode_recursively(rpm))
38             bom = rpm['BOM']
39             if bom:
40                 for material in bom:
41                     component = {'Source RPM': rpm['Source RPM']}
42                     for field in material:
43                         component[bom_field_map[field]] = material[field]
44                     components.append(unicode_recursively(component))
45         return components
46
47     def gen_components(self, path):
48         write_to(path, to_json(self.components))
49
50     def gen_components_csv(self, path):
51         csv = CsvConverter(self.components,
52                            preferred_field_order=['Name', 'Version', 'Release', 'Source RPM'])
53         write_to(path, csv.convert_to_ms_excel(text_fields=['Version']))
54
55
56 def unicode_recursively(something):
57     if isinstance(something, dict):
58         return {unicode_recursively(key): unicode_recursively(value) for key, value in
59                 something.iteritems()}
60     elif isinstance(something, list):
61         return [unicode_recursively(element) for element in something]
62     elif isinstance(something, unicode):
63         return something.encode('utf-8')
64     return something
65
66
67 def parse(args):
68     p = argparse.ArgumentParser(
69         description='Process rpmdata for multitude of tools',
70         formatter_class=argparse.ArgumentDefaultsHelpFormatter)
71     p.add_argument('--verbose', '-v', action='store_true',
72                    help='More verbose logging')
73     p.add_argument('--rpmdata-path', required=True,
74                    help='RPM data json file path')
75     p.add_argument('--output-components',
76                    help='Component list that includes also RPM sub-components')
77     p.add_argument('--output-components-csv',
78                    help='Component list that includes also RPM sub-components as CSV')
79     args = p.parse_args(args)
80     return args
81
82
83 def main(input_args):
84     args = parse(input_args)
85     if args.verbose:
86         set_logging(debug=True)
87     else:
88         set_logging(debug=False)
89     p = RpmDataProcesser(read_json(args.rpmdata_path))
90     if args.output_components:
91         p.gen_components(args.output_components)
92     if args.output_components_csv:
93         p.gen_components_csv(args.output_components_csv)
94
95
96 if __name__ == "__main__":
97     main(sys.argv[1:])