-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path__init__.py
More file actions
209 lines (177 loc) · 8.53 KB
/
Copy path__init__.py
File metadata and controls
209 lines (177 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import importlib.util
import importlib
from .nodes.node import ConfigManager
from server import PromptServer
from aiohttp import web
config_manager = ConfigManager()
import os
import subprocess
import logging
import requests
routes = PromptServer.instance.routes
@routes.post('/my_node/set_key')
async def set_key(request):
try:
data = await request.json()
key = data.get("api_key", "")
config_manager.set_api_key(key)
print(f"收到保存请求,KEY长度: {len(key)}")
except Exception as e:
print(f"保存KEY失败: {str(e)}")
return web.json_response({"msg": f"保存失败: {e}"}, status=500)
return web.json_response({"msg": "ok"})
@routes.get('/my_node/get_key')
async def get_key(request):
oneapi_url, oneapi_token = config_manager.get_api_config()
# 读取pyproject.toml中的version
import toml
pyproject_path = os.path.join(os.path.dirname(__file__), "pyproject.toml")
with open(pyproject_path, "r", encoding="utf-8") as f:
pyproject_data = toml.load(f)
version = pyproject_data["project"]["version"]
return web.json_response({
"msg": oneapi_token,
"version": version
})
@routes.get('/my_node/get_user')
async def get_user(request):
oneapi_url, oneapi_token = config_manager.get_api_config()
oneapi_token = oneapi_token[3:]
response = requests.get(f"https://mojieaigc.com/api/userinfoo?oneapi_token={oneapi_token}")
data = response.json()
print(f"用户信息响应: {data}")
username = data.get("username", "未知用户")
quota = data.get("quota", 0)
quota = round(quota/100, 2)
print(f"获取用户信息: {username}, 配额: {quota}")
return web.json_response({
"msg": oneapi_token,
"username": username,
"quota": quota
})
@routes.get('/my_node/get_furniture_styles')
async def get_furniture_styles(request):
try:
url = "http://admin.qihuaimage.com/items/furniture_style"
response = requests.get(url)
response.raise_for_status()
result = response.json()
# 处理数据:创建去重的parentname列表和以parentname为键的字典
data = result.get('data', [])
# 创建去重的parentname列表
parentname_list = list(set(item['parentname'] for item in data))
parentname_list.sort() # 排序
# 创建以parentname为键,typename列表为值的字典
parentname_dict = {}
for item in data:
parentname = item['parentname']
typename = item['typename']
if parentname not in parentname_dict:
parentname_dict[parentname] = []
parentname_dict[parentname].append(typename)
# 返回家具风格数据
return web.json_response({
"furniture_types": parentname_list,
"style_dict": parentname_dict
})
except Exception as e:
logging.error(f"获取家具风格数据失败: {str(e)}")
return web.json_response({"error": str(e)}, status=500)
@routes.post('/my_node/update')
async def update(request):
try:
oneapi_url, oneapi_token = config_manager.get_api_config()
repo_dir = os.path.dirname(__file__)
logging.info(f"[update] 仓库目录: {repo_dir}")
logging.info("[update] 开始更新")
def run_ok(cmd):
try:
result = subprocess.run(
cmd, cwd=repo_dir, text=True, capture_output=True, check=True
)
# 成功:仅用于调试的简洁日志;不要用输出内容当成功条件
logging.info(f"[update] ✔ {' '.join(cmd)}\nstdout: {result.stdout.strip()}\nstderr: {result.stderr.strip()}")
return True
except subprocess.CalledProcessError as e:
logging.error(f"[update] ✖ {' '.join(cmd)}\nstdout: {e.stdout.strip() if e.stdout else ''}\nstderr: {e.stderr.strip() if e.stderr else ''}")
return False
# 检查是否存在.git目录,如果不存在则初始化git仓库
git_dir = os.path.join(repo_dir, ".git")
if not os.path.exists(git_dir):
logging.info("[update] .git目录不存在,正在初始化git仓库")
if not run_ok(["git", "init"]):
logging.error("[update] git初始化失败")
return web.json_response({"msg": "git初始化失败"}, status=500)
logging.info("[update] git仓库初始化成功")
def ensure_remote(name, url):
try:
remotes = subprocess.check_output(["git", "remote"], cwd=repo_dir, text=True).splitlines()
if name not in remotes:
run_ok(["git", "remote", "add", name, url])
logging.info(f"[update] 新增远程: {name} → {url}")
else:
current_url = subprocess.check_output(["git", "remote", "get-url", name], cwd=repo_dir, text=True).strip()
if current_url != url:
run_ok(["git", "remote", "set-url", name, url])
logging.info(f"[update] 更新远程 {name} → {url}")
except Exception as e:
logging.error(f"[update] ensure_remote 出错: {e}")
def get_remote_head_branch(remote):
# 解析 "git remote show <remote>" 里的 "HEAD branch: xxx"
try:
info = subprocess.check_output(["git", "remote", "show", remote], cwd=repo_dir, text=True, errors="ignore")
for line in info.splitlines():
if "HEAD branch" in line:
return line.split(":")[-1].strip()
except Exception as e:
logging.error(f"[update] 获取 {remote} 默认分支失败: {e}")
return None
GITHUB_NAME, GITHUB_URL = "github", "https://github.com/MoJIeAIGC/comfyui-MJAPI-party.git"
GITEE_NAME, GITEE_URL = "gitee", "https://gitee.com/moja_1/comfyui-MJAPI-party.git"
ensure_remote(GITHUB_NAME, GITHUB_URL)
ensure_remote(GITEE_NAME, GITEE_URL)
def hard_reset(remote):
# 自动识别远程默认分支,失败则尝试 main/master
branch = get_remote_head_branch(remote) or "main"
logging.info(f"[update] {remote} 默认分支: {branch}")
# fetch 默认分支;若失败且不是 master,则再尝试 master
if not run_ok(["git", "fetch", remote, branch]):
if branch != "master" and run_ok(["git", "fetch", remote, "master"]):
branch = "master"
else:
return False
# 用 checkout -B 强制把本地 <branch> 指向 remote/<branch>
# -B:存在则重置,不存在则创建;-f 强制切换避免工作区阻塞
if not run_ok(["git", "checkout", "-f", "-B", branch, f"{remote}/{branch}"]):
return False
# 强制把工作区/索引对齐远程
if not run_ok(["git", "reset", "--hard", f"{remote}/{branch}"]):
return False
# (可选)清理未跟踪文件:如需更“干净”,解除注释下一行
# run_ok(["git", "clean", "-fd"])
logging.info(f"[update] 已成功同步到 {remote}/{branch}")
return True
# 国内网络优先用 Gitee,同步失败再回退 GitHub
success = hard_reset(GITHUB_NAME) or hard_reset(GITEE_NAME)
if not success:
logging.error("[update] GitHub 和 Gitee 同步都失败")
return web.json_response({"msg": "更新失败"}, status=500)
logging.info("[update] 更新完成")
if oneapi_token:
config_manager.set_api_key(oneapi_token)
logging.info(f"更新完成,设置API_KEY长度: {len(oneapi_token)}")
return web.json_response({"msg": "更新完成,已是最新版本,重启comfyui生效"})
except Exception as e:
logging.exception("[update] 发生异常")
return web.json_response({"msg": f"更新失败: {e}"}, status=500)
node_list = [
"node",
]
WEB_DIRECTORY = "./web"
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
for module_name in node_list:
imported_module = importlib.import_module(f".nodes.{module_name}", __name__)
NODE_CLASS_MAPPINGS = {**NODE_CLASS_MAPPINGS, **imported_module.NODE_CLASS_MAPPINGS}
NODE_DISPLAY_NAME_MAPPINGS = {**NODE_DISPLAY_NAME_MAPPINGS, **imported_module.NODE_DISPLAY_NAME_MAPPINGS}
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]