fix: issue 306 and clear unused import

This commit is contained in:
fatelei 2023-08-27 00:16:30 +08:00
parent 714e913e38
commit 5a742d7aa6
11 changed files with 48 additions and 49 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@ __pycache__
.idea .idea
logs logs
.venv .venv
.vscode .vscode
.pytest_cache

View file

@ -1,9 +1,8 @@
from fastapi import FastAPI import sqlite3
from pydantic import BaseModel
from api.exception.command_exception import CommandException from api.exception.command_exception import CommandException
from api.utils import const from api.utils import const
from api.model.user import User
import sqlite3
def dict_factory(cursor, row): def dict_factory(cursor, row):
d = {} d = {}

View file

@ -1,33 +1,19 @@
from ast import Constant
import os
import io
import sys
import platform
import shutil
import time
import subprocess
import requests import requests
import json import json
import datetime
import socket
import re import re
from threading import Thread
from redis import Redis
from rq import Queue
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, ScheduledJobRegistry, CanceledJobRegistry
from api.utils import shell_execute, docker, const from api.utils import shell_execute, docker, const
from api.model.app import App from api.model.app import App
from api.service import db from api.service import db
from api.model.response import Response
from api.model.config import Config from api.model.config import Config
from api.model.status_reason import StatusReason from api.model.status_reason import StatusReason
from api.utils.log import myLogger from api.utils.log import myLogger
from redis import Redis
from rq import Queue, Worker, Connection
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, ScheduledJobRegistry, CanceledJobRegistry
from api.exception.command_exception import CommandException from api.exception.command_exception import CommandException
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.date import DateTrigger
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.events import EVENT_SCHEDULER_PAUSED, EVENT_SCHEDULER_RESUMED,EVENT_SCHEDULER_STARTED,EVENT_SCHEDULER_SHUTDOWN
# 指定 Redis 容器的主机名和端口 # 指定 Redis 容器的主机名和端口
redis_conn = Redis(host='websoft9-redis', port=6379) redis_conn = Redis(host='websoft9-redis', port=6379)
@ -141,16 +127,18 @@ def AppStoreUpdate():
myLogger.info_logger("You click update appstore, but not need to update") myLogger.info_logger("You click update appstore, but not need to update")
# 获取 update info # 获取 update info
def get_update_list(): def get_update_list(url: str=None):
local_path = '/data/apps/websoft9/version.json' local_path = '/data/apps/websoft9/version.json'
local_version = "0" artifact_url = const.ARTIFACT_URL
if url:
artifact_url = url
try: try:
op = shell_execute.execute_command_output_all("cat " + local_path)['result'] op = shell_execute.execute_command_output_all("cat " + local_path)['result']
local_version = json.loads(op)['VERSION'] local_version = json.loads(op)['VERSION']
except: except:
local_version = "0.0.0" local_version = "0.0.0"
version_cmd = "wget -O version.json " + const.ARTIFACT_URL + "/version.json && cat version.json" version_cmd = f"wget -O version.json {artifact_url}/version.json && cat version.json"
latest = shell_execute.execute_command_output_all(version_cmd)['result'] latest = shell_execute.execute_command_output_all(version_cmd)['result']
version = json.loads(latest)['VERSION'] version = json.loads(latest)['VERSION']
ret = {} ret = {}
@ -161,8 +149,8 @@ def get_update_list():
if compared_version(local_version, version) == -1: if compared_version(local_version, version) == -1:
ret['update'] = True ret['update'] = True
cmd = "wget -O CHANGELOG.md " + const.ARTIFACT_URL + "/CHANGELOG.md && head -n 20 CHANGELOG.md " cmd = f"wget -O CHANGELOG.md {artifact_url}/CHANGELOG.md && head -n 20 CHANGELOG.md"
change_log_contents = shell_execute.execute_command_output_all(cmd)['result'] change_log_contents = shell_execute.execute_command_output(cmd)
change_log = change_log_contents.split('## ')[1].split('\n') change_log = change_log_contents.split('## ')[1].split('\n')
date = change_log[0].split()[-1] date = change_log[0].split()[-1]
for change in change_log[1:]: for change in change_log[1:]:

View file

@ -1,9 +1,6 @@
import os, io, sys, platform, shutil, time, json, datetime, psutil import json, psutil
import re, docker, requests import re
from api.utils import shell_execute
from dotenv import load_dotenv, find_dotenv
import dotenv
from pathlib import Path
from api.utils.log import myLogger from api.utils.log import myLogger
from api.utils import shell_execute, const from api.utils import shell_execute, const
from api.exception.command_exception import CommandException from api.exception.command_exception import CommandException

View file

@ -3,7 +3,6 @@ from api.utils.helper import Singleton
# This class is add/modify/list/delete item to item=value(键值对) model settings file # This class is add/modify/list/delete item to item=value(键值对) model settings file
__all__ = ['settings']
class SettingsFile(object): class SettingsFile(object):

View file

@ -1,5 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os, io, sys, platform, shutil, time, subprocess, json, datetime import subprocess
from api.utils.log import myLogger from api.utils.log import myLogger
from api.exception.command_exception import CommandException from api.exception.command_exception import CommandException
from api.utils import const from api.utils import const
@ -26,11 +27,11 @@ def execute_command_output_all(cmd_str):
if process.returncode == 0 and 'Fail' not in process.stdout and 'fail' not in process.stdout and 'Error' not in process.stdout and 'error' not in process.stdout: if process.returncode == 0 and 'Fail' not in process.stdout and 'fail' not in process.stdout and 'Error' not in process.stdout and 'error' not in process.stdout:
return {"code": "0", "result": process.stdout,} return {"code": "0", "result": process.stdout}
else: else:
myLogger.info_logger("Failed to execute cmd, output failed result") myLogger.info_logger("Failed to execute cmd, output failed result")
myLogger.info_logger(process) myLogger.info_logger(process)
raise CommandException(const.ERROR_SERVER_COMMAND,"Docker returns the original error", process.stderr) raise CommandException(const.ERROR_SERVER_COMMAND, "Docker returns the original error", process.stderr)

View file

@ -1,6 +1,6 @@
from fastapi import APIRouter from fastapi import APIRouter
from api.v1.routers import health, apps from .routers import health, apps
def get_api(): def get_api():

View file

@ -1,15 +1,12 @@
from typing import Optional, List from typing import Optional
from fastapi import APIRouter, status, Depends, Query, Request from fastapi import APIRouter, Query, Request
from pydantic import BaseModel, Field
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
import os, io, sys, platform, shutil, time, subprocess, json, datetime
from api.model.app import App
from api.model.response import Response from api.model.response import Response
from api.service import manage, db from api.service import manage, db
from api.utils import shell_execute, const
from api.utils.log import myLogger from api.utils.log import myLogger
from api.utils import const
from api.exception.command_exception import CommandException from api.exception.command_exception import CommandException
from api.settings.settings import settings from api.settings.settings import settings

View file

@ -13,7 +13,6 @@ from fastapi.openapi.docs import (
import api.v1.api as api_router_v1 import api.v1.api as api_router_v1
from api.utils.log import myLogger from api.utils.log import myLogger
from api.utils import shell_execute
from api.settings.settings import settings from api.settings.settings import settings

View file

@ -5,4 +5,6 @@ apscheduler
docker docker
psutil psutil
gunicorn gunicorn
python-dotenv python-dotenv
pytest
httpx

16
appmanage/test_main.py Normal file
View file

@ -0,0 +1,16 @@
from unittest.mock import patch
from fastapi.testclient import TestClient
from .main import app
client = TestClient(app)
# current not used, because the project layout is not right.
@patch("api.v1.routers.apps.manage")
def test_app_update_list(manage):
manage.get_update_list.return_value = {'date': '', 'content': ''}
response = client.get("/AppUpdateList")
assert response.status_code == 200