/
addonmanager_workers.py
907 lines (804 loc) · 40.7 KB
/
addonmanager_workers.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# -*- coding: utf-8 -*-
#***************************************************************************
#* *
#* Copyright (c) 2019 Yorik van Havre <yorik@uncreated.net> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
import os
import re
import shutil
import stat
import sys
import tempfile
import FreeCAD
from PySide import QtCore
import addonmanager_utilities as utils
from addonmanager_utilities import translate # this needs to be as is for pylupdate
from addonmanager_macro import Macro
## @package AddonManager_workers
# \ingroup ADDONMANAGER
# \brief Multithread workers for the addon manager
# Blacklisted addons
MACROS_BLACKLIST = ["BOLTS",
"WorkFeatures",
"how to install",
"PartsLibrary",
"FCGear"]
# These addons will print an additional message informing the user
OBSOLETE = ["assembly2",
"drawing_dimensioning",
"cura_engine"]
# These addons will print an additional message informing the user Python2 only
PY2ONLY = ["geodata",
"GDT",
"timber",
"flamingo",
"reconstruction",
"animation"]
# Minimum Git binary versions for installing/updating Wbs and loading the list of macros
GITWBMIN = "2.14.99"
GITMACMIN = "2.11.99"
NOGIT = False # for debugging purposes, set this to True to always use http downloads
"""Multithread workers for the Addon Manager"""
class UpdateWorker(QtCore.QThread):
"""This worker updates the list of available workbenches"""
info_label = QtCore.Signal(str)
addon_repo = QtCore.Signal(object)
progressbar_show = QtCore.Signal(bool)
done = QtCore.Signal()
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
"populates the list of addons"
self.progressbar_show.emit(True)
u = utils.urlopen("https://github.com/FreeCAD/FreeCAD-addons")
if not u:
self.progressbar_show.emit(False)
self.done.emit()
self.stop = True
return
p = u.read()
if sys.version_info.major >= 3 and isinstance(p, bytes):
p = p.decode("utf-8")
u.close()
p = p.replace("\n"," ")
p = re.findall("octicon-file-submodule(.*?)message",p)
basedir = FreeCAD.getUserAppDataDir()
moddir = basedir + os.sep + "Mod"
repos = []
# querying official addons
for l in p:
#name = re.findall("data-skip-pjax=\"true\">(.*?)<",l)[0]
res = re.findall("title=\"(.*?) @",l)
if res:
name = res[0]
else:
print("AddonMananger: Debug: couldn't find title in",l)
continue
self.info_label.emit(name)
#url = re.findall("title=\"(.*?) @",l)[0]
url = utils.getRepoUrl(l)
if url:
addondir = moddir + os.sep + name
#print ("found:",name," at ",url)
if os.path.exists(addondir) and os.listdir(addondir):
# make sure the folder exists and it contains files!
state = 1
else:
state = 0
repos.append([name,url,state])
# querying custom addons
customaddons = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons").GetString("CustomRepositories","").split("\n")
for url in customaddons:
if url:
name = url.split("/")[-1]
if name.lower().endswith(".git"):
name = name[:-4]
addondir = moddir + os.sep + name
if not os.path.exists(addondir):
state = 0
else:
state = 1
repos.append([name,url,state])
if not repos:
self.info_label.emit(translate("AddonsInstaller", "Unable to download addon list."))
else:
repos = sorted(repos, key=lambda s: s[0].lower())
for repo in repos:
self.addon_repo.emit(repo)
self.info_label.emit(translate("AddonsInstaller", "Workbenches list was updated."))
git_exe = utils.checkGitBinary()
import platform
try:
out = os.popen(git_exe + ' --version','r')
except:
if platform.system() == 'Windows':
FreeCAD.Console.PrintLog(translate("AddonsInstaller","The Git executable has not been found in the path.")+"\n")
else:
FreeCAD.Console.PrintLog(translate("AddonsInstaller","The Git binary has not been found.")+"\n")
from distutils.version import StrictVersion
if 'out' in locals():
out_string = out.read()
out.close()
result = re.search('(\d+\.\d+\.\d+)',out_string)
git_version = StrictVersion(result.group(1))
if platform.system() == 'Windows':
FreeCAD.Console.PrintLog(translate("AddonsInstaller","Found Git executable version ")+str(git_version)+"\n")
else:
FreeCAD.Console.PrintLog(translate("AddonsInstaller","Found Git binary version ")+str(git_version)+"\n")
else:
git_version = StrictVersion("0.0.0")
self.progressbar_show.emit(False)
self.done.emit()
self.stop = True
class InfoWorker(QtCore.QThread):
"""This worker retrieves the description text of a workbench"""
addon_repos = QtCore.Signal(object)
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
i = 0
for repo in self.repos:
url = repo[1]
u = utils.urlopen(url)
if not u:
self.stop = True
return
p = u.read()
if sys.version_info.major >= 3 and isinstance(p, bytes):
p = p.decode("utf-8")
u.close()
desc = re.findall("<meta property=\"og:description\" content=\"(.*?)\"",p)
if desc:
desc = desc[0]
else:
desc = "Unable to retrieve addon description"
self.repos[i].append(desc)
i += 1
self.addon_repos.emit(self.repos)
self.stop = True
class CheckWBWorker(QtCore.QThread):
"""This worker checks for available updates for all workbenches"""
enable = QtCore.Signal(int)
mark = QtCore.Signal(str)
addon_repos = QtCore.Signal(object)
def __init__(self,repos):
QtCore.QThread.__init__(self)
self.repos = repos
def run(self):
if NOGIT:
self.stop = True
return
try:
import git
except:
self.stop = True
return
basedir = FreeCAD.getUserAppDataDir()
moddir = basedir + os.sep + "Mod"
upds = []
gitpython_warning = False
for repo in self.repos:
if repo[2] == 1: #installed
#print("Checking for updates for",repo[0])
clonedir = moddir + os.sep + repo[0]
if os.path.exists(clonedir):
self.repos[self.repos.index(repo)][2] = 2 # mark as already installed AND already checked for updates
if not os.path.exists(clonedir + os.sep + '.git'):
# Repair addon installed with raw download
bare_repo = git.Repo.clone_from(repo[1], clonedir + os.sep + '.git', bare=True)
try:
with bare_repo.config_writer() as cw:
cw.set('core', 'bare', False)
except AttributeError:
if not gitpython_warning:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated GitPython detected, consider upgrading with pip.")+"\n")
gitpython_warning = True
cw = bare_repo.config_writer()
cw.set('core', 'bare', False)
del cw
repo = git.Repo(clonedir)
repo.head.reset('--hard')
gitrepo = git.Git(clonedir)
try:
gitrepo.fetch()
except:
print("AddonManager: Unable to fetch git updates for repo",repo[0])
else:
if "git pull" in gitrepo.status():
self.mark.emit(repo[0])
upds.append(repo[0])
self.repos[self.repos.index(repo)][2] = 3 # mark as already installed AND already checked for updates AND update available
self.addon_repos.emit(self.repos)
self.enable.emit(len(upds))
self.stop = True
class FillMacroListWorker(QtCore.QThread):
"""This worker populates the list of macros"""
add_macro_signal = QtCore.Signal(Macro)
info_label_signal = QtCore.Signal(str)
progressbar_show = QtCore.Signal(bool)
def __init__(self, repo_dir):
QtCore.QThread.__init__(self)
self.repo_dir = repo_dir
self.macros = []
def run(self):
"""Populates the list of macros"""
self.retrieve_macros_from_git()
self.retrieve_macros_from_wiki()
[self.add_macro_signal.emit(m) for m in sorted(self.macros, key=lambda m: m.name.lower())]
if self.macros:
self.info_label_signal.emit(translate('AddonsInstaller', 'List of macros successfully retrieved.'))
self.progressbar_show.emit(False)
self.stop = True
def retrieve_macros_from_git(self):
"""Retrieve macros from FreeCAD-macros.git
Emits a signal for each macro in
https://github.com/FreeCAD/FreeCAD-macros.git
"""
git = None
import os
try:
import git
except ImportError:
self.info_label_signal.emit("GitPython not installed! Cannot retrieve macros from Git")
FreeCAD.Console.PrintWarning(translate('AddonsInstaller', 'GitPython not installed! Cannot retrieve macros from git, fallback to using the Wiki')+"\n")
if git:
git_exe = utils.checkGitBinary()
try:
out = os.popen(git_exe + ' --version','r')
except:
pass
if 'out' in locals():
out_string = out.read()
out.close()
result = re.search('(\d+\.\d+\.\d+)',out_string)
from distutils.version import StrictVersion
git_version = StrictVersion(result.group(1))
git_version_min = StrictVersion(GITMACMIN)
if git_version < git_version_min:
import platform
if platform.system() == 'Windows':
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated Git executable detected, consider upgrading or ensure the path to the most recent version is the first one to be found. Cannot retrieve macros from git, fallback to using the Wiki")+"\n")
else:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated Git binary detected, consider upgrading. Cannot retrieve macros from git, fallback to using the Wiki")+"\n")
git = None
else:
self.info_label_signal.emit("Git binary not installed! Cannot retrieve macros from Git")
import platform
if platform.system() == 'Windows':
FreeCAD.Console.PrintWarning(translate('AddonsInstaller', 'Git exectuable not found. Cannot retrieve macros using git, fallback to using the Wiki')+"\n")
else:
FreeCAD.Console.PrintWarning(translate('AddonsInstaller', 'Git binary not found. Cannot retrieve macros using git, fallback to using the Wiki')+"\n")
git = None
if git:
self.info_label_signal.emit('Downloading list of macros from git...')
try:
git.Repo.clone_from('https://github.com/FreeCAD/FreeCAD-macros.git', self.repo_dir)
except:
FreeCAD.Console.PrintWarning(translate('AddonsInstaller', 'Something went wrong with the Git Macro retrieval, fallback to using the Wiki')+"\n")
return
for dirpath, _, filenames in os.walk(self.repo_dir):
if '.git' in dirpath:
continue
for filename in filenames:
if filename.lower().endswith('.fcmacro'):
macro = Macro(filename[:-8]) # Remove ".FCMacro".
macro.on_git = True
macro.src_filename = os.path.join(dirpath, filename)
self.macros.append(macro)
def retrieve_macros_from_wiki(self):
"""Retrieve macros from the wiki
Read the wiki and emit a signal for each found macro.
Reads only the page https://www.freecadweb.org/wiki/Macros_recipes
"""
self.info_label_signal.emit("Downloading list of macros from the FreeCAD wiki...")
self.progressbar_show.emit(True)
u = utils.urlopen("https://www.freecadweb.org/wiki/Macros_recipes")
if not u:
return
p = u.read()
u.close()
if sys.version_info.major >= 3 and isinstance(p, bytes):
p = p.decode("utf-8")
macros = re.findall('title="(Macro.*?)"', p)
macros = [mac for mac in macros if ('translated' not in mac)]
for mac in macros:
macname = mac[6:] # Remove "Macro ".
macname = macname.replace("&","&")
if (macname not in MACROS_BLACKLIST) and ('recipes' not in macname.lower()):
macro = Macro(macname)
macro.on_wiki = True
self.macros.append(macro)
class ShowWorker(QtCore.QThread):
"""This worker retrieves info of a given workbench"""
info_label = QtCore.Signal(str)
addon_repos = QtCore.Signal(object)
progressbar_show = QtCore.Signal(bool)
def __init__(self, repos, idx):
# repos is a list of [name,url,installbit,descr]
# name : Addon name
# url : Addon repository location
# installbit: 0 = Addon is not installed
# 1 = Addon is installed
# 2 = Addon is installed and checked for available updates (none pending)
# 3 = Addon is installed and has a pending update
# descr : Addon description
QtCore.QThread.__init__(self)
self.repos = repos
self.idx = idx
def run(self):
self.progressbar_show.emit(True)
self.info_label.emit(translate("AddonsInstaller", "Retrieving description..."))
if len(self.repos[self.idx]) == 4:
desc = self.repos[self.idx][3]
else:
u = None
url = self.repos[self.idx][1]
self.info_label.emit(translate("AddonsInstaller", "Retrieving info from") + ' ' + str(url))
desc = ""
# get the README if possible
readmeurl = utils.getReadmeUrl(url)
if not readmeurl:
print("Debug: README not found for",url)
u = utils.urlopen(readmeurl)
if not u:
print("Debug: README not found at",readmeurl)
if u:
p = u.read()
if sys.version_info.major >= 3 and isinstance(p, bytes):
p = p.decode("utf-8")
u.close()
readmeregex = utils.getReadmeRegex(url)
if readmeregex:
readme = re.findall(readmeregex,p,flags=re.MULTILINE|re.DOTALL)
if readme:
desc += readme[0]
if not desc:
# fall back to the description text
u = utils.urlopen(url)
if not u:
self.progressbar_show.emit(False)
self.stop = True
return
p = u.read()
if sys.version_info.major >= 3 and isinstance(p, bytes):
p = p.decode("utf-8")
u.close()
descregex = utils.getDescRegex(url)
if descregex:
desc = re.findall(descregex,p)
if desc:
desc = "<br/>"+desc[0]
if not desc:
desc = "Unable to retrieve addon description"
self.repos[self.idx].append(desc)
self.addon_repos.emit(self.repos)
# Addon is installed so lets check if it has an update
if self.repos[self.idx][2] == 1:
upd = False
# checking for updates
if not NOGIT:
try:
import git
except:
pass
else:
repo = self.repos[self.idx]
clonedir = FreeCAD.getUserAppDataDir() + os.sep + "Mod" + os.sep + repo[0]
if os.path.exists(clonedir):
if not os.path.exists(clonedir + os.sep + '.git'):
# Repair addon installed with raw download
bare_repo = git.Repo.clone_from(repo[1], clonedir + os.sep + '.git', bare=True)
try:
with bare_repo.config_writer() as cw:
cw.set('core', 'bare', False)
except AttributeError:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated GitPython detected, consider upgrading with pip.")+"\n")
cw = bare_repo.config_writer()
cw.set('core', 'bare', False)
del cw
repo = git.Repo(clonedir)
repo.head.reset('--hard')
gitrepo = git.Git(clonedir)
gitrepo.fetch()
if "git pull" in gitrepo.status():
upd = True
# If there is an update pending, lets user know via the UI
if upd:
message = "<div style=\"width: 100%;text-align: center;background: #75AFFD;\"><br/><strong style=\"background: #397FF7;color: #FFFFFF;\">" + translate("AddonsInstaller", "An update is available for this addon.")
message += "</strong><br/></div><hr/>" + desc + '<br/><br/>Addon repository: <a href="' + self.repos[self.idx][1] + '">' + self.repos[self.idx][1] + '</a>'
self.repos[self.idx][2] = 3 # mark as already installed AND already checked for updates AND update is available
# If there isn't, indicate that this addon is already installed
else:
message = "<div style=\"width: 100%;text-align: center;background: #C1FEB2;\"><br/><strong style=\"background: #00B629;color: #FFFFFF;\">" + translate("AddonsInstaller", "This addon is already installed.") + "</strong><br/></div><hr/>"
message += desc + '<br/><br/>Addon repository: <a href="' + self.repos[self.idx][1] + '">' + self.repos[self.idx][1] + '</a>'
self.repos[self.idx][2] = 2 # mark as already installed AND already checked for updates
# Let the user know the install path for this addon
message += '<br/>' + translate("AddonInstaller","Installed location")+": "+ FreeCAD.getUserAppDataDir() + os.sep + "Mod" + os.sep + self.repos[self.idx][0]
self.addon_repos.emit(self.repos)
elif self.repos[self.idx][2] == 2:
message = "<div style=\"width: 100%;text-align: center;background: #C1FEB2;\"><br/><strong style=\"background: #00B629;color: #FFFFFF;\">" + translate("AddonsInstaller", "This addon is already installed.") + "</strong><br></div><hr/>"
message += desc + '<br/><br/>Addon repository: <a href="' + self.repos[self.idx][1] + '">' + self.repos[self.idx][1] + '</a>'
message += '<br/>' + translate("AddonInstaller","Installed location")+": "+ FreeCAD.getUserAppDataDir() + os.sep + "Mod" + os.sep + self.repos[self.idx][0]
elif self.repos[self.idx][2] == 3:
message = "<div style=\"width: 100%;text-align: center;background: #75AFFD;\"><br/><strong style=\"background: #397FF7;color: #FFFFFF;\">" + translate("AddonsInstaller", "An update is available for this addon.")
message += "</strong><br/></div><hr/>" + desc + '<br/><br/>Addon repository: <a href="' + self.repos[self.idx][1] + '">' + self.repos[self.idx][1] + '</a>'
message += '<br/>' + translate("AddonInstaller","Installed location")+": "+ FreeCAD.getUserAppDataDir() + os.sep + "Mod" + os.sep + self.repos[self.idx][0]
else:
message = desc + '<br/><br/>Addon repository: <a href="' + self.repos[self.idx][1] + '">' + self.repos[self.idx][1] + '</a>'
# If the Addon is obsolete, let the user know through the Addon UI
if self.repos[self.idx][0] in OBSOLETE:
message = " <div style=\"width: 100%; text-align:center; background: #FFB3B3;\"><strong style=\"color: #FFFFFF; background: #FF0000;\">"+translate("AddonsInstaller","This addon is marked as obsolete")+"</strong><br/><br/>"
message += translate("AddonsInstaller","This usually means it is no longer maintained, and some more advanced addon in this list provides the same functionality.")+"<br/></div><hr/>" + desc
# If the Addon is Python 2 only, let the user know through the Addon UI
if self.repos[self.idx][0] in PY2ONLY:
message = " <div style=\"width: 100%; text-align:center; background: #ffe9b3;\"><strong style=\"color: #FFFFFF; background: #ff8000;\">"+translate("AddonsInstaller","This addon is marked as Python 2 Only")+"</strong><br/><br/>"
message += translate("AddonsInstaller","This workbench may no longer be maintained and installing it on a Python 3 system will more than likely result in errors at startup or while in use.")+"<br/></div><hr/>" + desc
self.info_label.emit( message )
self.progressbar_show.emit(False)
self.mustLoadImages = True
l = self.loadImages( message, self.repos[self.idx][1], self.repos[self.idx][0])
if l:
self.info_label.emit( l )
self.stop = True
def stopImageLoading(self):
"this stops the image loading process and allow the thread to terminate earlier"
self.mustLoadImages = False
def loadImages(self,message,url,wbName):
"checks if the given page contains images and downloads them"
# QTextBrowser cannot display online images. So we download them
# here, and replace the image link in the html code with the
# downloaded version
imagepaths = re.findall("<img.*?src=\"(.*?)\"",message)
if imagepaths:
storedimages = []
store = os.path.join(FreeCAD.getUserAppDataDir(),"AddonManager","Images")
if not os.path.exists(store):
os.makedirs(store)
for path in imagepaths:
if not self.mustLoadImages:
return None
origpath = path
if "?" in path:
# remove everything after the ?
path = path.split("?")[0]
if not path.startswith("http"):
path = utils.getserver(url) + path
name = path.split("/")[-1]
if name and path.startswith("http"):
storename = os.path.join(store,name)
if len(storename) >= 260:
remainChars = 259 - (len(store) + len(wbName) + 1)
storename = os.path.join(store,wbName+name[-remainChars:])
if not os.path.exists(storename):
try:
u = utils.urlopen(path)
imagedata = u.read()
u.close()
except:
print("AddonManager: Debug: Error retrieving image from",path)
else:
f = open(storename,"wb")
f.write(imagedata)
f.close()
# resize the image to 300x300px if needed
from PySide import QtCore,QtGui
img = QtGui.QImage(storename)
if (img.width() > 300) or (img.height() > 300):
pix = QtGui.QPixmap()
pix = pix.fromImage(img.scaled(300,300,QtCore.Qt.KeepAspectRatio,QtCore.Qt.FastTransformation))
pix.save(storename, "jpeg",100)
message = message.replace("src=\""+origpath,"src=\"file:///"+storename.replace("\\","/"))
#print(message)
return message
return None
class GetMacroDetailsWorker(QtCore.QThread):
"""Retrieve the macro details for a macro"""
info_label = QtCore.Signal(str)
progressbar_show = QtCore.Signal(bool)
def __init__(self, macro):
QtCore.QThread.__init__(self)
self.macro = macro
def run(self):
self.progressbar_show.emit(True)
self.info_label.emit(translate("AddonsInstaller", "Retrieving description..."))
if not self.macro.parsed and self.macro.on_git:
self.info_label.emit(translate('AddonsInstaller', 'Retrieving info from git'))
self.macro.fill_details_from_file(self.macro.src_filename)
if not self.macro.parsed and self.macro.on_wiki:
self.info_label.emit(translate('AddonsInstaller', 'Retrieving info from wiki'))
mac = self.macro.name.replace(' ', '_')
mac = mac.replace('&', '%26')
mac = mac.replace('+', '%2B')
url = 'https://www.freecadweb.org/wiki/Macro_' + mac
self.macro.fill_details_from_wiki(url)
if self.macro.is_installed():
already_installed_msg = ('<strong style=\"background: #00B629;\">'
+ translate("AddonsInstaller", "This macro is already installed.")
+ '</strong><br>')
else:
already_installed_msg = ''
message = (already_installed_msg
+ "<h1>"+self.macro.name+"</h1>"
+ self.macro.desc
+ '<br/><br/>Macro location: <a href="'
+ self.macro.url
+ '">'
+ self.macro.url
+ '</a>')
self.info_label.emit(message)
self.progressbar_show.emit(False)
self.stop = True
class InstallWorker(QtCore.QThread):
"This worker installs a workbench"
info_label = QtCore.Signal(str)
progressbar_show = QtCore.Signal(bool)
mark_recompute = QtCore.Signal(str)
def __init__(self, repos, idx):
QtCore.QThread.__init__(self)
self.idx = idx
self.repos = repos
def run(self):
"installs or updates the selected addon"
git = None
if sys.version_info.major > 2 and self.repos[self.idx][0] in PY2ONLY:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "User requested installing/updating a Python 2 workbench on a system running Python 3 - ")+str(self.repos[self.idx][0])+"\n")
import os
try:
import git
except Exception as e:
self.info_label.emit("GitPython not found.")
print(e)
FreeCAD.Console.PrintWarning(translate("AddonsInstaller","GitPython not found. Using standard download instead.")+"\n")
try:
import zipfile
except:
self.info_label.emit("no zip support.")
FreeCAD.Console.PrintError(translate("AddonsInstaller","Your version of python doesn't appear to support ZIP files. Unable to proceed.")+"\n")
return
try:
import StringIO as io
except ImportError: # StringIO is not available with python3
import io
if git:
git_exe = utils.checkGitBinary()
try:
out = os.popen(git_exe + ' --version','r')
except:
pass
if 'out' in locals():
out_string = out.read()
out.close()
result = re.search('(\d+\.\d+\.\d+)',out_string)
from distutils.version import StrictVersion
git_version = StrictVersion(result.group(1))
git_version_min = StrictVersion(GITWBMIN)
if git_version < git_version_min:
import platform
if platform.system() == 'Windows':
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated Git executable detected, consider upgrading or ensure the path to the most recent version is the first one to be found.")+"\n")
else:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated Git binary detected, consider upgrading.")+"\n")
git = None
else:
self.info_label.emit("Git binary not found.")
import platform
if platform.system() == 'Windows':
FreeCAD.Console.PrintWarning(translate("AddonsInstaller","Git executable not found. Using standard download instead.")+"\n")
else:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller","Git binary not found. Using standard download instead.")+"\n")
try:
import zipfile
except:
self.info_label.emit("no zip support.")
FreeCAD.Console.PrintError(translate("AddonsInstaller","Your version of python doesn't appear to support ZIP files. Unable to proceed.")+"\n")
return
try:
import StringIO as io
except ImportError: # StringIO is not available with python3
import io
if not isinstance(self.idx,list):
self.idx = [self.idx]
for idx in self.idx:
if idx < 0:
return
if not self.repos:
return
if NOGIT:
git = None
basedir = FreeCAD.getUserAppDataDir()
moddir = basedir + os.sep + "Mod"
if not os.path.exists(moddir):
os.makedirs(moddir)
clonedir = moddir + os.sep + self.repos[idx][0]
self.progressbar_show.emit(True)
if os.path.exists(clonedir):
self.info_label.emit("Updating module...")
if git:
if not os.path.exists(clonedir + os.sep + '.git'):
# Repair addon installed with raw download
bare_repo = git.Repo.clone_from(self.repos[idx][1], clonedir + os.sep + '.git', bare=True)
try:
with bare_repo.config_writer() as cw:
cw.set('core', 'bare', False)
except AttributeError:
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Outdated GitPython detected, consider upgrading with pip.")+"\n")
cw = bare_repo.config_writer()
cw.set('core', 'bare', False)
del cw
repo = git.Repo(clonedir)
repo.head.reset('--hard')
repo = git.Git(clonedir)
try:
answer = repo.pull() + "\n\n" + translate("AddonsInstaller", "Workbench successfully updated. Please restart FreeCAD to apply the changes.")
except:
print("Error updating module",self.repos[idx][1]," - Please fix manually")
answer = repo.status()
print(answer)
else:
# Update the submodules for this repository
repo_sms = git.Repo(clonedir)
for submodule in repo_sms.submodules:
submodule.update(init=True, recursive=True)
else:
answer = self.download(self.repos[idx][1],clonedir) + "\n\n" + translate("AddonsInstaller", "Workbench successfully updated. Please restart FreeCAD to apply the changes.")
else:
self.info_label.emit("Checking module dependencies...")
depsok,answer = self.checkDependencies(self.repos[idx][1])
if depsok:
if git:
self.info_label.emit("Cloning module...")
repo = git.Repo.clone_from(self.repos[idx][1], clonedir, branch='master')
# Make sure to clone all the submodules as well
if repo.submodules:
repo.submodule_update(recursive=True)
else:
self.info_label.emit("Downloading module...")
self.download(self.repos[idx][1],clonedir)
answer = translate("AddonsInstaller", "Workbench successfully installed. Please restart FreeCAD to apply the changes.")
# symlink any macro contained in the module to the macros folder
macro_dir = FreeCAD.getUserMacroDir(True)
if not os.path.exists(macro_dir):
os.makedirs(macro_dir)
if os.path.exists(clonedir):
for f in os.listdir(clonedir):
if f.lower().endswith(".fcmacro"):
print("copying macro:",f)
utils.symlink(os.path.join(clonedir, f), os.path.join(macro_dir, f))
FreeCAD.ParamGet('User parameter:Plugins/'+self.repos[idx][0]).SetString("destination",clonedir)
answer += "\n\n"+translate("AddonsInstaller", "A macro has been installed and is available under Macro -> Macros menu")+":"
answer += "\n<b>" + f + "</b>"
self.progressbar_show.emit(False)
self.info_label.emit(answer)
self.mark_recompute.emit(self.repos[idx][0])
self.stop = True
def checkDependencies(self,baseurl):
"checks if the repo contains a metadata.txt and check its contents"
import FreeCADGui
ok = True
message = ""
depsurl = baseurl.replace("github.com","raw.githubusercontent.com")
if not depsurl.endswith("/"):
depsurl += "/"
depsurl += "master/metadata.txt"
mu = utils.urlopen(depsurl)
if mu:
# metadata.txt found
depsfile = mu.read()
mu.close()
# urllib2 gives us a bytelike object instead of a string. Have to consider that
try:
depsfile = depsfile.decode('utf-8')
except AttributeError:
pass
deps = depsfile.split("\n")
for l in deps:
if l.startswith("workbenches="):
depswb = l.split("=")[1].split(",")
for wb in depswb:
if wb.strip():
if not wb.strip() in FreeCADGui.listWorkbenches().keys():
if not wb.strip()+"Workbench" in FreeCADGui.listWorkbenches().keys():
ok = False
message += translate("AddonsInstaller","Missing workbench") + ": " + wb + ", "
elif l.startswith("pylibs="):
depspy = l.split("=")[1].split(",")
for pl in depspy:
if pl.strip():
try:
__import__(pl.strip())
except:
ok = False
message += translate("AddonsInstaller","Missing python module") +": " + pl + ", "
elif l.startswith("optionalpylibs="):
opspy = l.split("=")[1].split(",")
for pl in opspy:
if pl.strip():
try:
__import__(pl.strip())
except:
message += translate("AddonsInstaller","Missing optional python module (doesn't prevent installing)") +": " + pl + ", "
if message and (not ok):
message = translate("AddonsInstaller", "Some errors were found that prevent to install this workbench") + ": <b>" + message + "</b>. "
message += translate("AddonsInstaller","Please install the missing components first.")
return ok, message
def download(self,baseurl,clonedir):
"downloads and unzip a zip version from a git repo"
import zipfile
bakdir = None
if os.path.exists(clonedir):
bakdir = clonedir+".bak"
if os.path.exists(bakdir):
shutil.rmtree(bakdir)
os.rename(clonedir,bakdir)
os.makedirs(clonedir)
zipurl = utils.getZipUrl(baseurl)
if not zipurl:
return translate("AddonsInstaller", "Error: Unable to locate zip from") + " " + baseurl
try:
print("Downloading "+zipurl)
u = utils.urlopen(zipurl)
except:
return translate("AddonsInstaller", "Error: Unable to download") + " " + zipurl
if not u:
return translate("AddonsInstaller", "Error: Unable to download") + " " + zipurl
if sys.version_info.major < 3:
import StringIO as io
_stringio = io.StringIO
else:
import io
_stringio = io.BytesIO
zfile = _stringio()
zfile.write(u.read())
zfile = zipfile.ZipFile(zfile)
master = zfile.namelist()[0] # github will put everything in a subfolder
zfile.extractall(clonedir)
u.close()
zfile.close()
for filename in os.listdir(clonedir+os.sep+master):
shutil.move(clonedir+os.sep+master+os.sep+filename, clonedir+os.sep+filename)
os.rmdir(clonedir+os.sep+master)
if bakdir:
shutil.rmtree(bakdir)
return translate("AddonsInstaller", "Successfully installed") + " " + zipurl
class CheckSingleWorker(QtCore.QThread):
"""Worker to check for updates for a single addon"""
updateAvailable = QtCore.Signal(bool)
def __init__(self, name):
QtCore.QThread.__init__(self)
self.name = name
def run(self):
try:
import git
except:
return
FreeCAD.Console.PrintLog("Checking for available updates of the "+name+" addon\n")
addondir = os.path.join(FreeCAD.getUserAppDataDir(),"Mod",name)
if os.path.exists(addondir):
if os.path.exists(addondir + os.sep + '.git'):
gitrepo = git.Git(addondir)
try:
gitrepo.fetch()
if "git pull" in gitrepo.status():
self.updateAvailable.emit(True)
return
except:
# can fail for any number of reasons, ex. not being online
pass
self.updateAvailable.emit(False)