forked from LinuxCNC/linuxcnc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_ini
More file actions
executable file
·436 lines (388 loc) · 17.8 KB
/
update_ini
File metadata and controls
executable file
·436 lines (388 loc) · 17.8 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/python
THIS_VERSION = "1.0"
import sys
import os
import shutil
import linuxcnc
import re
import datetime
from Tkinter import *
import tkMessageBox
def copysection(block):
#Just makes a straight copy of blocks that don't need any work
regex = "^\s*\[%s\](.+?)(?:^\s*\[|\Z)" % block
section = re.search(regex, inistring, re.M | re.DOTALL)
newini.write("\n[%s]" % block)
if section != None:
newini.write(section.group(1))
all_sections.remove(block)
else:
newini.write("\n#No Content\n")
def writeifexists(file, section, src_item, dest_item = "None"):
#Writes a new entry to the file, but only if it exists
if dest_item == 'None': dest_item = src_item
val = ini.find(section, src_item)
if val: file.write("%s = %s\n" % (dest_item, val))
force = 0
dialogs = 0
filename = None
for opt in sys.argv[1:]:
if opt == '-d':
dialogs = 1
r = Tk()
r.option_add('*Dialog.msg.font', 'Times 12')
r.option_add('*Dialog.msg.wrapLength', '6i')
elif opt == '-f':
force = 1
elif opt[0] == '-':
print "Unknown command line option to update_ini, exiting"
exit()
elif os.path.isfile(opt):
filename = opt
if filename == None:
t = """Usage: update_ini [-d] [-f] filename.ini\n
If the -d flag is used then a dialog box will be displayed
describing the purpose of this script, and giving the user the option
to change their minds\nIf the -f flag is used then no questions will be
asked and the conversion will proceed blindly"""
if dialogs:
tkMessageBox.showerror('invalid options', str(t))
elif not force:
print t
exit()
if dialogs:
ret = tkMessageBox._show("Confirm automatic update",
"This version of LinuxCNC separates the concepts of Axes and "
"Joints which necessitates changes to the INI and HAL files. "
"The changes required are described here:\n"
"http://linuxcnc.org/docs/devel/html/ in the section "
"'Getting Started with LinuxCNC' -> 'Updating LinuxCNC'\n"
"The [EMC]VERSION data in your INI file indicates that your "
"configuration requires update.\n"
"A script exists that can attempt to automatically "
"reconfigure your configuration files.\nPress 'Yes' to perform "
"the conversion, 'No' to continue with the current configuration "
"files or 'Cancel' to exit LinuxCNC.\n"
"The process can not be automatically reversed, though a "
"backup version of your entire existing config will be created.",
tkMessageBox.QUESTION, tkMessageBox.YESNOCANCEL)
if ret == 'cancel': exit(42)
elif ret == 'no': exit(0)
try:
ini = linuxcnc.ini(filename)
except:
t = "%s is not a valid ini file" % filename
if dialogs:
tkMessageBox.showerror('invalid options', t)
elif not force:
print t
exit()
version = ini.find('EMC', 'VERSION')
if not version:
version = "0.0"
if version == "$Revision$":
pass
elif version >= THIS_VERSION:
t = """The supplied INI file is already at version %s and should not need
updating""" % version
if dialogs:
tkMessageBox.showerror('conversion not needed', t)
elif not force:
print t
exit()
if ini.find('KINS', 'JOINTS') and not force:
if dialogs:
if tkMessageBox.askquestion("Already Converted",
"The supplied INI file already has a [KINS] section. this probably "
"means that it was previously converted by hand. Continue conversion?"
"(Change [EMC]VERSION to %s to suppress these messages)"
% THIS_VERSION) != 'yes':
exit(0)
else:
if raw_input("The supplied INI file already has a [KINS] section."
"this probably means that it was previously converted by hand. "
"Continue y/N? (Change [EMC]VERSION to %s to suppress these messages)"
% THIS_VERSION) != "y":
exit(0)
# Looks like we are good to go, so first let's put the original configs
# somewhere safe.
basedir = os.path.dirname(os.path.abspath(filename))
backupdir = os.path.join(basedir, os.path.splitext(os.path.basename(filename))[0] + ".old")
while os.path.isdir(backupdir):
backupdir += ".old"
os.mkdir(backupdir)
old_ini = os.path.join(backupdir, os.path.basename(filename))
for f in os.listdir(basedir):
if os.path.isdir(os.path.join(basedir, os.path.basename(f))):
pass
else:
shutil.copy(os.path.join(basedir, os.path.basename(f)),
os.path.join(backupdir, os.path.basename(f)))
#From now on, we use the backup copy as the reference
ini = linuxcnc.ini(old_ini)
#And the hal files too.
halfiles = ini.findall('HAL', 'HALFILE')
halfiles += ini.findall('HAL', 'POSTGUI_HALFILE')
halfiles += ['touchy.hal']
print "halfiles = ", halfiles
halpaths = []
for halfile in halfiles:
if os.path.isfile(os.path.join(basedir, halfile)):
halpaths.append(os.path.join(basedir, halfile))
elif (os.path.isfile(os.path.join(basedir, "hallib", halfile))):
os.system('cp ' + os.path.join(basedir, "hallib", halfile) + ' ' +
os.path.join(basedir, halfile))
halpaths.append(os.path.join(basedir, halfile))
elif halfile == "touchy.hal":
pass
else:
print "halfile %s not found\n" % halfile
print "halpaths = ", halpaths
inistring = open(filename,'r').read()
newini = open(filename, 'w')
# Get a list of all sections
all_sections = re.findall("^\s*\[(.+)\]", inistring, re.MULTILINE)
# A C-style Switch would be nice here, to allow us to fall through successive
# version updates.
# At the moment there is only one update, but any future updates should be
# a second "if version == 1.0" and so on. The first "if" needs to change the
# version string, though.
if version == "$Revision$" or version < "1.0":
newini.write("# This config file was created %s by the update_ini script\n" % datetime.datetime.now())
newini.write("# The original config files may be found in the %s directory\n\n" % backupdir)
# reproduce everything before the first [section] verbatim
section = re.match("(.*?)^\[", inistring, re.DOTALL | re.MULTILINE)
if section !=None:
newini.write(section.group(1))
#[EMC] Section, change the version number
all_sections.remove("EMC")
section = re.search("\[EMC\](.+?)\n\[", inistring, re.DOTALL)
if section: section = section.group(1)
newini.write("[EMC]\n")
if section != None:
if version != "0.0":
section = re.sub("VERSION (.+)", "VERSION = %s" % THIS_VERSION, section)
else:
newini.write("# The version string for this INI file.\n")
newini.write("VERSION = %s\n" % THIS_VERSION)
newini.write(section)
else:
newini.write("VERSION = %s\n" % THIS_VERSION)
#These sections don't need any work.
copysection("DISPLAY")
copysection("FILTER")
copysection("RS274NGC")
copysection("EMCMOT")
copysection("TASK")
copysection("HAL")
copysection("HALUI")
# In JA [TRAJ] expects MAX_LINEAR_VELOCITY not MAX_VELOCITY
all_sections.remove("TRAJ")
section = re.search("\[TRAJ\](.+?)\n\[", inistring, re.DOTALL)
if section: section = section.group(1)
newini.write("[TRAJ]\n")
if section != None:
if not re.search("MAX_LINEAR_VELOCITY", section):
section = re.sub("MAX_VELOCITY", "MAX_LINEAR_VELOCITY", section)
if not re.search("DEFAULT_LINEAR_VELOCITY", section):
section = re.sub("DEFAULT_VELOCITY", "DEFAULT_LINEAR_VELOCITY", section)
if not re.search("MAX_LINEAR_ACCELERATION", section):
section = re.sub("MAX_ACCELERATION", "MAX_LINEAR_ACCELERATION", section)
if not re.search("DEFAULT_ACCELERATION", section):
section = re.sub("DEFAULT_ACCELERATION", "DEFAULT_LINEAR_ACCELERATION", section)
newini.write(section)
copysection("EMCIO")
# Insert the new-fangled [KINS] section
joints = ini.find("TRAJ", "JOINTS")
if joints == None: joints = ini.find("TRAJ", "AXES")
if joints == None: joints = "3"
joints = int(joints)
# Search the Halfiles to find the kinematics.
kins = None
kinstype = None
coordinates = list("XYZABCUVW")
coords_entry = False
for halfile in halpaths:
hal = open(os.path.join(os.path.dirname(filename), halfile), 'r')
for line in hal.readlines():
match = re.match('(?:#autoconverted|loadrt) +(\w+kins)', line)
if match:
kins = match.group(1)
match = re.search('coordinates *= *(\w+)', line)
if match:
coordinates = list(match.group(1))
coords_entry = 1
match = re.search('kinstype *= *(\w+)', line)
if kinstype: kinstype = match.group(1)
break
if kins: break
if not kins: kins = "trivkins"
#gantrykins and gentrivkins are gone, so need special treatment
if kins == "gantrykins":
kins = "trivkins"
kinstype = "BOTH"
coords_entry = True
for halfile in halpaths:
hal = open(os.path.join(os.path.dirname(filename), halfile), 'r')
for line in hal.readlines():
match = re.match('setp +gantrykins.joint-(\d) +(\d)', line)
if match:
j = int(match.group(1))
if j > joints: joints = j
a = int(match.group(2))
coordinates[j] = 'XYZABCUVW'[a]
coordinates = ''.join(coordinates)
if kins == "gentrivkins":
kins = "trivkins" #trivkins has the same defaults as gentrivkins
newini.write("\n\n[KINS]\n")
newini.write("KINEMATICS = %s" % kins)
if coords_entry: newini.write(" coordinates=%s" % coordinates[: joints])
if kinstype: newini.write(" kinstype=%s" % kinstype)
newini.write("\n")
newini.write("#This is a best-guess at the number of joints, it should be checked\n")
newini.write("JOINTS = %i\n" % joints)
j = 0
lock_mask = 0x0
while 1:
# Search preferentially in "[JOINT_N] in case the file is part-converted already
if re.search("^(\[JOINT_%i\])"%j, inistring, re.MULTILINE):
if re.search("^(\[AXIS_%s\])" % "XYZABCUVW"[j], inistring, re.MULTILINE):
copysection("AXIS_%s" % "XYZABCUVW"[j])
copysection("JOINT_%i" % j)
else:
newini.write("\n[AXIS_%s]\n" % "XYZABCUVW"[j])
writeifexists(newini, "JOINT_%i" % j, "HOME")
writeifexists(newini, "JOINT_%i" % j, "MIN_LIMIT")
writeifexists(newini, "JOINT_%i" % j, "MAX_LIMIT")
writeifexists(newini, "JOINT_%i" % j, "MAX_VELOCITY")
writeifexists(newini, "JOINT_%i" % j, "MAX_ACCELERATION")
copysection("[JOINT_%i]" % j)
elif re.search("^\[AXIS_%i\]" % j, inistring, re.MULTILINE): #Looks like an [AXIS_0] style layout
newini.write("\n[AXIS_%s]\n" % "XYZABCUVW"[j])
writeifexists(newini, "AXIS_%i" % j, "MIN_LIMIT")
writeifexists(newini, "AXIS_%i" % j, "MAX_LIMIT")
writeifexists(newini, "AXIS_%i" % j, "MAX_VELOCITY")
writeifexists(newini, "AXIS_%i" % j, "MAX_ACCELERATION")
if ini.find("AXIS_%i" % j, "LOCKING_INDEXER"):
lock_mask |= 1 << j
newini.write("LOCKING_INDEXER_JOINT = %i\n" % j)
newini.write("\n[JOINT_%i]\n" % j)
section = re.search("\[AXIS_%i\](.+?)(\n\[|$)" % j, inistring, re.DOTALL)
all_sections.remove("AXIS_%i" % j)
if section:
newini.write(section.group(1))
else:
print "File parsing error, found an [AXIS_%i] section, but no content" % j
exit()
elif j >= 9:
break
else:
pass
j += 1
# If there were any custom sections, tag them on the end.
while all_sections:
copysection(all_sections[0])
# and turn the locking mask into a string
if lock_mask:
lock_string = 'unlock_joints_mask=%i' % lock_mask
else:
lock_string = ""
#That's the INI file done:
newini.close()
# Now change all the pin names etc in the linked HAL files.
# Any machine can be jogged in world mode (in theory) but joint-mode jog-enable
# is not auto-linked for safety.
subs = {'axis.(.).active':'joint.\\1.active',
'axis.(.).amp-enable-out': 'joint.\\1.amp-enable-out',
'axis.(.).amp-fault-in': 'joint.\\1.amp-fault-in',
'axis.(.).backlash-corr': 'joint.\\1.backlash-corr',
'axis.(.).backlash-filt': 'joint.\\1.backlash-filt',
'axis.(.).backlash-vel': 'joint.\\1.backlash-vel',
'axis.(.).coarse-pos-cmd': 'joint.\\1.coarse-pos-cmd',
'axis.(.).error': 'joint.\\1.error',
'axis.(.).f-error': 'joint.\\1.f-error',
'axis.(.).f-error-lim': 'joint.\\1.f-error-lim',
'axis.(.).f-errored': 'joint.\\1.f-errored',
'axis.(.).faulted': 'joint.\\1.faulted',
'axis.(.).free-pos-cmd': 'joint.\\1.free-pos-cmd',
'axis.(.).free-tp-enable': 'joint.\\1.free-tp-enable',
'axis.(.).free-vel-lim': 'joint.\\1.free-vel-lim',
'axis.(.).home-state': 'joint.\\1.home-state',
'axis.(.).home-sw-in': 'joint.\\1.home-sw-in',
'axis.(.).homed': 'joint.\\1.homed',
'axis.(.).homing': 'joint.\\1.homing',
'axis.(.).in-position': 'joint.\\1.in-position',
'axis.(.).index-enable': 'joint.\\1.index-enable',
'axis.(.).joint-pos-cmd': 'joint.\\1.pos-cmd',
'axis.(.).joint-pos-fb': 'joint.\\1.pos-fb',
'axis.(.).joint-vel-cmd': 'joint.\\1.vel-cmd',
'axis.(.).kb-jog-active': 'joint.\\1.kb-jog-active',
'axis.(.).motor-offset': 'joint.\\1.motor-offset',
'axis.(.).motor-pos-cmd': 'joint.\\1.motor-pos-cmd',
'axis.(.).motor-pos-fb': 'joint.\\1.motor-pos-fb',
'axis.(.).neg-hard-limit': 'joint.\\1.neg-hard-limit',
'axis.(.).neg-lim-sw-in': 'joint.\\1.neg-lim-sw-in',
'axis.(.).pos-hard-limit': 'joint.\\1.pos-hard-limit',
'axis.(.).pos-lim-sw-in': 'joint.\\1.pos-lim-sw-in',
'axis.(.).wheel-jog-active': 'joint.\\1.wheel-jog-active',
'axis.(.).unlock': 'joint.\\1.unlock',
'axis.(.).is-unlocked': 'joint.\\1.is-unlocked',
'axis.(.).jog-enable': '@ax\\1@.jog-enable',
'axis.(.).jog-counts': 'joint.\\1.jog-counts @ax\\1@.jog-counts',
'axis.(.).jog-scale': 'joint.\\1.jog-scale @ax\\1@.jog-scale',
'axis.(.).jog-vel-mode': 'joint.\\1.jog-vel-mode @ax\\1@.jog-vel-mode',
'halui.axis.(.).pos-commanded':'halui.@ax\\1@.pos-commanded',
'halui.axis.(.).pos-feedback':'halui.@ax\\1@.pos-feedback',
'halui.axis.(.).pos-relative':'halui.@ax\\1@.pos-relative',
'halui.joint.(.).is-selected':'halui.@ax\\1@.is-selected',
'halui.jog.(.).analog': 'halui.@ax\\1@.analog',
'halui.jog.(.).increment': 'halui.@ax\\1@.increment',
'halui.jog.(.).increment-minus':'halui.@ax\\1@.increment-minus',
'halui.jog.(.).increment-plus':'halui.@ax\\1@.increment-plus',
'halui.jog.(.).minus': 'halui.@ax\\1@.minus',
'halui.jog.(.).plus': 'halui.@ax\\1@.plus',
'halui.joint.(.).select': 'halui.@ax\\1@.select',
'halui.jog.selected.increment':'halui.axis.selected.increment',
'halui.jog.selected.increment-minus':'halui.axis.selected.increment-minus',
'halui.jog.selected.increment-plus':'halui.axis.selected.increment-plus',
'halui.jog.selected.minus': 'halui.axis.selected.minus',
'halui.jog.selected.plus': 'halui.axis.selected.plus',
'halui.jog-deadband': 'halui.axis.jog-deadband',
'halui.jog-speed': 'halui.axis.jog-speed',
'halui.joint.selected': 'halui.joint.selected',
'halui.joint.selected.is_homed':'halui.joint.selected.is-homed',
'halui.joint.selected.on-soft-limit':'halui.joint.selected.on-soft-max-limit',
'num_joints=\[TRAJ\]AXES': 'num_joints=[KINS]JOINTS',
'loadrt(.+kins)': 'loadrt [KINS]KINEMATICS\n#autoconverted \\1',
'\[AXIS_(.)\]': '[JOINT_\\1]',
'(setp +gantrykins.+)': '# \\1',
'(.+servo_period_ns.+)': '\\1 %s' % lock_string
}
# converts @axN@ pattern to axis.L and splits any malformed setp from mpg
subs2 = {
'^\s*setp\s+(\S+)\s+(\S+)\s+(\S+)\s*$':
'setp \\1 \\3\nsetp \\2 \\3\n',
'@ax0@': 'axis.%s' % coordinates[0].lower(),
'@ax1@': 'axis.%s' % coordinates[1].lower(),
'@ax2@': 'axis.%s' % coordinates[2].lower(),
'@ax3@': 'axis.%s' % coordinates[3].lower(),
'@ax4@': 'axis.%s' % coordinates[4].lower(),
'@ax5@': 'axis.%s' % coordinates[5].lower(),
'@ax6@': 'axis.%s' % coordinates[6].lower(),
'@ax7@': 'axis.%s' % coordinates[7].lower(),
'@ax8@': 'axis.%s' % coordinates[8].lower()
}
for halfile in halpaths:
halstring = open(halfile,'r').read()
for sub in subs:
halstring = re.sub(sub, subs[sub], halstring, flags=re.M)
for sub in subs2:
halstring = re.sub(sub, subs2[sub], halstring, flags=re.M)
newhal = open(halfile, 'w')
newhal.write(halstring)
newhal.close()
elif version == "1.0":
print """This file does not need converting, and furthermore execution
should never have reached this spot"""
if force:
shutil.rmtree(backupdir)