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.

110 lines
3.7 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
from sh import apt_get
10 years ago
from sh import ErrorReturnCode
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 upgrade`
"""
global apt_get
apt_get = apt_get.bake("-y")
try:
for line in apt_get.update(_iter=True):
Log.info(self, Log.ENDC+line+Log.OKBLUE, end=' ')
except ErrorReturnCode as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to run apt-get update")
def dist_upgrade():
10 years ago
"""
Similar to `apt-get upgrade`
"""
try:
apt_cache = apt.cache.Cache()
apt_cache.update()
apt_cache.open(None)
apt_cache.upgrade(True)
success = (apt_cache.commit(
apt.progress.text.AcquireProgress(),
apt.progress.base.InstallProgress()))
# apt_cache.close()
return success
except AttributeError as e:
Log.error(self, 'AttributeError: ' + str(e))
except FetchFailedException as e:
Log.debug(self, 'SystemError: ' + str(e))
Log.error(self, 'Unable to Fetch update')
10 years ago
def install(self, packages):
global apt_get
apt_get = apt_get.bake("-y")
try:
10 years ago
for line in apt_get.install("-o",
"Dpkg::Options::=--force-confold",
*packages, _iter=True):
Log.info(self, Log.ENDC+line+Log.OKBLUE, end=' ')
except ErrorReturnCode as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to run apt-get install")
10 years ago
def remove(self, packages, auto=False, purge=False):
global apt_get
apt_get = apt_get.bake("-y")
try:
if purge:
for line in apt_get.purge(*packages, _iter=True):
Log.info(self, Log.ENDC+line+Log.OKBLUE, end=' ')
else:
for line in apt_get.remove(*packages, _iter=True):
Log.info(self, Log.ENDC+line+Log.OKBLUE, end=' ')
except ErrorReturnCode as e:
Log.debug(self, "{0}".format(e))
10 years ago
Log.error(self, "Unable to remove packages")
10 years ago
def auto_clean(self):
10 years ago
"""
Similar to `apt-get autoclean`
"""
try:
orig_out = sys.stdout
sys.stdout = open(self.app.config.get('log.logging', 'file'),
encoding='utf-8', mode='a')
apt_get.autoclean("-y")
sys.stdout = orig_out
except ErrorReturnCode as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to apt-get autoclean")
def auto_remove(self):
10 years ago
"""
Similar to `apt-get autoremove`
"""
try:
Log.debug(self, "Running apt-get autoremove")
apt_get.autoremove("-y")
except ErrorReturnCode as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to apt-get autoremove")
def is_installed(self, package_name):
10 years ago
"""
Checks if package is available in cache and is installed or not
returns True if installed otherwise returns False
"""
apt_cache = apt.cache.Cache()
apt_cache.open()
if (package_name.strip() in apt_cache and
apt_cache[package_name.strip()].is_installed):
# apt_cache.close()
return True
# apt_cache.close()
return False