-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublic.py
140 lines (122 loc) · 4.06 KB
/
public.py
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
import hashlib
import os
import json
import stat
import uuid
from datetime import datetime
import folder_paths
import sys
import uuid
import re
from comfy.cli_args import parser
import urllib
import urllib.request
import urllib.parse
args = parser.parse_args()
if args and args.listen:
pass
else:
args = parser.parse_args([])
import time
def read_json_from_file(name, path='json/', type_1='json'):
base_url = find_project_custiom_nodes_path() + 'ComfyUI_Appstore/config/' + path
if not os.path.exists(base_url + name):
return None
with open(base_url + name, 'r') as f:
data = f.read()
if data == '':
return None
if type_1 == 'json':
try:
data = json.loads(data)
return data
except ValueError as e:
return None
if type_1 == 'str':
return data
def write_json_to_file(data, name, path='json/', type_1='str'):
base_url = find_project_custiom_nodes_path() + 'ComfyUI_Appstore/config/' + path
if not os.path.exists(base_url):
os.makedirs(base_url)
if type_1 == 'str':
str_data = str(data)
with open(base_url + name, 'w') as f:
f.write(str_data)
elif type_1 == 'json':
with open(base_url + name, 'w') as f:
json.dump(data, f, indent=2)
def get_output(uniqueid, path='json/api/'):
output = read_json_from_file(uniqueid, path, 'json')
if output is not None:
return output
return None
def get_port_from_cmdline():
for i, arg in enumerate(sys.argv):
if arg == '--port' and i + 1 < len(sys.argv):
try:
return int(sys.argv[i + 1])
except ValueError:
pass
match = re.search(r'--port[=\s]*(\d+)', arg)
if match:
try:
return int(match.group(1))
except ValueError:
pass
return 8188
def replace_time_format_in_filename(filename_prefix):
def compute_vars(input):
now = datetime.now()
custom_formats = {
"yyyy": "%Y",
"yy": "%y",
"MM": "%m",
"dd": "%d",
"HH": "%H",
"mm": "%M",
"ss": "%S",
}
date_formats = re.findall(r"%date:(.*?)%", input)
for date_format in date_formats:
original_format = date_format
for custom_format, strftime_format in custom_formats.items():
date_format = date_format.replace(custom_format, strftime_format)
formatted_date = now.strftime(date_format)
input = input.replace(f"%date:{original_format}%", formatted_date)
return input
return compute_vars(filename_prefix)
def is_execution_model_version_supported():
try:
import comfy_execution
return True
except:
return False
def find_project_custiom_nodes_path():
absolute_path = folder_paths.folder_names_and_paths["custom_nodes"][0][0]
if not absolute_path.endswith(os.sep):
absolute_path += os.sep
return absolute_path
def get_time():
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def get_mac_address():
mac = uuid.getnode()
return ':'.join(('%012X' % mac)[i:i + 2] for i in range(0, 12, 2))
def generate_unique_subdomain(mac_address, port):
unique_key = f"{mac_address}:{port}"
hash_object = hashlib.sha256(unique_key.encode())
subdomain = hash_object.hexdigest()[:12]
return subdomain
def set_executable_permission(file_path):
try:
st = os.stat(file_path)
os.chmod(file_path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
print(f"Execution permissions set on {file_path}")
except Exception as e:
print(f"Failed to set execution permissions: {e}")
def download_file(url, dest_path):
try:
with urllib.request.urlopen(url) as response, open(dest_path, 'wb') as out_file:
data = response.read()
out_file.write(data)
except Exception as e:
print(f"Failed to download the file: {e}")