Switch to sqlite3

This commit is contained in:
Dimitris Zlatanidis 2022-05-29 00:16:55 +03:00
parent 290e3a01c7
commit df3da1ea84
5 changed files with 238 additions and 44 deletions

View file

@ -241,6 +241,9 @@ class MetaData:
# slackpkg lib path # slackpkg lib path
slackpkg_lib_path = "/var/lib/slackpkg/" slackpkg_lib_path = "/var/lib/slackpkg/"
# database name
db = "database.slpkg"
# computer architecture # computer architecture
if comp_arch in ["off", "OFF"]: if comp_arch in ["off", "OFF"]:
arch = os.uname()[4] arch = os.uname()[4]

View file

@ -24,6 +24,8 @@
import os import os
import shutil import shutil
import sqlite3
from models.models import Database
from slpkg.utils import Utils from slpkg.utils import Utils
from slpkg.repositories import Repo from slpkg.repositories import Repo
@ -54,6 +56,7 @@ class Initialization(Utils):
self.slpkg_tmp_packages = self.meta.slpkg_tmp_packages self.slpkg_tmp_packages = self.meta.slpkg_tmp_packages
self.slpkg_tmp_patches = self.meta.slpkg_tmp_patches self.slpkg_tmp_patches = self.meta.slpkg_tmp_patches
self.constructing() self.constructing()
self.database()
def constructing(self): def constructing(self):
"""Creating the all necessary directories """Creating the all necessary directories
@ -75,6 +78,15 @@ class Initialization(Utils):
self.make_dir(paths_basic) self.make_dir(paths_basic)
self.make_dirs(paths_extra) self.make_dirs(paths_extra)
def database(self):
"""Initializing the database
"""
self.db = "database.slpkg"
db_lib = self.lib_path + "database/"
self.make_dirs([db_lib])
self.con = sqlite3.connect(db_lib + self.db)
self.cur = self.con.cursor()
def make_dir(self, path: list): def make_dir(self, path: list):
for p in path: for p in path:
if not os.path.exists(p): if not os.path.exists(p):
@ -626,6 +638,9 @@ class Initialization(Utils):
self.down(lib_path, CHECKSUMS_MD5, repo) self.down(lib_path, CHECKSUMS_MD5, repo)
self.down(lib_path, FILELIST_TXT, repo) self.down(lib_path, FILELIST_TXT, repo)
self.down(log_path, ChangeLog_txt, repo) self.down(log_path, ChangeLog_txt, repo)
if repo == 'sbo':
self.cur.execute("DROP TABLE IF EXISTS sbo")
self.con.commit()
def merge(self, path, outfile, infiles): def merge(self, path, outfile, infiles):
"""Merge files """Merge files
@ -728,14 +743,30 @@ class Update:
else: else:
print(self.error, end="") print(self.error, end="")
print() # new line at end print() # new line at end
self.check_db()
raise SystemExit() raise SystemExit()
def check_db(self):
sbo_db = Database("sbo", "SLACKBUILDS.TXT")
if sbo_db.table_exists() == 0:
sbo_db.create_sbo_table()
sbo_db.insert_sbo_table()
def done_msg(self, repo): def done_msg(self, repo):
print(f"{self.grey}Check repository " print(f"{self.grey}Check repository "
f"[{self.cyan}{repo}{self.grey}] ... " f"[{self.cyan}{repo}{self.grey}] ... "
f"{self.endc}", end="", flush=True) f"{self.endc}", end="", flush=True)
def database(text_file):
db_lib = _meta_.lib_path + "database"
init = Initialization(False)
init.make_dirs([db_lib])
data = Database("sbo", text_file)
data.create_sbo_table()
data.insert_sbo_table()
def check_exists_repositories(repo): def check_exists_repositories(repo):
"""Checking if repositories exists by PACKAGES.TXT file """Checking if repositories exists by PACKAGES.TXT file
""" """

0
slpkg/models/__init.py__ Normal file
View file

110
slpkg/models/models.py Normal file
View file

@ -0,0 +1,110 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# models.py file is part of slpkg.
# Copyright 2014-2022 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
# All rights reserved.
# Slpkg is a user-friendly package manager for Slackware installations
# https://gitlab.com/dslackw/slpkg
# Slpkg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sqlite3
from progress.bar import Bar
from slpkg.__metadata__ import MetaData as _meta_
class Database:
def __init__(self, table_name, text_file):
self.lib_path = _meta_.lib_path
self.table_name = table_name
self.text_file = text_file
self.db = _meta_.db
self.con = sqlite3.connect(f'{self.lib_path}database/{self.db}')
self.cur = self.con.cursor()
def table_exists(self):
self.cur.execute("""SELECT count(name)
FROM sqlite_master
WHERE type='table'
AND name='{}'""".format(self.table_name))
return self.cur.fetchone()[0]
def create_sbo_table(self):
self.cur.execute('''CREATE TABLE IF NOT EXISTS {}
(name text, location text, files text, version text,
download text, download64 text, md5sum text,
md5sum64 text, requires text, short_desc text)
'''.format(self.table_name))
self.con.commit()
def insert_sbo_table(self):
self.sbo = [
'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_file = self.open_file(f"{self.lib_path}sbo_repo/SLACKBUILDS.TXT")
bar = Bar("Database sbo creating", max=len(sbo_file),
suffix='%(percent)d%% - %(eta)ds')
for i, line in enumerate(sbo_file, 1):
if line.startswith(self.sbo[0]):
name = line.replace(self.sbo[0], '').strip()
if line.startswith(self.sbo[1]):
location = line.replace(self.sbo[1], '').strip()
if line.startswith(self.sbo[2]):
files = line.replace(self.sbo[2], '').strip()
if line.startswith(self.sbo[3]):
version = line.replace(self.sbo[3], '').strip()
if line.startswith(self.sbo[4]):
download = line.replace(self.sbo[4], '').strip()
if line.startswith(self.sbo[5]):
download64 = line.replace(self.sbo[5], '').strip()
if line.startswith(self.sbo[6]):
md5sum = line.replace(self.sbo[6], '').strip()
if line.startswith(self.sbo[7]):
md5sum64 = line.replace(self.sbo[7], '').strip()
if line.startswith(self.sbo[8]):
requires = line.replace(self.sbo[8], '').strip()
if line.startswith(self.sbo[9]):
short_desc = line.replace(self.sbo[9], '').strip()
if i % 11 == 0:
values = [(name, location, files, version, download,
download64, md5sum, md5sum64, requires, short_desc),
]
self.cur.executemany("""INSERT INTO {} VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""".format(
self.table_name), values)
self.con.commit()
bar.next()
bar.finish()
self.con.close()
def open_file(self, file):
with open(file, 'r', encoding='utf-8') as f:
return f.readlines()

View file

@ -22,6 +22,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sqlite3
from slpkg.utils import Utils from slpkg.utils import Utils
from slpkg.__metadata__ import MetaData as _meta_ from slpkg.__metadata__ import MetaData as _meta_
@ -32,6 +33,7 @@ class SBoGrep(Utils):
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.meta = _meta_ self.meta = _meta_
self.db = self.meta.db
self.arch64 = "x86_64" self.arch64 = "x86_64"
self.line_name = "SLACKBUILD NAME: " self.line_name = "SLACKBUILD NAME: "
self.line_files = "SLACKBUILD FILES: " self.line_files = "SLACKBUILD FILES: "
@ -43,6 +45,9 @@ class SBoGrep(Utils):
self.line_md5_64 = f"SLACKBUILD MD5SUM_{self.arch64}: " self.line_md5_64 = f"SLACKBUILD MD5SUM_{self.arch64}: "
self.line_des = "SLACKBUILD SHORT DESCRIPTION: " self.line_des = "SLACKBUILD SHORT DESCRIPTION: "
self.sbo_txt = self.meta.lib_path + "sbo_repo/SLACKBUILDS.TXT" self.sbo_txt = self.meta.lib_path + "sbo_repo/SLACKBUILDS.TXT"
self.sbo_db = f"{self.meta.lib_path}database/{self.db}"
self.con = sqlite3.connect(self.sbo_db)
self.cur = self.con.cursor()
self.SLACKBUILDS_TXT = self.read_file(self.sbo_txt) self.SLACKBUILDS_TXT = self.read_file(self.sbo_txt)
def _names_grabbing(self): def _names_grabbing(self):
@ -61,72 +66,117 @@ class SBoGrep(Utils):
def source(self): def source(self):
"""Grab sources downloads links """Grab sources downloads links
""" """
source, source64, = "", "" # for line in self.SLACKBUILDS_TXT.splitlines():
for line in self.SLACKBUILDS_TXT.splitlines(): # if line.startswith(self.line_name):
if line.startswith(self.line_name): # sbo_name = line[17:].strip()
sbo_name = line[17:].strip() # if line.startswith(self.line_down):
if line.startswith(self.line_down): # if sbo_name == self.name and line[21:].strip():
if sbo_name == self.name and line[21:].strip(): # source = line[21:]
source = line[21:] # if line.startswith(self.line_down_64):
if line.startswith(self.line_down_64): # if sbo_name == self.name and line[28:].strip():
if sbo_name == self.name and line[28:].strip(): # source64 = line[28:]
source64 = line[28:]
source, source64 = self.cur.execute("""SELECT download, download64
FROM sbo
WHERE name = '{}'""".format(
self.name)).fetchone()
return self._sorting_arch(source, source64) return self._sorting_arch(source, source64)
def requires(self): def requires(self):
"""Grab package requirements """Grab package requirements
""" """
for line in self.SLACKBUILDS_TXT.splitlines(): requires = self.cur.execute("""SELECT requires
if line.startswith(self.line_name): FROM sbo
sbo_name = line[17:].strip() WHERE name = '{}'""".format(
if line.startswith(self.line_req): self.name)).fetchone()
if sbo_name == self.name: return requires[0].split()
return line[21:].strip().split()
# for line in self.SLACKBUILDS_TXT.splitlines():
# if line.startswith(self.line_name):
# sbo_name = line[17:].strip()
# if line.startswith(self.line_req):
# if sbo_name == self.name:
# # print(line[21:].strip().split())
# return line[21:].strip().split()
def version(self): def version(self):
"""Grab package version """Grab package version
""" """
for line in self.SLACKBUILDS_TXT.splitlines(): version = self.cur.execute("""SELECT version
if line.startswith(self.line_name): FROM sbo
sbo_name = line[17:].strip() WHERE name = '{}'""".format(
if line.startswith(self.line_ver): self.name)).fetchone()
if sbo_name == self.name: return version[0]
return line[20:].strip()
# for line in self.SLACKBUILDS_TXT.splitlines():
# if line.startswith(self.line_name):
# sbo_name = line[17:].strip()
# if line.startswith(self.line_ver):
# if sbo_name == self.name:
# print(line[20:].strip())
#
# return line[20:].strip()
def checksum(self): def checksum(self):
"""Grab checksum string """Grab checksum string
""" """
md5sum, md5sum64, = [], [] md5sum, md5sum64, = [], []
for line in self.SLACKBUILDS_TXT.splitlines(): mds5, md5s64 = self.cur.execute("""SELECT md5sum, md5sum64
if line.startswith(self.line_name): FROM sbo
sbo_name = line[17:].strip() WHERE name = '{}'""".format(
if line.startswith(self.line_md5_64): self.name)).fetchone()
if sbo_name == self.name and line[26:].strip(): if mds5:
md5sum64 = line[26:].strip().split() md5sum.append(mds5)
if line.startswith(self.line_md5): if md5s64:
if sbo_name == self.name and line[19:].strip(): md5sum64.append(md5s64)
md5sum = line[19:].strip().split()
return self._sorting_arch(md5sum, md5sum64) return self._sorting_arch(md5sum, md5sum64)
# md5sum, md5sum64, = [], []
# for line in self.SLACKBUILDS_TXT.splitlines():
# if line.startswith(self.line_name):
# sbo_name = line[17:].strip()
# if line.startswith(self.line_md5_64):
# if sbo_name == self.name and line[26:].strip():
# md5sum64 = line[26:].strip().split()
# if line.startswith(self.line_md5):
# if sbo_name == self.name and line[19:].strip():
# md5sum = line[19:].strip().split()
# print(md5sum, md5sum64)
# return self._sorting_arch(md5sum, md5sum64)
def description(self): def description(self):
"""Grab package version """Grab package description
""" """
for line in self.SLACKBUILDS_TXT.splitlines(): desc = self.cur.execute("""SELECT short_desc
if line.startswith(self.line_name): FROM sbo
sbo_name = line[17:].strip() WHERE name = '{}'""".format(
if line.startswith(self.line_des): self.name)).fetchone()
if sbo_name == self.name: return desc[0]
return line[31:].strip()
# for line in self.SLACKBUILDS_TXT.splitlines():
# if line.startswith(self.line_name):
# sbo_name = line[17:].strip()
# if line.startswith(self.line_des):
# if sbo_name == self.name:
# print(line[31:].strip())
#
# return line[31:].strip()
def files(self): def files(self):
"""Grab files """Grab files
""" """
for line in self.SLACKBUILDS_TXT.splitlines(): files = self.cur.execute("""SELECT files
if line.startswith(self.line_name): FROM sbo
sbo_name = line[17:].strip() WHERE name = '{}'""".format(
if line.startswith(self.line_files): self.name)).fetchone()
if sbo_name == self.name: return files[0]
return line[18:].strip()
# for line in self.SLACKBUILDS_TXT.splitlines():
# if line.startswith(self.line_name):
# sbo_name = line[17:].strip()
# if line.startswith(self.line_files):
# if sbo_name == self.name:
# return line[18:].strip()
def _sorting_arch(self, arch, arch64): def _sorting_arch(self, arch, arch64):
"""Return sources by arch """Return sources by arch