slpkg/slpkg/blacklist.py

146 lines
4.8 KiB
Python
Raw Normal View History

2014-09-29 07:41:57 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# blacklist.py file is part of slpkg.
# Copyright 2014 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
# All rights reserved.
# Utility for easy management packages in Slackware
# 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/>.
import os
2014-10-11 04:06:04 +02:00
2014-10-14 05:01:38 +02:00
from colors import RED, GREEN, ENDC
2014-09-29 07:41:57 +02:00
from __metadata__ import bls_path
2014-10-06 21:44:44 +02:00
2014-10-04 00:44:18 +02:00
class BlackList(object):
2014-10-14 05:01:38 +02:00
2014-09-29 07:41:57 +02:00
'''
2014-10-04 00:44:18 +02:00
Blacklist class to add, remove or listed packages
in blacklist file.
2014-09-29 07:41:57 +02:00
'''
2014-10-04 00:44:18 +02:00
def __init__(self):
'''
Initialization blacklist file if not exist in /etc/slpkg
create it by default.
'''
blacklist_conf = [
2014-10-14 05:01:38 +02:00
"# This is the blacklist file. Each package listed here may not be\n",
"# installed be upgraded be find or deleted.\n",
"# NOTE: The settings here affect all repositories.\n",
"#\n",
"# An example syntax is as follows:\n",
"# add a package from SBo repository:\n",
"# brasero\n",
"#\n",
"# Add package from slackware repository:\n",
"# example add package 'wicd-1.7.2.4-x86_64-4.txz':\n",
"# wicd\n",
"#\n",
"# Sometimes the automatic kernel update creates problems because you\n",
"# may need to file intervention 'lilo'. The slpkg automatically detects\n",
"# if the core has been upgraded and running 'lilo'. If you want to avoid\n",
"# any problems uncomment the lines below.\n",
"#\n",
"# kernel-firmware\n",
"# kernel-generic\n",
"# kernel-generic-smp\n",
"# kernel-headers\n",
"# kernel-huge\n",
"# kernel-huge-smp\n",
"# kernel-modules\n",
"# kernel-modules-smp\n",
"# kernel-source\n"
"#\n",
"#\n",
"# aaa_elflibs can't be updated.\n",
"aaa_elflibs\n"
]
2014-10-04 00:44:18 +02:00
self.blackfile = bls_path + "blacklist"
if not os.path.exists(bls_path):
os.mkdir(bls_path)
if not os.path.isfile(self.blackfile):
2014-10-04 07:39:17 +02:00
with open(self.blackfile, "w") as conf:
2014-10-04 00:44:18 +02:00
for line in blacklist_conf:
conf.write(line)
conf.close()
2014-10-14 05:01:38 +02:00
2014-10-04 00:44:18 +02:00
def packages(self):
'''
2014-10-14 05:01:38 +02:00
Return blacklist packages from /etc/slpkg/blacklist
2014-10-04 00:44:18 +02:00
configuration file.
'''
blacklist = []
with open(self.blackfile, "r") as black_conf:
for read in black_conf:
read = read.lstrip()
if not read.startswith("#"):
2014-10-14 05:01:38 +02:00
blacklist.append(read.replace("\n", ""))
2014-10-04 00:44:18 +02:00
black_conf.close()
return blacklist
2014-09-29 07:41:57 +02:00
2014-10-04 00:44:18 +02:00
def listed(self):
'''
Print blacklist packages
'''
exit = 0
print("\nPackages in blacklist:\n")
for black in self.packages():
if black:
2014-10-11 04:06:04 +02:00
print("{0}{1}{2}".format(GREEN, black, ENDC))
2014-09-29 07:41:57 +02:00
exit = 1
2014-10-04 00:44:18 +02:00
if exit == 1:
2014-10-14 05:01:38 +02: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):
'''
Add blacklist packages if not exist
'''
exit = 0
blacklist = self.packages()
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:
2014-10-11 04:06:04 +02:00
print("{0}{1}{2}".format(GREEN, pkg, ENDC))
2014-10-04 00:44:18 +02:00
black_conf.write(pkg + "\n")
exit = 1
2014-10-11 04:06:04 +02:00
black_conf.close()
2014-10-04 00:44:18 +02:00
if exit == 1:
2014-10-14 05:01:38 +02:00
print # new line at exit
2014-10-04 00:44:18 +02:00
def remove(self, pkgs):
'''
Remove packages from blacklist
'''
exit = 0
print("\nRemove packages from blacklist:\n")
2014-10-11 04:06:04 +02:00
with open(self.blackfile, "r") as black_conf:
lines = black_conf.read()
black_conf.close()
with open(self.blackfile, "w") as black_conf:
2014-10-04 00:44:18 +02:00
for line in lines.splitlines():
if line not in pkgs:
2014-10-11 04:06:04 +02:00
black_conf.write(line + "\n")
2014-10-04 00:44:18 +02:00
else:
2014-10-11 04:06:04 +02:00
print("{0}{1}{2}".format(RED, line, ENDC))
2014-10-04 00:44:18 +02:00
exit = 1
2014-10-11 04:06:04 +02:00
black_conf.close()
2014-10-04 00:44:18 +02:00
if exit == 1:
2014-10-14 05:01:38 +02:00
print # new line at exit