2014-09-29 07:41:57 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# blacklist.py file is part of slpkg.
|
|
|
|
|
2015-05-21 01:00:07 +02:00
|
|
|
# Copyright 2014-2015 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
|
2014-09-29 07:41:57 +02:00
|
|
|
# All rights reserved.
|
|
|
|
|
2014-12-19 05:57:56 +01:00
|
|
|
# Slpkg is a user-friendly package manager for Slackware installations
|
2014-09-29 07:41:57 +02:00
|
|
|
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-05-28 05:07:13 +02:00
|
|
|
|
2015-01-31 01:47:36 +01:00
|
|
|
from utils import Utils
|
2015-06-18 13:42:33 +02:00
|
|
|
from splitting import split_package
|
2015-06-07 07:26:42 +02:00
|
|
|
from __metadata__ import MetaData as _meta_
|
2014-09-29 07:41:57 +02:00
|
|
|
|
2014-10-06 21:44:44 +02:00
|
|
|
|
2014-10-04 00:44:18 +02:00
|
|
|
class BlackList(object):
|
2015-06-19 05:38:13 +02:00
|
|
|
"""Blacklist class to add, remove or listed packages
|
|
|
|
in blacklist file."""
|
2014-10-04 00:44:18 +02:00
|
|
|
def __init__(self):
|
2015-06-08 05:28:49 +02:00
|
|
|
self.meta = _meta_
|
2014-10-21 07:50:59 +02:00
|
|
|
self.quit = False
|
2014-11-22 11:01:32 +01:00
|
|
|
self.blackfile = "/etc/slpkg/blacklist"
|
2015-01-31 01:47:36 +01:00
|
|
|
self.black_conf = Utils().read_file(self.blackfile)
|
2014-10-16 04:10:24 +02:00
|
|
|
|
2015-06-18 13:42:33 +02:00
|
|
|
def get_black(self):
|
2015-06-19 05:38:13 +02:00
|
|
|
"""Return blacklist packages from /etc/slpkg/blacklist
|
|
|
|
configuration file."""
|
2014-10-04 00:44:18 +02:00
|
|
|
blacklist = []
|
2014-10-16 04:10:24 +02:00
|
|
|
for read in self.black_conf.splitlines():
|
|
|
|
read = read.lstrip()
|
|
|
|
if not read.startswith("#"):
|
|
|
|
blacklist.append(read.replace("\n", ""))
|
2014-10-04 00:44:18 +02:00
|
|
|
return blacklist
|
2014-09-29 07:41:57 +02:00
|
|
|
|
2014-10-04 00:44:18 +02:00
|
|
|
def listed(self):
|
2015-06-19 05:38:13 +02:00
|
|
|
"""Print blacklist packages
|
2015-06-05 13:42:52 +02:00
|
|
|
"""
|
2014-10-04 00:44:18 +02:00
|
|
|
print("\nPackages in blacklist:\n")
|
2015-06-18 13:42:33 +02:00
|
|
|
for black in self.get_black():
|
2014-10-04 00:44:18 +02:00
|
|
|
if black:
|
2015-06-08 05:28:49 +02:00
|
|
|
print("{0}{1}{2}".format(self.meta.color["GREEN"], black,
|
|
|
|
self.meta.color["ENDC"]))
|
2014-10-21 07:50:59 +02:00
|
|
|
self.quit = True
|
|
|
|
if self.quit:
|
2014-11-13 12:32:40 +01:00
|
|
|
print("") # new line at exit
|
2014-09-29 07:41:57 +02:00
|
|
|
|
2014-10-04 00:44:18 +02:00
|
|
|
def add(self, pkgs):
|
2015-06-19 05:38:13 +02:00
|
|
|
"""Add blacklist packages if not exist
|
2015-06-05 13:42:52 +02:00
|
|
|
"""
|
2015-06-18 13:42:33 +02:00
|
|
|
blacklist = self.get_black()
|
2014-10-11 04:06:04 +02:00
|
|
|
pkgs = set(pkgs)
|
2014-10-04 00:44:18 +02:00
|
|
|
print("\nAdd packages in blacklist:\n")
|
|
|
|
with open(self.blackfile, "a") as black_conf:
|
|
|
|
for pkg in pkgs:
|
|
|
|
if pkg not in blacklist:
|
2015-06-08 05:28:49 +02:00
|
|
|
print("{0}{1}{2}".format(self.meta.color["GREEN"], pkg,
|
|
|
|
self.meta.color["ENDC"]))
|
2014-10-04 00:44:18 +02:00
|
|
|
black_conf.write(pkg + "\n")
|
2014-10-21 07:50:59 +02:00
|
|
|
self.quit = True
|
2014-10-11 04:06:04 +02:00
|
|
|
black_conf.close()
|
2014-10-21 07:50:59 +02:00
|
|
|
if self.quit:
|
2014-11-13 12:32:40 +01:00
|
|
|
print("") # new line at exit
|
2014-10-04 00:44:18 +02:00
|
|
|
|
|
|
|
def remove(self, pkgs):
|
2015-06-19 05:38:13 +02:00
|
|
|
"""Remove packages from blacklist
|
2015-06-05 13:42:52 +02:00
|
|
|
"""
|
2014-10-04 00:44:18 +02:00
|
|
|
print("\nRemove packages from blacklist:\n")
|
2014-10-16 04:10:24 +02:00
|
|
|
with open(self.blackfile, "w") as remove:
|
|
|
|
for line in self.black_conf.splitlines():
|
2014-10-04 00:44:18 +02:00
|
|
|
if line not in pkgs:
|
2014-10-16 04:10:24 +02:00
|
|
|
remove.write(line + "\n")
|
2014-10-04 00:44:18 +02:00
|
|
|
else:
|
2015-06-08 05:28:49 +02:00
|
|
|
print("{0}{1}{2}".format(self.meta.color["RED"], line,
|
|
|
|
self.meta.color["ENDC"]))
|
2014-10-21 07:50:59 +02:00
|
|
|
self.quit = True
|
2014-10-16 04:10:24 +02:00
|
|
|
remove.close()
|
2014-10-21 07:50:59 +02:00
|
|
|
if self.quit:
|
2014-11-13 12:32:40 +01:00
|
|
|
print("") # new line at exit
|
2015-06-18 13:42:33 +02:00
|
|
|
|
|
|
|
def packages(self, pkgs, repo):
|
2015-06-19 05:38:13 +02:00
|
|
|
"""Return packages in blacklist or by repository
|
|
|
|
"""
|
2015-06-25 19:01:13 +02:00
|
|
|
self.black = []
|
2015-06-18 13:42:33 +02:00
|
|
|
for bl in self.get_black():
|
2015-06-19 05:38:13 +02:00
|
|
|
pr = bl.split(":")
|
2015-06-18 13:42:33 +02:00
|
|
|
for pkg in pkgs:
|
2015-06-25 19:01:13 +02:00
|
|
|
self.__priority(pr, repo, pkg)
|
|
|
|
self.__blackpkg(bl, repo, pkg)
|
|
|
|
return self.black
|
2015-06-25 04:27:33 +02:00
|
|
|
|
2015-06-25 05:07:01 +02:00
|
|
|
def __add(self, repo, pkg):
|
2015-06-25 04:27:33 +02:00
|
|
|
"""Split packages by repository
|
|
|
|
"""
|
2015-06-25 05:07:01 +02:00
|
|
|
if repo == "sbo":
|
|
|
|
return pkg
|
|
|
|
else:
|
|
|
|
return split_package(pkg)[0]
|
2015-06-25 19:01:13 +02:00
|
|
|
|
|
|
|
def __priority(self, pr, repo, pkg):
|
|
|
|
"""Add packages in blacklist by priority
|
|
|
|
"""
|
|
|
|
if (pr[0] == repo and pr[1].startswith("*") and
|
|
|
|
pr[1].endswith("*")):
|
|
|
|
if pr[1][1:-1] in pkg:
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
elif pr[0] == repo and pr[1].endswith("*"):
|
|
|
|
if pkg.startswith(pr[1][:-1]):
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
elif pr[0] == repo and pr[1].startswith("*"):
|
|
|
|
if pkg.endswith(pr[1][1:]):
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
elif pr[0] == repo and "*" not in pr[1]:
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
|
|
|
|
def __blackpkg(self, bl, repo, pkg):
|
|
|
|
"""Add packages in blacklist
|
|
|
|
"""
|
|
|
|
if bl.startswith("*") and bl.endswith("*"):
|
|
|
|
if bl[1:-1] in pkg:
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
elif bl.endswith("*"):
|
|
|
|
if pkg.startswith(bl[:-1]):
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
elif bl.startswith("*"):
|
|
|
|
if pkg.endswith(bl[1:]):
|
|
|
|
self.black.append(self.__add(repo, pkg))
|
|
|
|
if bl not in self.black and "*" not in bl:
|
|
|
|
self.black.append(bl)
|