diff --git a/slpkg/__metadata__.py b/slpkg/__metadata__.py index 99c42418..22794666 100644 --- a/slpkg/__metadata__.py +++ b/slpkg/__metadata__.py @@ -241,6 +241,9 @@ class MetaData: # slackpkg lib path slackpkg_lib_path = "/var/lib/slackpkg/" + # database name + db = "database.slpkg" + # computer architecture if comp_arch in ["off", "OFF"]: arch = os.uname()[4] diff --git a/slpkg/init.py b/slpkg/init.py index fc6666ba..0202c69c 100644 --- a/slpkg/init.py +++ b/slpkg/init.py @@ -24,6 +24,8 @@ import os import shutil +import sqlite3 +from models.models import Database from slpkg.utils import Utils from slpkg.repositories import Repo @@ -54,6 +56,7 @@ class Initialization(Utils): self.slpkg_tmp_packages = self.meta.slpkg_tmp_packages self.slpkg_tmp_patches = self.meta.slpkg_tmp_patches self.constructing() + self.database() def constructing(self): """Creating the all necessary directories @@ -75,6 +78,15 @@ class Initialization(Utils): self.make_dir(paths_basic) 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): for p in path: if not os.path.exists(p): @@ -626,6 +638,9 @@ class Initialization(Utils): self.down(lib_path, CHECKSUMS_MD5, repo) self.down(lib_path, FILELIST_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): """Merge files @@ -728,14 +743,30 @@ class Update: else: print(self.error, end="") print() # new line at end + self.check_db() 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): print(f"{self.grey}Check repository " f"[{self.cyan}{repo}{self.grey}] ... " 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): """Checking if repositories exists by PACKAGES.TXT file """ diff --git a/slpkg/models/__init.py__ b/slpkg/models/__init.py__ new file mode 100644 index 00000000..e69de29b diff --git a/slpkg/models/models.py b/slpkg/models/models.py new file mode 100644 index 00000000..696114f2 --- /dev/null +++ b/slpkg/models/models.py @@ -0,0 +1,110 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +# models.py file is part of slpkg. + +# Copyright 2014-2022 Dimitris Zlatanidis +# 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 . + + +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() diff --git a/slpkg/sbo/greps.py b/slpkg/sbo/greps.py index 34d19fa4..542cc513 100644 --- a/slpkg/sbo/greps.py +++ b/slpkg/sbo/greps.py @@ -22,6 +22,7 @@ # along with this program. If not, see . +import sqlite3 from slpkg.utils import Utils from slpkg.__metadata__ import MetaData as _meta_ @@ -32,6 +33,7 @@ class SBoGrep(Utils): def __init__(self, name): self.name = name self.meta = _meta_ + self.db = self.meta.db self.arch64 = "x86_64" self.line_name = "SLACKBUILD NAME: " self.line_files = "SLACKBUILD FILES: " @@ -43,6 +45,9 @@ class SBoGrep(Utils): self.line_md5_64 = f"SLACKBUILD MD5SUM_{self.arch64}: " self.line_des = "SLACKBUILD SHORT DESCRIPTION: " 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) def _names_grabbing(self): @@ -61,72 +66,117 @@ class SBoGrep(Utils): def source(self): """Grab sources downloads links """ - source, source64, = "", "" - for line in self.SLACKBUILDS_TXT.splitlines(): - if line.startswith(self.line_name): - sbo_name = line[17:].strip() - if line.startswith(self.line_down): - if sbo_name == self.name and line[21:].strip(): - source = line[21:] - if line.startswith(self.line_down_64): - if sbo_name == self.name and line[28:].strip(): - source64 = line[28:] +# for line in self.SLACKBUILDS_TXT.splitlines(): +# if line.startswith(self.line_name): +# sbo_name = line[17:].strip() +# if line.startswith(self.line_down): +# if sbo_name == self.name and line[21:].strip(): +# source = line[21:] +# if line.startswith(self.line_down_64): +# if sbo_name == self.name and line[28:].strip(): +# 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) def requires(self): """Grab package requirements """ - 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: - return line[21:].strip().split() + requires = self.cur.execute("""SELECT requires + FROM sbo + WHERE name = '{}'""".format( + self.name)).fetchone() + return requires[0].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): """Grab package version """ - 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: - return line[20:].strip() + version = self.cur.execute("""SELECT version + FROM sbo + WHERE name = '{}'""".format( + self.name)).fetchone() + return version[0] + +# 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): """Grab checksum string """ 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() + mds5, md5s64 = self.cur.execute("""SELECT md5sum, md5sum64 + FROM sbo + WHERE name = '{}'""".format( + self.name)).fetchone() + if mds5: + md5sum.append(mds5) + if md5s64: + md5sum64.append(md5s64) 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): - """Grab package version + """Grab package description """ - 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: - return line[31:].strip() + desc = self.cur.execute("""SELECT short_desc + FROM sbo + WHERE name = '{}'""".format( + self.name)).fetchone() + return desc[0] + +# 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): """Grab files """ - 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() + files = self.cur.execute("""SELECT files + FROM sbo + WHERE name = '{}'""".format( + self.name)).fetchone() + return files[0] + +# 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): """Return sources by arch