Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_installed_version(self, command):
"""
Run command, see if it works. If the output has a version number in
x.y.z format, return that. If it doesn't, but the command ran, return
True. If it fails, return False. ezpz.
"""
try:
# If this fails, it almost always throws.
# NOTE: the split is to handle multi-argument commands. There's
# cases where this is not intended, e.g. it won't handle arguments
# with spaces! But currently this is preferable to running the
# command in a shell.
ver = subproc.match_output(
command.split(),
r'(?P[0-9]+\.[0-9]+(\.[0-9]+)?)',
'ver'
)
if ver is None:
self.log.debug("Could run, but couldn't find a version number.")
return True
self.log.debug("Found version number: {0}".format(ver))
return ver
except (subprocess.CalledProcessError, OSError):
# We'll assume it's not installed
return False
except Exception as e:
self.log.error("Running `{0}` failed.".format(command))
self.log.trace(str(e))
return False
def get_installed_version(self, pkgname):
"""
Return the currently installed version. If pkgname is not installed,
return None.
"""
try:
# '-Qi' will return non-zero if package does not exist, thus will throw
# Output is sth like local/ x.x.x.x-x
ver = subproc.match_output(
[self.command, "-Qi", pkgname],
r'Version[ ]*: (?P[0-9,.]*)',
'ver'
)
if ver is None:
self.log.debug("Looks like pacman -Qi can't find package {pkg}".format(pkg=pkgname))
return False
self.log.debug("Package {0} has version {1}".format(pkgname, ver))
return ver
except subprocess.CalledProcessError:
# This usually means the packet is not installed
return False
except Exception as ex:
self.log.error("Parsing `{0} -Qi` failed.".format(self.command))
self.log.trace(str(ex))
return False
def get_available_version(self, pkgname):
"""
Check which version is available.
"""
if self.cache:
self.log.trace("Checking apt for `{0}'".format(pkgname))
(ver, is_installed) = self.check_cache(pkgname)
if ver:
self.log.debug("Package {0} has version {1} in repositories".format(pkgname, ver))
return ver
else:
try:
self.log.trace("Checking {0} for `{1}'".format(self.searchcmd, pkgname))
ver = subproc.match_output(
[self.searchcmd, "show", pkgname],
r'Version: (?:\d+:)?(?P[0-9]+\.[0-9]+\.[0-9]+|[0-9]+\.[0-9]+|[0-9]+[a-z]+|[0-9]+).*\n',
'ver'
)
if ver is None:
return False
if ver:
self.log.debug("Package {0} has version {1} in repositories".format(pkgname, ver))
return ver
except subprocess.CalledProcessError:
# Could be an issue, but most likely it means the package doesn't exist.
self.log.debug(
"{cmd} show {pkg} failed.".format(cmd=self.searchcmd, pkg=pkgname)
)
return False
def get_available_version(self, pkgname):
"""
See if 'pip search' finds our package.
"""
try:
output_match = subproc.match_output(
[self.cmd, "search", pkgname],
r'^\b{pkg}\b'.format(pkg=pkgname),
)
return bool(output_match)
except subproc.CalledProcessError:
return False
except Exception as ex:
self.log.error("Error running `{cmd} search {pkg}`"
.format(cmd=self.cmd, pkg=pkgname))
self.log.debug(ex)
return False
def get_installed_version(self, pkgname):
"""
Use dpkg (or python-apt) to determine and return the currently installed version.
If pkgname is not installed, return None.
"""
if self.cache:
(ver, is_installed) = self.check_cache(pkgname)
if is_installed:
self.log.debug("Package {0} has version {1} installed".format(pkgname, ver))
return ver if is_installed else False
else:
try:
ver = subproc.match_output(
["dpkg", "-s", pkgname],
r'^Version: (?:\d+:)?(?P[0-9]+\.[0-9]+\.[0-9]+|[0-9]+\.[0-9]+|[0-9]+[a-z]+|[0-9]+)',
'ver'
)
if ver is None:
self.log.debug("Looks like dpkg -s can't find package {pkg}. This is most likely a bug.".format(pkg=pkgname))
return False
self.log.debug("Package {0} has version {1} installed".format(pkgname, ver))
return ver
except subprocess.CalledProcessError:
# This usually means the packet is not installed -- not a problem.
return False
except Exception as e:
self.log.error("Running dpkg -s failed.")
self.log.trace(str(e))
return False
def get_available_version(self, pkgname):
"""
Return a version that we can install through this package manager.
"""
try:
ver = subproc.match_output(
[self.command, "info", pkgname],
r'^Version\s+:\s+(?P.*$)',
'ver',
env=utils.dict_merge(os.environ, {'LC_ALL': 'C'}),
)
if ver is None:
return False
self.log.debug("Package {0} has version {1} in {2}".format(pkgname, ver, self.command))
return ver
except subprocess.CalledProcessError as ex:
# This usually means the package was not found, so don't worry
self.log.trace("`{0} info' returned non-zero exit status.".format(self.command))
self.log.trace(str(ex))
return False
except Exception as ex:
self.log.error("Error parsing {0} info".format(self.command))
def get_available_version(self, pkgname):
"""
Return a version that we can install through this package manager.
"""
try:
ver = subproc.match_output(
[self.command, "-Si", pkgname],
r'Version[ ]*: (?P[0-9,.]*)',
'ver'
) or False
if ver:
self.log.debug("Package {0} has version {1} in {2}".format(pkgname, ver, self.command))
return ver
except subprocess.CalledProcessError as ex:
# This usually means the package was not found, so don't worry
self.log.trace("`{0} -Si' returned non-zero exit status.".format(self.command))
self.log.trace(str(ex))
return False
except Exception as ex:
self.log.error("Error parsing {0} -Si".format(self.command))
self.log.error(str(ex))
return False
def get_available_version(self, pkgname):
"""
Return a version that we can install through this package manager.
"""
try:
ver = subproc.match_output(
[self.command, "info", pkgname],
r'^Version\s+:\s+(?P.*$)',
'ver',
env=utils.dict_merge(os.environ, {'LC_ALL': 'C'}),
)
if ver is None:
return False
self.log.debug("Package {0} has version {1} in {2}".format(pkgname, ver, self.command))
return ver
except subproc.CalledProcessError as ex:
# This usually means the package was not found, so don't worry
self.log.trace("`{0} info' returned non-zero exit status.".format(self.command))
self.log.trace(str(ex))
return False
except Exception as ex:
self.log.error("Error parsing {0} info".format(self.command))