-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodo.py
260 lines (214 loc) · 8.31 KB
/
dodo.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
from __future__ import print_function
import glob
import sys
import os
import re
import colorama
from colorama import Fore, Back, Style
from subprocess import call
from doit.tools import create_folder
from configobj import ConfigObj
colorama.init()
DOIT_CONFIG = {'verbosity': 1, 'reporter':'executed-only', 'default_tasks': ['scad_to_stl', 'jscad_to_stl', 'stl_to_gcode']}
OPENSCAD = 'openscad'
OPENJSCAD = 'jscad'
SLIC3R = 'prusa-slicer'
SLIC3R_PROFILE_FOLDER = './slicer_profiles'
SLIC3R_DEFAULT_PROFILES = ['default', 'pla', 'normal']
OCTOPI_SERVER = '[email protected]'
OCTOPI_UPLOAD_FOLDER = '.octoprint/uploads/'
TAG_STL = Style.BRIGHT + Back.BLUE + Fore.WHITE + " STL " + Style.RESET_ALL
TAG_GCODE = Style.BRIGHT + Back.GREEN + Fore.WHITE + " GCODE " + Style.RESET_ALL
TAG_RSYNC = Style.BRIGHT + Back.MAGENTA + Fore.WHITE + " RSYNC " + Style.RESET_ALL
TAG_STL_PLAIN = "[ STL ]"
TAG_GCODE_PLAIN = "[ GCODE ]"
TAG_RSYNC_PLAIN = "[ RSYNC ]"
def has_ansi():
return hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
def output_for_scad(scad):
dir = os.path.join(os.path.dirname(scad), 'stl')
file = os.path.splitext(os.path.basename(scad))[0] + '.stl'
file = os.path.join(dir, file)
return (dir, file)
def output_for_jscad(jscad):
dir = os.path.join(os.path.dirname(jscad), 'stl')
file = os.path.splitext(os.path.basename(jscad))[0] + '.stl'
file = os.path.join(dir, file)
fileTmp = os.path.splitext(os.path.basename(jscad))[0] + '.tmp.stl'
fileTmp = os.path.join(dir, fileTmp)
return (dir, file, fileTmp)
def output_for_stl(stl):
dir = os.path.dirname(stl)
file = os.path.splitext(os.path.basename(stl))[0] + '.gcode'
file = os.path.join(dir, file)
return (dir, file)
def depfile_for_scad(scad):
path = os.path.join(os.path.dirname(scad), '.deps')
file = os.path.basename(scad) + '.deps'
return os.path.join(path, file)
def dependencies_for_scad(scad):
deps = [scad]
if (os.path.exists(depfile_for_scad(scad))):
with open(depfile_for_scad(scad)) as f:
next(f)
for line in f:
line = re.sub(r'\\', '', line).strip()
deps += [line]
return deps
def dependencies_for_jscad(jscad):
path = os.path.dirname(jscad)
deps = [jscad]
pattern = re.compile("require\(['\"](.*)['\"]\)")
for line in open(jscad):
for match in re.finditer(pattern, line):
ref = match.groups()[0]
if ref.startswith("."):
file = os.path.join(path, ref)
deps += dependencies_for_jscad(file)
if ref.startswith("sph_jscad_utils"):
deps += dependencies_for_jscad(ref)
return deps
def slic3r_properties_for_stl(stl):
path = os.path.dirname(stl)
while path != '':
# Procura um especifico para aquele stl
file = os.path.join(path, os.path.splitext(os.path.basename(stl))[0] + '.slic3r.properties')
if os.path.exists(file):
return file
file = os.path.join(path, 'slic3r.properties')
if os.path.exists(file):
return file
path = os.path.dirname(path)
def profiles_from_properties(properties):
config = ConfigObj(properties)
if 'profiles' in config:
profiles = config['profiles']
return profiles.strip().split(' ')
return SLIC3R_DEFAULT_PROFILES
def set_env(properties):
if properties:
config = ConfigObj(properties)
if 'm600' in config:
m600 = config['m600']
os.environ['SLIC3R_CUSTOM_PAUSES'] = m600
return
os.environ['SLIC3R_CUSTOM_PAUSES'] = ''
def move_file(source, dest):
os.rename(source, dest)
def profile_files(profiles):
files = profile_file_if_exists('default')
if 'chiquinha' in profiles:
files = []
for profile in profiles:
files += profile_file_if_exists(profile)
for sub_profile in profiles:
files += profile_file_if_exists(profile + '_' + sub_profile)
return files
def profile_file_if_exists(profile):
profile = os.path.join(SLIC3R_PROFILE_FOLDER, profile + '.ini')
if os.path.exists(profile):
return [profile]
else:
return []
def deploy_folder_for(dir):
ret = []
for part in dir.split(os.sep):
if part != 'files' and part != 'stl':
ret += [part.replace(' ', '_')]
return os.path.join(OCTOPI_UPLOAD_FOLDER, os.sep.join(ret))
def title(task):
name = task.name
if name.endswith('-rsync'):
tag = TAG_RSYNC if has_ansi() else TAG_RSYNC_PLAIN
name = name[:-6]
elif name.endswith('stl'):
tag = TAG_STL if has_ansi() else TAG_STL_PLAIN
elif name.endswith('gcode'):
tag = TAG_GCODE if has_ansi() else TAG_GCODE_PLAIN
basename = os.path.basename(name)
path = os.path.dirname(name).split(os.sep)[1]
rest = Style.BRIGHT + basename + Style.RESET_ALL if has_ansi() else basename
return '%s %s %s' % (tag, path, rest)
def task_scad_to_stl():
"""Generate STLs"""
for root, dirs, files in os.walk("."):
if root.endswith('lib'):
continue
for scad in glob.glob(root + '/*.scad'):
if 'node_modules' in scad:
continue
(pathstl, stl) = output_for_scad(scad)
pathdeps = os.path.dirname(depfile_for_scad(scad))
yield {
'name': stl,
'title': title,
'actions': [
(create_folder, [pathstl]),
(create_folder, [pathdeps]),
[OPENSCAD, "-m", "make", "-o", stl, "-d", depfile_for_scad(scad), scad]],
'file_dep': dependencies_for_scad(scad),
'targets': [stl]
}
def task_jscad_to_stl():
"""Generate STLs"""
for root, dirs, files in os.walk("."):
if root.endswith('lib'):
continue
for jscad in glob.glob(root + '/*.jscad'):
if 'node_modules' in jscad:
continue
(pathstl, stl, tmp) = output_for_jscad(jscad)
# pathdeps = os.path.dirname(depfile_for_jscad(jscad))
yield {
'name': stl,
'title': title,
'actions': [
(create_folder, [pathstl]),
[OPENJSCAD, jscad, "-o", tmp],
(move_file, [tmp, stl])
],
'file_dep': dependencies_for_jscad(jscad),
'targets': [stl]
}
def task_stl_to_gcode():
"""Generate gcode"""
for root, dirs, files in os.walk("."):
for stl in glob.glob(root + '/*.stl'):
if 'node_modules' in stl:
continue
(pathgcode, gcode) = output_for_stl(stl)
slic3r_properties = slic3r_properties_for_stl(stl)
if slic3r_properties:
profiles = profile_files(profiles_from_properties(slic3r_properties)) + [slic3r_properties]
else:
profiles = profile_files(SLIC3R_DEFAULT_PROFILES)
profiles_args = []
for profile in profiles:
profiles_args += ['--load', profile]
common_args = []
# if not 'chiquinha' in profiles:
common_args += ['--center', '125,105']
yield {
'name': gcode,
'title': title,
'actions': [
(create_folder, [pathgcode]),
(set_env, [slic3r_properties]),
[SLIC3R, stl, '-g', '--post-process', './post_process.py', '--output', gcode] + common_args + profiles_args],
'file_dep': [stl] + profiles,
'targets': [gcode]
}
def task_deploy():
"""Send gcode files to server"""
for root, dirs, files in os.walk("."):
for gcode in glob.glob(root + '/*.gcode'):
yield {
'name': gcode + "-rsync",
'title': title,
'actions': [
['ssh', OCTOPI_SERVER, 'mkdir -p "' + deploy_folder_for(root) + '"'],
['rsync', '-azhe', 'ssh', '--partial', '--progress',
gcode, OCTOPI_SERVER + ':"' + os.path.join(deploy_folder_for(root), os.path.basename(gcode)) + '"']],
'task_dep':['scad_to_stl', 'jscad_to_stl', 'stl_to_gcode'],
'file_dep': [gcode]
}