Kaydet (Commit) 0f43451c authored tarafından Suleyman Poyraz's avatar Suleyman Poyraz

Maximum useful, minimum dependency

üst 0942c2ca
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob) # Copyright (C) 2005 - 2007, TUBITAK/UEKAE
# #
# This program is free software; you can redistribute it and/or modify it under # This program 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 # the terms of the GNU General Public License as published by the Free
...@@ -12,30 +12,36 @@ ...@@ -12,30 +12,36 @@
import os import os
import re import re
import shelve
import hashlib import hashlib
import inary
import inary.context as ctx
import inary.db.lazydb as lazydb
import gettext import gettext
__trans = gettext.translation('inary', fallback=True) __trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext _ = __trans.gettext
import inary # FIXME:
import inary.context as ctx # We could traverse through files.xml files of the packages to find the path and
# the package - a linear search - as some well known package managers do. But the current
# file conflict mechanism of inary prevents this and needs a fast has_file function.
# So currently filesdb is the only db and we cant still get rid of rebuild-db :/
class FilesDB(lazydb.LazyDB):
class FilesDB():
def __init__(self): def __init__(self):
try: self.filesdb = {}
import plyvel self.__check_filesdb()
except ImportError:
raise ImportError(_("LevelDB not found! Please install LevelDB and plyvel!"))
self.files_ldb_path = os.path.join(ctx.config.info_dir(), ctx.const.files_ldb)
self.filesdb = plyvel.DB(self.files_ldb_path, create_if_missing=True)
if not [f for f in os.listdir(self.files_ldb_path) if f.endswith('.ldb')]:
if ctx.scom: self.destroy()
self.create_filesdb()
def __del__(self): def __del__(self):
self.close() self.close()
def has_file(self, path):
if key in self.filesdb:
return True
def create_filesdb(self): def create_filesdb(self):
ctx.ui.info(inary.util.colorize(_('Creating files database...'), 'blue')) ctx.ui.info(inary.util.colorize(_('Creating files database...'), 'blue'))
installdb = inary.db.installdb.InstallDB() installdb = inary.db.installdb.InstallDB()
...@@ -47,36 +53,60 @@ class FilesDB(): ...@@ -47,36 +53,60 @@ class FilesDB():
ctx.ui.info(inary.util.colorize(_('Added files database...'), 'blue')) ctx.ui.info(inary.util.colorize(_('Added files database...'), 'blue'))
def get_file(self, path): def get_file(self, path):
return self.filesdb.get(hashlib.md5(path.encode('utf-8')).digest()), path key=hashlib.md5(path.encode('utf-8')).hexdigest()
return self.filesdb.get(key), path
def search_file(self, term): def search_file(self, term):
pkg, path = self.get_file(term) if self.has_file(term):
if pkg: pkg, path = self.get_file(term)
return [(pkg,[path])] return [(pkg,[path])]
installdb = inary.db.installdb.InstallDB() installdb = inary.db.installdb.InstallDB()
found = [] found = []
for pkg in installdb.list_installed(): for pkg in installdb.list_installed():
files_xml = open(os.path.join(installdb.package_path(pkg), ctx.const.files_xml)).read() files_xml = open(os.path.join(installdb.package_path(pkg), ctx.const.files_xml)).read()
paths = re.compile('<Path>(.*?{}.*?)</Path>'.format(re.escape(term)), re.I).findall(files_xml) paths = re.compile('<Path>(.*?%s.*?)</Path>' % re.escape(term), re.I).findall(files_xml)
if paths: if paths:
found.append((pkg, paths)) found.append((pkg, paths))
return found return found
def add_files(self, pkg, files): def add_files(self, pkg, files):
self.__check_filesdb()
ctx.ui.info(inary.util.colorize(_('* Adding \'{}\' to db... '), 'purple').format(pkg), noln=True)
for f in files.list: for f in files.list:
key=bytes(hashlib.md5(f.path.encode('utf-8')).digest()) key=hashlib.md5(f.path.encode('utf-8')).hexdigest()
value= bytes(pkg.encode('utf-8')) self.filesdb[key] = pkg
self.filesdb.put(key=key, value=value)
def remove_files(self, files): def remove_files(self, files):
for f in files: for f in files:
self.filesdb.delete(bytes(hashlib.md5(f.path.encode('utf-8')).digest())) key=bytes(hashlib.md5(f.path.encode('utf-8')).hexdigest())
if hashlib.md5(key).hexdigest() in self.filesdb:
del self.filesdb[key]
def destroy(self): def destroy(self):
ctx.ui.info(inary.util.colorize(_('Cleaning files database folder... '), 'green'), noln=True) files_db = os.path.join(ctx.config.info_dir(), ctx.const.files_db)
for f in os.listdir(self.files_ldb_path): os.unlink(os.path.join(self.files_ldb_path, f)) if os.path.exists(files_db):
ctx.ui.info(inary.util.colorize(_('done.'), 'green')) os.unlink(files_db)
def close(self): def close(self):
if not self.filesdb.closed: self.filesdb.close() if isinstance(self.filesdb, shelve.DbfilenameShelf):
self.filesdb.close()
def __check_filesdb(self):
ctx.ui.info(_("Checking filesdb..."))
if isinstance(self.filesdb, shelve.DbfilenameShelf):
return
files_db = os.path.join(ctx.config.info_dir(), ctx.const.files_db)
if not os.path.exists(files_db):
flag = "n"
elif os.access(files_db, os.W_OK):
flag = "w"
else:
flag = "r"
self.filesdb = shelve.open(files_db, flag)
if flag == "n":
self.create_filesdb()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment