slpkg/slpkg/new_config.py

228 lines
7.4 KiB
Python
Raw Normal View History

2015-08-01 09:30:27 +03:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# new_config.py file is part of slpkg.
# Copyright 2014-2015 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
# 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 <http://www.gnu.org/licenses/>.
import os
2015-08-03 07:23:54 +03:00
import shutil
2015-08-03 09:07:42 +03:00
import itertools
2015-08-01 09:30:27 +03:00
2015-08-21 02:15:43 +03:00
from slpkg.messages import Msg
from slpkg.utils import Utils
from slpkg.__metadata__ import MetaData as _meta_
2015-08-01 09:30:27 +03:00
class NewConfig(object):
2015-08-04 01:06:04 +03:00
"""Manage .new configuration files
"""
2015-08-01 09:30:27 +03:00
def __init__(self):
self.meta = _meta_
2015-08-21 07:46:33 +03:00
self.msg = Msg()
2015-08-01 16:08:29 +03:00
self.red = self.meta.color["RED"]
2015-08-03 09:07:42 +03:00
self.green = self.meta.color["GREEN"]
2015-08-01 16:08:29 +03:00
self.endc = self.meta.color["ENDC"]
2015-08-03 07:23:54 +03:00
self.br = ""
if self.meta.use_colors in ["off", "OFF"]:
self.br = ")"
2015-08-04 00:49:38 +03:00
self.etc = "/etc/"
2015-08-01 09:30:27 +03:00
self.news = []
2015-08-04 01:31:51 +03:00
def run(self):
"""print .new configuration files
"""
self.find_new()
for n in self.news:
print("{0}".format(n))
print("")
2015-08-21 07:46:33 +03:00
self.msg.template(78)
2015-08-04 01:31:51 +03:00
print("| Installed {0} new configuration files:".format(
len(self.news)))
2015-08-21 07:46:33 +03:00
self.msg.template(78)
2015-08-04 01:31:51 +03:00
self.choices()
2015-08-01 09:30:27 +03:00
def find_new(self):
"""Find all '.new' files from /etc/ folder
and subfolders
"""
2015-08-01 16:08:29 +03:00
print("\nSearch for .new configuration files:\n")
for path, dirs, files in os.walk(self.etc):
2015-08-04 04:21:44 +03:00
del dirs # delete unsed
2015-08-01 09:30:27 +03:00
for f in files:
if f.endswith(".new"):
self.news.append(os.path.join(path, f))
2015-08-04 00:49:38 +03:00
if not self.news:
print(" No new configuration files\n")
raise SystemExit()
2015-08-01 09:30:27 +03:00
2015-08-01 16:08:29 +03:00
def choices(self):
"""Menu options for new configuration files
"""
2015-08-04 00:49:38 +03:00
print("| {0}K{1}{2}eep the old and .new files, no changes".format(
self.red, self.endc, self.br))
2015-08-03 07:23:54 +03:00
print("| {0}O{1}{2}verwrite all old configuration files with new "
"ones".format(self.red, self.endc, self.br))
print("| The old files will be saved with suffix .old")
print("| {0}R{1}{2}emove all .new files".format(
self.red, self.endc, self.br))
2015-08-04 00:49:38 +03:00
print("| {0}P{1}{2}rompt K, O, R, D, M option for each single "
"file".format(self.red, self.endc, self.br))
2015-08-21 07:46:33 +03:00
self.msg.template(78)
2015-08-03 17:00:16 +03:00
try:
2015-08-03 18:29:37 +03:00
choose = raw_input("\nWhat would you like to do [K/O/R/P]? ")
2015-09-15 16:01:06 +03:00
except EOFError:
2015-08-03 17:00:16 +03:00
print("")
raise SystemExit()
2015-08-04 00:49:38 +03:00
print("")
2015-08-03 18:29:37 +03:00
if choose in ("K", "k"):
2015-08-04 04:21:44 +03:00
self.keep()
2015-08-03 18:29:37 +03:00
elif choose in ("O", "o"):
2015-08-01 16:08:29 +03:00
self.overwrite_all()
2015-08-03 18:29:37 +03:00
elif choose in ("R", "r"):
2015-08-01 16:08:29 +03:00
self.remove_all()
2015-08-03 18:29:37 +03:00
elif choose in ("P", "p"):
2015-08-01 16:08:29 +03:00
self.prompt()
def overwrite_all(self):
2015-08-03 07:23:54 +03:00
"""Overwrite all .new files and keep
old with suffix .old
"""
for n in self.news:
2015-08-03 17:00:16 +03:00
self._overwrite(n)
2015-08-01 16:08:29 +03:00
def remove_all(self):
2015-08-03 07:23:54 +03:00
"""Remove all .new files
"""
for n in self.news:
2015-08-04 00:49:38 +03:00
self._remove(n)
print("")
2015-08-01 16:08:29 +03:00
def prompt(self):
2015-08-03 17:00:16 +03:00
"""Select file
"""
2015-08-21 07:46:33 +03:00
self.msg.template(78)
2015-08-03 07:23:54 +03:00
print("| Choose what to do file by file:")
2015-08-14 14:16:39 +03:00
print("| {0}K{1}{2}eep, {3}O{4}{5}verwrite, {6}R{7}{8}emove, "
2015-08-03 07:23:54 +03:00
"{9}D{10}{11}iff, {12}M{13}{14}erge".format(
self.red, self.endc, self.br, self.red, self.endc, self.br,
self.red, self.endc, self.br, self.red, self.endc, self.br,
self.red, self.endc, self.br))
2015-08-21 07:46:33 +03:00
self.msg.template(78)
2015-08-03 18:29:37 +03:00
print("")
2015-08-03 17:00:16 +03:00
self.i = 0
try:
while self.i < len(self.news):
self.question(self.news[self.i])
self.i += 1
2015-09-15 16:01:06 +03:00
except EOFError:
2015-08-03 17:00:16 +03:00
print("")
raise SystemExit()
2015-08-03 07:23:54 +03:00
def question(self, n):
2015-08-03 17:00:16 +03:00
"""Choose what do to file by file
"""
2015-08-04 00:49:38 +03:00
print("")
2015-08-03 07:23:54 +03:00
prompt_ask = raw_input("{0} [K/O/R/D/M]? ".format(n))
2015-08-03 18:29:37 +03:00
print("")
if prompt_ask in ("K", "k"):
2015-08-03 17:00:16 +03:00
self.keep()
2015-08-03 18:29:37 +03:00
elif prompt_ask in ("O", "o"):
2015-08-03 07:23:54 +03:00
self._overwrite(n)
2015-08-03 18:29:37 +03:00
elif prompt_ask in ("R", "r"):
2015-08-03 17:00:16 +03:00
self._remove(n)
2015-08-03 18:29:37 +03:00
elif prompt_ask in ("D", "d"):
2015-08-03 17:00:16 +03:00
self.diff(n)
self.i -= 1
2015-08-03 18:29:37 +03:00
elif prompt_ask in ("M", "m"):
self.merge(n)
2015-08-01 16:08:29 +03:00
2015-08-03 17:00:16 +03:00
def _remove(self, n):
"""Remove one single file
"""
if os.path.isfile(n):
os.remove(n)
2015-08-04 00:49:38 +03:00
if not os.path.isfile(n):
print("File '{0}' removed".format(n))
2015-08-01 16:08:29 +03:00
2015-08-03 07:23:54 +03:00
def _overwrite(self, n):
2015-08-03 17:00:16 +03:00
"""Overwrite old file with new and keep file with suffix .old
"""
2015-08-03 07:23:54 +03:00
if os.path.isfile(n[:-4]):
shutil.copy2(n[:-4], n[:-4] + ".old")
2015-08-04 00:49:38 +03:00
print("Old file {0} saved as {1}.old".format(
n[:-4].split("/")[-1], n[:-4].split("/")[-1]))
2015-08-03 07:23:54 +03:00
if os.path.isfile(n):
shutil.move(n, n[:-4])
2015-08-04 00:49:38 +03:00
print("New file {0} overwrite as {1}".format(
n.split("/")[-1], n[:-4].split("/")[-1]))
2015-08-03 07:23:54 +03:00
def keep(self):
2015-08-01 16:08:29 +03:00
pass
2015-08-03 09:07:42 +03:00
def diff(self, n):
"""Print the differences between the two files
"""
2015-08-03 17:00:16 +03:00
if os.path.isfile(n[:-4]):
diff1 = Utils().read_file(n[:-4]).splitlines()
if os.path.isfile(n):
diff2 = Utils().read_file(n).splitlines()
lines, l, c = [], 0, 0
2015-08-03 09:07:42 +03:00
for a, b in itertools.izip_longest(diff1, diff2):
l += 1
if a != b:
2015-08-03 18:29:37 +03:00
for s1, s2 in itertools.izip_longest(str(a), str(b)):
2015-08-03 09:07:42 +03:00
c += 1
if s1 != s2:
break
2015-08-04 00:49:38 +03:00
print("@@ -{0},{1} +{2},{3} @@\n".format(l, c, l, c))
2015-08-03 09:07:42 +03:00
for line in lines[-3:]:
print("{0}".format(line))
2015-08-04 00:49:38 +03:00
if a is None:
a = ""
print("{0}{1}{2}{3}".format(self.red, "-", self.endc, a))
if b is None:
b = ""
print("{0}{1}{2}{3}".format(self.green, "+", self.endc, b))
2015-08-03 09:07:42 +03:00
lines = []
c = 0
else:
lines.append(a)
2015-08-03 18:29:37 +03:00
def merge(self, n):
2015-08-04 00:49:38 +03:00
"""Merge new file into old
"""
2015-08-04 01:38:26 +03:00
if os.path.isfile(n[:-4]):
old = Utils().read_file(n[:-4]).splitlines()
if os.path.isfile(n):
new = Utils().read_file(n).splitlines()
2015-08-03 18:29:37 +03:00
with open(n[:-4], "w") as out:
for l1, l2 in itertools.izip_longest(old, new):
2015-08-04 00:49:38 +03:00
if l1 is None:
l1 = ""
if l2 is None:
l2 = ""
2015-08-03 18:29:37 +03:00
if l1 != l2:
out.write(l2 + "\n")
else:
out.write(l1 + "\n")
2015-08-04 00:49:38 +03:00
print("The file {0} merged in file {1}".format(
n.split("/")[-1], n[:-4].split("/")[-1]))