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.

246 lines
9.5 KiB

"""EasyEngine package installation using apt-get module."""
10 years ago
import apt
import apt_pkg
10 years ago
import sys
from ee.core.logging import Log
10 years ago
class EEAptGet():
10 years ago
"""Generic apt-get intialisation"""
10 years ago
def update(self):
10 years ago
"""Similar to apt-get update"""
# app.log.debug("Update cache")
cache = apt.Cache()
fprogress = apt.progress.text.AcquireProgress()
iprogress = apt.progress.base.InstallProgress()
10 years ago
cache.update()
def upgrade(self, packages):
10 years ago
"""Similar to apt-get update"""
cache = apt.Cache()
fprogress = apt.progress.text.AcquireProgress()
iprogress = apt.progress.base.InstallProgress()
10 years ago
my_selected_packages = []
# Cache Initialization
if not cache:
cache = apt.Cache()
10 years ago
# Cache Read
cache.open()
10 years ago
for package in packages:
pkg = cache[package]
10 years ago
# Check Package Installed
if pkg.is_installed:
# Check Package is Upgradeble
if pkg.is_upgradable:
with cache.actiongroup():
10 years ago
# Mark Package for Upgrade
pkg.mark_upgrade()
my_selected_packages.append(pkg.installed)
10 years ago
else:
print("\'{package_name}-{package_ver.version}\'"
"already the newest version"
.format(package_name=pkg.name,
package_ver=pkg.installed))
10 years ago
# Check if packages available for install.
if len(my_selected_packages) > 0:
print("The following packages will be upgraded:"
"\n {pkg_name}"
.format(pkg_name=my_selected_packages))
print("{pkg_install_count} newly installed."
.format(pkg_upgrade_count=len(my_selected_packages)))
print("Need to get {req_download} bytes of archives"
.format(req_download=cache.required_download))
10 years ago
print("After this operation, {space} bytes of"
"additional disk space will be used."
.format(space=cache.required_space))
10 years ago
try:
# Commit changes in cache (actually install)
10 years ago
cache.commit(fprogress, iprogres)
10 years ago
except Exception as e:
print("package installation failed. [{err}]"
.format(err=str(e)))
return(False)
return(True)
def install(self, packages):
10 years ago
"""Installation of packages"""
cache = apt.Cache()
fprogress = apt.progress.text.AcquireProgress()
iprogress = apt.progress.base.InstallProgress()
10 years ago
my_selected_packages = []
# Cache Initialization
if not cache:
cache = apt.Cache()
10 years ago
# Cache Read
cache.open()
10 years ago
10 years ago
for package in packages:
try:
pkg = cache[package]
except KeyError as e:
continue
10 years ago
# Check Package Installed
if pkg.is_installed or pkg.marked_install:
# Check Package is Upgradeble
if pkg.is_upgradable:
print("latest version of \'{package_name}\' available."
.format(package_name=pkg.installed))
else:
# Check if package already marked for install
if not pkg.marked_install:
print("\'{package_name}-{package_ver}\'"
"already the newest version"
.format(package_name=pkg.shortname,
10 years ago
package_ver=pkg.installed))
10 years ago
else:
with cache.actiongroup():
10 years ago
# Mark Package for Installation
pkg.mark_install()
my_selected_packages.append(pkg.name)
10 years ago
10 years ago
# Check if packages available for install.
if cache.install_count > 0:
10 years ago
print("The following NEW packages will be installed:"
"\n {pkg_name}"
.format(pkg_name=my_selected_packages))
print("{pkg_install_count} newly installed."
.format(pkg_install_count=cache.install_count))
10 years ago
print("Need to get {req_download} bytes of archives"
.format(req_download=cache.required_download))
10 years ago
print("After this operation, {space:.2f} MB of"
" additional disk space will be used."
.format(space=cache.required_space/1e6))
10 years ago
try:
# Commit changes in cache (actually install)
10 years ago
cache.commit()
10 years ago
except Exception as e:
10 years ago
print("package installation failed. [{err}]"
10 years ago
.format(err=str(e)))
10 years ago
return(False)
return(True)
10 years ago
def remove(self, packages, auto=False, purge=False):
def __dependencies_loop(cache, deplist, pkg, onelevel=True):
""" Loops through pkg's dependencies.
Returns a list with every package found. """
if onelevel:
onelevellist = []
if not pkg.is_installed:
return
for depf in pkg.installed.dependencies:
for dep in depf:
# if (dep.name in cache and not cache[dep.name]
# in deplist):
# deplist.append(cache[dep.name])
# __dependencies_loop(cache, deplist, cache[dep.name])
# if onelevel:
if dep.name in cache:
if (cache[dep.name].is_installed and
10 years ago
cache[dep.name].is_auto_installed):
onelevellist.append(cache[dep.name])
# if onelevel:
return onelevellist
cache = apt.Cache()
fprogress = apt.progress.text.AcquireProgress()
iprogress = apt.progress.base.InstallProgress()
onelevel = []
10 years ago
my_selected_packages = []
10 years ago
# Cache Initialization
if not cache:
cache = apt.Cache()
10 years ago
# Cache Read
cache.open()
10 years ago
for package in packages:
print("processing", package)
try:
pkg = cache[package]
except KeyError as e:
Log.debug(self, "{0}".format(e))
continue
if not pkg.is_installed:
Log.info(self, "Package '{package_name}' is not installed,"
" so not removed."
.format(package_name=pkg.name))
continue
my_selected_packages.append(pkg.name)
# How logic works:
# 1) We loop trough dependencies's dependencies and add them to
# the list.
# 2) We sequentially remove every package in list
# - via is_auto_installed we check if we can safely remove it
deplist = []
onelevel = onelevel + __dependencies_loop(cache, deplist, pkg,
onelevel=True)
# Mark for deletion the first package, to fire up
# auto_removable Purge?
10 years ago
try:
if purge:
pkg.mark_delete(purge=True)
else:
pkg.mark_delete(purge=False)
except SystemError as e:
Log.debug(self, "{0}".format(e))
apt.ProblemResolver(cache).remove(pkg)
# print(pkg.inst_state)
# Log.error(self, "Unable to purge packages.")
for dep in onelevel:
my_selected_packages.append(dep.name)
try:
if purge:
dep.mark_delete(purge=True)
else:
dep.mark_delete(purge=False)
except SystemError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to purge depedencies.")
10 years ago
# Check if packages available for remove/update.
if cache.delete_count > 0:
# app.log.debug('packages will be REMOVED ')
10 years ago
print("The following packages will be REMOVED:"
10 years ago
"\n {pkg_name}"
.format(pkg_name=my_selected_packages))
print("{pkg_remove_count} to remove."
.format(pkg_remove_count=cache.delete_count))
# app.log.debug('bytes disk space will be freed')
10 years ago
print("After this operation, {space:.2f} MB disk space "
"will be freed.".format(space=cache.required_space/1e6))
10 years ago
try:
cache.commit(fprogress, iprogress)
10 years ago
except Exception as e:
# app.log.error('Sorry, package installation failed ')
10 years ago
print("Sorry, package installation failed [{err}]"
.format(err=str(e)))
10 years ago
return(False)
return(True)
def is_installed(self, package):
cache = apt.Cache()
fprogress = apt.progress.text.AcquireProgress()
iprogress = apt.progress.base.InstallProgress()
# Cache Initialization
if not cache:
cache = apt.Cache()
# Cache Read
cache.open()
try:
pkg = cache[package]
# Check Package Installed
if pkg.is_installed:
return True
else:
return False
except KeyError as e:
Log.debug(self, "{0}".format(e))
except Exception as e:
return False