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.

199 lines
7.0 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
class EEAptGet():
10 years ago
"""Generic apt-get intialisation"""
10 years ago
def update(self):
10 years ago
"""
Similar to `apt-get upgrade`
"""
try:
apt_cache = apt.cache.Cache()
import sys
orig_out = sys.stdout
sys.stdout = open(self.app.config.get('log.logging', 'file'), 'a')
apt_cache.update(apt.progress.text.AcquireProgress())
sys.stdout = orig_out
# 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 Exception as e:
Log.debug(self, 'SystemError: ' + str(e))
Log.error(self, 'Unable to Fetch 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):
10 years ago
"""
Similar to `apt-get install`
"""
apt_pkg.init()
# #apt_pkg.PkgSystemLock()
10 years ago
global apt_cache
apt_cache = apt.cache.Cache()
def install_package(self, package_name):
pkg = apt_cache[package_name.strip()]
if package_name.strip() in apt_cache:
if pkg.is_installed:
#apt_pkg.PkgSystemUnLock()
Log.debug(self, 'Trying to install a package that '
'is already installed (' +
package_name.strip() + ')')
#apt_cache.close()
return False
10 years ago
else:
try:
# print(pkg.name)
pkg.mark_install()
except Exception as e:
Log.debug(self, str(e))
Log.error(self, str(e))
else:
#apt_cache.close()
Log.error(self, 'Unknown package selected (' +
package_name.strip() + ')')
10 years ago
for package in packages:
if not install_package(self, package):
continue
10 years ago
10 years ago
if apt_cache.install_count > 0:
try:
#apt_pkg.PkgSystemUnLock()
orig_out = sys.stdout
sys.stdout = open(self.app.config.get('log.logging', 'file'),
'a')
result = apt_cache.commit(apt.progress.text.AcquireProgress(),
apt.progress.base.InstallProgress())
sys.stdout = orig_out
10 years ago
#apt_cache.close()
return result
except SystemError as e:
Log.debug(self, 'SystemError: ' + str(e))
Log.error(self, 'SystemError: ' + str(e))
#apt_cache.close()
except Exception as e:
Log.debug(self, str(e))
Log.error(self, str(e))
def remove(self, packages, auto=False, purge=False):
10 years ago
"""
Similar to `apt-get remove/purge`
purge packages if purge=True
"""
apt_pkg.init()
# apt_pkg.PkgSystemLock()
10 years ago
global apt_cache
apt_cache = apt.cache.Cache()
def remove_package(self, package_name, purge=False):
pkg = apt_cache[package_name.strip()]
if package_name.strip() in apt_cache:
if not pkg.is_installed:
# apt_pkg.PkgSystemUnLock()
Log.debug(self, 'Trying to uninstall a package '
'that is not installed (' +
package_name.strip() + ')')
return False
else:
try:
# print(pkg.name)
pkg.mark_delete(purge)
except SystemError as e:
Log.debug(self, 'SystemError: ' + str(e))
return False
else:
# apt_cache.close()
Log.error(self, 'Unknown package selected (' +
package_name.strip() + ')')
10 years ago
for package in packages:
if not remove_package(self, package, purge=purge):
continue
10 years ago
if apt_cache.delete_count > 0:
try:
# apt_pkg.PkgSystemUnLock()
orig_out = sys.stdout
sys.stdout = open(self.app.config.get('log.logging', 'file'),
'a')
result = apt_cache.commit(apt.progress.text.AcquireProgress(),
apt.progress.base.InstallProgress())
sys.stdout = orig_out
10 years ago
# apt_cache.close()
return result
except SystemError as e:
Log.debug(self, 'SystemError: ' + str(e))
return False
except Exception as e:
Log.debug(self, str(e))
Log.error(self, str(e))
# apt_cache.close()
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'), '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