Added configs command

This commit is contained in:
Dimitris Zlatanidis 2022-12-28 00:04:58 +02:00
parent a811fbbb82
commit dcd950b1d4
7 changed files with 96 additions and 30 deletions

View file

@ -1,3 +1,7 @@
4.4.1 - 28/12/2022
Added:
- configs command to read and edit configuration file
4.4.0 - 23/12/2022
Added:
- New command to tracking the dependencies

View file

@ -4,7 +4,7 @@
slpkg - [OPTIONS] [COMMAND] <packages>
.SH SYNAPSES
.P
slpkg [-h|-v] [update] [upgrade] [check-updates] [clean-logs] [clean-tmp] [-b, build] [-i, install] [-d, download]
slpkg [-h|-v] [update] [upgrade] [check-updates] [configs] [clean-logs] [clean-tmp] [-b, build] [-i, install] [-d, download]
[-r, remove] [-f, find] [-w, view] [-s, search] [-e, dependees] [-t, tracking] --yes, --jobs, --resolve-off,
--reinstall, --skip-installed, --full-reverse, --search
.SH DESCRIPTION
@ -32,6 +32,11 @@ check-updates
Check if there is any news on the SlackBuild's ChangeLog.txt file.
.RE
.P
configs
.RS
Edit the configuration /etc/slpkg/slpkg.toml file.
.RE
.P
clean-logs
.RS
Cleans dependencies log tracking. After that procedure you should remove dependencies by hand.

View file

@ -57,10 +57,10 @@ class Configs:
# Overwrite with user configuration.
config_file: str = f'{etc_path}/{prog_name}.toml'
if os.path.isfile(config_file):
with open(config_file, 'rb') as conf:
configs = tomli.load(conf)
try:
with open(config_file, 'rb') as conf:
configs = tomli.load(conf)
config = configs['configs']
# OS architecture by default
@ -96,8 +96,8 @@ class Configs:
# Dialog utility
dialog: str = config['dialog']
except KeyError as error:
print(f"Error: {error}: in the configuration file "
except (KeyError, tomli.TOMLDecodeError) as error:
raise SystemExit(f"\nError: {error}: in the configuration file "
"'/etc/slpkg/slpkg.toml'\n")
# Creating the paths if not exists

View file

@ -1,6 +1,8 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import tomli
import locale
from dialog import Dialog
@ -28,3 +30,47 @@ class DialogBox:
tags = packages
return code, tags
def form_configs(self):
""" Read and write the configuration file. """
elements = []
config_file: str = f'{self.configs.etc_path}/{self.configs.prog_name}.toml'
if os.path.isfile(config_file):
# Load the toml config file.
try:
with open(config_file, 'rb') as conf:
configs = tomli.load(conf)
except (KeyError, tomli.TOMLDecodeError) as error:
raise SystemExit(f"\nError: {error}: in the configuration file "
"'/etc/slpkg/slpkg.toml'\n")
# Creating the elements for the dialog form.
for i, (key, value) in enumerate(configs['configs'].items(), start=1):
if value is True:
value = 'true'
elif value is False:
value = 'false'
elements += [
(key, i, 1, value, i, 17, 47, 200, '0x0')
]
text = f'Edit the configuration file: {config_file}.'
title = ' Configuration File '
code, tags = self.d.mixedform(text=text, title=title, elements=elements,
height=30, width=70)
os.system('clear')
if code == 'ok':
# Read the original config file.
with open(config_file, 'r') as toml_file:
orig_config = toml_file.readlines()
# Write the new values to the config file.
with open(config_file, 'w') as patch_toml:
for line in orig_config:
for key, value in zip(configs['configs'].keys(), tags):
if line.lstrip().startswith(key):
line = f' {key} = "{value}"\n'
if line.lstrip().startswith(('colors =', 'dialog =')):
line = line.replace('"', '')
patch_toml.write(line)

View file

@ -185,6 +185,35 @@ class Argparse:
raise SystemExit()
self.usage.help(1)
def edit_configs(self):
if len(self.args) == 1 and not self.flags:
self.dialog.form_configs()
raise SystemExit()
self.usage.help(1)
def clean_logs(self):
if [f for f in self.flags if f not in [self.flag_yes]]:
self.usage.help(1)
if len(self.args) == 1:
self.check.database()
logs = CleanLogsDependencies(self.flags)
logs.clean()
raise SystemExit()
self.usage.help(1)
def clean_tmp(self):
if len(self.args) == 1 and not self.flags:
path = self.configs.tmp_path
tmp_slpkg = self.configs.tmp_slpkg
folder = self.configs.prog_name
self.utils.remove_folder_if_exists(path, folder)
self.utils.create_folder(tmp_slpkg, 'build')
raise SystemExit()
self.usage.help(1)
def build(self):
if [f for f in self.flags if f not in [self.flag_yes,
self.flag_jobs,
@ -360,29 +389,6 @@ class Argparse:
raise SystemExit()
self.usage.help(1)
def clean_logs(self):
if [f for f in self.flags if f not in [self.flag_yes]]:
self.usage.help(1)
if len(self.args) == 1:
self.check.database()
logs = CleanLogsDependencies(self.flags)
logs.clean()
raise SystemExit()
self.usage.help(1)
def clean_tmp(self):
if len(self.args) == 1 and not self.flags:
path = self.configs.tmp_path
tmp_slpkg = self.configs.tmp_slpkg
folder = self.configs.prog_name
self.utils.remove_folder_if_exists(path, folder)
self.utils.create_folder(tmp_slpkg, 'build')
raise SystemExit()
self.usage.help(1)
def main():
args = sys.argv
@ -398,6 +404,7 @@ def main():
'update': argparse.update,
'upgrade': argparse.upgrade,
'check-updates': argparse.check_updates,
'configs': argparse.edit_configs,
'clean-logs': argparse.clean_logs,
'clean-tmp': argparse.clean_tmp,
'build': argparse.build,

View file

@ -3,6 +3,7 @@
import os
import tomli
import shutil
import tarfile
@ -61,3 +62,4 @@ class Utilities:
tag = ''.join(package[len(name + version + arch + build) + 4:].split('-'))
return [name, version, arch, build, tag]

View file

@ -23,7 +23,8 @@ class Usage:
f'Usage: {Configs.prog_name} [{self.yellow}OPTIONS{self.endc}] [{self.cyan}COMMAND{self.endc}] <packages>\n'
f'\n slpkg [{self.yellow}OPTIONS{self.endc}] [--yes, --jobs, --resolve-off, --reinstall]\n'
f' slpkg [{self.yellow}OPTIONS{self.endc}] [--skip-installed, --full-reverse, --search]\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [update, upgrade, check-updates, clean-logs, clean-tmp]\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [update, upgrade, check-updates, configs]\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [clean-logs, clean-tmp]\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [-b, build, -i, install, -d, download, -r, remove] <packages>\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [-f, find, -w, view, -s, search, -e, dependees] <packages>\n'
f' slpkg [{self.cyan}COMMAND{self.endc}] [-t, tracking] <packages>\n'
@ -42,6 +43,7 @@ class Usage:
f' {self.red}update{self.endc} Update the package lists.\n'
f' {self.cyan}upgrade{self.endc} Upgrade all the packages.\n'
f' {self.cyan}check-updates{self.endc} Check for news on ChangeLog.txt.\n'
f' {self.cyan}configs{self.endc} Edit the configuration file.\n'
f' {self.cyan}clean-logs{self.endc} Clean dependencies log tracking.\n'
f' {self.cyan}clean-tmp{self.endc} Delete all the downloaded sources.\n'
f' {self.cyan}-b, build{self.endc} <packages> Build only the packages.\n'