-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpluginloader.py
681 lines (599 loc) · 18.6 KB
/
pluginloader.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# -*- coding: utf-8 -*-
#-------------------------------------------------
#-- plugin loader
#--
#-- microelly 2015
#--
#-- GNU Lesser General Public License (LGPL)
#-------------------------------------------------
import FreeCAD,PySide,os,FreeCADGui,time
from PySide import QtCore, QtGui, QtSvg
from PySide.QtGui import *
global QtGui
global MyAction,say,saye
from configmanager import ConfigManager
import WebGui
__vers__=' version 0.20'
from tools2 import *
import sys, os, zipfile, traceback, time, yaml, urllib, re, platform, pprint
from datetime import datetime
from os.path import expanduser
home = expanduser("~")
__dir__=home+ '/.FreeCAD/Mod/pluginloader'
__dir__=FreeCAD.ConfigGet('UserAppData')+"/Mod/plugins"
ta=FreeCAD.ParamGet('User parameter:Plugins')
fn=ta.GetString("configfile")
os=platform.system()
localdirs=ta.GetBool("localdirs",True)
usbstick=ta.GetBool("usbstick",False)
school=ta.GetBool("school",False)
ta.SetBool("localdirs",localdirs)
ta.SetBool("usbstick",usbstick)
ta.SetBool("school",school)
#def say(s):
# App.Console.PrintMessage(str(s)+"\n")
#global saye
#saye=say
import FreeCAD,os,time,sys,traceback
def sayexc1(mess=''):
exc_type, exc_value, exc_traceback = sys.exc_info()
ttt=repr(traceback.format_exception(exc_type, exc_value,exc_traceback))
lls=eval(ttt)
l=len(lls)
l2=lls[(l-3):]
FreeCAD.Console.PrintError(mess + "\n" +"--> ".join(l2))
#----------------
#
# Plugin loader - install macros, libraries and extra workbenches
#
#-----------------
def timeString():
localtime = time.localtime()
timeString = time.strftime("%Y-%m-%dT%H:%M:%SZ", localtime)
return timeString
# read from file and converted to python
def pathMacro(s):
'''
replace shortname by os path
'''
global localdirs,usbstick,school
kk=('Linux', 'Arch', '4.0.1-1-ARCH', '#1 SMP PREEMPT Wed Apr 29 12:00:26 CEST 2015', 'x86_64')
import os
try:
kk=os.uname()
except:
kk="NOUNAME"
match = re.search('ARCH', kk[2])
if match:
arch=True
else:
arch=False
for k in ["UserHomePath","UserAppData","AppHomePath"]:
pat=r"(.*)"+k+"/"+"(.*)"
m = re.match(pat, s)
if m:
pre=m.group(1)
post=m.group(2)
inn=FreeCAD.ConfigGet(k)
#
# force location sensitive dirs ----------------------------------
#
# localdirs=True
# usbstick=False
# school=False
if localdirs:
inn=FreeCAD.ConfigGet("UserAppData")
if usbstick:
inn=FreeCAD.ConfigGet("AppHomePath")
if school:
inn=FreeCAD.ConfigGet("AppHomePath")
#
# ----------------------------------------------------------------
#
if arch:
if k == "AppHomePath": #Force sensible Plugin folder
if inn == "/usr/":
inn=inn+"share/freecad/"
if inn == "/usr/bin/":
inn="/usr/share/freecad/"
if k == "UserHomePath":
s2=pre+inn+"/"+post
else:
s2=pre+inn+post
else:
if k == "UserHomePath":
s2=pre+inn+"/"+post
else:
s2=pre+inn+post
s=s2
return s
def set_defaults(conf):
for key in conf['plugins'].keys():
for att in conf['defaults'].keys():
if not conf['plugins'][key].has_key(att):
conf['plugins'][key][att]=conf['defaults'][att]
return(conf)
# where am I ?
where=ta.GetString("whereAmI")
if where == 'stick':
__dir__="e:/FreeCAD_0.16.4924_x86_dev_bin/Mod/plugins"
saye("I am on my windows stick")
if where == 'home':
saye('I am at home')
fn=0
if not fn:
fn=__dir__+"/pluginloaderconfig.yaml"
ta.SetString("configfile",fn)
class MyAction( QtGui.QAction):
def __init__(self, name,t,method,*args):
QtGui.QAction.__init__(self,QtGui.QIcon('icons:breakpoint.svg'),name, t)
self.cmd=method
def run(self):
say("!"+self.cmd+"!")
exec(self.cmd)
say("done")
class PluginloaderWidget(QtGui.QWidget):
def __init__(self, master,*args):
QtGui.QWidget.__init__(self, *args)
self.master=master
FreeCAD.mywidget=self
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.config=master.config
self.vollabel = QtGui.QLabel('1. Select Packages ...')
self.vollabel2 = QtGui.QLabel('2. Show Package Info ...')
self.lab2 = QtGui.QLabel(str(datetime.now()))
self.vollabel3 = QtGui.QLabel('3. Install/Update ...')
self.pushButton02 = QtGui.QPushButton()
self.pushButton02.clicked.connect(self.on_pushButton02_clicked)
self.pushButton02.setText("Display ")
self.pushButton03 = QtGui.QPushButton()
self.pushButton03.clicked.connect(self.on_pushButton03_clicked)
self.pushButton03.setText("Run")
self.listWidget = QtGui.QListWidget()
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
kl=self.config.keys()
say(kl)
for k in sorted(kl):
pcm=ConfigManager(k)
hide=pcm.get('_hide_',False)
if not self.config[k]['status'] == "ignore" and not hide:
item = QtGui.QListWidgetItem(k)
self.listWidget.addItem(item)
layout = QtGui.QGridLayout()
self.setStyleSheet("QListWidget,QPushButton {background-color: lightblue;color:brown;} ")
line=4
layout.addWidget(self.vollabel, 0, 0)
line=3
layout.addWidget(self.listWidget, line, 0,1,4)
line+=1
layout.addWidget(self.vollabel2, line, 0)
line+=1
layout.addWidget(self.lab2, line, 0)
line+=1
layout.addWidget(self.pushButton02, line, 0,1,4)
line+=1
layout.addWidget(self.vollabel3, line, 0)
line+=1
layout.addWidget(self.pushButton03, line, 0,1,4)
line+=1
line+=1
self.setLayout(layout)
self.setWindowTitle("Plugin Loader" + __vers__)
def on_pushButton02_clicked(self):
text=""
for sel in self.listWidget.selectedItems():
plugin=sel.text()
text += "*** "+ sel.text() + " ***"
say(text)
say(self.config[sel.text()])
say("ok")
text += "\n" + str(self.config[sel.text()]['author'])
say(text)
text += "\n" + str(self.config[sel.text()]['source'])
say(text)
text += "\n" + str(self.config[sel.text()]['description'])
say(text)
text += "\n"
mess=""
try:
import re
source=str(self.config[sel.text()]['source'])
say(source)
m = re.match(r"https://github.com/(.*)/archive/master.zip", source)
say(m)
say(m.group(1))
gitdate='no date from git'
if m:
fn='https://api.github.com/repos/' + m.group(1) + '/commits'
import urllib,json
data=urllib.urlopen(fn).read()
d = json.loads(data)
dit=d[0]
gitdate=dit['commit']['committer']['date']
say(gitdate)
installdate=FreeCAD.ParamGet('User parameter:/Plugins/'+plugin).GetString("installdate")
if installdate >gitdate:
mess="--- package " + plugin + " is up to date ---"
else:
mess="!!! update for " + plugin + " recommented !!!"
FreeCAD.ParamGet('User parameter:/Plugins/'+plugin).SetString("gitdate",gitdate)
except:
sayexc()
text += "my install: " + FreeCAD.ParamGet('User parameter:/Plugins/'+plugin).GetString("installdate") + "\n"
text += "git version: " + FreeCAD.ParamGet('User parameter:/Plugins/'+plugin).GetString("gitdate","not implemented") + "\n"
text += "\n" + mess + "\n\n"
self.lab2.setText(text)
if self.config[sel.text()].has_key('web'):
WebGui.openBrowser( str(self.config[sel.text()]['web']))
say("done")
def on_pushButton03_clicked(self):
seli=[]
if len(self.listWidget.selectedItems())==0:
dlge("nothing selected - nothing to do")
else:
m=QtGui.QWidget()
# Qt::WindowStaysOnBottomHint
try:
FreeCAD.Console.PrintMessage(str(self))
self.setWindowFlags(QtCore.Qt.WindowStaysOnBottomHint)
dial = QtGui.QMessageBox.question( m,'Message', "Are you sure to install?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.show()
except:
sayexc1()
if dial==PySide.QtGui.QMessageBox.StandardButton.No: return
for sel in self.listWidget.selectedItems():
seli.append(sel.text())
say(sel.text())
self.master.install(sel.text())
self.show()
say("run done")
def transformkeylist(base,liste,val):
key=liste.pop(0)
if base.has_key(key) and len(liste) !=0:
transformkeylist(base[key],liste,val)
else:
if len(liste)==0:
base[key]=val
else:
base[key]={}
transformkeylist(base[key],liste,val)
def delete_key(dict, key):
del dict[key]
return dict
def transformkeytree(base):
cc=base
kks=cc.keys()
kks.sort()
for k in kks:
base=cc
val=cc[k]
if k.__class__ == str:
cc=delete_key(cc,k)
kl=k.split(' ')
liste=kl
else:
liste=[k]
transformkeylist(base,liste,val)
return cc
myconfigstring='''
#------------------------------------------------
#-- individual configuration of the plugin manager
#--
#-- microelly 2015
#--
#-- GNU Lesser General Public License (LGPL)
#-------------------------------------------------
# here you can configure your own package list, toolbars and tabs
#
# plugins section
#
#plugins:
# <<: *plugins
#
# toolbars section
#
# 1. subkey = Name of the Workbench
# 2. subkey = Name of the Toolbar
# 3. subkey = Name of the Item
# 4. subkeys: Confogiuration of the Item (Icon, Command, ...)
#
toolbars:
<<: *toolbars
#
# tabs section
#
# 1. subkey = Name of the Tab
# 2. subkey = Name of the Item
# 3. subkeys: Confogiuration of the Item (Icon, Command, ...)
#
tabs:
<<: *tabs
'''
class PluginLoader(object):
def __init__(self):
mcf=''
config3={}
try:
say("pluginmanager config file "+ fn)
stream = open(fn, 'r').read()
try:
z=ConfigManager('')
fn2=z.get('userconfigfile',"/usr/lib/freecad/Mod/plugins/myconfig.yaml")
say("pluginmanager userconfig file "+ fn2)
mcf = open(fn2, 'r').read()
except:
sayexc("userconfigfile not available",True)
try:
z=ConfigManager('')
fn2=z.get('userconfigfile',"/usr/lib/freecad/Mod/plugins/myconfig.yaml")
say("pluginmanager userconfig file "+ fn2)
mcf = open(fn2, 'w')
mcf.write(myconfigstring)
mcf.close()
mcf = open(fn2, 'r').read()
except:
sayexc("userconfigfile not available and cannot be created",True)
all=stream + mcf
config3 = yaml.load(all)
# config3=set_defaults(config3)
except:
sayexc("Error in " + fn2,True)
say("Try only:" + fn)
config3 = yaml.load(stream)
config3=set_defaults(config3)
for plin in config3['plugins'].keys():
for k in config3['plugins'][plin].keys():
if config3['plugins'][plin][k] and config3['plugins'][plin][k].__class__ == dict:
print "dict"
print config3['plugins'][plin][k]
if os in config3['plugins'][plin][k].keys():
# replace
config3['plugins'][plin][k]=config3['plugins'][plin][k][os]
for att in ['destdir','exec','icon','backup']:
try:
config3['plugins'][plin][att]=pathMacro(config3['plugins'][plin][att])
except:
sayexc(config3['data'][plin][att])
if plin=='defaulttest':
pprint.pprint(config3['plugins'][plin])
for plin in config3['data'].keys():
for att in ['destdir','exec','icon','backup']:
if config3['data'][plin].has_key(att):
try:
config3['data'][plin][att]=pathMacro(config3['data'][plin][att])
except:
sayexc(config3['data'][plin][att])
self.config=config3['plugins']
self.base=config3['base']
self.config3=config3
self.keys=self.config.keys
#----------------------------
if config3.has_key('keys') and config3['keys'].has_key("keyserver"):
try:
for ku in config3['keys']['keyserver'].keys():
yy=transformkeytree(config3['keys']['keyserver'][ku])
config3['keys']['keyserver'][ku]=yy
except:
sayexc()
#----------------------------
# self.register()
def start(self):
''' start dialog linked inside InitGui.py of the pluginmanager'''
s=PluginloaderWidget(self)
self.widget=s
s.show()
#check the version local against the web
def getwebVersionDate(self,plugin):
# get the date of the last update of the master
fn=pathMacro(self.base['tmprelease'])
say(fn)
try:
tg=urllib.urlretrieve(self.config[plugin]['timestamp'],fn)
f = open(fn)
lines = f.readlines()
rc=lines[0]
except:
saye("no web version info available")
rc=''
return rc
def getlocalVersionDate(self,plugin):
# get the date of the last localk sync
try:
fn=self.config[plugin]['destdir']+"/release"
f = open(fn)
lines = f.readlines()
rc=lines[0]
except:
saye("no local version info available")
rc='99999999999999999999999'
return rc
def install(self,item):
say(self.config[item])
self.widget.lab2.setText("Install start")
self.widget.show()
plugin=item
say("---install or update !"+plugin+ "!")
if plugin == 'pluginloader':
say("update MYSELF --------------- "+plugin)
fn=FreeCAD.ConfigGet('UserAppData')+"/Mod/plugins/installer.py";d={};exec(open(fn).read(),d,d)
now=timeString()
self.config[plugin]['installed']=now
FreeCAD.ParamGet('User parameter:/Plugins/'+plugin).SetString("installdate",now)
return
if self.config[plugin].has_key('status') and self.config[plugin]['status'] == 'ignore':
saye('ignore')
return
localVersion=self.getlocalVersionDate(plugin)
webVersion=self.getwebVersionDate(plugin)
say("Local Version: "+localVersion)
say("Web Version : "+webVersion)
needUpdate = localVersion < webVersion
needUpdate = True
if needUpdate:
saye("Need Update")
else:
saye("No Update")
zipextract=self.base['zipex']
zipextract=pathMacro(zipextract)
zipextract2=zipextract
say(zipextract)
import os
directory=os.path.dirname(zipextract)
if not os.path.exists(directory):
say("create dir " + directory)
os.makedirs(directory)
if self.config[plugin]['method']=='7z':
zipfilename=zipextract+".7z"
else:
zipfilename=zipextract+".zip"
say("zipfile: " + zipfilename)
zipextract += '/'
if needUpdate:
if not self.config[plugin].has_key('source'):
saye("no source given - nothing to download")
else:
say(self.config[plugin]['source'])
if not self.config[plugin].has_key('sourcedir'):
saye("sourcedir not given - install aborted")
return
if not self.config[plugin].has_key('destdir'):
saye("destdir not given -- install aborted")
return
say("before download ...")
say(self.config[plugin]['destdir'])
if self.config[plugin].has_key('format') and self.config[plugin]['format']=='flatfile':
zipfilename=zipextract+"../a.txt"
say(zipfilename)
tg=urllib.urlretrieve(self.config[plugin]['source'],zipfilename)
targetfile=tg[0]
say("targetfile:"+targetfile)
if self.config[plugin].has_key('format') and self.config[plugin]['format']=='flatfile':
saye("Flat file")
zipextract=zipfilename
else:
if self.config[plugin]['method']=='7z':
import subprocess
# problem geht nicht
#subprocess.call(['7z', 'x', zipfilename, zipextract2])
os.system("cd /home/thomas/tmp; 7z x "+zipfilename+" PCB >/tmp/aa")
zipextract="/home/thomas/tmp/PCB"
else:
fh = open(zipfilename, 'rb')
zfile = zipfile.ZipFile(fh)
zfile.extractall(zipextract)
fh.close()
say("extracts")
if self.config[plugin]['sourcedir'] =='.':
source=zipextract
else:
source=zipextract+self.config[plugin]['sourcedir']
say(source)
destination=self.config[plugin]['destdir']
say(destination)
# hier muss fehlerhandlich verbessert werden !!
if self.config[plugin].has_key('backup'):
try:
say("backup " + destination)
say(" to " + self.config[plugin]['backup']+"/bak."+str(time.time()))
if not os.path.exists(self.config[plugin]['backup']):
say("create dir " + self.config[plugin]['backup'])
os.makedirs(self.config[plugin]['backup'])
os.rename(destination,self.config[plugin]['backup']+"/bak."+str(time.time()))
except:
sayexc("hier")
if self.config[plugin].has_key('format') and self.config[plugin]['format']=='flatfile':
pass
else:
pass
#if not os.path.exists(directory):
# os.makedirs(destination)
say("move")
say("destination:"+destination+"!")
say("source:" +source+'!')
if self.config[plugin].has_key('format') and self.config[plugin]['format']=='flatfile':
import shutil
say("move by shutil")
shutil.move(source, destination)
else:
import shutil
if os.path.exists(directory):
say("dir exists:" + directory)
try:
directory=os.path.dirname(destination)
base=os.path.basename(destination)
sdirectory=os.path.dirname(source)
sbase=os.path.basename(source)
dest2=directory+"/"+sbase
say(directory)
say(sdirectory)
say(dest2)
say(base)
if not os.path.exists(directory):
say("create dir " + directory)
os.makedirs(directory)
say("move "+source+" to "+dest2)
if self.config[plugin].has_key('replace'):
say("REPLACE")
p= self.config[plugin]['replace']['pattern']
d= self.config[plugin]['replace']['data']
f= self.config[plugin]['replace']['files']
say(f)
d=pathMacro(d)
for fil in f:
try:
cmd="sed -e 's#"+str(p)+"#"+str(d)+"#' " + source + "/" + fil + ">" + source + "/"+fil+".sed"
say(cmd)
os.system(cmd)
os.rename(source + "/"+fil+".sed" , source + "/" + fil)
except:
sayexc()
shutil.move(source, dest2)
say ("rename " +dest2+" to "+destination)
try:
os.rename(dest2, destination)
except:
sayexc()
os.rename(destination,destination+".err")
os.rename(dest2, destination)
shutil.rmtree(destination+".err")
except:
sayexc()
say("install done")
self.widget.lab2.setText(str(item) + " install done ----")
dlgi(str(item) + " installed")
now=timeString()
self.config[plugin]['installed']=now
FreeCAD.ParamGet('User parameter:/Plugins/'+plugin).SetString("installdate",now)
def setParams(self):
ta=FreeCAD.ParamGet('User parameter:Plugins')
ta.SetString("pluginlist","")
pluginlist=[]
for k in self.config.keys():
if not self.config[k]['status'] == 'ignore':
t=FreeCAD.ParamGet('User parameter:Plugins/'+k)
pcm=ConfigManager(k)
hide=pcm.get('_hide_',False)
if hide:
continue
pluginlist.append(k)
t.SetString("name",self.config[k]["name"])
if self.config[k].has_key("author"): t.SetString("author",self.config[k]["author"])
t.SetString("destination",self.config[k]["destdir"])
t.SetInt('installed',1)
itemlist=[]
if self.config[k].has_key('menuitems'):
for menu in self.config[k]['menuitems'].keys():
itemlist.append(menu)
tm=FreeCAD.ParamGet('User parameter:Plugins/'+k+'/'+menu)
tm.SetString("exec",self.config[k]['menuitems'][menu]['exec'])
if self.config[k].has_key('menu'):
menu=self.config[k]['menu']
itemlist.append(menu)
tm=FreeCAD.ParamGet('User parameter:Plugins/'+k+'/'+menu)
tm.SetString("exec",self.config[k]['exec'])
ms=";".join(itemlist)
if ms <>"":
t.SetString("menulist",ms)
ps=";".join(pluginlist)
ta.SetString("pluginlist",ps)