slpkg/slpkg/remove_packages.py
Dimitris Zlatanidis 91eba2ffea Fixed packaging
2022-06-17 19:36:07 +03:00

79 lines
2.6 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import subprocess
from slpkg.metadata import Metadata
from dataclasses import dataclass
from slpkg.views.views import ViewMessage
from slpkg.models.models import LogsDependencies
from slpkg.models.models import session as Session
@dataclass
class RemovePackages:
''' Remove installed packages. '''
packages: str
flags: list
session: str = Session
log_packages: str = Metadata.log_packages
repo_tag: str = Metadata.repo_tag
removepkg: str = Metadata.removepkg
color: str = Metadata.colour
def remove(self):
''' Remove package with dependencies. '''
self.installed_packages = []
view = ViewMessage()
view.remove_packages()
for pkg in self.packages:
self.view_installed_packages(pkg)
self.dependencies = self.session.query(
LogsDependencies.requires).filter(
LogsDependencies.name == pkg).first()
if self.dependencies and '--resolve-off' not in self.flags:
print('\nDependencies:')
for dep in self.dependencies[0].split():
self.view_installed_packages(dep)
view.question(self.flags)
self.remove_packages()
self.delete_main_logs()
if self.dependencies and '--resolve-off' not in self.flags:
self.delete_deps_logs()
def remove_packages(self):
''' Run Slackware command to remove the packages. '''
for package in self.installed_packages:
command = f'{self.removepkg} {package}'
subprocess.call(command, shell=True)
def delete_main_logs(self):
''' Delete main packages from logs. '''
for pkg in self.packages:
self.session.query(LogsDependencies).filter(
LogsDependencies.name == pkg).delete()
self.session.commit()
def delete_deps_logs(self):
''' Delete depends packages from logs. '''
for pkg in self.dependencies[0].split():
self.session.query(LogsDependencies).filter(
LogsDependencies.name == pkg).delete()
self.session.commit()
def view_installed_packages(self, name):
''' View and creates list with packages for remove. '''
installed = os.listdir(self.log_packages)
color = self.color()
for package in installed:
if package.startswith(name) and self.repo_tag in package:
self.installed_packages.append(package)
print(f'[{color["RED"]} delete {color["ENDC"]}] -> {package}')