Продолжаю работу над БД. Начальная реализация удаления пакетов.

This commit is contained in:
Евгений Титаренко 2023-07-23 23:31:28 +03:00
parent c292a7f516
commit b9fe58a482
3 changed files with 68 additions and 4 deletions

View file

@ -49,13 +49,13 @@ def install(projects: list, mc_ver, loader):
projects_ids = [] projects_ids = []
already_installed = [] already_installed = []
def get_mod_info_from_db(slug): def get_project_info_from_db(slug):
_db = mcgetdb.McGetDB(mcfs.mc_dir) _db = mcgetdb.McGetDB(mcfs.mc_dir)
return _db.select_mod(slug) + _db.select_shader(slug) + _db.select_resourcepack(slug) + _db.select_modpack(slug) return _db.select_mod(slug) + _db.select_shader(slug) + _db.select_resourcepack(slug) + _db.select_modpack(slug)
def check_project(slug): def check_project(slug):
if __name__ == "__main__": if __name__ == "__main__":
mod_info = get_mod_info_from_db(slug) mod_info = get_project_info_from_db(slug)
if mod_info: if mod_info:
already_installed.append(mod_info[0]) already_installed.append(mod_info[0])
return return
@ -204,6 +204,50 @@ def clean():
print("Nothing to clear.") print("Nothing to clear.")
def remove(projects): # TODO: 1. Проверка зависимостей? 2. Модпаки
if __name__ == "__main__":
db = mcgetdb.McGetDB(mcfs.mc_dir)
mods_to_remove = []
resources_to_remove = []
shaders_to_remove = []
modpacks_to_remove = []
for project in projects:
for res in db.select_mod(project):
mods_to_remove.append(res)
for res in db.select_resourcepack(project):
resources_to_remove.append(res)
for res in db.select_shader(project):
shaders_to_remove.append(res)
for res in db.select_modpack(project):
modpacks_to_remove.append(res)
for slug, title, filename, version, _ in mods_to_remove:
local_path = os.path.join("mods", filename)
mcfs.remove(local_path)
db.remove_mod(slug)
print(f"Mod {title} v.{version} was removed.")
for _, title, filename, version, _ in resources_to_remove:
local_path = os.path.join("resourcepacks", filename)
mcfs.remove(local_path)
db.remove_resourcepack(slug)
print(f"Resource pack {title} v.{version} was removed.")
for _, title, filename, version, _ in shaders_to_remove:
local_path = os.path.join("shaderpacks", filename)
mcfs.remove(local_path)
db.remove_shader(slug)
print(f"Shader pack {title} v.{version} was removed.")
for _, title, filename, version, _ in modpacks_to_remove:
raise NotImplementedError
# local_path = os.path.join("modpacks", filename)
# mcfs.remove(local_path)
# db.remove_modpack(slug)
# print(f"Mod pack {title} v.{version} was removed.")
else:
raise NotImplementedError("Not available in module mode")
if __name__ == "__main__": if __name__ == "__main__":
def exit(): def exit():
@ -273,6 +317,10 @@ if __name__ == "__main__":
parser_validate = subparsers.add_parser("validate", help="Validate the installation") parser_validate = subparsers.add_parser("validate", help="Validate the installation")
parser_clean = subparsers.add_parser("clean", help="Clean the cache of this program") parser_clean = subparsers.add_parser("clean", help="Clean the cache of this program")
parser_remove = subparsers.add_parser("remove", help="Remove installed packages")
parser_remove.add_argument("projects", nargs="+")
kwargs = vars(parser.parse_args()) # Получаем все поля получившегося Namespace и пихаем в словарь kwargs = vars(parser.parse_args()) # Получаем все поля получившегося Namespace и пихаем в словарь
if not properties: if not properties:
exit() exit()

View file

@ -129,3 +129,11 @@ def check_file_hash(path, ref_hash):
data = f.read(buffer_size) data = f.read(buffer_size)
hash.update(data) hash.update(data)
return hash.hexdigest() == ref_hash return hash.hexdigest() == ref_hash
def remove(mc_dir_rel_path):
path = os.path.join(mc_dir, mc_dir_rel_path)
if is_path_exist(path):
os.remove(path)
else:
raise FileNotFoundError

View file

@ -63,6 +63,11 @@ class McGetDB:
self.select_shader = self.__db_select_by_col("shaderpacks", "slug") self.select_shader = self.__db_select_by_col("shaderpacks", "slug")
self.select_modpack = self.__db_select_by_col("modpacks", "slug") self.select_modpack = self.__db_select_by_col("modpacks", "slug")
self.remove_mod = self.__db_remove_by_col("mods", "slug")
self.remove_resourcepack = self.__db_remove_by_col("resourcepacks", "slug")
self.remove_shader = self.__db_remove_by_col("shaderpacks", "slug")
self.remove_modpack = self.__db_remove_by_col("modpacks", "slug")
def get_properties(self): def get_properties(self):
properties = None properties = None
with self.db as db: with self.db as db:
@ -100,8 +105,11 @@ class McGetDB:
return res.fetchall() return res.fetchall()
return selection_func return selection_func
def remove_mod(self): def __db_remove_by_col(self, table, col):
pass def removal_func(col_value):
with self.db as db:
db.execute(f'DELETE FROM {table} WHERE {col} = "{col_value}"')
return removal_func
def update_mod(self): def update_mod(self):
pass pass