#!/usr/bin/python3 # -*- coding: utf-8 -*- from pathlib import Path from slpkg.configs import Configs from slpkg.queries import SBoQueries from slpkg.utilities import Utilities from slpkg.repositories import Repositories from slpkg.models.models import session as Session from slpkg.models.models import SBoTable, PonceTable, BinariesTable class CreateData(Configs): """ Reads the SLACKBUILDS.TXT file and inserts them into the database. """ def __init__(self): super(Configs, self).__init__() self.session = Session self.utils = Utilities() self.query = SBoQueries('') self.repositories = Repositories() self.repos: dict = self.repositories.configs() def install_sbo_table(self) -> None: """ Install the data for SBo repository. """ sbo_tags = [ 'SLACKBUILD NAME:', 'SLACKBUILD LOCATION:', 'SLACKBUILD FILES:', 'SLACKBUILD VERSION:', 'SLACKBUILD DOWNLOAD:', 'SLACKBUILD DOWNLOAD_x86_64:', 'SLACKBUILD MD5SUM:', 'SLACKBUILD MD5SUM_x86_64:', 'SLACKBUILD REQUIRES:', 'SLACKBUILD SHORT DESCRIPTION:' ] sbo_table = SBoTable path = Path(self.sbo_repo_path, self.repos['SBO_REPO_TXT']) if self.repos['PONCE_REPO']: sbo_table = PonceTable path = Path(self.ponce_repo_path, self.repos['PONCE_REPO_TXT']) sbo_file: list = self.utils.read_file(path) cache: list = [] # init cache print('Creating the database... ', end='', flush=True) for i, line in enumerate(sbo_file, 1): for tag in sbo_tags: if line.startswith(tag): line = line.replace(tag, '').strip() cache.append(line) if (i % 11) == 0: data: str = sbo_table(name=cache[0], location=cache[1].split('/')[1], files=cache[2], version=cache[3], download=cache[4], download64=cache[5], md5sum=cache[6], md5sum64=cache[7], requires=cache[8], short_description=cache[9]) self.session.add(data) cache: list = [] # reset cache after 11 lines print('Done') self.session.commit() def install_gnome_data(self) -> None: """ Install the data for SBo repository. """ repo: str = 'gnome' pkg_tag = [ 'PACKAGE NAME:', 'PACKAGE MIRROR:', 'PACKAGE LOCATION:', 'PACKAGE SIZE (compressed):', 'PACKAGE SIZE (uncompressed):', 'PACKAGE DESCRIPTION:', ] path = Path(self.lib_path, 'repositories', repo, self.repos['GNOME_REPO_PKG_TXT']) pkg_txt: list = self.utils.read_file(path) cache: list = [] # init cache print('Creating the database... ', end='', flush=True) for i, line in enumerate(pkg_txt, 1): if line.startswith(pkg_tag[0]): package_name = line.replace(pkg_tag[0], '').strip() name: str = self.utils.split_binary_pkg(package_name)[0] version: str = self.utils.split_binary_pkg(package_name)[1] cache.append(name) cache.append(version) cache.append(package_name) if line.startswith(pkg_tag[1]): package_mirror = line.replace(pkg_tag[1], '').strip() if not package_mirror.endswith('/'): package_mirror: str = f'{package_mirror}/' cache.append(package_mirror) if line.startswith(pkg_tag[2]): package_location = line.replace(pkg_tag[2], '').strip() cache.append(package_location[1:]) # Do not install (.) dot if line.startswith(pkg_tag[3]): package_size_comp = line.replace(pkg_tag[3], '').strip() cache.append(package_size_comp) if line.startswith(pkg_tag[4]): package_size_uncomp = line.replace(pkg_tag[4], '').strip() cache.append(package_size_uncomp) if line.startswith(pkg_tag[5]): package_description = line.replace(pkg_tag[5], '').strip() cache.append(package_description) if len(cache) == 8: data: str = BinariesTable( repo=repo, name=cache[0], version=cache[1], package=cache[2], mirror=cache[3], location=cache[4], size_comp=cache[5], size_uncomp=cache[6], description=cache[7], ) self.session.add(data) cache: list = [] print('Done') self.session.commit()