Compare commits
1 Commits
master
..
5a528d8de8
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a528d8de8 |
@@ -25,10 +25,6 @@ class HistoryFileNotFoundError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DistroUnsupported(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def detect_distro():
|
def detect_distro():
|
||||||
"""Get the Linux distribution"""
|
"""Get the Linux distribution"""
|
||||||
distro = None
|
distro = None
|
||||||
@@ -51,8 +47,8 @@ def detect_distro():
|
|||||||
return distro
|
return distro
|
||||||
|
|
||||||
|
|
||||||
def compare_debian_version(a, b):
|
def compare_version(a, b):
|
||||||
"""Compare Debian package versions"""
|
"""Compare package versions"""
|
||||||
re_digits_non_digits = re.compile(r'\d+|\D+')
|
re_digits_non_digits = re.compile(r'\d+|\D+')
|
||||||
re_digits = re.compile(r'\d+')
|
re_digits = re.compile(r'\d+')
|
||||||
re_digit = re.compile(r'\d')
|
re_digit = re.compile(r'\d')
|
||||||
@@ -146,155 +142,6 @@ def compare_debian_version(a, b):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def compare_redhat_version(a, b):
|
|
||||||
"""Compare RedHat package versions"""
|
|
||||||
def compare_parts(part1, part2):
|
|
||||||
if part1 == part2:
|
|
||||||
return 0
|
|
||||||
p1 = list(part1)
|
|
||||||
p2 = list(part2)
|
|
||||||
|
|
||||||
while p1 or p2:
|
|
||||||
for c in list(p1):
|
|
||||||
if not c.isalnum() and not c == '~':
|
|
||||||
p1.pop(0)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
for c in list(p2):
|
|
||||||
if not c.isalnum() and not c == '~':
|
|
||||||
p2.pop(0)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
try:
|
|
||||||
c1 = p1[0]
|
|
||||||
except IndexError:
|
|
||||||
c1 = ''
|
|
||||||
|
|
||||||
try:
|
|
||||||
c2 = p2[0]
|
|
||||||
except IndexError:
|
|
||||||
c2 = ''
|
|
||||||
|
|
||||||
if c1 == '~' and c2 == '~':
|
|
||||||
p1.pop(0)
|
|
||||||
p2.pop(0)
|
|
||||||
elif c1 == '~':
|
|
||||||
return 1
|
|
||||||
elif c2 == '~':
|
|
||||||
return -1
|
|
||||||
|
|
||||||
if not c1 and not c2:
|
|
||||||
break
|
|
||||||
|
|
||||||
lhs_is_digit = c1.isdigit()
|
|
||||||
|
|
||||||
if c1.isdigit():
|
|
||||||
r1 = []
|
|
||||||
for cx in list(p1):
|
|
||||||
if cx.isdigit():
|
|
||||||
r1.append(p1.pop(0))
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
r2 = []
|
|
||||||
for c in list(p2):
|
|
||||||
if c.isdigit():
|
|
||||||
r2.append(p2.pop(0))
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
if r1:
|
|
||||||
while r1 and r1[0] == '0':
|
|
||||||
r1.pop(0)
|
|
||||||
if r2:
|
|
||||||
while r2 and r2[0] == '0':
|
|
||||||
r2.pop(0)
|
|
||||||
|
|
||||||
if r1 == r2:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not r2:
|
|
||||||
if lhs_is_digit:
|
|
||||||
return -1
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
if len(r1) != len(r2):
|
|
||||||
if len(r1) > len(r2):
|
|
||||||
return -1
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
elif r1 == r2:
|
|
||||||
return 0
|
|
||||||
elif r1 > r2:
|
|
||||||
return -1
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
else:
|
|
||||||
r1 = []
|
|
||||||
for cx in list(p1):
|
|
||||||
if cx.isalpha():
|
|
||||||
r1.append(p1.pop(0))
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
r2 = []
|
|
||||||
for cy in list(p2):
|
|
||||||
if cy.isalpha():
|
|
||||||
r2.append(p2.pop(0))
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
if not r2:
|
|
||||||
if lhs_is_digit:
|
|
||||||
return -1
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
if len(r1) != len(r2):
|
|
||||||
if len(r1) > len(r2):
|
|
||||||
return -1
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
elif r1 == r2:
|
|
||||||
continue
|
|
||||||
elif r1 > r2:
|
|
||||||
return -1
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
return 0
|
|
||||||
|
|
||||||
e1 = int(a.epoch)
|
|
||||||
e2 = int(b.epoch)
|
|
||||||
|
|
||||||
if e1 < e2:
|
|
||||||
return 1
|
|
||||||
if e1 > e2:
|
|
||||||
return -1
|
|
||||||
|
|
||||||
v1 = a.version
|
|
||||||
v2 = b.version
|
|
||||||
|
|
||||||
rc = compare_parts(v1, v2)
|
|
||||||
|
|
||||||
if rc != 0:
|
|
||||||
return rc
|
|
||||||
|
|
||||||
r1 = a.release
|
|
||||||
r2 = b.release
|
|
||||||
|
|
||||||
rc = compare_parts(r1, r2)
|
|
||||||
|
|
||||||
if rc != 0:
|
|
||||||
return rc
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def compare_version(v1, v2, distro):
|
|
||||||
if distro in RH_FAMILY:
|
|
||||||
return compare_redhat_version(v1, v2)
|
|
||||||
elif distro in DEB_FAMILY:
|
|
||||||
return compare_debian_version(v1, v2)
|
|
||||||
raise DistroUnsupported(distro)
|
|
||||||
|
|
||||||
|
|
||||||
def dict_factory(cursor, row):
|
def dict_factory(cursor, row):
|
||||||
d = dict()
|
d = dict()
|
||||||
for idx, col in enumerate(cursor.description):
|
for idx, col in enumerate(cursor.description):
|
||||||
@@ -535,8 +382,6 @@ def list_packages(distro):
|
|||||||
raise HistoryFileNotFoundError()
|
raise HistoryFileNotFoundError()
|
||||||
elif distro in DEB_FAMILY:
|
elif distro in DEB_FAMILY:
|
||||||
packages = list_packages_dpkg()
|
packages = list_packages_dpkg()
|
||||||
else:
|
|
||||||
raise DistroUnsupported(distro)
|
|
||||||
|
|
||||||
return packages
|
return packages
|
||||||
|
|
||||||
@@ -561,8 +406,7 @@ def package_full(d, distro):
|
|||||||
if release:
|
if release:
|
||||||
package.extend(['-', release])
|
package.extend(['-', release])
|
||||||
return ''.join(package)
|
return ''.join(package)
|
||||||
raise DistroUnsupported(distro)
|
return '{name}'.format(**d) # need a better default
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def package_name(d, distro):
|
def package_name(d, distro):
|
||||||
@@ -571,7 +415,7 @@ def package_name(d, distro):
|
|||||||
return '{name}.{arch}'.format(**d)
|
return '{name}.{arch}'.format(**d)
|
||||||
elif distro in DEB_FAMILY:
|
elif distro in DEB_FAMILY:
|
||||||
return '{name}:{arch}'.format(**d)
|
return '{name}:{arch}'.format(**d)
|
||||||
raise DistroUnsupported(distro)
|
return '{name}'.format(**d)
|
||||||
|
|
||||||
|
|
||||||
def is_valid_input(d):
|
def is_valid_input(d):
|
||||||
@@ -636,7 +480,7 @@ def main(options):
|
|||||||
current = current_packages.get(pkg)
|
current = current_packages.get(pkg)
|
||||||
imported = imported_packages.get(pkg)
|
imported = imported_packages.get(pkg)
|
||||||
|
|
||||||
rc = compare_version(current, imported, distro)
|
rc = compare_version(current, imported)
|
||||||
|
|
||||||
if rc == 1 and options.upgraded:
|
if rc == 1 and options.upgraded:
|
||||||
logger.debug('Upgrade: {0} (current) - {1} (imported)'.format(current, imported))
|
logger.debug('Upgrade: {0} (current) - {1} (imported)'.format(current, imported))
|
||||||
@@ -680,7 +524,7 @@ if __name__ == '__main__':
|
|||||||
parser_import = subparsers.add_parser('import')
|
parser_import = subparsers.add_parser('import')
|
||||||
parser_import.add_argument('file', help='JSON file containing packages')
|
parser_import.add_argument('file', help='JSON file containing packages')
|
||||||
parser_import.add_argument('--name-only', action='store_true',
|
parser_import.add_argument('--name-only', action='store_true',
|
||||||
help='Output package names and architecture (no version)')
|
help='Output package names and architectyure (no version)')
|
||||||
|
|
||||||
pkg_group = parser_import.add_mutually_exclusive_group()
|
pkg_group = parser_import.add_mutually_exclusive_group()
|
||||||
pkg_group.add_argument('--upgraded', action='store_true',
|
pkg_group.add_argument('--upgraded', action='store_true',
|
||||||
|
|||||||
Reference in New Issue
Block a user