From f44d4595c0db8c4f5604459aa2e03506bad29f58 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Thu, 16 Jul 2015 02:03:52 +0300 Subject: [PATCH] Added health check installed packages --- README.rst | 5 ++- man/slpkg.8 | 9 +++- slpkg/arguments.py | 2 + slpkg/health.py | 107 +++++++++++++++++++++++++++++++++++++++++++++ slpkg/main.py | 19 ++++++-- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 slpkg/health.py diff --git a/README.rst b/README.rst index decb0057..b6fdedfe 100644 --- a/README.rst +++ b/README.rst @@ -250,9 +250,9 @@ Using pip: Bbinary packages: -Slackware: `slpkg-2.6.1-i486-1_dsw.txz `_ +Slackware: `slpkg-2.6.2-i486-1_dsw.txz `_ -Slackware64: `slpkg-2.6.1-x86_64-1_dsw.txz `_ +Slackware64: `slpkg-2.6.2-x86_64-1_dsw.txz `_ Upgrade @@ -341,6 +341,7 @@ Command Line Tool Usage repository. update slpkg Upgrade the program directly from repository. + health, --silent Health check installed packages. Optional arguments: -h, --help Print this help message and exit. diff --git a/man/slpkg.8 b/man/slpkg.8 index 37fa6aad..4659ddd1 100644 --- a/man/slpkg.8 +++ b/man/slpkg.8 @@ -19,7 +19,8 @@ Usage: slpkg Commands: [update, --only=[...]] [upgrade, --only=[...]] [repo-add [repository name] [URL]] [repo-remove [repository]] [repo-list] - [repo-info [repository]] [update [slpkg]] + [repo-info [repository]] [update [slpkg]] + [health, --silent] Optional arguments: [-h] [-v] @@ -93,6 +94,12 @@ View repository information. .PP You can check for new versions and update slpkg itself. +.SS health, health check installed packages +\fBslpkg\fP \fBhealth\fP, \fB--silent\fP +.PP +This command get each file from "/var/log/packages/" directory and read file list from +package to check if installed. + .SH OPTIONS .PP The following arguments are available. diff --git a/slpkg/arguments.py b/slpkg/arguments.py index a4a0d506..dc834c58 100644 --- a/slpkg/arguments.py +++ b/slpkg/arguments.py @@ -54,6 +54,7 @@ Commands: repository. update slpkg Upgrade the program directly from repository. + health, --silent Health check installed packages. Optional arguments: -h, --help Print this help message and exit. @@ -127,6 +128,7 @@ def usage(repo): [repo-add [repository name] [URL]] [repo-remove [repository]] [repo-list] [repo-info [repository]] [update [slpkg]] + [health, --silent] Optional arguments: [-h] [-v] diff --git a/slpkg/health.py b/slpkg/health.py new file mode 100644 index 00000000..5eb25782 --- /dev/null +++ b/slpkg/health.py @@ -0,0 +1,107 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# health.py file is part of slpkg. + +# Copyright 2014-2015 Dimitris Zlatanidis +# All rights reserved. + +# Slpkg is a user-friendly package manager for Slackware installations + +# https://github.com/dslackw/slpkg + +# Slpkg 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. +# This program 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 this program. If not, see . + + +import os +import sys + +from messages import Msg +from __metadata__ import MetaData as _meta_ + +from pkg.find import find_package + + +class PackageHealth(object): + """Health check installed packages + """ + def __init__(self, mode): + self.mode = mode + self.meta = _meta_ + self.pkg_path = _meta_.pkg_path + self.installed = [] + self.cn = 0 + + def packages(self): + """Get all installed packages from /var/log/packages/ path + """ + self.installed = find_package("", self.pkg_path) + + def check(self, line, pkg): + line = line.replace("\n", "") + try: + if (not line.endswith("/") and + not line.endswith(".new") and + not line.startswith("dev/") and + not line.startswith("install/") and + "/incoming/" not in line): + if not os.path.isfile("/" + line): + self.cn += 1 + print("Not installed: {0}/{1}{2} --> {3}".format( + self.meta.color["RED"], line, self.meta.color["ENDC"], + pkg)) + elif not self.mode: + print(line) + except (KeyboardInterrupt, IOError): + print("") + sys.exit(0) + + def test(self): + """Get started test each package and read file list + """ + self.packages() + self.cf = 0 + try: + for pkg in self.installed: + self.lf = 0 + with open(self.pkg_path + pkg, "r") as fopen: + for line in fopen: + self.cf += 1 # count all files + self.lf += 1 # count each package files + if self.lf > 19: + self.check(line, pkg) + except KeyboardInterrupt: + print("") + sys.exit(0) + self.results() + + def results(self): + """Print results + """ + print("") + per = int(round((float(self.cf) / (self.cf + self.cn)) * 100)) + if per > 90: + color = self.meta.color["GREEN"] + elif per < 90 and per > 60: + color = self.meta.color["YELLOW"] + elif per < 60: + color = self.meta.color["RED"] + health = "{0}{1}%{2}".format(color, str(per), self.meta.color["ENDC"]) + Msg().template(78) + print("| {0}{1}{2}{3}{4}".format( + "Total files", " " * 7, "Not installed", " " * 40, "Health")) + Msg().template(78) + print("| {0}{1}{2}{3}{4:>4}".format( + self.cf, " " * (18-len(str(self.cf))), + self.cn, " " * (55-len(str(self.cn))), + health)) + Msg().template(78) diff --git a/slpkg/main.py b/slpkg/main.py index 8454b9a4..ff14b54f 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -27,9 +27,9 @@ import sys import getpass from load import Regex +from desc import PkgDesc from messages import Msg from auto_pkg import Auto -from desc import PkgDesc from config import Config from checks import Updates from queue import QueuePkgs @@ -39,6 +39,7 @@ from repositories import Repo from tracking import track_dep from blacklist import BlackList from version import prog_version +from health import PackageHealth from pkg_find import find_from_repos from arguments import options, usage from slpkg_update import it_self_update @@ -70,8 +71,8 @@ class ArgParse(object): # checking if repositories exists if len(self.args) > 1 and self.args[0] not in [ "-h", "--help", "-v", "--version", "upgrade", "repo-list", - "repo-add", "repo-remove", "update", "update-slpkg", "-g", - "--config" + "repo-add", "repo-remove", "update", "update-slpkg", + "health", "-g", "--config" ]: check_exists_repositories() @@ -159,6 +160,17 @@ class ArgParse(object): else: usage("") + def command_health(self): + """Check package health + """ + if len(self.args) == 1 and self.args[0] == "health": + PackageHealth(mode="").test() + elif (len(self.args) == 2 and self.args[0] == "health" and + self.args[1] == "--silent"): + PackageHealth(mode=self.args[1]).test() + else: + usage("") + def auto_build(self): """Auto built tool """ @@ -521,6 +533,7 @@ def main(): "repo-add": argparse.command_repo_add, "repo-remove": argparse.command_repo_remove, "repo-info": argparse.command_repo_info, + "health": argparse.command_health, "-a": argparse.auto_build, "--autobuild": argparse.auto_build, "-l": argparse.pkg_list,