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

Maximum useful, minimum dependency

üst 0942c2ca
# -*- 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
# the terms of the GNU General Public License as published by the Free
......@@ -12,30 +12,36 @@
import os
import re
import shelve
import hashlib
import inary
import inary.context as ctx
import inary.db.lazydb as lazydb
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import inary
import inary.context as ctx
# FIXME:
# 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):
try:
import plyvel
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()
self.filesdb = {}
self.__check_filesdb()
def __del__(self):
self.close()
def has_file(self, path):
if key in self.filesdb:
return True
def create_filesdb(self):
ctx.ui.info(inary.util.colorize(_('Creating files database...'), 'blue'))
installdb = inary.db.installdb.InstallDB()
......@@ -47,36 +53,60 @@ class FilesDB():
ctx.ui.info(inary.util.colorize(_('Added files database...'), 'blue'))
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):
pkg, path = self.get_file(term)
if pkg:
if self.has_file(term):
pkg, path = self.get_file(term)
return [(pkg,[path])]
installdb = inary.db.installdb.InstallDB()
found = []
for pkg in installdb.list_installed():
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:
found.append((pkg, paths))
return found
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:
key=bytes(hashlib.md5(f.path.encode('utf-8')).digest())
value= bytes(pkg.encode('utf-8'))
self.filesdb.put(key=key, value=value)
key=hashlib.md5(f.path.encode('utf-8')).hexdigest()
self.filesdb[key] = pkg
def remove_files(self, 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):
ctx.ui.info(inary.util.colorize(_('Cleaning files database folder... '), 'green'), noln=True)
for f in os.listdir(self.files_ldb_path): os.unlink(os.path.join(self.files_ldb_path, f))
ctx.ui.info(inary.util.colorize(_('done.'), 'green'))
files_db = os.path.join(ctx.config.info_dir(), ctx.const.files_db)
if os.path.exists(files_db):
os.unlink(files_db)
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