Switch to pathlib

This commit is contained in:
Dimitris Zlatanidis 2022-12-30 17:40:11 +02:00
parent ccfcef3468
commit 983abeff98
11 changed files with 47 additions and 35 deletions

View file

@ -3,6 +3,7 @@
import os
import urllib3
from pathlib import Path
from slpkg.configs import Configs
@ -18,8 +19,8 @@ class CheckUpdates:
print(end='\rChecking for news in the Changelog.txt file... ')
local_date = 0
local_chg_txt = os.path.join(self.configs.sbo_repo_path,
self.configs.sbo_chglog_txt)
local_chg_txt = Path(self.configs.sbo_repo_path,
self.configs.sbo_chglog_txt)
http = urllib3.PoolManager()
repo = http.request(

View file

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import os
from pathlib import Path
from slpkg.configs import Configs
from slpkg.queries import SBoQueries
@ -73,7 +74,7 @@ class Check:
def database(self):
""" Checking for empty table """
db = os.path.join(self.configs.db_path, self.configs.database)
db = Path(self.configs.db_path, self.configs.database)
if not SBoQueries('').sbos() or not os.path.isfile(db):
raise SystemExit('\nYou need to update the package lists first.\n'
'Please run slpkg update.\n')

View file

@ -1,8 +1,9 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import hashlib
from pathlib import Path
from typing import Union
from slpkg.views.views import ViewMessage
@ -13,9 +14,9 @@ class Md5sum:
def __init__(self, flags: list):
self.flags = flags
def check(self, path: str, source: str, checksum: str, name: str):
def check(self, path: Union[str, Path], source: str, checksum: str, name: str):
""" Checksum the source. """
source_file = os.path.join(path, source.split('/')[-1])
source_file = Path(path, source.split('/')[-1])
md5 = self.read_file(source_file)
@ -30,7 +31,7 @@ class Md5sum:
view.question()
@staticmethod
def read_file(filename: str) -> bytes:
def read_file(filename: Union[str, Path]) -> bytes:
""" Reads the text file. """
with open(filename, 'rb') as f:
return f.read()

View file

@ -5,6 +5,7 @@ import os
import tomli
import platform
from pathlib import Path
from dataclasses import dataclass
@ -13,7 +14,7 @@ class LoadConfigs:
def file(path: str, file: str) -> dict:
try:
""" Load the configs from the file. """
config_file = os.path.join(path, f'{file}.toml')
config_file = Path(path, f'{file}.toml')
if os.path.isfile(config_file):
with open(config_file, 'rb') as conf:
return tomli.load(conf)
@ -34,14 +35,14 @@ class Configs:
# All necessary paths
tmp_path: str = '/tmp'
tmp_slpkg: str = os.path.join(tmp_path, prog_name)
build_path: str = os.path.join('tmp', prog_name, 'build')
download_only: str = os.path.join(tmp_slpkg, '')
lib_path: str = os.path.join('/var/lib/', prog_name)
etc_path: str = os.path.join('/etc/', prog_name)
db_path: str = os.path.join(lib_path, 'database')
sbo_repo_path: str = os.path.join(lib_path, 'repository')
log_packages: str = os.path.join('/var', 'log', 'packages')
tmp_slpkg: str = Path(tmp_path, prog_name)
build_path: str = Path('tmp', prog_name, 'build')
download_only: str = Path(tmp_slpkg, '')
lib_path: str = Path('/var/lib/', prog_name)
etc_path: str = Path('/etc/', prog_name)
db_path: str = Path(lib_path, 'database')
sbo_repo_path: str = Path(lib_path, 'repository')
log_packages: str = Path('/var', 'log', 'packages')
# Database name
database: str = f'database.{prog_name}'

View file

@ -1,6 +1,8 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
from pathlib import Path
from typing import Union
from slpkg.configs import Configs
from slpkg.models.models import SBoTable
@ -28,7 +30,7 @@ class CreateData:
'SLACKBUILD REQUIRES:',
'SLACKBUILD SHORT DESCRIPTION:'
]
path = os.path.join(self.configs.sbo_repo_path, self.configs.sbo_txt)
path = Path(self.configs.sbo_repo_path, self.configs.sbo_txt)
sbo_file = self.read_file(path)
cache = [] # init cache
@ -57,7 +59,7 @@ class CreateData:
self.session.commit()
@staticmethod
def read_file(file: str) -> list:
def read_file(file: Union[str, Path]) -> list:
""" Reads the text file. """
with open(file, 'r', encoding='utf-8') as f:
return f.readlines()

View file

@ -2,8 +2,10 @@
# -*- coding: utf-8 -*-
import locale
from dialog import Dialog
from pathlib import Path
from typing import Union
from slpkg.configs import Configs
from slpkg.views.version import Version
@ -46,7 +48,7 @@ class DialogBox:
if self.configs.dialog:
self.d.msgbox(text, height, width)
def textbox(self, text: str, height: int, width: int):
def textbox(self, text: Union[str, Path], height: int, width: int):
""" Display a text box. """
if self.configs.dialog:
self.d.textbox(text, height, width)

View file

@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
import subprocess
from pathlib import Path
from typing import Union
from slpkg.configs import Configs
@ -12,7 +14,7 @@ class Wget:
def __init__(self):
self.wget_options: str = Configs.wget_options
def download(self, path: str, url: str):
def download(self, path: Union[str, Path], url: str):
""" Wget downloader. """
subprocess.call(f'wget {self.wget_options} --directory-prefix={path}'
f' {url}', shell=True)

View file

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import os
from pathlib import Path
from slpkg.configs import Configs
from slpkg.configs import LoadConfigs
@ -15,12 +16,12 @@ class FormConfigs:
self.configs = Configs()
self.load_configs = LoadConfigs()
self.dialog = DialogBox()
self.config_file = f'{self.configs.etc_path}/{self.configs.prog_name}.toml'
self.config_file = Path(self.configs.etc_path, f'{self.configs.prog_name}.toml')
def edit(self):
""" Read and write the configuration file. """
elements = []
config_file = os.path.join(self.configs.etc_path, f'{self.configs.prog_name}.toml')
config_file = Path(self.configs.etc_path, f'{self.configs.prog_name}.toml')
if os.path.isfile(config_file):
# Load the toml config file.

View file

@ -6,6 +6,7 @@ import time
import subprocess
import multiprocessing
from pathlib import Path
from collections import OrderedDict
from slpkg.dialog_box import DialogBox
@ -168,7 +169,7 @@ class Slackbuilds:
def patch_sbo_tag(self, sbo):
""" Patching SBo TAG from the configuration file. """
sbo_script = os.path.join(self.configs.build_path, sbo, f'{sbo}.SlackBuild')
sbo_script = Path(self.configs.build_path, sbo, f'{sbo}.SlackBuild')
if os.path.isfile(sbo_script):
with open(sbo_script, 'r', encoding='utf-8') as f:
@ -225,7 +226,7 @@ class Slackbuilds:
def build_the_script(self, path: str, name: str):
""" Run the .SlackBuild script. """
folder = os.path.join(path, name, '')
folder = f'{Path(path, name)}/'
execute = f'{folder}./{name}.SlackBuild'
# Change to root privileges
@ -251,7 +252,7 @@ class Slackbuilds:
""" Download the sources. """
wget = Wget()
path = os.path.join(self.configs.build_path, name)
path = Path(self.configs.build_path, name)
checksums = SBoQueries(name).checksum()

View file

@ -2,8 +2,7 @@
# -*- coding: utf-8 -*-
import os
from os import path
from pathlib import Path
from slpkg.downloader import Wget
from slpkg.configs import Configs
@ -52,8 +51,8 @@ class UpdateRepository:
@staticmethod
def delete_file(folder: str, txt_file: str):
""" Delete the file. """
file = os.path.join(folder, txt_file)
if path.exists(file):
file = Path(folder, txt_file)
if os.path.exists(file):
os.remove(file)
def delete_sbo_data(self):

View file

@ -4,6 +4,7 @@
import os
import shutil
import tarfile
from pathlib import Path
from slpkg.configs import Configs
from slpkg.blacklist import Blacklist
@ -26,7 +27,7 @@ class Utilities:
@staticmethod
def untar_archive(path: str, archive: str, ext_path: str):
""" Untar the file to the build folder. """
tar_file = os.path.join(path, archive)
tar_file = Path(path, archive)
untar = tarfile.open(tar_file)
untar.extractall(ext_path)
untar.close()
@ -34,21 +35,21 @@ class Utilities:
@staticmethod
def remove_file_if_exists(path: str, file: str):
""" Clean the old files. """
archive = os.path.join(path, file)
archive = Path(path, file)
if os.path.isfile(archive):
os.remove(archive)
@staticmethod
def remove_folder_if_exists(path: str, folder: str):
""" Clean the old folders. """
directory = os.path.join(path, folder)
directory = Path(path, folder)
if os.path.isdir(directory):
shutil.rmtree(directory)
@staticmethod
def create_folder(path: str, folder: str):
""" Creates folder. """
directory = os.path.join(path, folder)
directory = Path(path, folder)
if not os.path.isdir(directory):
os.makedirs(directory)