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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
26 return self.get('Epoch', '0')
30 return self['Version']
34 return self['Release']
38 return self['Architecture']
42 return self.get('Vendor', '')
46 return self.get('License', '')
49 return '{}-{}-{}.{}'.format(self.name,
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):
61 class RpmInfoParser(object):
63 Parse 'rpm -qi' output
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())
70 def parse_multiple(self, rpm_info_output_multiple):
73 for line in rpm_info_output_multiple.splitlines():
74 if re.match(r'^Name\s+:.*', line):
78 packages[package_index] += '\n' + line
79 return [self.parse_package(pkg) for pkg in packages]
82 def parse_package(rpm_info_output):
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)
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
95 if not result[current_key]:
96 result[current_key] = line
98 result[current_key] = result[current_key] + '\n' + line