2014-08-29 22:55:11 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2014-08-29 22:55:11 +02:00
|
|
|
# === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
#
|
|
|
|
# Copyright 2014, Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
|
2017-02-01 11:33:37 +01:00
|
|
|
# Copyright 2015-2017, Teo Mrnjavac <teo@kde.org>
|
2017-02-04 12:43:30 +01:00
|
|
|
# Copyright 2016-2017, Kyle Robbertze <kyle@aims.ac.za>
|
2017-03-29 20:37:00 +02:00
|
|
|
# Copyright 2017, Alf Gaida <agaida@siduction.org>
|
2014-08-29 22:55:11 +02:00
|
|
|
#
|
|
|
|
# Calamares is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Calamares is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
import abc
|
|
|
|
from string import Template
|
2016-11-02 17:00:08 +01:00
|
|
|
import subprocess
|
2017-08-28 22:26:26 +02:00
|
|
|
|
2014-08-29 22:55:11 +02:00
|
|
|
import libcalamares
|
2015-08-06 12:13:21 +02:00
|
|
|
from libcalamares.utils import check_target_env_call, target_env_call
|
2015-02-18 15:47:24 +01:00
|
|
|
|
2017-03-29 20:37:00 +02:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
class PackageManager(metaclass=abc.ABCMeta):
|
2017-03-29 20:37:00 +02:00
|
|
|
"""
|
2017-08-28 22:26:26 +02:00
|
|
|
Package manager base class. A subclass implements package management
|
|
|
|
for a specific backend, and must have a class property `backend`
|
|
|
|
with the string identifier for that backend.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
Subclasses are collected below to populate the list of possible
|
|
|
|
backends.
|
2015-02-20 20:54:25 +01:00
|
|
|
"""
|
2017-08-28 22:26:26 +02:00
|
|
|
backend = None
|
2014-08-29 22:55:11 +02:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
@abc.abstractmethod
|
2015-03-02 00:28:14 +01:00
|
|
|
def install(self, pkgs, from_local=False):
|
2017-08-29 10:18:47 +02:00
|
|
|
"""
|
|
|
|
Install a list of packages (named) into the system.
|
|
|
|
Although this handles lists, in practice it is called
|
|
|
|
with one package at a time.
|
|
|
|
|
|
|
|
@param pkgs: list[str]
|
|
|
|
list of package names
|
|
|
|
@param from_local: bool
|
|
|
|
if True, then these are local packages (on disk) and the
|
|
|
|
pkgs names are paths.
|
|
|
|
"""
|
2017-08-28 22:26:26 +02:00
|
|
|
pass
|
2014-08-29 22:55:11 +02:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
@abc.abstractmethod
|
2014-08-29 22:55:11 +02:00
|
|
|
def remove(self, pkgs):
|
2017-08-29 10:18:47 +02:00
|
|
|
"""
|
|
|
|
Removes packages.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
2017-08-29 10:18:47 +02:00
|
|
|
@param pkgs: list[str]
|
|
|
|
list of package names
|
2015-02-20 20:54:25 +01:00
|
|
|
"""
|
2017-08-28 22:26:26 +02:00
|
|
|
pass
|
2014-08-29 22:55:11 +02:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
@abc.abstractmethod
|
2016-11-02 13:03:44 +01:00
|
|
|
def update_db(self):
|
2017-08-28 22:26:26 +02:00
|
|
|
pass
|
2016-11-02 13:03:44 +01:00
|
|
|
|
2017-01-26 09:36:05 +01:00
|
|
|
def run(self, script):
|
|
|
|
if script != "":
|
2017-02-20 13:22:18 +01:00
|
|
|
check_target_env_call(script.split(" "))
|
2017-01-26 09:36:05 +01:00
|
|
|
|
2017-08-29 10:18:47 +02:00
|
|
|
def install_package(self, packagedata, from_local=False):
|
|
|
|
"""
|
|
|
|
Install a package from a single entry in the install list.
|
|
|
|
This can be either a single package name, or an object
|
|
|
|
with pre- and post-scripts.
|
|
|
|
|
|
|
|
@param packagedata: str|dict
|
|
|
|
@param from_local: bool
|
|
|
|
see install.from_local
|
|
|
|
"""
|
|
|
|
if isinstance(packagedata, str):
|
|
|
|
self.install([packagedata], from_local=from_local)
|
|
|
|
else:
|
|
|
|
self.run(packagedata["pre-script"])
|
|
|
|
self.install([packagedata["package"]], from_local=from_local)
|
|
|
|
self.run(packagedata["post-script"])
|
|
|
|
|
2015-02-18 15:47:24 +01:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
class PMPackageKit(PackageManager):
|
|
|
|
backend = "packagekit"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
for pkg in pkgs:
|
|
|
|
check_target_env_call(["pkcon", "-py", "install", pkg])
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
for pkg in pkgs:
|
|
|
|
check_target_env_call(["pkcon", "-py", "remove", pkg])
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["pkcon", "refresh"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMZypp(PackageManager):
|
|
|
|
backend = "zypp"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["zypper", "--non-interactive",
|
2017-08-29 10:18:47 +02:00
|
|
|
"--quiet-install", "install",
|
|
|
|
"--auto-agree-with-licenses",
|
|
|
|
"install"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["zypper", "--non-interactive",
|
2017-08-29 10:18:47 +02:00
|
|
|
"remove"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["zypper", "--non-interactive", "update"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMYum(PackageManager):
|
|
|
|
backend = "yum"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["yum", "install", "-y"] + pkgs)
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["yum", "--disablerepo=*", "-C", "-y",
|
2017-08-29 10:18:47 +02:00
|
|
|
"remove"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
# Doesn't need updates
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PMDnf(PackageManager):
|
|
|
|
backend = "dnf"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["dnf", "install", "-y"] + pkgs)
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
# ignore the error code for now because dnf thinks removing a
|
|
|
|
# nonexistent package is an error
|
|
|
|
target_env_call(["dnf", "--disablerepo=*", "-C", "-y",
|
2017-08-29 10:18:47 +02:00
|
|
|
"remove"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
# Doesn't need to update explicitly
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PMUrpmi(PackageManager):
|
2017-08-29 10:18:47 +02:00
|
|
|
backend = "urpmi"
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["urpmi", "--download-all", "--no-suggests",
|
2017-08-29 10:18:47 +02:00
|
|
|
"--no-verify-rpm", "--fastunsafe",
|
|
|
|
"--ignoresize", "--nolock",
|
|
|
|
"--auto"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["urpme", "--auto"] + pkgs)
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["urpmi.update", "-a"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMApt(PackageManager):
|
|
|
|
backend = "apt"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["apt-get", "-q", "-y", "install"] + pkgs)
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["apt-get", "--purge", "-q", "-y",
|
2017-08-29 10:18:47 +02:00
|
|
|
"remove"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
check_target_env_call(["apt-get", "--purge", "-q", "-y",
|
2017-08-29 10:18:47 +02:00
|
|
|
"autoremove"])
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["apt-get", "update"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMPacman(PackageManager):
|
|
|
|
backend = "pacman"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
if from_local:
|
|
|
|
pacman_flags = "-U"
|
|
|
|
else:
|
|
|
|
pacman_flags = "-Sy"
|
|
|
|
|
|
|
|
check_target_env_call(["pacman", pacman_flags,
|
2017-08-29 10:18:47 +02:00
|
|
|
"--noconfirm"] + pkgs)
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["pacman", "-Rs", "--noconfirm"] + pkgs)
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["pacman", "-Sy"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMPortage(PackageManager):
|
|
|
|
backend = "portage"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["emerge", "-v"] + pkgs)
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["emerge", "-C"] + pkgs)
|
|
|
|
check_target_env_call(["emerge", "--depclean", "-q"])
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["emerge", "--sync"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMEntropy(PackageManager):
|
|
|
|
backend = "entropy"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
check_target_env_call(["equo", "i"] + pkgs)
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
check_target_env_call(["equo", "rm"] + pkgs)
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
check_target_env_call(["equo", "update"])
|
|
|
|
|
|
|
|
|
|
|
|
class PMDummy(PackageManager):
|
|
|
|
backend = "dummy"
|
|
|
|
|
|
|
|
def install(self, pkgs, from_local=False):
|
|
|
|
libcalamares.utils.debug("Installing " + str(pkgs))
|
|
|
|
|
|
|
|
def remove(self, pkgs):
|
|
|
|
libcalamares.utils.debug("Removing " + str(pkgs))
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
libcalamares.utils.debug("Updating DB")
|
|
|
|
|
2017-08-29 10:18:47 +02:00
|
|
|
def run(self, script):
|
|
|
|
libcalamares.utils.debug("Running script '" + str(script) + "'")
|
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
|
2017-08-29 12:33:07 +02:00
|
|
|
# Collect all the subclasses of PackageManager defined above,
|
|
|
|
# and index them based on the backend property of each class.
|
2017-08-28 22:26:26 +02:00
|
|
|
backend_managers = [
|
|
|
|
(c.backend, c)
|
|
|
|
for c in globals().values()
|
2017-08-29 10:18:47 +02:00
|
|
|
if type(c) is abc.ABCMeta and issubclass(c, PackageManager) and c.backend]
|
2017-08-28 22:26:26 +02:00
|
|
|
|
|
|
|
|
2017-08-29 12:33:07 +02:00
|
|
|
def subst_locale(plist):
|
|
|
|
"""
|
|
|
|
Returns a locale-aware list of packages, based on @p plist.
|
|
|
|
Package names that contain LOCALE are localized with the
|
|
|
|
BCP47 name of the chosen system locale; if the system
|
|
|
|
locale is 'en' (e.g. English, US) then these localized
|
|
|
|
packages are dropped from the list.
|
|
|
|
|
|
|
|
@param plist: list[str|dict]
|
|
|
|
Candidate packages to install.
|
|
|
|
@return: list[str|dict]
|
|
|
|
"""
|
2017-01-26 07:29:46 +01:00
|
|
|
locale = libcalamares.globalstorage.value("locale")
|
2017-08-29 12:33:07 +02:00
|
|
|
if not locale:
|
|
|
|
return plist
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for packagedata in plist:
|
|
|
|
if isinstance(packagedata, str):
|
|
|
|
packagename = packagedata
|
|
|
|
else:
|
|
|
|
packagename = packagedata["package"]
|
|
|
|
|
|
|
|
# Update packagename: substitute LOCALE, and drop packages
|
|
|
|
# if locale is en and LOCALE is in the package name.
|
|
|
|
if locale != "en":
|
|
|
|
packagename = Template(packagename).safe_substitute(LOCALE=locale)
|
|
|
|
elif 'LOCALE' in packagename:
|
|
|
|
packagename = None
|
|
|
|
|
|
|
|
if packagename is not None:
|
|
|
|
# Put it back in packagedata
|
|
|
|
if isinstance(packagedata, str):
|
|
|
|
packagedata = packagename
|
|
|
|
else:
|
|
|
|
packagedata["package"] = packagename
|
|
|
|
|
|
|
|
ret.append(packagedata)
|
|
|
|
|
2017-01-26 07:29:46 +01:00
|
|
|
return ret
|
2016-12-01 11:59:44 +01:00
|
|
|
|
2017-03-29 20:37:00 +02:00
|
|
|
|
2014-08-29 22:55:11 +02:00
|
|
|
def run_operations(pkgman, entry):
|
2017-03-29 20:37:00 +02:00
|
|
|
"""
|
|
|
|
Call package manager with given parameters.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param pkgman:
|
|
|
|
:param entry:
|
|
|
|
"""
|
2014-08-29 22:55:11 +02:00
|
|
|
for key in entry.keys():
|
2017-01-26 07:29:46 +01:00
|
|
|
entry[key] = subst_locale(entry[key])
|
2014-08-29 22:55:11 +02:00
|
|
|
if key == "install":
|
2017-08-29 12:33:07 +02:00
|
|
|
if all([isinstance(x, str) for x in entry[key]]):
|
|
|
|
pkgman.install(entry[key])
|
|
|
|
else:
|
|
|
|
for package in entry[key]:
|
|
|
|
pkgman.install_package(package)
|
2016-11-02 17:00:08 +01:00
|
|
|
elif key == "try_install":
|
2017-03-29 20:37:00 +02:00
|
|
|
# we make a separate package manager call for each package so a
|
|
|
|
# single failing package won't stop all of them
|
2017-02-01 11:33:37 +01:00
|
|
|
for package in entry[key]:
|
2017-08-29 10:18:47 +02:00
|
|
|
try:
|
|
|
|
pkgman.install_package(package)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
warn_text = "WARNING: could not install package "
|
|
|
|
warn_text += str(package)
|
|
|
|
libcalamares.utils.debug(warn_text)
|
2014-08-29 22:55:11 +02:00
|
|
|
elif key == "remove":
|
|
|
|
pkgman.remove(entry[key])
|
2016-11-02 17:00:08 +01:00
|
|
|
elif key == "try_remove":
|
2017-02-01 11:33:37 +01:00
|
|
|
for package in entry[key]:
|
|
|
|
try:
|
|
|
|
pkgman.remove([package])
|
|
|
|
except subprocess.CalledProcessError:
|
2017-03-29 20:37:00 +02:00
|
|
|
warn_text = "WARNING: could not remove package "
|
2017-04-18 18:10:49 +02:00
|
|
|
warn_text += package
|
|
|
|
libcalamares.utils.debug(warn_text)
|
2015-03-04 16:19:26 +01:00
|
|
|
elif key == "localInstall":
|
2015-03-02 00:28:14 +01:00
|
|
|
pkgman.install(entry[key], from_local=True)
|
2014-08-29 22:55:11 +02:00
|
|
|
|
2015-02-18 15:47:24 +01:00
|
|
|
|
2014-08-29 22:55:11 +02:00
|
|
|
def run():
|
2017-03-29 20:37:00 +02:00
|
|
|
"""
|
|
|
|
Calls routine with detected package manager to install locale packages
|
2015-02-21 10:46:01 +01:00
|
|
|
or remove drivers not needed on the installed system.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-08-29 22:55:11 +02:00
|
|
|
backend = libcalamares.job.configuration.get("backend")
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2017-08-28 22:26:26 +02:00
|
|
|
for identifier, impl in backend_managers:
|
|
|
|
if identifier == backend:
|
|
|
|
pkgman = impl()
|
|
|
|
break
|
|
|
|
else:
|
2015-06-14 13:25:37 +02:00
|
|
|
return "Bad backend", "backend=\"{}\"".format(backend)
|
2014-08-29 22:55:11 +02:00
|
|
|
|
|
|
|
operations = libcalamares.job.configuration.get("operations", [])
|
2015-06-14 13:25:37 +02:00
|
|
|
|
2016-11-02 13:03:44 +01:00
|
|
|
update_db = libcalamares.job.configuration.get("update_db", False)
|
|
|
|
if update_db and libcalamares.globalstorage.value("hasInternet"):
|
|
|
|
pkgman.update_db()
|
|
|
|
|
2014-08-29 22:55:11 +02:00
|
|
|
for entry in operations:
|
|
|
|
run_operations(pkgman, entry)
|
|
|
|
|
2014-10-27 18:39:11 +01:00
|
|
|
if libcalamares.globalstorage.contains("packageOperations"):
|
2017-03-29 20:37:00 +02:00
|
|
|
run_operations(pkgman,
|
|
|
|
libcalamares.globalstorage.value("packageOperations"))
|
2014-08-29 22:55:11 +02:00
|
|
|
|
|
|
|
return None
|