amprolla is an apt repository merger originally intended for use with the Devuan infrastructure. This version is the third iteration of the software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.4 KiB

6 years ago
# See LICENSE file for copyright and license details.
6 years ago
"""
Parsing functions/helpers
"""
6 years ago
import time
6 years ago
def get_time(date):
"""
Gets epoch time
"""
6 years ago
return time.mktime(time.strptime(date, "%a, %d %b %Y %H:%M:%S %Z"))
6 years ago
def get_date(relfile):
"""
Gets the date from the contents of a Release file
"""
6 years ago
date = None
contents = relfile.split('\n')
for line in contents:
if line.startswith('Date: '):
date = line.split(': ')[1]
break
return date
6 years ago
6 years ago
def parse_release(reltext):
6 years ago
"""
Parses a Release file and returns a dict of the files we need
key = filename, value = sha256 checksum
"""
hashes = {}
contents = reltext.split('\n')
sha256 = False
for line in contents:
if sha256 is True and line != '':
filename = line.split()[2]
checksum = line.split()[0]
hashes[filename] = checksum
elif line.startswith('SHA256:'):
sha256 = True
return hashes
def parse_release_head(reltext):
6 years ago
"""
Parses the header of the release file to grab needed metadata
"""
metadata = {}
contents = reltext.split('\n')
md5sum = False
for line in contents:
if md5sum is True:
break
elif line.startswith('MD5Sum:'):
md5sum = True
else:
k = line.split(': ')[0]
v = line.split(': ')[1]
metadata[k] = v
return metadata
6 years ago
def parse_package(entry):
6 years ago
"""
Parses a single Packages entry
"""
pkgs = {}
contents = entry.split('\n')
key = ''
value = ''
for line in contents:
if line.startswith(' '):
value += '\n' + line
else:
pkgs[key] = value
v = line.split(':', 1)
key = v[0]
value = v[1][1:]
if key:
pkgs[key] = value
return pkgs
6 years ago
def parse_packages(pkgtext):
"""
Parses our package file contents into a hashmap
key: package name, value: entire package paragraph as a hashmap
"""
_map = {}
6 years ago
pkgs = pkgtext.split('\n\n')
6 years ago
for pkg in pkgs:
single = pkg.split('\n')
for line in single:
if line.startswith('Package: '):
key = line.split(': ')[1]
_map[key] = parse_package(pkg)
break
return _map
6 years ago
def parse_dependencies(dependencies):
"""
Parses a dependency line from a debian Packages file.
Example line::
'lib6 (>= 2.4), libdbus-1-3 (>= 1.0.2), foo'
Output::
{'lib6': '(>= 2.4)', 'libdbus-1-3': '(>= 1.0.2)', 'foo': None}
"""
r = {}
for pkg_plus_version in dependencies.split(', '):
v = pkg_plus_version.split(' ', 1)
name = v[0]
# If we get passed an empty string, the name is '', and we just
# outright stop
if not name:
return {}
if len(v) == 2:
version = v[1]
r[name] = version
else:
r[name] = None
return r
6 years ago
6 years ago
def compare_dict(d1, d2):
"""
Compares two dicts
Takes two dicts and returns a dict of tuples with the differences.
Example input:
d1={'foo': 'bar'}, 22={'foo': 'baz'}
Example output:
{'foo': ('bar', 'baz')}
"""
6 years ago
d1_keys = set(d1.keys())
d2_keys = set(d2.keys())
intersect_keys = d1_keys.intersection(d2_keys)
6 years ago
modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]}
6 years ago
return modified