-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.py
288 lines (206 loc) · 7.77 KB
/
system.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import bpy
import os
import sys
import re
import json
from pprint import pprint
from tempfile import gettempdir
from shutil import rmtree
from . registration import get_prefs
from .. import bl_info
enc = sys.getfilesystemencoding()
def abspath(path):
return os.path.abspath(bpy.path.abspath(path))
def quotepath(path):
if " " in path:
path = '"%s"' % (path)
return path
def add_path_to_recent_files(path):
try:
recent_path = bpy.utils.user_resource('CONFIG', path="recent-files.txt")
with open(recent_path, "r+", encoding=enc) as f:
content = f.read()
f.seek(0, 0)
f.write(path.rstrip('\r\n') + '\n' + content)
except (IOError, OSError, FileNotFoundError):
pass
def get_next_files(filepath, next=True, debug=False):
current_dir = os.path.dirname(filepath)
current_file = os.path.basename(filepath)
blend_files = sorted([f for f in os.listdir(current_dir) if os.path.splitext(f)[1].startswith('.blend')])
current_idx = blend_files.index(current_file)
if debug:
print()
print("files:")
for idx, file in enumerate(blend_files):
if idx == current_idx:
print(" >", file)
else:
print(" ", file)
next_file = None
next_backup_file = None
if next:
next_blend_files = blend_files[current_idx + 1:]
else:
next_blend_files = blend_files[:current_idx]
next_blend_files.reverse()
if debug:
print()
nextstr = 'next' if next else 'previous'
print(f"{nextstr} files:")
for file in next_blend_files:
if debug:
print(" ", file)
ext = os.path.splitext(file)[1]
if next_file is None and ext== '.blend':
next_file = file
if next_backup_file is None and ext.startswith('.blend'):
next_backup_file = file
if next_file and next_backup_file:
break
if debug:
print()
print(f"{nextstr} file:", next_file)
print(f"{nextstr} file (incl. backups):", next_backup_file)
return current_dir, next_file, next_backup_file
def get_temp_dir(context):
temp_dir = context.preferences.filepaths.temporary_directory
if not temp_dir:
temp_dir = gettempdir()
return temp_dir
def open_folder(path):
import platform
import subprocess
if platform.system() == "Windows":
os.startfile(path)
elif platform.system() == "Darwin":
subprocess.Popen(["open", path])
else:
os.system('xdg-open "%s" %s &' % (path, "> /dev/null 2> /dev/null")) # > sends stdout, 2> sends stderr
def makedir(pathstring):
if not os.path.exists(pathstring):
os.makedirs(pathstring)
return pathstring
def get_incremented_paths(currentblend):
path = os.path.dirname(currentblend)
filename = os.path.basename(currentblend)
filenameRegex = re.compile(r"(.+)\.blend\d*$")
mo = filenameRegex.match(filename)
if mo:
name = mo.group(1)
numberendRegex = re.compile(r"(.*?)(\d+)$")
mo = numberendRegex.match(name)
if mo:
basename = mo.group(1)
numberstr = mo.group(2)
else:
basename = name + "_"
numberstr = "000"
number = int(numberstr)
incr = number + 1
incrstr = str(incr).zfill(len(numberstr))
incrname = basename + incrstr + ".blend"
return os.path.join(path, incrname), os.path.join(path, name + '_01.blend')
def remove_folder(path):
if (exists := os.path.exists(path)) and (isfolder := os.path.isdir(path)):
try:
rmtree(path)
return True
except Exception as e:
print(f"WARNING: Error while trying to remove {path}: {e}")
elif exists:
print(f"WARNING: Couldn't remove {path}, it's not a folder!")
else:
print(f"WARNING: Couldn't remove {path}, it doesn't exist!")
return False
def load_json(pathstring):
try:
with open(pathstring, 'r') as f:
jsondict = json.load(f)
return jsondict
except json.decoder.JSONDecodeError:
return False
def save_json(jsondict, pathstring):
try:
with open(pathstring, 'w') as f:
json.dump(jsondict, f, indent=4)
except PermissionError:
pass
def printd(d, name=''):
print(f"\n{name}")
pprint(d, sort_dicts=False)
update_files = None
def get_update_files(force=False):
global update_files
if update_files is None or force:
update_files = []
home_dir = os.path.expanduser('~')
if os.path.exists(home_dir):
download_dir = os.path.join(home_dir, 'Downloads')
home_files = [(f, os.path.join(home_dir, f)) for f in os.listdir(home_dir) if f.startswith(bl_info['name']) and f.endswith('.zip')]
dl_files = [(f, os.path.join(download_dir, f)) for f in os.listdir(download_dir) if f.startswith(bl_info['name']) and f.endswith('.zip')] if os.path.exists(download_dir) else []
zip_files = home_files + dl_files
for filename, path in zip_files:
split = filename.split('_')
if len(split) == 2:
tail = split[1].replace('.zip', '')
s = tail.split('.')
if len(s) >= 3:
try:
version = tuple(int(v) for v in s[:3])
except:
continue
if tail == '.'.join(str(v) for v in bl_info['version']):
continue
update_files.append((path, tail, version))
update_files = sorted(update_files, key=lambda x: (x[2], x[1]))
return update_files
def get_bl_info_from_file(path):
if os.path.exists(path):
lines = ""
with open(path) as f:
for line in f:
if line := line.strip():
lines += (line)
else:
break
try:
blinfo = eval(lines.replace('bl_info = ', ''))
except:
print(f"WARNING: failed reading bl_info from {path}")
return
if 'name' in blinfo and 'version' in blinfo:
name = blinfo['name']
version = blinfo['version']
if name == bl_info['name']:
if version != bl_info['version']:
return blinfo
else:
print(f"WARNING: Versions are identical, an update would be pointless")
else:
print(f"WARNING: Addon Mismatch, you can't update {bl_info['name']} to {name}")
else:
print(f"WARNING: failed reading bl_info from {path}, path does not exist")
def verify_update():
path = get_prefs().path
update_path = os.path.join(path, '_update')
if os.path.exists(update_path):
init_path = os.path.join(update_path, bl_info['name'], 'icon.py')
blinfo = get_bl_info_from_file(init_path)
if blinfo:
get_prefs().update_msg = f"{blinfo['name']} {'.'.join(str(v) for v in blinfo['version'])} is ready to be installed."
get_prefs().show_update = True
else:
remove_folder(update_path)
def install_update():
path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
update_path = os.path.join(path, '_update')
if os.path.exists(update_path):
src = os.path.join(update_path, bl_info['name'])
if os.path.exists(src):
dst = os.path.join(os.path.dirname(path), f"_update_{bl_info['name']}")
if os.path.exists(dst):
remove_folder(dst)
os.rename(src, dst)
remove_folder(path)
os.rename(dst, path)