Updated for repositories file

This commit is contained in:
Dimitris Zlatanidis 2023-03-28 09:15:55 +03:00
parent d22c3709a5
commit ae54803e25

View file

@ -14,8 +14,10 @@ class NewConfig:
self.flags: list = flags
self.etc_path: str = '/etc/slpkg'
self.slpkg_config = Path(self.etc_path, 'slpkg.toml')
self.slpkg_repositories = Path(self.etc_path, 'repositories.toml')
self.blacklist_config = Path(self.etc_path, 'blacklist.toml')
self.slpkg_config_new = Path(self.etc_path, 'slpkg.toml.new')
self.repositories_new = Path(self.etc_path, 'repositories.toml.new')
self.blacklist_config_new = Path(self.etc_path, 'blacklist.toml.new')
self.color: dict = {}
@ -59,12 +61,15 @@ class NewConfig:
def check(self):
""" Checks for .new files. """
print('Checking for NEW configuration files...')
if self.slpkg_config_new.is_file() or self.blacklist_config_new.is_file():
if self.slpkg_config_new.is_file() or self.blacklist_config_new.is_file() or self.repositories_new.is_file():
print('\nThere are NEW files:\n')
if self.slpkg_config_new.is_file():
print(f"{self.bgreen:>12}{self.slpkg_config_new}{self.endc}")
if self.repositories_new.is_file():
print(f"{self.bgreen:>12}{self.repositories_new}{self.endc}")
if self.blacklist_config_new.is_file():
print(f"{self.bgreen:>12}{self.blacklist_config_new}{self.endc}")
@ -109,6 +114,9 @@ class NewConfig:
if self.slpkg_config_new.is_file():
self.overwrite_config_file()
if self.repositories_new.is_file():
self.overwrite_repositories_file()
if self.blacklist_config_new.is_file():
self.overwrite_blacklist_file()
print() # new line
@ -122,6 +130,15 @@ class NewConfig:
shutil.move(self.slpkg_config_new, self.slpkg_config)
print(f"mv {self.slpkg_config_new} -> {self.green}{self.slpkg_config}{self.endc}")
def overwrite_repositories_file(self):
""" Copy tne repositories.toml.new file and rename the old to .orig. """
if self.slpkg_config.is_file():
shutil.copy(self.slpkg_repositories, f"{self.slpkg_repositories}.orig")
print(f"\ncp {self.green}{self.slpkg_repositories}{self.endc} -> {self.slpkg_repositories}.orig")
shutil.move(self.repositories_new, self.slpkg_repositories)
print(f"mv {self.repositories_new} -> {self.green}{self.slpkg_repositories}{self.endc}")
def overwrite_blacklist_file(self):
""" Copy tne blacklist.toml.new file and rename the old to .orig. """
if self.blacklist_config.is_file():
@ -134,17 +151,24 @@ class NewConfig:
def remove(self):
""" Removes the .new files. """
print() # new line
self.remove_config_file()
self.remove_blacklist_file()
self.remove_config_new_file()
self.remove_repositories_new_file()
self.remove_blacklist_new_file()
print() # new line
def remove_config_file(self):
def remove_config_new_file(self):
""" Remove slpkg.toml.new file. """
if self.slpkg_config_new.is_file():
self.slpkg_config_new.unlink()
print(f"rm {self.red}{self.slpkg_config_new}{self.endc}")
def remove_blacklist_file(self):
def remove_repositories_new_file(self):
""" Remove repositories.toml.new file. """
if self.repositories_new.is_file():
self.repositories_new.unlink()
print(f"rm {self.red}{self.repositories_new}{self.endc}")
def remove_blacklist_new_file(self):
""" Remove blacklist.toml.new file. """
if self.blacklist_config_new.is_file():
self.blacklist_config_new.unlink()
@ -169,13 +193,33 @@ class NewConfig:
print() # new line
if make.lower() == 'r':
print() # new line
self.remove_config_file()
self.remove_config_new_file()
print() # new line
if make.lower() == 'd':
self.diff_files(self.slpkg_config_new, self.slpkg_config)
if make.lower() == 'v':
self.vimdiff(self.slpkg_config_new, self.slpkg_config)
if self.repositories_new.is_file():
make = input(f'{self.bgreen}{self.repositories_new}{self.endc} - '
f'({self.byellow}K{self.endc}/{self.byellow}O{self.endc}/'
f'{self.byellow}R{self.endc}/{self.byellow}D{self.endc}/'
f'{self.byellow}V{self.endc}): ')
if make.lower() == 'k':
pass
if make.lower() == 'o':
self.overwrite_repositories_file()
print() # new line
if make.lower() == 'r':
print() # new line
self.remove_repositories_new_file()
print() # new line
if make.lower() == 'd':
self.diff_files(self.repositories_new, self.slpkg_repositories)
if make.lower() == 'v':
self.vimdiff(self.repositories_new, self.slpkg_repositories)
if self.blacklist_config_new.is_file():
make = input(f'{self.bgreen}{self.blacklist_config_new}{self.endc} - '
f'({self.byellow}K{self.endc}/{self.byellow}O{self.endc}/'
@ -189,7 +233,7 @@ class NewConfig:
print() # new line
if make.lower() == 'r':
print() # new line
self.remove_blacklist_file()
self.remove_blacklist_new_file()
print() # new line
if make.lower() == 'd':
self.diff_files(self.blacklist_config_new, self.blacklist_config)
@ -197,7 +241,7 @@ class NewConfig:
self.vimdiff(self.blacklist_config_new, self.blacklist_config)
@staticmethod
def diff_files(file1, file2):
def diff_files(file2, file1):
""" Diff the .new and the current file. """
with open(file1, 'r') as f1:
with open(file2, 'r') as f2: