Temporary commit

This commit is contained in:
Liu Zhicong
2023-08-14 12:44:19 -07:00
parent 9814889d34
commit 3d8f6fd5c8
21 changed files with 380 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
from .agent_manager import agent_manager,agent_manager_client
from .agent import ai_agent
from .templete import ai_agent_templete
+3
View File
@@ -0,0 +1,3 @@
class ai_agent:
def __init__(self) -> None:
pass
@@ -0,0 +1,40 @@
from .agent import ai_agent
from .templete import ai_agent_templete
class agent_manager:
_instance = None
def __init__(self) -> None:
pass
def __new__(cls):
if cls._instance is None:
cls._instance = super(agent_manager, cls).__new__(cls)
return cls._instance
def initial(self,root_dir:str) -> None:
pass
def get(self,agent_id) -> ai_agent:
pass
def get_templete(self,templete_id) -> ai_agent_templete:
pass
def install(self,templete_id) -> int:
installer = None
#installer.install(templete_id)
pass
def create(self,template,agent_name,agent_last_name,agent_introduce) -> ai_agent:
pass
class agent_manager_client:
def __init__(self) -> None:
pass
+3
View File
@@ -0,0 +1,3 @@
class ai_agent_templete:
def __init__(self) -> None:
pass
+1
View File
@@ -0,0 +1 @@
from .cid import content_id
+8
View File
@@ -0,0 +1,8 @@
class content_id:
def __init__(self) -> None:
pass
def create_from_str(cid_str:str):
pass
+124
View File
@@ -0,0 +1,124 @@
import logging
import toml
from .pkg import pkg_info,pkg_media_info
from .installer import pkg_installer
logger = logging.getLogger(__name__)
#pkg_env_mgr,以cfg_path为key管理多个pkg_env,是个单件
class pkg_env_mgr:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(pkg_env_mgr, cls).__new__(cls)
return cls._instance
def __init__(self) -> None:
self._pkg_envs = {}
pass
def get_env(self,cfg_path:str) -> pkg_env:
if cfg_path in self._pkg_envs:
return self._pkg_envs[cfg_path]
else:
pkg_env = pkg_env(cfg_path)
self._pkg_envs[cfg_path] = pkg_env
return pkg_env
def get_user_env(self) -> pkg_env:
pass
def get_system_env(self) -> pkg_env:
pass
class pkg_env:
def __init__(self,cfg_path:str,inheritance = True) -> None:
self.prefixs : list[str]= []
self.is_strict : bool = True
self.parent_envs : list[pkg_env] = [pkg_env_mgr().get_user_env()]
self.index_dbs = None
self.cfg_path = cfg_path
self._load_pkg_cfg(cfg_path)
pass
def load(self,pkg_name:str,cid:None) -> pkg_media_info:
if cid is None:
#lookup pkg's cid from indexDb
pkg_id,verion_str,cid = pkg_info.parse_pkg_name(pkg_name)
if cid is None:
#lookup include pkg-index-db search
pkg_info = self.lookup(pkg_id,verion_str)
if pkg_info is not None:
return self.load(pkg_id,pkg_info.cid)
if self.is_strict is False:
#load pkg not in index-db, easy for debug
for prefix in self.prefixs:
fullpath = prefix + pkg_name
logger.debug("try load {pkg_name} at {fullpath} ...")
if pkg_env.is_valied_media(fullpath):
return self.load_pkg_media_info(fullpath)
logger.warn(f"load {pkg_name} failed: pkg not found in index-db and pkg_dirs")
return None
else:
#got cid here~
return self.load(pkg_id,cid)
else:
#search pkg_id#cid and load pkg_media_info
for prefix in self.prefixs:
fullpath =f"{prefix}{pkg_id}#{cid}"
logger.debug(f"try load {pkg_name} from {fullpath}")
if pkg_env.is_valied_media(fullpath):
media_info = self.load_pkg_media_info(fullpath)
return media_info
logger.warn(f"load {pkg_id}#{cid} error,not found")
def get_pkg_media_info(self,pkg_name:str)->pkg_media_info:
pass
def load_pkg_media_info(self,pkg_full_path:str) -> pkg_media_info:
pass
def lookup(self,pkg_id:str,version_str:str) -> pkg_info:
pass
def get_installer(self) -> pkg_installer:
pass
@classmethod
def is_valied_media(pkg_full_path:str) -> bool:
pass
def _load_pkg_cfg(self,cfg_path:str):
if cfg_path is None:
return
cfg = None
if len(cfg_path) < 1:
return
try:
cfg = toml.load(cfg_path)
except Exception as e:
logger.error(f"read pkg cfg from {cfg_path} failed! unexpected error occurred: {str(e)}")
return
if cfg:
if cfg.env:
if cfg.env.is_strict is not None:
self.is_strict = cfg.env.is_strict
if cfg.env.prefixs is not None:
self.prefixs = self._preprocess_prefixs(cfg.env.prefixs)
def _preprocess_prefixs(self,prefixs):
pass
@@ -0,0 +1 @@
+102
View File
@@ -0,0 +1,102 @@
# installer download pkg by cid, than install it to target dir
import logging
import asyncio
import aiohttp
import aiofiles
from threading import RLock
from ndn_client import content_id
from .pkg import pkg_info
from .env import pkg_env
logger = logging.getLogger(__name__)
INSTALL_TASK_STATE_DONE = 0
INSTALL_TASK_STATE_CHECK_DEPENDENCY = 1
INSTALL_TASK_STATE_INSTALL_DEPENDENCY = 2
INSTALL_TASK_STATE_DOWNLOADING = 3
INSTALL_TASK_STATE_INSTALLING = 4
INSTALL_TAKS_STATE_ERROR = 5
class install_task:
def __init__(self,owner:pkg_env) -> None:
self.owner = owner
self.state = INSTALL_TASK_STATE_CHECK_DEPENDENCY
self.working_task = None
self.error_str = None
class pkg_installer:
def __init__(self,owner_env:pkg_env) -> None:
self.all_tasks = {}
self.owner_env = owner_env
def install(self,pkg_name:str,
install_from_dependency = False, can_upgrade = True,options = None)->Tuple[install_task,str]:
media_info = self.owner_env.get_media_info(pkg_name)
is_upgrade = False
if media_info is not None:
is_upgrade = True
if can_upgrade is False:
error_str = f"pkg:{pkg_name} already installed and can't upgrade"
logger.error(error_str)
return None,error_str
pkg_id,version_str,content_id = pkg_info.parse_pkg_name(pkg_name)
the_pkg_info = self.owner_env.lookup(pkg_id,version_str,content_id)
if the_pkg_info is None:
error_str = f"pkg:{pkg_name} ,content_id:{content_id} not found in index db"
logger.error(error_str)
return None,error_str
result_task = self.all_tasks.get(the_pkg_info.cid)
if result_task is not None:
return result_task,None
result_task = install_task()
self.all_tasks[the_pkg_info.cid] = result_task
async def install_pkg():
# check dependency
if await self.check_dependency(the_pkg_info) is False:
error_str = f"pkg:{pkg_name} check dependency failed"
logger.error(error_str)
result_task.error_str = error_str
result_task.state = INSTALL_TAKS_STATE_ERROR
return
# install dependency
result_task.state = INSTALL_TASK_STATE_INSTALL_DEPENDENCY
for depend_pkg_name in the_pkg_info.depends:
# TODO:这里可能需要用安装队列,直接等待安装完成可能会太慢,也可能会导致循环等待
sub_task,err_str = self.install(depend_pkg_name,True,can_upgrade,options)
result_task.working_task = asyncio.create_task(install_pkg())
return result_task,None
def uninstall(self):
pass
async def check_dependency(self,pkg:pkg_info) -> bool:
#TODO:这里可能会有循环依赖导致的递归无法退出的问题,需要记录已经检查过的pkg
for depend_pkg_name in pkg.depends:
depend_pkg_info = self.owner_env.lookup(depend_pkg_name)
if depend_pkg_info is None:
logger.warn(f"{pkg.name}'s depend pkg {depend_pkg_name} not found in index db")
return False
if await self.check_dependency(depend_pkg_info) is False:
return False
return True
+22
View File
@@ -0,0 +1,22 @@
class pkg_info:
def __init__(self) -> None:
self.name = ""
self.cid = None
self.depends : list[str] = None
pass
def parse_pkg_name(pkg_name:str) -> Tuple[str, str, str]:
#return pkg_id,version_str,cid
pass
@property
def cid(self) -> str:
return self.cid
class pkg_media_info:
def __init__(self) -> None:
pass