diff --git a/slpkg/__metadata__.py b/slpkg/__metadata__.py index e47b395f..e120a9f4 100644 --- a/slpkg/__metadata__.py +++ b/slpkg/__metadata__.py @@ -238,7 +238,7 @@ class MetaData: slackpkg_lib_path = "/var/lib/slackpkg/" # database name - db = "/database/database.slpkg" + db = "database/database.slpkg" # computer architecture if comp_arch in ["off", "OFF"]: diff --git a/slpkg/init.py b/slpkg/init.py index 00a2117a..e4ce9d65 100644 --- a/slpkg/init.py +++ b/slpkg/init.py @@ -24,17 +24,17 @@ import os import shutil -import sqlite3 from slpkg.utils import Utils from slpkg.repositories import Repo from slpkg.file_size import FileSize from slpkg.downloader import Download -from slpkg.models.models import Database +from slpkg.models.data import Database from slpkg.__metadata__ import MetaData as _meta_ from slpkg.slack.mirrors import mirrors from slpkg.slack.slack_version import slack_ver +from slpkg.models.models import SBoTable, session class Initialization(Utils): @@ -45,6 +45,7 @@ class Initialization(Utils): self.check = check self.meta = _meta_ self.arch = _meta_.arch + self.session = session self.conf_path = self.meta.conf_path self.log_path = self.meta.log_path self.lib_path = self.meta.lib_path @@ -76,15 +77,6 @@ class Initialization(Utils): self.make_dir(paths_basic) self.make_dirs(paths_extra) - self.database() - - def database(self): - """Initializing the database - """ - db_lib = self.lib_path + self.meta.db - self.make_dirs([db_lib]) - self.con = sqlite3.connect(db_lib) - self.cur = self.con.cursor() def make_dir(self, path: list): for p in path: @@ -638,8 +630,8 @@ class Initialization(Utils): 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() + self.session.query(SBoTable).delete() # delete all data + self.session.commit() def merge(self, path, outfile, infiles): """Merging files @@ -716,6 +708,7 @@ class Update: self.endc = _meta_.color["ENDC"] self.done = f"{self.green}Done{self.endc}\n" self.error = f"{self.red}Error{self.endc}\n" + self.session = session def run(self, repos): """Updates repositories lists @@ -746,12 +739,11 @@ class Update: raise SystemExit() def check_db(self): - """Checking if the table exists + """Checking if the table is empty """ - sbo_db = Database("sbo", "SLACKBUILDS.TXT") - if sbo_db.table_exists() == 0: - sbo_db.create_sbo_table() - sbo_db.insert_sbo_table() + db = Database() + if self.session.query(SBoTable).first() is None: + db.insert_sbo_table() def done_msg(self, repo): print(f"{self.grey}Check repository " diff --git a/slpkg/models/data.py b/slpkg/models/data.py new file mode 100644 index 00000000..4655eb8a --- /dev/null +++ b/slpkg/models/data.py @@ -0,0 +1,81 @@ +#!/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 . + + +from progress.bar import Bar +from slpkg.__metadata__ import MetaData as _meta_ +from slpkg.models.models import SBoTable, session + + +class Database: + + def __init__(self): + self.lib_path = _meta_.lib_path + self.session = session + + def insert_sbo_table(self): + """Grabbing data line by line and inserting them into the database + """ + + 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_file = self.open_file(f"{self.lib_path}sbo_repo/SLACKBUILDS.TXT") + + bar = Bar("Creating sbo database", max=len(sbo_file), + suffix="%(percent)d%% - %(eta)ds") + + cache = [] # init cache + + for i, line in enumerate(sbo_file, 1): + + for s in sbo_tags: + if line.startswith(s): + line = line.replace(s, "").strip() + cache.append(line) + + if (i % 11) == 0: + data = SBoTable(name=cache[0], location=cache[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 = [] # reset cache after 11 lines + bar.next() + bar.finish() + self.session.commit() + + def open_file(self, file): + with open(file, "r", encoding="utf-8") as f: + return f.readlines() diff --git a/slpkg/models/models.py b/slpkg/models/models.py index 85e63111..5dfd0958 100644 --- a/slpkg/models/models.py +++ b/slpkg/models/models.py @@ -22,82 +22,37 @@ # along with this program. If not, see . -import sqlite3 -from progress.bar import Bar +from sqlalchemy.orm import sessionmaker +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import create_engine, Column, Integer, Text + from slpkg.__metadata__ import MetaData as _meta_ +lib_path = _meta_.lib_path +db = _meta_.db -class Database: +DATABASE_URI = f"sqlite:///{lib_path}{db}" +engine = create_engine(DATABASE_URI) - 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}{self.db}") - self.cur = self.con.cursor() +session = sessionmaker(engine)() +Base = declarative_base() - def table_exists(self): - """Checking if the table exists - """ - 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() +class SBoTable(Base): - def insert_sbo_table(self): - """Grabbing data line by line and inserting them into the database - """ - 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:" - ] + __tablename__ = "sbotable" - sbo_file = self.open_file(f"{self.lib_path}sbo_repo/SLACKBUILDS.TXT") + id = Column(Integer, primary_key=True) + name = Column(Text) + location = Column(Text) + files = Column(Text) + version = Column(Text) + download = Column(Text) + download64 = Column(Text) + md5sum = Column(Text) + md5sum64 = Column(Text) + requires = Column(Text) + short_description = Column(Text) - bar = Bar("Creating sbo database", max=len(sbo_file), - suffix="%(percent)d%% - %(eta)ds") - cache = [] # init cache - - for i, line in enumerate(sbo_file, 1): - - for s in self.sbo: - if line.startswith(s): - line = line.replace(s, "").strip() - cache.append(line) - - if (i % 11) == 0: - values = [ - (cache[0], cache[1], cache[2], cache[3], cache[4], - cache[5], cache[6], cache[7], cache[8], cache[9]), - ] - self.cur.executemany("""INSERT INTO {} VALUES - (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""".format( - self.table_name), values) - self.con.commit() - cache = [] # reset cache after 11 lines - bar.next() - bar.finish() - self.con.close() - - def open_file(self, file): - with open(file, "r", encoding="utf-8") as f: - return f.readlines() +Base.metadata.create_all(engine) diff --git a/slpkg/sbo/greps.py b/slpkg/sbo/greps.py index 9a9c8d81..99d19e59 100644 --- a/slpkg/sbo/greps.py +++ b/slpkg/sbo/greps.py @@ -22,9 +22,9 @@ # along with this program. If not, see . -import sqlite3 from slpkg.utils import Utils from slpkg.__metadata__ import MetaData as _meta_ +from slpkg.models.models import SBoTable, session class SBoGrep(Utils): @@ -35,14 +35,12 @@ class SBoGrep(Utils): self.meta = _meta_ self.db = self.meta.db self.arch64 = "x86_64" - self.sbo_db = f"{self.meta.lib_path}{self.db}" - self.con = sqlite3.connect(self.sbo_db) - self.cur = self.con.cursor() + self.session = session def _names_grabbing(self): """Generator that collecting all packages names """ - names = self.cur.execute("SELECT name FROM sbo").fetchall() + names = self.session.query(SBoTable.name).all() for n in names: yield n[0] @@ -55,60 +53,61 @@ class SBoGrep(Utils): def source(self): """Grabs sources downloads links """ - source, source64 = self.cur.execute("""SELECT download, download64 - FROM sbo - WHERE name = '{}'""".format( - self.name)).fetchone() + source, source64 = self.session.query( + SBoTable.download, SBoTable.download64).filter( + SBoTable.name == self.name).first() + return self._sorting_arch(source, source64) def requires(self): """Grabs package requirements """ - requires = self.cur.execute("""SELECT requires - FROM sbo - WHERE name = '{}'""".format( - self.name)).fetchone() + requires = self.session.query( + SBoTable.requires).filter( + SBoTable.name == self.name).first() + return requires[0].split() def version(self): """Grabs package version """ - version = self.cur.execute("""SELECT version - FROM sbo - WHERE name = '{}'""".format( - self.name)).fetchone() + version = self.session.query( + SBoTable.version).filter( + SBoTable.name == self.name).first() + return version[0] def checksum(self): """Grabs checksum string """ md5sum, md5sum64, = [], [] - mds5, md5s64 = self.cur.execute("""SELECT md5sum, md5sum64 - FROM sbo - WHERE name = '{}'""".format( - self.name)).fetchone() + mds5, md5s64 = self.session.query( + SBoTable.md5sum, SBoTable.md5sum64).filter( + SBoTable.name == self.name).first() + if mds5: md5sum.append(mds5) if md5s64: md5sum64.append(md5s64) + return self._sorting_arch(md5sum, md5sum64) def description(self): """Grabs package description """ - desc = self.cur.execute("""SELECT short_desc - FROM sbo - WHERE name = '{}'""".format( - self.name)).fetchone() + desc = self.session.query( + SBoTable.short_description).filter( + SBoTable.name == self.name).first() + return desc[0] def files(self): """Grabs files """ - files = self.cur.execute("""SELECT files - FROM sbo - WHERE name = '{}'""".format( - self.name)).fetchone() + files = self.session.query( + SBoTable.files).filter( + SBoTable.name == self.name).first() + return files[0] def _sorting_arch(self, arch, arch64): @@ -116,4 +115,5 @@ class SBoGrep(Utils): """ if self.meta.arch == self.arch64 and arch64: return arch64 + return arch diff --git a/slpkg/sbo/search.py b/slpkg/sbo/search.py index 0eefeeaa..443cb3a5 100644 --- a/slpkg/sbo/search.py +++ b/slpkg/sbo/search.py @@ -22,25 +22,17 @@ # along with this program. If not, see . -import sqlite3 from slpkg.repositories import Repo -from slpkg.__metadata__ import MetaData as _meta_ - from slpkg.slack.slack_version import slack_ver +from slpkg.models.models import SBoTable, session def sbo_search_pkg(name): """Search for package path in SLACKBUILDS.TXT file and return url """ - db = _meta_.db - lib_path = _meta_.lib_path - con = sqlite3.connect(f"{lib_path}{db}") - cur = con.cursor() - - location = cur.execute("""SELECT location - FROM sbo - WHERE name = '{}'""".format(name)).fetchone() + location = session.query(SBoTable.location).filter( + SBoTable.name == name).first() repo = Repo() sbo = repo.default_repository()["sbo"]