-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_subfolder.py
98 lines (77 loc) · 2.31 KB
/
convert_subfolder.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
import os
import sys
import time
import commandline
from subprocess import call
from subprocess import Popen
from subprocess import list2cmdline
from optparse import OptionParser
def cpu_count():
''' Returns the number of CPUs in the system
'''
num = 1
if sys.platform == 'win32':
try:
num = int(os.environ['NUMBER_OF_PROCESSORS'])
except (ValueError, KeyError):
pass
elif sys.platform == 'darwin':
try:
num = int(os.popen('sysctl -n hw.ncpu').read())
except ValueError:
pass
else:
try:
num = os.sysconf('SC_NPROCESSORS_ONLN')
except (ValueError, OSError, AttributeError):
pass
return num
def exec_commands(cmds):
''' Exec commands in parallel in multiple process
(as much as we have CPU)
'''
if not cmds: return # empty list
def done(p):
return p.poll() is not None
def success(p):
return p.returncode == 0
def fail():
sys.exit(1)
max_task = cpu_count()
processes = []
while True:
while cmds and len(processes) < max_task:
task = cmds.pop()
processes.append(Popen(task))
for p in processes:
if done(p):
if success(p):
processes.remove(p)
else:
fail()
if not processes and not cmds:
break
else:
time.sleep(0.05)
commands = []
(options,args) = commandline.create();
inputdir = args[0];
outputdir = args[1];
subfolder_stub = ["python","convert_subfolder.py"];
mesh_stub = ["python",options.usedScript+".py"];
options_stub = [];
for value in (options.__dict__.items()):
options_stub.append("--" + value[0] + "=" + value[1]);
if not os.path.exists(outputdir):
os.makedirs(outputdir)
listdir = os.listdir(inputdir)
i = 1
for mesh in listdir:
if os.path.isdir(inputdir+"\\"+mesh):
finalargs = subfolder_stub + options_stub + [inputdir+"\\"+mesh,outputdir+"\\"+mesh];
call(finalargs);
else:
if(mesh[-3:].lower() == "nif"):
finalargs = mesh_stub + options_stub + [inputdir+"\\"+mesh,outputdir+"\\"+mesh];
commands.append(finalargs);
exec_commands(commands)