saliacoel commited on
Commit
0bc79bc
·
verified ·
1 Parent(s): 4556558

Upload salia_console_get_node.py

Browse files
Files changed (1) hide show
  1. salia_console_get_node.py +175 -0
salia_console_get_node.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shlex
3
+ import subprocess
4
+ from urllib.parse import urlparse
5
+
6
+
7
+ NODES_PATH = "/ComfyUI/custom_nodes/comfyui-salia_online/nodes"
8
+ DEFAULT_REPO_PATH = "https://huggingface.co/saliacoel/x/resolve/main/"
9
+
10
+
11
+ class Salia_Console_Get_Node:
12
+ @classmethod
13
+ def INPUT_TYPES(cls):
14
+ return {
15
+ "required": {
16
+ "items": (
17
+ "STRING",
18
+ {
19
+ "default": "",
20
+ "multiline": True,
21
+ "placeholder": "salia_example_node_1, salia_example_node_2.py, https://huggingface.co/other_hf/other_repo/resolve/main/salia_example_node_3.py, salia_example_4_nodepack.zip",
22
+ },
23
+ ),
24
+ },
25
+ }
26
+
27
+ RETURN_TYPES = ("STRING",)
28
+ RETURN_NAMES = ("status",)
29
+ FUNCTION = "get_nodes"
30
+ CATEGORY = "salia/online"
31
+ OUTPUT_NODE = True
32
+
33
+ @classmethod
34
+ def IS_CHANGED(cls, items):
35
+ return float("nan")
36
+
37
+ def _log(self, message: str):
38
+ print(f"[Salia_Console_Get_Node] {message}")
39
+
40
+ def _split_items(self, items: str):
41
+ parts = items.replace("\n", ",").split(",")
42
+ cleaned = []
43
+
44
+ for part in parts:
45
+ part = part.strip().strip('"').strip("'")
46
+ if part:
47
+ cleaned.append(part)
48
+
49
+ return cleaned
50
+
51
+ def _is_url(self, value: str) -> bool:
52
+ # Accept both https:// and http:// as already-complete URLs.
53
+ return value.startswith(("https://", "http://"))
54
+
55
+ def _has_supported_extension(self, value: str) -> bool:
56
+ target = urlparse(value).path if self._is_url(value) else value
57
+ target = target.lower()
58
+ return target.endswith(".py") or target.endswith(".zip")
59
+
60
+ def _normalize_item(self, item: str) -> str:
61
+ item = item.strip().strip('"').strip("'")
62
+
63
+ # 1) Add .py if it is neither .py nor .zip
64
+ if not self._has_supported_extension(item):
65
+ item += ".py"
66
+
67
+ # 2) Prefix default Hugging Face repo if it is not already a URL
68
+ if not self._is_url(item):
69
+ item = DEFAULT_REPO_PATH + item.lstrip("/")
70
+
71
+ return item
72
+
73
+ def _filename_from_url(self, url: str) -> str:
74
+ filename = os.path.basename(urlparse(url).path)
75
+ if not filename:
76
+ raise ValueError(f"Could not determine filename from URL: {url}")
77
+ return filename
78
+
79
+ def _run_command(self, args, cwd=None):
80
+ command_text = " ".join(shlex.quote(str(arg)) for arg in args)
81
+ if cwd:
82
+ self._log(f"$ cd {cwd} && {command_text}")
83
+ else:
84
+ self._log(f"$ {command_text}")
85
+
86
+ completed = subprocess.run(
87
+ args,
88
+ cwd=cwd,
89
+ capture_output=True,
90
+ text=True,
91
+ )
92
+
93
+ if completed.stdout:
94
+ print(completed.stdout)
95
+ if completed.stderr:
96
+ print(completed.stderr)
97
+
98
+ return completed
99
+
100
+ def get_nodes(self, items: str):
101
+ entries = self._split_items(items)
102
+
103
+ if not entries:
104
+ self._log("No items provided.")
105
+ return ("OK: 0\nERROR: 0",)
106
+
107
+ total_count = len(entries)
108
+ ok_count = total_count
109
+ error_count = 0
110
+
111
+ try:
112
+ os.makedirs(NODES_PATH, exist_ok=True)
113
+ except Exception as e:
114
+ self._log(f"Could not create/access nodes path: {e}")
115
+ return (f"OK: 0\nERROR: {total_count}",)
116
+
117
+ for original_item in entries:
118
+ try:
119
+ url = self._normalize_item(original_item)
120
+ filename = self._filename_from_url(url)
121
+ lower_filename = filename.lower()
122
+
123
+ self._log(f"Resolved: {original_item} -> {url}")
124
+
125
+ if lower_filename.endswith(".py"):
126
+ completed = self._run_command(
127
+ ["wget", "-O", filename, url],
128
+ cwd=NODES_PATH,
129
+ )
130
+ if completed.returncode != 0:
131
+ raise RuntimeError(f"wget failed for {filename}")
132
+
133
+ elif lower_filename.endswith(".zip"):
134
+ completed = self._run_command(["apt-get", "update"])
135
+ if completed.returncode != 0:
136
+ raise RuntimeError("apt-get update failed")
137
+
138
+ completed = self._run_command(["apt-get", "install", "-y", "unzip"])
139
+ if completed.returncode != 0:
140
+ raise RuntimeError("apt-get install -y unzip failed")
141
+
142
+ completed = self._run_command(
143
+ ["wget", "-O", filename, url],
144
+ cwd=NODES_PATH,
145
+ )
146
+ if completed.returncode != 0:
147
+ raise RuntimeError(f"wget failed for {filename}")
148
+
149
+ completed = self._run_command(
150
+ ["unzip", "-o", filename],
151
+ cwd=NODES_PATH,
152
+ )
153
+ if completed.returncode != 0:
154
+ raise RuntimeError(f"unzip failed for {filename}")
155
+
156
+ else:
157
+ raise RuntimeError(f"Unsupported file type: {filename}")
158
+
159
+ except Exception as e:
160
+ error_count += 1
161
+ ok_count -= 1
162
+ self._log(f"ERROR for '{original_item}': {e}")
163
+
164
+ summary = f"OK: {ok_count}\nERROR: {error_count}"
165
+ self._log(summary.replace("\n", " | "))
166
+ return (summary,)
167
+
168
+
169
+ NODE_CLASS_MAPPINGS = {
170
+ "Salia_Console_Get_Node": Salia_Console_Get_Node,
171
+ }
172
+
173
+ NODE_DISPLAY_NAME_MAPPINGS = {
174
+ "Salia_Console_Get_Node": "Salia_Console_Get_Node",
175
+ }