Updated for remove packages

This commit is contained in:
Dimitris Zlatanidis 2023-05-10 21:11:16 +03:00
parent 687640c73e
commit 61b8773d8a
2 changed files with 114 additions and 136 deletions

View file

@ -1,12 +1,14 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import time
import subprocess
from multiprocessing import Process
from slpkg.configs import Configs
from slpkg.utilities import Utilities
from slpkg.dialog_box import DialogBox
from slpkg.views.views import ViewMessage
from slpkg.progress_bar import ProgressBar
from slpkg.models.models import LogsDependencies
@ -22,17 +24,19 @@ class RemovePackages(Configs):
self.flags: list = flags
self.session = Session
self.dialogbox = DialogBox()
self.utils = Utilities()
self.progress = ProgressBar()
self.view = ViewMessage(flags)
self.process_message: str = ''
self.logs_dependencies: dict = {}
self.process_message: str = str()
self.installed_packages: list = []
self.dependencies: list = []
self.stderr = None
self.stdout = None
self.option_resolve_off: bool = self.utils.is_option(
self.option_for_resolve_off: bool = self.utils.is_option(
['-O', '--resolve-off'], flags)
self.option_for_no_silent: bool = self.utils.is_option(
@ -43,80 +47,102 @@ class RemovePackages(Configs):
def remove(self) -> None:
""" Removes package with dependencies. """
self.installed_packages, self.dependencies = self.view.remove_packages(self.packages)
self.query_logs_dependencies()
self.save_dependencies_for_remove()
self.choose_dependencies_for_remove()
self.save_packages_for_remove()
self.view.remove_packages(self.packages, self.dependencies)
self.view.question()
start: float = time.time()
self.remove_packages()
if self.dependencies and not self.option_resolve_off:
self.delete_deps_logs()
self.delete_main_logs()
elapsed_time: float = time.time() - start
self.utils.finished_time(elapsed_time)
def save_dependencies_for_remove(self):
if not self.option_for_resolve_off:
for package in self.packages:
if self.logs_dependencies.get(package):
requires: tuple = self.logs_dependencies[package]
for require in requires:
installed: str = self.utils.is_package_installed(require)
if installed and require not in self.packages:
self.dependencies.append(require)
self.installed_packages.append(installed)
def save_packages_for_remove(self):
for package in self.packages:
installed: str = self.utils.is_package_installed(package)
if installed:
self.installed_packages.append(installed)
def remove_packages(self) -> None:
""" Run Slackware command to remove the packages. """
for package in self.installed_packages:
name: str = self.utils.split_binary_pkg(package)[0]
self.process_message: str = f"package '{name}' to remove"
if self.check_in_logs_for_dependencies_to_remove(name):
if self.is_dependency_to_other(name):
command: str = f'{self.removepkg} {package}'
self.multi_process(command, package)
self.delete_package_from_logs(name)
def check_in_logs_for_dependencies_to_remove(self, name: str) -> bool:
def is_dependency_to_other(self, name: str) -> bool:
dependencies: list = []
logs: dict = self.query_dependencies()
for package, requires in logs.items():
if name in requires:
dependencies.append(package)
if self.logs_dependencies.get(name):
for package, requires in self.logs_dependencies.items():
if name in requires:
dependencies.append(package)
if dependencies and not self.option_for_yes and self.ask_question:
print(f"\n{self.bold}{self.violet}WARNING!{self.endc}: The package '{self.red}{name}{self.endc}' "
f"is a dependency for the packages:")
for dep in dependencies:
print(f"{self.cyan:>16}-> {dep}{self.endc}")
self.view_warning_message(dependencies, name)
answer: str = input('\nDo you want to remove? [y/N] ')
if answer not in ['Y', 'y']:
return False
print()
return True
def query_dependencies(self) -> dict:
""" Returns a dictionary with the package name and the dependencies
before they are saved with the command slpkg install. """
logs_deps: dict = {}
def view_warning_message(self, dependencies: list, name: str) -> None:
print(f"\n{self.bold}{self.violet}WARNING!{self.endc}: The package '{self.red}{name}{self.endc}' "
f"is a dependency for the packages:")
for dep in dependencies:
print(f"{self.cyan:>16}-> {dep}{self.endc}")
def query_logs_dependencies(self) -> None:
package_requires: tuple = self.session.query(
LogsDependencies.name, LogsDependencies.requires).all()
for package in package_requires:
if package[0] not in self.packages:
logs_deps[package[0]] = package[1].split()
self.logs_dependencies[package[0]] = package[1].split()
return logs_deps
def delete_main_logs(self) -> None:
""" Deletes main packages from logs. """
for pkg in self.packages:
def delete_package_from_logs(self, package) -> None:
if not self.option_for_resolve_off:
self.session.query(LogsDependencies).filter(
LogsDependencies.name == pkg).delete()
self.session.commit()
LogsDependencies.name == package).delete()
self.session.commit()
def delete_deps_logs(self) -> None:
""" Deletes depends packages from logs. """
for pkg in self.dependencies:
self.session.query(LogsDependencies).filter(
LogsDependencies.name == pkg).delete()
self.session.commit()
def choose_dependencies_for_remove(self):
height: int = 10
width: int = 70
list_height: int = 0
choices: list = []
title: str = " Choose dependencies you want to remove "
for package in self.dependencies:
inst_pkg: str = self.utils.is_package_installed(package)
inst_ver: str = self.utils.split_binary_pkg(inst_pkg)[1]
choices.extend([(package, inst_ver, True, f'Package: {inst_pkg}')])
text: str = f'There are {len(choices)} dependencies:'
code, self.dependencies = self.dialogbox.checklist(text, self.dependencies, title, height,
width, list_height, choices)
os.system('clear')
def multi_process(self, command: str, package: str) -> None:
""" Starting multiprocessing remove process. """
if self.silent_mode and not self.option_for_no_silent:
done: str = f' {self.byellow} Done{self.endc}'

View file

@ -1,7 +1,6 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import re
from typing import Any
from pathlib import Path
@ -12,7 +11,6 @@ from slpkg.views.ascii import Ascii
from slpkg.utilities import Utilities
from slpkg.dialog_box import DialogBox
from slpkg.repositories import Repositories
from slpkg.models.models import LogsDependencies
from slpkg.models.models import session as Session
@ -32,8 +30,6 @@ class ViewMessage(Configs):
self.repos = Repositories()
self.download_only = None
self.installed_packages: list = []
self.is_binary: bool = self.utils.is_binary_repo(repository)
self.option_for_resolve_off: bool = self.utils.is_option(
@ -46,48 +42,48 @@ class ViewMessage(Configs):
['-y', '--yes'], flags)
def view_packages(self, package: str, mode: str) -> None:
""" Printing the main packages. """
size: str = str()
color: str = self.red
color: str = self.endc
repository: str = self.repository
if self.is_binary:
if mode == 'remove':
installed = self.utils.is_package_installed(package)
pkg: list = self.utils.split_binary_pkg(installed)
version: str = pkg[1]
repo_tag: str = pkg[4]
size: str = self.utils.get_file_size(Path(self.log_packages, installed))
repository = repo_tag.lower().replace('_', '')
elif self.is_binary:
version: str = self.data[package][0]
# size: str = self.utils.convert_file_sizes(int(self.data[package][4])) <- It's going to replace with this
size: str = self.utils.convert_file_sizes(
int(''.join(re.findall(r'\d+', self.data[package][4])))
)
int(''.join(re.findall(r'\d+', self.data[package][4]))))
else:
version: str = self.data[package][2]
# Default highlight colors for all modes
if mode == 'remove':
color: str = self.red
if mode in ['install', 'download']:
color: str = self.cyan
if mode == 'build':
color: str = self.yellow
if mode == 'upgrade':
color: str = self.violet
package: str = self._package_and_version(package)
package: str = self.build_package_and_version(package)
# Change the colors for install mode
if self.utils.is_package_installed(package) and mode == 'install':
color: str = self.grey
if self.upgrade.is_package_upgradeable(package) and mode == 'install':
color: str = self.violet
if (self.utils.is_package_installed(package) and mode == 'install'
and self.option_for_reinstall):
color: str = self.violet
package: str = self._package_and_version(package)
if mode == 'install':
# Change the colors for install mode
if self.utils.is_package_installed(package):
color: str = self.grey
if self.upgrade.is_package_upgradeable(package):
color: str = self.violet
if self.utils.is_package_installed(package) and self.option_for_reinstall:
color: str = self.violet
package: str = self.build_package_and_version(package)
self.ascii.draw_view_package(package, version, size, color, self.repository)
def _package_and_version(self, package: str) -> str:
installed_package: str = self.utils.is_package_installed(package)
version: str = self.utils.split_binary_pkg(installed_package)[1]
return f'{package}-{version}'
def view_skipping_packages(self, package: str, version: str) -> None:
""" Print the skipping packages. """
print(f'{self.yellow}Skipping{self.endc}: {package}-{version} {self.red}(already installed){self.endc}')
self.ascii.draw_view_package(package, version, size, color, repository)
def build_packages(self, slackbuilds: list, dependencies: list) -> None:
""" View packages for build only. """
@ -138,79 +134,35 @@ class ViewMessage(Configs):
self.summary(slackbuilds, dependencies=[], option=mode)
def remove_packages(self, packages: list) -> Any:
def remove_packages(self, packages: list, dependencies: list) -> Any:
""" View remove packages. """
mode = 'remove'
pkgs, dependencies = [], []
for pkg in packages:
pkgs.append(pkg)
requires: tuple = self.session.query(
LogsDependencies.requires).filter(
LogsDependencies.name == pkg).first()
if requires:
dependencies.extend(requires[0].split())
if dependencies and not self.option_for_resolve_off:
dependencies: list = self.choose_dependencies_for_remove(list(set(dependencies)))
self.ascii.draw_package_title_box('The following packages will be removed:',
'slpkg remove packages')
for pkg in packages:
self.view_packages(pkg, mode)
for pkg in pkgs:
if pkg not in dependencies:
self._view_removed(pkg)
if dependencies and not self.option_for_resolve_off:
if dependencies:
self.ascii.draw_middle_line()
self.ascii.draw_dependency_line()
for pkg in dependencies:
self._view_removed(pkg)
else:
dependencies: list = []
self.view_packages(pkg, mode)
self.summary(pkgs, dependencies, option=mode)
self.summary(packages, dependencies, option=mode)
return self.installed_packages, dependencies
def logs_packages(self, dependencies: list) -> None:
""" View the logging packages. """
print('The following logs will be removed:\n')
for dep in dependencies:
print(f'{self.yellow}{dep[0]}{self.endc}')
self.ascii.draw_log_package(dep[1])
def _view_removed(self, name: str) -> None:
""" View and creates list with packages for remove. """
installed = self.utils.is_package_installed(name)
if installed:
pkg: list = self.utils.split_binary_pkg(installed)
self.installed_packages.append(installed)
size: str = self.utils.get_file_size(Path(self.log_packages, installed))
self.ascii.draw_view_package(pkg[0], pkg[1], size, self.red,
repo=pkg[4].lower().replace('_', ''))
def choose_dependencies_for_remove(self, dependencies: list) -> list:
""" Choose packages for remove using the dialog box. """
height: int = 10
width: int = 70
list_height: int = 0
choices: list = []
title: str = " Choose dependencies you want to remove "
for package in dependencies:
inst_pkg: str = self.utils.is_package_installed(package)
inst_ver: str = self.utils.split_binary_pkg(inst_pkg)[1]
choices.extend([(package, inst_ver, True, f'Package: {inst_pkg}')])
text: str = f'There are {len(choices)} dependencies:'
code, tags = self.dialogbox.checklist(text, dependencies, title, height,
width, list_height, choices)
if not code:
return dependencies
os.system('clear')
return tags
print('Note: After cleaning you should remove them one by one.')
def summary(self, packages: list, dependencies: list, option: str) -> None:
packages.extend(dependencies)
summary_message: str = ''
summary_message: str = str()
install = upgrade = remove = size_comp = size_uncomp = size_rmv = 0
for pkg in packages:
@ -269,14 +221,14 @@ class ViewMessage(Configs):
print(summary_message)
def logs_packages(self, dependencies: list) -> None:
""" View the logging packages. """
print('The following logs will be removed:\n')
for dep in dependencies:
print(f'{self.yellow}{dep[0]}{self.endc}')
self.ascii.draw_log_package(dep[1])
def build_package_and_version(self, package: str) -> str:
installed_package: str = self.utils.is_package_installed(package)
version: str = self.utils.split_binary_pkg(installed_package)[1]
return f'{package}-{version}'
print('Note: After cleaning you should remove them one by one.')
def view_skipping_packages(self, package: str, version: str) -> None:
""" Print the skipping packages. """
print(f'{self.yellow}Skipping{self.endc}: {package}-{version} {self.red}(already installed){self.endc}')
def question(self) -> None:
""" Manage to proceed. """