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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from tools.convert import to_json, CsvConverter
20 from tools.log import set_logging
21 from tools.io import write_to, read_json
24 class RpmDataProcesser(object):
25 def __init__(self, rpmdata):
26 self.components = self._get_components(rpmdata)
29 def _get_components(rpmdata):
31 bom_field_map = {'name': 'Name',
34 'source-url': 'Source URL',
35 'crypto-capable': 'Crypto capable'}
37 components.append(unicode_recursively(rpm))
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))
47 def gen_components(self, path):
48 write_to(path, to_json(self.components))
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']))
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')
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)
84 args = parse(input_args)
86 set_logging(debug=True)
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)
96 if __name__ == "__main__":