huangrunheng 3 лет назад
Родитель
Сommit
24b0179c75

+ 5 - 0
module/server/__init__.py

@@ -0,0 +1,5 @@
+# This must be the first to import
+from module.logger import logger  # Change folder
+import deploy.logger
+
+deploy.logger.logger = logger

+ 65 - 0
module/server/app.py

@@ -0,0 +1,65 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import argparse
+from fastapi import FastAPI
+
+from module.logger import logger
+
+from module.server.home_router import home_app
+from module.server.script_router import script_app
+
+app = FastAPI(
+    title='OAS',
+    description='OAS web service',
+    version='0.0.0',
+)
+app.include_router(home_app)
+app.include_router(script_app)
+
+@app.on_event("startup")
+async def startup_event():
+    logger.info('OAS web service startup done')
+    pass
+
+@app.on_event("shutdown")
+async def shutdown_event():
+    logger.info('OAS web service shutdown done')
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+def fastapi_app():
+    parser = argparse.ArgumentParser(description="OAS web service")
+    parser.add_argument(
+        "-k", "--key", type=str, help="Password of OAS. No password by default"
+    )
+    parser.add_argument(
+        "--cdn",
+        action="store_true",
+        help="Use jsdelivr cdn for pywebio static files (css, js). Self host cdn by default.",
+    )
+    parser.add_argument(
+        "--run",
+        nargs="+",
+        type=str,
+        help="Run OAS by config names on startup",
+    )
+    args, _ = parser.parse_known_args()
+
+
+    return app

+ 57 - 0
module/server/config.py

@@ -0,0 +1,57 @@
+"""
+Copy from https://github.com/LmeSzinc/AzurLaneAutoScript
+"""
+from typing import Optional, Union
+from filelock import FileLock
+
+from deploy.config import DeployConfig as _DeployConfig
+from deploy.utils import *
+
+
+def poor_yaml_read_with_lock(file):
+    if not os.path.exists(file):
+        return {}
+
+    with FileLock(f"{file}.lock"):
+        return poor_yaml_read(file)
+
+
+def poor_yaml_write_with_lock(data, file, template_file=DEPLOY_TEMPLATE):
+    folder = os.path.dirname(file)
+    if not os.path.exists(folder):
+        os.mkdir(folder)
+
+    with FileLock(f"{file}.lock"):
+        with FileLock(f"{DEPLOY_TEMPLATE}.lock"):
+            return poor_yaml_write(data, file, template_file)
+
+
+class DeployConfig(_DeployConfig):
+    def show_config(self):
+        pass
+
+    def read(self):
+        """
+        Read and update deploy config, copy `self.configs` to properties.
+        """
+        self.config = poor_yaml_read_with_lock(DEPLOY_TEMPLATE)
+        self.config.update(poor_yaml_read_with_lock(self.file))
+
+        for key, value in self.config.items():
+            if hasattr(self, key):
+                super().__setattr__(key, value)
+
+    def write(self):
+        """
+        Write `self.config` into deploy config.
+        """
+        poor_yaml_write_with_lock(self.config, self.file)
+
+    def __setattr__(self, key: str, value):
+        """
+        Catch __setattr__, copy to `self.config`, write deploy config.
+        """
+        super().__setattr__(key, value)
+        if key[0].isupper() and key in self.config:
+            self.config[key] = value
+            self.write()

+ 90 - 0
module/server/config_manager.py

@@ -0,0 +1,90 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import re
+from pathlib import Path
+
+from module.logger import logger
+
+class ConfigManager:
+
+    @staticmethod
+    def all_script_files() -> list[str]:
+        """
+        获取所有的脚本文件 除了tmplate
+        :return: ['oas1', 'oas2']
+        """
+        # 获取某个路径的所有json文件名
+        config_path = Path.cwd() / 'config'
+        json_files = config_path.glob('*.json')
+        result = []
+        for json in json_files:
+            if json.stem == 'template':
+                continue
+            result.append(json.stem)
+        return result
+
+    @staticmethod
+    def all_json_file() -> list:
+        """
+        获取所有的json文件
+        :return: ['oas1', 'oas2']
+        """
+        # 获取某个路径的所有json文件名
+        config_path = Path.cwd() / 'config'
+        json_files = config_path.glob('*.json')
+        result = []
+        for json in json_files:
+            if json.stem == 'template':
+                result.insert(0, json.stem)
+            else:
+                result.append(json.stem)
+        return result
+
+    @staticmethod
+    def copy(file: str, template: str = 'template') -> None:
+        """
+        复制一个配置文件
+        :param file:  不带json后缀
+        :param template:
+        :return:
+        """
+        config_path = Path.cwd() / 'config'
+        template_path = config_path / f'{template}.json'
+        file_path = config_path / f'{file}.json'
+        if file_path.exists():
+            logger.error(f'{file_path} is exists')
+            return
+
+        with open(template_path, 'r', encoding='utf-8') as f:
+            template_content = f.read()
+        with open(file_path, 'w', encoding='utf-8') as f:
+            f.write(template_content)
+        logger.info(f'copy {template_path} to {file_path}')
+
+
+    @staticmethod
+    def generate_script_name(self) -> str:
+        """
+        生成一个新的配置的名字
+        :return:
+        """
+        all_script_files = self.all_script_files()
+        if not all_script_files:
+            return 'oas1'
+
+        script_numbers = []
+        for script_file in all_script_files:
+            match = re.search(r'\d+', script_file)
+            if match:
+                script_number = int(match.group())
+                script_numbers.append(script_number)
+
+        if not script_numbers:
+            return 'oas1'
+        script_numbers.sort()
+        new_script_number = script_numbers[-1] + 1
+        return f'oas{new_script_number}'
+
+
+

+ 20 - 0
module/server/home_router.py

@@ -0,0 +1,20 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+from fastapi import APIRouter
+
+
+home_app = APIRouter(
+    prefix="/home",
+    tags=["home"],
+)
+
+@home_app.get('/test')
+async def home_test():
+    return {'message':'test'}
+
+@home_app.get('/home_menu')
+async def home_menu():
+    return ['Home', 'Updater', 'Tool']
+
+

+ 36 - 0
module/server/main_manager.py

@@ -0,0 +1,36 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# 主进程的管理
+# github https://github.com/runhey
+from multiprocessing.managers import SyncManager
+
+from module.logger import logger
+from module.config.config import Config
+from module.server.script_process import ScriptProcess
+from module.server.config_manager import ConfigManager
+
+class MainManager(ConfigManager):
+    config_cache :Config = None  # 缓存当前切换的配置
+    script_process :dict[str: ScriptProcess] = None  # 脚本进程
+
+    def __init__(self) -> None:
+        super().__init__()
+        self.manager = SyncManager()  # 进程管理器
+        self.manager.start()
+
+
+
+    def ensure_config_cache(self, config_name):
+        if self.config_cache is None:
+            if config_name not in self.all_script_files():
+                logger.warning(f'[{config_name}] script file does not exist')
+                return None
+            self.config_cache = Config(config_name)
+        elif self.config_cache.config_name != config_name:
+            self.config_cache = Config(config_name)
+        return self.config_cache
+
+
+
+
+

+ 89 - 0
module/server/script_process.py

@@ -0,0 +1,89 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# 脚本进程
+# github https://github.com/runhey
+import multiprocessing
+# from multiprocessing import Process, Queue
+from enum import Enum
+
+from module.logger import logger
+
+class ScriptState(int, Enum):
+    INACTIVE = 1
+    RUNNING = 2
+    WARNING = 3
+    UPDATING = 4
+
+class ScriptProcess:
+
+    def __init__(self, config_name: str) -> None:
+        self.config_name = config_name  # config_name
+        self.log_pipe_out, self.log_pipe_in = multiprocessing.Pipe(False)
+        self.state_queue = multiprocessing.Queue()
+        # self.state: ScriptState = ScriptState.INACTIVE
+        self._process = multiprocessing.Process(target=self.func,
+                                                args=(self.state_queue, self.log_pipe_in,),
+                                                name=self.config_name,
+                                                daemon=True)
+
+
+
+    def start(self):
+        if self._process is None:
+            logger.warning(f'Script {self.config_name} is not initialized')
+            return
+        if self._process.is_alive():
+            logger.warning(f'Script {self.config_name} is already running and first stop it')
+            self.stop()
+        self._process.start()
+
+    def stop(self):
+        if self._process is None:
+            logger.warning(f'Script {self.config_name} is not initialized')
+            return
+        if not self._process.is_alive():
+            logger.warning(f'Script {self.config_name} is not running')
+            return
+        self._process.terminate()
+
+
+
+
+    def func(self, state_queue, log_pipe_in):
+        self.start_log(log_pipe_in)
+        try:
+            from script import Script
+            # script = Script(config_name=self.config_name)
+            # script.loop()
+            # 脚本启动
+            from time import sleep
+            logger.info(f'Script {self.config_name} start')
+            sleep(5)
+            logger.info(f'Script {self.config_name} end')
+            raise Exception('test')
+
+        except Exception as e:
+            logger.exception(f'Run script {self.config_name} error')
+            logger.error(f'Error: {e}')
+            raise
+
+    def start_log(self, log_pipe_in) -> None:
+        try:
+            from module.logger import set_file_logger, set_func_logger
+            set_file_logger(name=self.config_name)
+            set_func_logger(log_pipe_in.send)
+        except Exception as e:
+            logger.exception(f'Start log error')
+            logger.error(f'Error: {e}')
+            raise
+
+
+
+if __name__ == '__main__':
+    p = ScriptProcess('oas1')
+    p.start()
+    from time import sleep
+    sleep(10)
+    logger.info(p._process.exitcode)
+
+

+ 65 - 0
module/server/script_router.py

@@ -0,0 +1,65 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+from fastapi import APIRouter
+
+from module.logger import logger
+from module.server.main_manager import MainManager
+from module.server.script_process import ScriptProcess
+
+script_app = APIRouter()
+mm = MainManager()
+
+@script_app.get('/test')
+async def script_test():
+    return {'message':'test'}
+
+@script_app.get('/script_menu')
+async def script_menu():
+    if mm.config_cache is None:
+        logger.warning('未加载配置')
+        return {'message':'未加载配置'}
+    return mm.config_cache.gui_menu
+# ----------------------------------   配置文件管理   ----------------------------------
+@script_app.get('/config_list')
+async def config_list():
+    return mm.all_script_files()
+
+@script_app.post('/config_copy')
+async def config_copy(file: str, template: str = 'template'):
+    mm.copy(file, template)
+    return mm.all_script_files()
+
+
+# ---------------------------------   脚本实例管理   ----------------------------------
+@script_app.get('/{script_name}/start')
+async def script_start(script_name: str):
+    if script_name not in mm.script_process:
+        mm.script_process[script_name] = ScriptProcess(script_name)
+    mm.script_process[script_name].start()
+    return
+
+@script_app.get('/{script_name}/stop')
+async def script_stop(script_name: str):
+    if script_name not in mm.script_process:
+        logger.warning(f'[{script_name}] script process does not exist')
+        return
+    mm.script_process[script_name].stop()
+    return
+
+@script_app.get('/{script_name}/{task}/args')
+async def script_task(script_name: str, task: str):
+    if not mm.ensure_config_cache(task):
+        raise Exception(f'[{script_name}] script file does not exist')
+    return mm.config_cache.get_task_args(task)
+
+@script_app.put('/{script_name}/{task}/{group}/{argument}/value/{value}')
+async def script_task(script_name: str, task: str, group: str, argument: str, value):
+    if not mm.ensure_config_cache(task):
+        raise Exception(f'[{script_name}] script file does not exist')
+    mm.config_cache.set_task_args(task, group, argument, value)
+    return
+
+
+
+

+ 92 - 0
module/server/setting.py

@@ -0,0 +1,92 @@
+"""
+Copy from https://github.com/LmeSzinc/AzurLaneAutoScript
+"""
+import multiprocessing
+import threading
+from multiprocessing.managers import SyncManager
+from typing import TYPE_CHECKING, Callable, Generic, TypeVar
+
+if TYPE_CHECKING:
+    from module.config.config_updater import ConfigUpdater
+    from module.server.config import DeployConfig
+
+T = TypeVar("T")
+
+
+class cached_class_property(Generic[T]):
+    """
+    Code from https://github.com/dssg/dickens
+    Add typing support
+
+    Descriptor decorator implementing a class-level, read-only
+    property, which caches its results on the class(es) on which it
+    operates.
+    Inheritance is supported, insofar as the descriptor is never hidden
+    by its cache; rather, it stores values under its access name with
+    added underscores. For example, when wrapping getters named
+    "choices", "choices_" or "_choices", each class's result is stored
+    on the class at "_choices_"; decoration of a getter named
+    "_choices_" would raise an exception.
+    """
+
+    class AliasConflict(ValueError):
+        pass
+
+    def __init__(self, func: Callable[..., T]):
+        self.__func__ = func
+        self.__cache_name__ = '_{}_'.format(func.__name__.strip('_'))
+        if self.__cache_name__ == func.__name__:
+            raise self.AliasConflict(self.__cache_name__)
+
+    def __get__(self, instance, cls=None) -> T:
+        if cls is None:
+            cls = type(instance)
+
+        try:
+            return vars(cls)[self.__cache_name__]
+        except KeyError:
+            result = self.__func__(cls)
+            setattr(cls, self.__cache_name__, result)
+            return result
+
+
+class State:
+    """
+    Shared settings
+    """
+
+    _init = False
+    _clearup = False
+
+    restart_event: threading.Event = None
+    manager: SyncManager = None
+
+    @classmethod
+    def init(cls):
+        cls.manager = multiprocessing.Manager()
+        cls._init = True
+
+    @classmethod
+    def clearup(cls):
+        cls.manager.shutdown()
+        cls._clearup = True
+
+    @cached_class_property
+    def deploy_config(self) -> "DeployConfig":
+        """
+        Returns:
+            DeployConfig:
+        """
+        from module.server.config import DeployConfig
+
+        return DeployConfig()
+
+    @cached_class_property
+    def config_updater(self) -> "ConfigUpdater":
+        """
+        Returns:
+            ConfigUpdater:
+        """
+        from module.config.config_updater import ConfigUpdater
+
+        return ConfigUpdater()

+ 3 - 1
requirements-in.txt

@@ -17,4 +17,6 @@ inflection==0.5.1
 tqdm==4.65.0
 ppocr-onnx==0.0.3.9
 onepush==1.2.0
-paho-mqtt==1.6.1
+paho-mqtt==1.6.1
+fastapi==0.104.1
+uvicorn==0.23.2

+ 12 - 4
requirements.txt

@@ -6,6 +6,7 @@
 #
 
 adbutils==0.11.0          # via -r requirements-in.txt, uiautomator2
+anyio==3.7.1              # via fastapi, starlette
 anytree==2.8.0            # via -r requirements-in.txt
 apkutils2==1.0.0          # via adbutils
 cached-property==1.5.2    # via uiautomator2
@@ -13,18 +14,22 @@ certifi==2023.5.7         # via requests
 cffi==1.15.1              # via gevent
 charset-normalizer==3.1.0  # via requests
 cigam==0.0.3              # via apkutils2
-colorama==0.4.6           # via logzero, tqdm
+click==8.1.7              # via uvicorn
+colorama==0.4.6           # via click, logzero, tqdm
 coloredlogs==15.0.1       # via onnxruntime
 decorator==5.1.1          # via retry
 deprecated==1.2.13        # via uiautomator2
 deprecation==2.1.0        # via adbutils
+exceptiongroup==1.1.3     # via anyio
+fastapi==0.104.1          # via -r requirements-in.txt
 filelock==3.12.0          # via uiautomator2
 flatbuffers==23.5.26      # via onnxruntime
 future==0.18.3            # via zerorpc
 gevent==22.10.2           # via zerorpc
 greenlet==2.0.2           # via gevent
+h11==0.14.0               # via uvicorn
 humanfriendly==10.0       # via coloredlogs
-idna==3.4                 # via requests
+idna==3.4                 # via anyio, requests
 inflection==0.5.1         # via -r requirements-in.txt
 logzero==1.7.0            # via uiautomator2
 lxml==4.9.2               # via uiautomator2
@@ -45,7 +50,7 @@ protobuf==4.23.3          # via onnxruntime
 py==1.11.0                # via retry
 pyclipper==1.3.0.post4    # via ppocr-onnx
 pycparser==2.21           # via cffi
-pydantic==1.10.7          # via -r requirements-in.txt
+pydantic==1.10.7          # via -r requirements-in.txt, fastapi
 pyelftools==0.29          # via apkutils2
 pygments==2.15.1          # via rich
 pyparsing==3.0.9          # via packaging
@@ -62,12 +67,15 @@ rich==13.3.5              # via -r requirements-in.txt
 shapely==2.0.1            # via ppocr-onnx
 shiboken6==6.4.3          # via pyside6, pyside6-addons, pyside6-essentials
 six==1.16.0               # via adbutils, anytree, uiautomator2
+sniffio==1.3.0            # via anyio
+starlette==0.27.0         # via fastapi
 sympy==1.12               # via onnxruntime
 tqdm==4.65.0              # via -r requirements-in.txt
-typing-extensions==4.5.0  # via pydantic
+typing-extensions==4.8.0  # via fastapi, pydantic, uvicorn
 uiautomator2==2.16.17     # via -r requirements-in.txt
 uiautomator2cache==0.3.0.1  # via -r requirements-in.txt
 urllib3==2.0.2            # via requests
+uvicorn==0.23.2           # via -r requirements-in.txt
 websockets==11.0.3        # via -r requirements-in.txt
 whichcraft==0.6.1         # via adbutils, uiautomator2
 wrapt==1.15.0             # via -r requirements-in.txt, deprecated

+ 63 - 0
server.py

@@ -0,0 +1,63 @@
+# This Python file uses the following encoding: utf-8
+# Copy from https://github.com/LmeSzinc/AzurLaneAutoScript/gui.py
+import threading
+
+from module.logger import logger
+from module.server.setting import State
+
+def fun(ev: threading.Event):
+    import argparse
+    import asyncio
+    import sys
+
+    import uvicorn
+
+    # 不知道干啥的照着抄就行了
+    if sys.platform.startswith("win"):
+        asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
+
+    State.restart_event = ev
+
+    parser = argparse.ArgumentParser(description="Alas web service")
+    parser.add_argument(
+        "--host",
+        type=str,
+        help="Host to listen. Default to WebuiHost in deploy setting",
+    )
+    parser.add_argument(
+        "-p",
+        "--port",
+        type=int,
+        help="Port to listen. Default to WebuiPort in deploy setting",
+    )
+    parser.add_argument(
+        "-k", "--key", type=str, help="Password of alas. No password by default"
+    )
+    parser.add_argument(
+        "--cdn",
+        action="store_true",
+        help="Use jsdelivr cdn for pywebio static files (css, js). Self host cdn by default.",
+    )
+    parser.add_argument(
+        "--run",
+        nargs="+",
+        type=str,
+        help="Run alas by config names on startup",
+    )
+    args, _ = parser.parse_known_args()
+
+    host = args.host or State.deploy_config.WebuiHost or "0.0.0.0"
+    port = args.port or int(State.deploy_config.WebuiPort) or 22270
+
+    logger.hr("Launcher config")
+    logger.attr("Host", host)
+    logger.attr("Port", port)
+    logger.attr("Reload", ev is not None)
+
+    uvicorn.run("module.server.app:fastapi_app", host=host, port=port, factory=True)
+
+
+
+if __name__ == "__main__":
+    fun(None)
+