slpkg/slpkg/create_data.py

102 lines
3.1 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
2022-12-30 16:40:11 +01:00
2023-02-28 20:43:04 +01:00
import re
2022-12-30 16:40:11 +01:00
from pathlib import Path
from typing import Union
2023-02-28 20:43:04 +01:00
from slpkg.queries import SBoQueries
2022-06-18 20:35:23 +02:00
from slpkg.configs import Configs
2023-02-28 20:43:04 +01:00
from slpkg.utilities import Utilities
from slpkg.models.models import SBoTable, PonceTable
2022-06-17 18:36:07 +02:00
from slpkg.models.models import session as Session
2023-01-16 23:47:01 +01:00
class CreateData(Configs):
""" Reads the SLACKBUILDS.TXT file and inserts them into the database. """
def __init__(self):
2023-01-16 23:47:01 +01:00
super(Configs, self).__init__()
self.session = Session
2023-02-28 20:43:04 +01:00
self.utils = Utilities()
self.query = SBoQueries('')
def insert_sbo_table(self):
2022-12-21 10:19:47 +01:00
""" Install the data. """
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:'
]
2023-01-16 23:47:01 +01:00
path = Path(self.sbo_repo_path, self.sbo_txt)
2022-12-29 20:19:44 +01:00
sbo_file = self.read_file(path)
2023-02-28 22:54:56 +01:00
cache: list = [] # init cache
2023-01-02 12:42:32 +01:00
print('\nCreating the database... ', end='', flush=True)
for i, line in enumerate(sbo_file, 1):
2022-06-20 19:43:56 +02:00
for tag in sbo_tags:
if line.startswith(tag):
line = line.replace(tag, '').strip()
cache.append(line)
if (i % 11) == 0:
data = SBoTable(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)
2023-02-28 22:54:56 +01:00
cache: list = [] # reset cache after 11 lines
print('Done')
self.session.commit()
2023-02-28 20:43:04 +01:00
def insert_ponce_blacklist_packages(self):
sbos = self.query.sbos()
2023-02-28 21:15:47 +01:00
path = Path(self.slack_chglog_path, self.slack_chglog_txt)
slack_chglog = self.read_file(path)
2023-02-28 20:43:04 +01:00
2023-02-28 21:15:47 +01:00
for line in slack_chglog:
2023-02-28 20:43:04 +01:00
# Clean the line from white spaces.
2023-02-28 22:54:56 +01:00
line: str = line.strip()
2023-02-28 20:43:04 +01:00
if re.findall('Added[.]', line):
# Clean the line.
line = re.sub(r'Added.|.txz:', '', line)
2023-02-28 22:54:56 +01:00
pkg: str = line.split('/')[-1]
2023-02-28 20:43:04 +01:00
# Split and get the name only.
name = self.utils.split_installed_pkg(pkg)[0]
if name in sbos:
data = PonceTable(name=name)
self.session.add(data)
# Stop the date when the Slackware 15.0 released.
if line == 'Wed Feb 2 22:22:22 UTC 2022':
break
self.session.commit()
@staticmethod
2022-12-30 16:40:11 +01:00
def read_file(file: Union[str, Path]) -> list:
2022-12-22 21:26:59 +01:00
""" Reads the text file. """
with open(file, 'r', encoding='utf-8') as f:
return f.readlines()