Added sqlalchemy adapter

This commit is contained in:
Dimitris Zlatanidis 2022-05-30 21:39:59 +03:00
parent e0d3dc0e9d
commit 6d7ebf9e74
6 changed files with 148 additions and 128 deletions

View file

@ -238,7 +238,7 @@ class MetaData:
slackpkg_lib_path = "/var/lib/slackpkg/" slackpkg_lib_path = "/var/lib/slackpkg/"
# database name # database name
db = "/database/database.slpkg" db = "database/database.slpkg"
# computer architecture # computer architecture
if comp_arch in ["off", "OFF"]: if comp_arch in ["off", "OFF"]:

View file

@ -24,17 +24,17 @@
import os import os
import shutil import shutil
import sqlite3
from slpkg.utils import Utils from slpkg.utils import Utils
from slpkg.repositories import Repo from slpkg.repositories import Repo
from slpkg.file_size import FileSize from slpkg.file_size import FileSize
from slpkg.downloader import Download 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.__metadata__ import MetaData as _meta_
from slpkg.slack.mirrors import mirrors from slpkg.slack.mirrors import mirrors
from slpkg.slack.slack_version import slack_ver from slpkg.slack.slack_version import slack_ver
from slpkg.models.models import SBoTable, session
class Initialization(Utils): class Initialization(Utils):
@ -45,6 +45,7 @@ class Initialization(Utils):
self.check = check self.check = check
self.meta = _meta_ self.meta = _meta_
self.arch = _meta_.arch self.arch = _meta_.arch
self.session = session
self.conf_path = self.meta.conf_path self.conf_path = self.meta.conf_path
self.log_path = self.meta.log_path self.log_path = self.meta.log_path
self.lib_path = self.meta.lib_path self.lib_path = self.meta.lib_path
@ -76,15 +77,6 @@ class Initialization(Utils):
self.make_dir(paths_basic) self.make_dir(paths_basic)
self.make_dirs(paths_extra) 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): def make_dir(self, path: list):
for p in path: for p in path:
@ -638,8 +630,8 @@ class Initialization(Utils):
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': if repo == 'sbo':
self.cur.execute("DROP TABLE IF EXISTS sbo") self.session.query(SBoTable).delete() # delete all data
self.con.commit() self.session.commit()
def merge(self, path, outfile, infiles): def merge(self, path, outfile, infiles):
"""Merging files """Merging files
@ -716,6 +708,7 @@ class Update:
self.endc = _meta_.color["ENDC"] self.endc = _meta_.color["ENDC"]
self.done = f"{self.green}Done{self.endc}\n" self.done = f"{self.green}Done{self.endc}\n"
self.error = f"{self.red}Error{self.endc}\n" self.error = f"{self.red}Error{self.endc}\n"
self.session = session
def run(self, repos): def run(self, repos):
"""Updates repositories lists """Updates repositories lists
@ -746,12 +739,11 @@ class Update:
raise SystemExit() raise SystemExit()
def check_db(self): def check_db(self):
"""Checking if the table exists """Checking if the table is empty
""" """
sbo_db = Database("sbo", "SLACKBUILDS.TXT") db = Database()
if sbo_db.table_exists() == 0: if self.session.query(SBoTable).first() is None:
sbo_db.create_sbo_table() db.insert_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 "

81
slpkg/models/data.py Normal file
View file

@ -0,0 +1,81 @@
#!/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/>.
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()

View file

@ -22,82 +22,37 @@
# 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 sqlalchemy.orm import sessionmaker
from progress.bar import Bar from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, Text
from slpkg.__metadata__ import MetaData as _meta_ 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): session = sessionmaker(engine)()
self.lib_path = _meta_.lib_path Base = declarative_base()
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()
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): class SBoTable(Base):
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): __tablename__ = "sbotable"
"""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:"
]
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 Base.metadata.create_all(engine)
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()

View file

@ -22,9 +22,9 @@
# 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_
from slpkg.models.models import SBoTable, session
class SBoGrep(Utils): class SBoGrep(Utils):
@ -35,14 +35,12 @@ class SBoGrep(Utils):
self.meta = _meta_ self.meta = _meta_
self.db = self.meta.db self.db = self.meta.db
self.arch64 = "x86_64" self.arch64 = "x86_64"
self.sbo_db = f"{self.meta.lib_path}{self.db}" self.session = session
self.con = sqlite3.connect(self.sbo_db)
self.cur = self.con.cursor()
def _names_grabbing(self): def _names_grabbing(self):
"""Generator that collecting all packages names """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: for n in names:
yield n[0] yield n[0]
@ -55,60 +53,61 @@ class SBoGrep(Utils):
def source(self): def source(self):
"""Grabs sources downloads links """Grabs sources downloads links
""" """
source, source64 = self.cur.execute("""SELECT download, download64 source, source64 = self.session.query(
FROM sbo SBoTable.download, SBoTable.download64).filter(
WHERE name = '{}'""".format( SBoTable.name == self.name).first()
self.name)).fetchone()
return self._sorting_arch(source, source64) return self._sorting_arch(source, source64)
def requires(self): def requires(self):
"""Grabs package requirements """Grabs package requirements
""" """
requires = self.cur.execute("""SELECT requires requires = self.session.query(
FROM sbo SBoTable.requires).filter(
WHERE name = '{}'""".format( SBoTable.name == self.name).first()
self.name)).fetchone()
return requires[0].split() return requires[0].split()
def version(self): def version(self):
"""Grabs package version """Grabs package version
""" """
version = self.cur.execute("""SELECT version version = self.session.query(
FROM sbo SBoTable.version).filter(
WHERE name = '{}'""".format( SBoTable.name == self.name).first()
self.name)).fetchone()
return version[0] return version[0]
def checksum(self): def checksum(self):
"""Grabs checksum string """Grabs checksum string
""" """
md5sum, md5sum64, = [], [] md5sum, md5sum64, = [], []
mds5, md5s64 = self.cur.execute("""SELECT md5sum, md5sum64 mds5, md5s64 = self.session.query(
FROM sbo SBoTable.md5sum, SBoTable.md5sum64).filter(
WHERE name = '{}'""".format( SBoTable.name == self.name).first()
self.name)).fetchone()
if mds5: if mds5:
md5sum.append(mds5) md5sum.append(mds5)
if md5s64: if md5s64:
md5sum64.append(md5s64) md5sum64.append(md5s64)
return self._sorting_arch(md5sum, md5sum64) return self._sorting_arch(md5sum, md5sum64)
def description(self): def description(self):
"""Grabs package description """Grabs package description
""" """
desc = self.cur.execute("""SELECT short_desc desc = self.session.query(
FROM sbo SBoTable.short_description).filter(
WHERE name = '{}'""".format( SBoTable.name == self.name).first()
self.name)).fetchone()
return desc[0] return desc[0]
def files(self): def files(self):
"""Grabs files """Grabs files
""" """
files = self.cur.execute("""SELECT files files = self.session.query(
FROM sbo SBoTable.files).filter(
WHERE name = '{}'""".format( SBoTable.name == self.name).first()
self.name)).fetchone()
return files[0] return files[0]
def _sorting_arch(self, arch, arch64): def _sorting_arch(self, arch, arch64):
@ -116,4 +115,5 @@ class SBoGrep(Utils):
""" """
if self.meta.arch == self.arch64 and arch64: if self.meta.arch == self.arch64 and arch64:
return arch64 return arch64
return arch return arch

View file

@ -22,25 +22,17 @@
# 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.repositories import Repo from slpkg.repositories import Repo
from slpkg.__metadata__ import MetaData as _meta_
from slpkg.slack.slack_version import slack_ver from slpkg.slack.slack_version import slack_ver
from slpkg.models.models import SBoTable, session
def sbo_search_pkg(name): def sbo_search_pkg(name):
"""Search for package path in SLACKBUILDS.TXT file and """Search for package path in SLACKBUILDS.TXT file and
return url return url
""" """
db = _meta_.db location = session.query(SBoTable.location).filter(
lib_path = _meta_.lib_path SBoTable.name == name).first()
con = sqlite3.connect(f"{lib_path}{db}")
cur = con.cursor()
location = cur.execute("""SELECT location
FROM sbo
WHERE name = '{}'""".format(name)).fetchone()
repo = Repo() repo = Repo()
sbo = repo.default_repository()["sbo"] sbo = repo.default_repository()["sbo"]