Pin pip to 20.3.3 and disable tmpfs in DIB
[ta/build-tools.git] / tools / rpm.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import re
16
17
18 class RpmData(dict):
19
20     @property
21     def name(self):
22         return self['Name']
23
24     @property
25     def epoch(self):
26         return self.get('Epoch', '0')
27
28     @property
29     def version(self):
30         return self['Version']
31
32     @property
33     def release(self):
34         return self['Release']
35
36     @property
37     def arch(self):
38         return self['Architecture']
39
40     @property
41     def vendor(self):
42         return self.get('Vendor', '')
43
44     @property
45     def license(self):
46         return self.get('License', '')
47
48     def __str__(self):
49         return '{}-{}-{}.{}'.format(self.name,
50                                     self.version,
51                                     self.release,
52                                     self.arch)
53
54     def is_same_package_as(self, other):
55         for attr in ['name', 'epoch', 'version', 'release', 'arch']:
56             if getattr(self, attr) != getattr(other, attr):
57                 return False
58         return True
59
60
61 class RpmInfoParser(object):
62     """
63     Parse 'rpm -qi' output
64     """
65
66     def parse_file(self, rpm_info_installed_file_path):
67         with open(rpm_info_installed_file_path, 'r') as f:
68             return self.parse_multiple(f.read())
69
70     def parse_multiple(self, rpm_info_output_multiple):
71         packages = []
72         package_index = -1
73         for line in rpm_info_output_multiple.splitlines():
74             if re.match(r'^Name\s+:.*', line):
75                 packages.append(line)
76                 package_index += 1
77             else:
78                 packages[package_index] += '\n' + line
79         return [self.parse_package(pkg) for pkg in packages]
80
81     @staticmethod
82     def parse_package(rpm_info_output):
83         result = RpmData()
84         current_key = None
85         colon_location = rpm_info_output.splitlines()[0].find(':')
86         matcher = re.compile(r'^([A-Z][A-Za-z0-9 ]{{{}}}):( ?| .+)$'.format(colon_location - 1))
87         for line in rpm_info_output.splitlines():
88             match = matcher.match(line)
89             if match:
90                 parsed_key = match.group(1).rstrip()
91                 parsed_value = match.group(2).strip()
92                 result[parsed_key] = parsed_value
93                 current_key = parsed_key
94             else:
95                 if not result[current_key]:
96                     result[current_key] = line
97                 else:
98                     result[current_key] = result[current_key] + '\n' + line
99         return result