-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_copy_node.py
More file actions
141 lines (114 loc) · 5.19 KB
/
model_copy_node.py
File metadata and controls
141 lines (114 loc) · 5.19 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
import os
import shutil
from pathlib import Path
class ModelCopyNode:
"""
A ComfyUI node that copies a model file from the models folder to the output folder.
Inputs:
- file_path (str): Relative path to the file within the models folder
Outputs:
- success (bool): Whether the copy operation was successful
- output_path (str): Path where the file was copied to
- error_message (str): Error message if copy failed
"""
def __init__(self):
self.output_dir = "output"
self.models_dir = "models"
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"file_path": ("STRING", {
"default": "",
"multiline": False,
"placeholder": "e.g., checkpoint/model.safetensors"
})
}
}
RETURN_TYPES = ("BOOLEAN", "STRING", "STRING")
RETURN_NAMES = ("success", "output_path", "error_message")
FUNCTION = "copy_file"
CATEGORY = "file_operations"
def _get_models_directory_listing(self):
"""
Generate a listing of all files in the models directory and subdirectories.
Returns:
str: Formatted string listing all files with relative paths
"""
try:
models_path = Path(self.models_dir)
if not models_path.exists():
return f"Models directory '{self.models_dir}' does not exist"
if not models_path.is_dir():
return f"'{self.models_dir}' is not a directory"
file_list = []
for root, dirs, files in os.walk(models_path):
# Convert to relative path from models directory
rel_root = Path(root).relative_to(models_path)
for file in files:
if rel_root == Path('.'):
file_list.append(file)
else:
file_list.append(str(rel_root / file))
if not file_list:
return f"No files found in '{self.models_dir}' directory"
# Sort the file list for better readability
file_list.sort()
listing = f"Available files in '{self.models_dir}' directory:\n"
listing += "\n".join(file_list)
listing += f"\n\nTotal files: {len(file_list)}"
return listing
except Exception as e:
return f"Error generating file listing: {str(e)}"
def copy_file(self, file_path):
"""
Copy a file from the models folder to the output folder.
Args:
file_path (str): Relative path to the file within the models folder
Returns:
tuple: (success, output_path, error_message)
"""
try:
# Validate input
if not file_path or not file_path.strip():
error_msg = "File path cannot be empty\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg
# Clean the file path
file_path = file_path.strip()
# Construct full paths
models_full_path = Path(self.models_dir) / file_path
output_full_path = Path(self.output_dir) / Path(file_path).name
# Check if source file exists
if not models_full_path.exists():
error_msg = f"Source file not found: {models_full_path}\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg
# Check if source is actually a file
if not models_full_path.is_file():
error_msg = f"Source path is not a file: {models_full_path}\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg
# Create output directory if it doesn't exist
output_full_path.parent.mkdir(parents=True, exist_ok=True)
# Copy the file
shutil.copy2(models_full_path, output_full_path)
# Verify the copy was successful
if output_full_path.exists():
return True, str(output_full_path), ""
else:
error_msg = "File copy failed - destination file not found after copy\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg
except PermissionError:
error_msg = f"Permission denied when copying file\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg
except OSError as e:
error_msg = f"OS error: {str(e)}\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg
except Exception as e:
error_msg = f"Unexpected error: {str(e)}\n\n"
error_msg += self._get_models_directory_listing()
return False, "", error_msg