forked from Computiva/Kefir-After-Install-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kefir-after-install.py
executable file
·1206 lines (963 loc) · 40.7 KB
/
kefir-after-install.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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# Kefir After Install
#
# Version : Alpha
# Kefir After Install by Edilson Alzemand
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
from gi.repository import Gio
from gi.repository import GLib
from gi.repository import GdkPixbuf
from gi.repository import Pango
from xml.dom import minidom
import os
import sys
import subprocess
import time
import datetime
import shutil
import urllib2
import webbrowser
import fcntl
import apt
from softwareproperties.SoftwareProperties import SoftwareProperties
import threading
import platform
def set_proc_name(newname):
from ctypes import cdll, byref, create_string_buffer
libc = cdll.LoadLibrary('libc.so.6')
buff = create_string_buffer(len(newname)+1)
buff.value = newname
libc.prctl(15, byref(buff), 0, 0, 0)
def closeWindow(widget, event, window):
# Close window
widget.destroy()
# Kill apt and dpkg if busy
checkLocks()
# Exit
sys.exit()
def debugPrint(textToPrint):
if verboseDebug:
print textToPrint
def setPartnerRepoActive():
try:
p1 = subprocess.Popen(['sed -i "/^# deb .*partner/ s/^# //" /etc/apt/sources.list'], shell=True, stdout=subprocess.PIPE)
p2 = subprocess.Popen(['sed -i "/^# deb-src .*partner/ s/^# //" /etc/apt/sources.list'], shell=True, stdout=subprocess.PIPE)
debugPrint("[Notice] Ubuntu Partner Repo Active")
appendToLog("[Notice] Ubuntu Partner Repo Active")
except:
debugPrint("[Error] Ubuntu Partner Repo could not be activated")
appendToLog("[Error] Ubuntu Partner Repo could not be activated")
def checkLocks():
# Just remove any processes busy on startup that is keeping apt cache or dpkg busy
# Bit nasty not to check first is is locked - TODO
try:
# Try to remove locks if any nad kill processes
p4 = subprocess.Popen(['fuser -k /var/lib/dpkg/lock'], shell=True, stdout=subprocess.PIPE)
p5 = subprocess.Popen(['fuser -k /var/lib/apt/lists/lock'], shell=True, stdout=subprocess.PIPE)
debugPrint("[Notice] apt and dpkg locks removed" )
appendToLog("[Notice] apt and dpkg locks removed" )
except:
debugPrint("[Warning] Could not determine apt locks")
appendToLog("[Warning] Could not determine apt locks")
def refreshGui(delay=0.0001, wait=0.0001):
time.sleep(delay)
while Gtk.events_pending():
Gtk.main_iteration()
time.sleep(wait)
def checkInternetConnection(targetUrl):
try:
response=urllib2.urlopen(targetUrl, timeout=4)
debugPrint("[Notice] Internet Connection Active")
appendToLog("[Notice] Internet Connection Active")
return True
except:
debugPrint("[Error] Internet Offline")
appendToLog("[Error] Internet Offline")
return False
def setInstallLabel(state, selected):
# Used as helper for on_cell_toggle and on_changed
if state == 'installed':
installLabel=' installed'
if state == 'not-installed':
installLabel='will be installed'
if state == 'installed' and selected == True:
installLabel='will be re-installed'
if state == 'not-installed' and selected == False:
installLabel='not installed'
if state == 'error':
installLabel='did not install correctly'
return installLabel
def readRemoteFile(url):
# Read contents of remote file
try:
wp = urllib2.urlopen(url)
remoteContent = wp.read()
remoteContent = remoteContent.strip()
wp.close()
except:
debugPrint("[Error] Cannot read Remote Version File "+url)
appendToLog("[Error] Cannot read Remote Version File "+url)
remoteContent = 0
return remoteContent
def downloadFile(url, localdir):
# Download to instalDir
try:
webFile = urllib2.urlopen(url)
outFileName = url.split('/')[-1]
outFile = os.path.join(localdir,outFileName)
localFile = open(outFile, 'w')
localFile.write(webFile.read())
webFile.close()
localFile.close()
except:
debugPrint("[Error] Cannot download file "+url)
appendToLog("[Error] Cannot download file "+url)
def checkUpdate():
# Get local list version
try:
fp = open(localVersionPath, 'r' )
localVersion = fp.read()
localVersion = localVersion.strip()
fp.close()
except:
debugPrint("[Error] Cannot read Local Version File")
appendToLog("[Error] Cannot read Local Version File")
localVersion = 0
# Get remote list version
remoteVersion = readRemoteFile(remoteVersionPath)
# Decide to update list or not
if remoteVersion > localVersion :
debugPrint("[Notice] Downloading software list "+remoteVersion)
appendToLog("[Notice] Downloading software list "+remoteVersion)
# Download new version
downloadFile(remoteVersionPath, installDir)
# Download new XML list
downloadFile(remoteXmlPath, installDir)
def on_cell_toggle(cell, path, model):
# Toggle if not busy installing
if installStatus != 'busy' and installStatus != 'complete' :
if path is not None:
it = model.get_iter(path)
# have no idea why we need to do a int here
getPathInt = int(path)
itemInstallState = installStateList[getPathInt]
# Set icon depending on state
if itemInstallState == 'installed' and model[it][0] == False :
model[it][4] = GdkPixbuf.Pixbuf.new_from_file(iconPathReinstall)
if itemInstallState == 'installed' and model[it][0] == True :
model[it][4] = GdkPixbuf.Pixbuf.new_from_file(iconPathOk)
# Toggle state
model[it][0] = not model[it][0]
# Set label again because of re-install not showing on toggle
# NB sytax different from setting label on_changed
installLabel = setInstallLabel(itemInstallState, model[it][0])
# Set Progress bar text
#label.set_text("%s %s" %(model[it][1], installLabel))
progressBar.set_text("%s %s" %(model[it][1], installLabel))
def on_changed(selection, label):
installLabel = 'unknown'
# get the model and the iterator that points at the data in the model
(model, iter) = selection.get_selected()
# Sneaky syntax here compared to on_cell_toggle
it = int(model.get_string_from_iter(iter))
# Get path val from iter
#getPathInt = int(it)
# Get installstate
itemInstallState = installStateList[it]
# Get the label text
installLabel = setInstallLabel(itemInstallState, model[iter][0])
# set the label to a new value depending on the selection
# if not busy installing
if installStatus != 'busy':
#label.set_text("%s %s" %(model[iter][1], installLabel))
progressBar.set_text("%s %s" %(model[iter][1], installLabel))
return True
def pulse():
progressBar.pulse()
# call the function again
return True
def writeToFile(fileName,content,flag):
try:
fp = open(fileName, flag)
fp.write(content)
fp.close()
except IOError:
writeError="File Not Saved"
except:
writeError="File Not Saved"
raise
return
def appendToLog(content):
timeStamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
writeToFile(logFile, ('['+timeStamp+']'+' '+content+'\n'), 'a')
return
def on_install_button_active(button, model, itemSelectCount):
global pulseTimer
global installStatus
global cache
global view
global p1
# set busy flag
installStatus = 'busy'
# Remove CANCEL button
Gtk.Container.remove(grid, cancelButton)
# Count items
itemCount = len(model)
# Add PPA's
# Set progress
progressBar.set_fraction(0.1)
label.set_text("Installing new software sources...")
appendToLog("Installing new software sources...")
# Use APT module and SoftwareProperties to add-apt
sp = SoftwareProperties()
for listItem in range(itemCount):
# Check which items are selected True in list column 0
itemSelected = model[listItem][0]
if itemSelected:
if ppaList[listItem] != '' :
progressBar.set_text("Adding PPA for "+model[listItem][1])
# add-apt the python way ! taken from add-apt code
try:
sp.add_source_from_line(ppaList[listItem])
debugPrint("Added PPA - %s" % ppaList[listItem])
appendToLog("Added PPA - %s" % ppaList[listItem])
except:
debugPrint("[Error] Could not add PPA - %s" % ppaList[listItem])
appendToLog("[Error] Could not add PPA - %s" % ppaList[listItem])
# Save new apt list
sp.sourceslist.save()
progressBar.set_fraction(0.2)
# Add Keys
for listItem in range(itemCount):
# Check which items are selected True in list column 0
itemSelected = model[listItem][0]
if itemSelected:
if getAptKeyList[listItem] != '' :
debugPrint("Name : %s" % model[listItem][1])
debugPrint("Keys : %s" % getAptKeyList[listItem])
progressBar.set_text("Adding software key for "+model[listItem][1])
# Add key the bash way TODO do this differently to handle errors/timeout
# First check type of key wget or apt-key adv
if "recv-keys" in getAptKeyList[listItem] :
keyType='apt-key'
else:
keyType='wget'
try:
if keyType == 'wget':
# Just read Key URL and do the rest
p1 = subprocess.Popen(['wget', '-O', '-', getAptKeyList[listItem]], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['apt-key', 'add', '--yes', '--quiet', '-'], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
if keyType == 'apt-key':
# Run command as is
p6 = subprocess.Popen([getAptKeyList[listItem]], shell=True, stdout=subprocess.PIPE)
debugPrint("Key added for : %s" % model[listItem][1])
appendToLog("Key added for : %s" % model[listItem][1])
debugPrint("Key status: %s" % output)
appendToLog("Key status: %s" % output)
except:
debugPrint("[Error] Could not add key from - %s" % getAptKeyList[listItem])
appendToLog("[Error] Could not add key from - %s" % getAptKeyList[listItem])
progressBar.set_fraction(0.3)
# Manually write to file to add APT for DEB repos
for listItem in range(itemCount):
# Check which items are selected True in list column 0
itemSelected = model[listItem][0]
if itemSelected:
if aptListEntryList[listItem] != '' :
debugPrint("Name : %s" % model[listItem][1])
debugPrint("APT : %s" % aptListEntryList[listItem])
installTitles = installTitleList[listItem].split(' ')
progressBar.set_text("Adding APT Repository for "+model[listItem][1])
# write sources to file
try:
writeToFile(os.path.join(aptListPath, installTitles[0]+'.list'), 'deb '+aptListEntryList[listItem]+'\n', 'w')
#writeToFile(os.path.join(aptListPath, installTitles[0]+'.list'), 'deb-src '+aptListEntryList[listItem]+'\n', 'a')
writeToFile(os.path.join(aptListPath, installTitles[0]+'.list.save'), 'deb '+aptListEntryList[listItem]+'\w', 'w')
#writeToFile(os.path.join(aptListPath, installTitles[0]+'.list.save'), 'deb-src '+aptListEntryList[listItem]+'\n', 'a')
debugPrint("Added APT - %s" % aptListEntryList[listItem])
appendToLog("Added APT - %s" % aptListEntryList[listItem])
except:
debugPrint("[Error] Could not add APT - %s" % aptListEntryList[listItem])
appendToLog("[Error] Could not add APT - %s" % aptListEntryList[listItem])
onInstallError()
# Save new apt list
sp.sourceslist.save()
# We need to open the cache again before updating
progressBar.set_fraction(0.4)
progressBar.set_text('Reading Software List...')
debugPrint("[Progress] Open Cache...")
appendToLog("[Progress] Open Cache...")
cache.open()
# Now, lets update the package list
progressBar.set_fraction(0.5)
progressBar.set_text('Updating Software Center...')
debugPrint("[Progress] Cache update...")
appendToLog("[Progress] Cache update...")
pulseTimer = GLib.timeout_add(100, pulse)
try:
cache.update()
except:
appendToLog("[Warning] Cache update warnings. Not fatal - continue...")
debugPrint("[Warning] Cache update warnings. Not fatal - continue...")
GLib.source_remove(pulseTimer)
# Now we can do the same as 'apt-get upgrade' does
progressBar.set_fraction(0.7)
#progressBar.set_text('Updating Software ...')
#print "[progress] Updating Software ..."
#cache.upgrade()
# or we can play 'apt-get dist-upgrade'
#cache.upgrade(True)
# Q: Why does nothing happen?
# A: You forgot to call commit()!
debugPrint("[Progress] Commit Cache...")
appendToLog("[Progress] Commit Cache...")
progressBar.set_fraction(0.8)
progressBar.set_text('Updating Software Center...')
try:
#cache.commit()
cache.commit(aptAcquireProgress, aptInstallProgress)
except:
debugPrint("[Error] apt-get update failed")
# Software sources added Done
progressBar.set_fraction(1)
progressBar.set_text('')
label.set_text('Software sources added successfully')
appendToLog('Software sources added successfully')
# START installing apps one by one
# using itemSelectCount to do progress increments
progInc = float(100 / itemSelectCount) / 100
itemIncCount = 0
progressBar.set_fraction(0.02)
for listItem in range(itemCount):
# Check which items are selected True in list column 0
itemSelected = model[listItem][0]
if itemSelected:
# With selected items ...
label.set_text('Installing Software '+str(itemIncCount+1)+' of '+str(itemSelectCount)+' ...')
appendToLog('[Notice] Installing Software '+str(itemIncCount+1)+' of '+str(itemSelectCount)+' ...')
# Un Install first if RE-install is selected
if installStateList[listItem] == 'installed':
progressBar.set_text("Removing "+model[listItem][1])
# Set focus
view.set_cursor(listItem)
# open cache again for each install to update new packages list
cache.open()
# Get package list into array
installTitles = installTitleList[listItem].split(' ')
# mark packages for Remove
for itemToRemove in installTitles:
try:
cache[itemToRemove].mark_delete()
debugPrint("[Remove] Marked for Removal %s" % itemToRemove )
appendToLog("[Remove] Marked for Removal %s" % itemToRemove )
except:
debugPrint("[Error] Packages not found for %s" % model[listItem][1] )
appendToLog("[Error] Packages not found for %s" % model[listItem][1] )
# TODO show install failed not done
installError = "[Error] Packages not found for " + model[listItem][1]
# Commit new cache and remove each set of packages
try:
aptAcquireProgress.start()
progressTimer = GLib.timeout_add(200, renderCellProgress, model, listItem, 'remove')
cache.commit(aptAcquireProgress, aptInstallProgress)
aptAcquireProgress.stop()
GLib.source_remove(progressTimer)
# Update icon after removal
model[listItem][4] = GdkPixbuf.Pixbuf.new_from_file(iconPathBlank)
debugPrint("Un-Install complete for %s" % model[listItem][1] )
appendToLog("Un-Install complete for %s" % model[listItem][1] )
except:
debugPrint("[Error] Un-Install failed for %s" % model[listItem][1] )
appendToLog("[Error] Un-Install failed for %s" % model[listItem][1] )
installError = "[Error] Un-Install failed for " + model[listItem][1]
# Run Pre-install commands
# if item has pre-install run that now
if preInstallList[listItem] != '' :
progressBar.set_text("Running pre-install for "+model[listItem][1])
# Run pre install commands the bash way TODO do this differently to handle errors/timeout
try:
p1 = subprocess.Popen([preInstallList[listItem]], shell=True, stdout=subprocess.PIPE)
output = p1.communicate()[0]
debugPrint("Running Pre install script for : %s" % model[listItem][1])
debugPrint("Pre install script output: %s" % output)
appendToLog("Running Pre install script for : %s" % model[listItem][1])
appendToLog("Pre install script output: %s" % output)
except:
debugPrint("[Error] Pre install script error : %s" % model[listItem][1])
appendToLog("[Error] Pre install script error : %s" % model[listItem][1])
# Install software item FINALLY
installError = ''
# Initial cell progressbar value
model[listItem][3] = 0
progressBar.set_text("Installing "+model[listItem][1])
debugPrint("Installing %s" % model[listItem][1])
appendToLog("Installing %s" % model[listItem][1])
# Set focus
view.set_cursor(listItem)
# open cache again for each install to update new packages list
cache.open()
# Get package list into array
installTitles = installTitleList[listItem].split(' ')
# mark packages for install
for itemToInstall in installTitles:
try:
cache[itemToInstall].mark_install()
debugPrint("[Install] Marked for install %s" % itemToInstall )
appendToLog("[Install] Marked for install %s" % itemToInstall )
except:
debugPrint("[Error] Packages not found for %s" % model[listItem][1] )
appendToLog("[Error] Packages not found for %s" % model[listItem][1] )
# TODO show install failed not done
installError = "[Error] Packages not found for " + model[listItem][1]
# Commit new cache and install each set of packages
try:
#cache.upgrade()
aptAcquireProgress.start()
progressTimer = GLib.timeout_add(200, renderCellProgress, model, listItem, 'install')
cache.commit(aptAcquireProgress, aptInstallProgress)
#cache.commit()
debugPrint("Install complete for %s" % model[listItem][1] )
appendToLog("Install complete for %s" % model[listItem][1] )
except:
debugPrint("[Error] Installation failed for %s" % model[listItem][1] )
# TODO show install failed not done
installError = "[Error] Installation failed for " + model[listItem][1]
# END of Install
# Run POST-install commands
# if item has post-install run that now
if postInstallList[listItem] != '' :
progressBar.set_text("Running post-install for "+model[listItem][1])
# Run post install commands the bash way TODO do this differently to handle errors/timeout
try:
p1 = subprocess.Popen([postInstallList[listItem]], shell=True, stdout=subprocess.PIPE)
output = p1.communicate()[0]
debugPrint("Running Post install script for : %s" % model[listItem][1])
debugPrint("Post install script output: %s" % output)
appendToLog("Running Post install script for : %s" % model[listItem][1])
appendToLog("Post install script output: %s" % output)
except:
debugPrint("[Error] Post install script error : %s" % model[listItem][1])
appendToLog("[Error] Post install script error : %s" % model[listItem][1])
# Set Cell Progress bar and deselect when done
model[listItem][3] = 100
model[listItem][0] = False
GLib.source_remove(progressTimer)
aptAcquireProgress.stop()
time.sleep(0.1)
refreshGui()
time.sleep(0.1)
# Check if install ok and set icon
if installError == '':
iconPathMod = iconPathOk
installStateList[listItem]='installed'
else:
iconPathMod = iconPathError
installStateList[listItem]='error'
# Set icon
model[listItem][4] = GdkPixbuf.Pixbuf.new_from_file(iconPathMod)
# If selected Inc for each item as we know not how many here
# Move progress incrementally depending on number of install items
itemIncCount = itemIncCount + 1
displayInc = progInc * itemIncCount
# Update main progress bar at the end of each item install
progressBar.set_fraction(displayInc)
# All Done - The End -
# Remove Timers
GLib.source_remove(progressTimer)
progressBar.set_fraction(1)
progressBar.set_text('')
# Stop Spinner
spinner.stop()
os.system("python notify.py")
label.set_text('Installation Complete')
debugPrint('[END] Installation Complete')
appendToLog('[END] Installation Complete')
# Set focus
view.set_cursor(0)
# Reset installstatus
installStatus = 'complete'
# Remove Cancel Button and spinner
#Gtk.Container.remove(grid, cancelButton)
Gtk.Container.remove(grid, spinner)
# Activate Install Now/Done button
button.set_sensitive(True)
def on_install_thread(button, model):
global loop_thread
# If button set active and label set to done exit
if button.get_label() == 'Done':
sys.exit()
appendToLog('Install sequence initiated - Install Now')
# Count items before we start
itemCount = len(model)
# count selected items
itemSelectCount = 0
for listItem in range(itemCount):
# Check which items are selected True in list column 0
itemSelected = model[listItem][0]
if itemSelected:
itemSelectCount = itemSelectCount + 1
debugPrint('Number of items selected for install : %s' % str(itemSelectCount))
appendToLog('Number of items selected for install : %s' % str(itemSelectCount))
# Do nothing if no items selected
if itemSelectCount == 0 :
return
# Set button and progress
button.set_sensitive(False)
button.set_label("Done")
progressBar.set_fraction(0.05)
progressBar.set_text('Installation Started')
label.set_text("Installing new software ...")
appendToLog("Installing new software ...")
# Start Spinner
spinner.start()
# start the data function threaded
loop_thread = threading.Thread(target=on_install_button_active, args=[button, model, itemSelectCount])
#loop_thread = Process(target=on_install_button_active, args=(button, model))
loop_thread.start()
appendToLog('Install Thread started')
# timer to sync treads every second 1000ms to get feedback to gui
timerAsync = GLib.timeout_add(100, joinTreads)
def joinTreads():
global loop_thread
# Join Threads every second for 0.5 sec and relax
loop_thread.join(timeout=0.02)
#print "Status :", installStatus
#if installStatus == 'quit':
# loop_thread.join()
# sys.exit()
return True
def renderCancelDialog():
global installStatus
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.QUESTION,
Gtk.ButtonsType.YES_NO, appName )
dialog.set_default_size(400, 250)
dialog.format_secondary_text("Are you sure you would like to Cancel now ?\n")
response = dialog.run()
if response == Gtk.ResponseType.YES:
# loop_thread.join() not needed before it is started and causes issues here
if pulseTimer > 0 :
GLib.source_remove(pulseTimer)
if timerAsync > 0 :
GLib.source_remove(timerAsync)
# Set EXIT flag
installStatus = 'quit'
dialog.destroy()
# Exit
sys.exit()
elif response == Gtk.ResponseType.NO:
debugPrint("Cancel selected - Do nothing")
dialog.destroy()
dialog.destroy()
return
def renderOfflineDialog():
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.WARNING,
Gtk.ButtonsType.OK, appName+' - Offline' )
dialog.set_default_size(400, 250)
dialog.format_secondary_text("An internet connection is required by "+appName+"\nThis application will now quit")
response = dialog.run()
if response == Gtk.ResponseType.OK:
dialog.destroy()
sys.exit()
dialog.destroy()
def on_cancel_button_clicked(button):
renderCancelDialog()
def renderCellProgress(model, path, location):
# Get download part first
totalBytes = aptAcquireProgress.total_bytes
currentBytes = aptAcquireProgress.current_bytes
# Install part
installProgress = aptInstallProgress.percent
try:
percentDown = (currentBytes / (totalBytes * 1.0) * 100)
except(ZeroDivisionError):
percentDown = 0
if percentDown < 100 and location == 'install':
# while downloading show progress up to 50%
currentProgress = (percentDown / 2)
if percentDown == 100 and location == 'install':
# from 50% on we show the install progress
# first take a pause because installProgress still at 100 from previous instance
if percentDown == 100 and installProgress == 100:
time.sleep(0.3)
currentProgress = (installProgress / 2) + 50
if location == 'remove':
# count down
currentProgress = 100.0 - installProgress
if percentDown == 0 and installProgress > 0 and location == 'install':
# on reinstall and cache install
progressBar.set_text("Installing "+model[path][1])
currentProgress = installProgress
if percentDown == 0 and installProgress == 100 and location == 'install':
# another pause to catch up when new install starts and previous installProgress still active
time.sleep(0.5)
currentProgress = installProgress
# update each list item percent ... progressbar set to half - 50%
#currentProgress = float(aptInstallProgress.percent / 1.2)
# Set progress bar value
model[path][3] = int(currentProgress)
#debugPrint("location = %s" % location)
#debugPrint("currentBytes = %s" % currentBytes)
#debugPrint("totalBytes = %s" % totalBytes)
#debugPrint("percentDown = %s" % percentDown)
#debugPrint("installProgress = %s" % str(installProgress))
#debugPrint("[currentProgress] = %s" % currentProgress)
return True
def renderStartupSplash():
global splashWindow
global splashProgressBar
# Create Main window
splashWindow = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
splashWindow.set_title( appName+' '+appVersion )
#splashWindow.set_title( '' )
splashWindow.set_icon(GdkPixbuf.Pixbuf.new_from_file(iconPath))
splashWindow.set_border_width(10)
splashWindow.connect('delete-event', closeWindow, splashWindow)
splashWindow.set_default_size(300, 300)
splashWindow.set_position(Gtk.WindowPosition.CENTER)
splashWindow.set_resizable(False)
# Create an logo Image
logoImage = Gtk.Image()
# set the content of the image as the file filename.png
#pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(iconPath, 300, 300, True)
logoImage.set_from_file(iconPath)
#logoImage.set_from_pixbuf(pixbuf)
# Create Label
#splashLabel = Gtk.Label()
#splashLabel.set_text(" Checking installed software... ")
# Create progress bar
splashProgressBar = Gtk.ProgressBar()
splashProgressBar.set_show_text(True)
splashProgressBar.set_ellipsize(3)
splashProgressBar.set_fraction(0.1)
splashProgressBar.set_text('Initialising...')
# a grid to attach the elements
splashGrid = Gtk.Grid()
#splashGrid.set_column_homogeneous(True)
#splashGrid.set_row_homogeneous(True)
splashGrid.set_row_spacing(15)
splashGrid.attach(logoImage, 0, 0, 1, 1)
#splashGrid.attach(splashLabel, 0, 1, 1, 1)
splashGrid.attach(splashProgressBar, 0, 2, 1, 1)
# attach the grid to the window
splashWindow.add(splashGrid)
# Show Window
splashWindow.show_all()
def renderMainWindow():
global progressBar
global label
global view
global spinner
global grid
global cancelButton
# Create Main window
mainWindow = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
mainWindow.set_title( appName+' '+appVersion )
mainWindow.set_icon(GdkPixbuf.Pixbuf.new_from_file(iconPath))
mainWindow.set_border_width(10)
mainWindow.connect('delete-event', closeWindow, mainWindow)
mainWindow.set_default_size(560, 400)
mainWindow.set_position(Gtk.WindowPosition.CENTER)
mainWindow.set_resizable(True)
view = Gtk.TreeView(menuItemStore)
# Make TreeView resize vertically only
view.set_hexpand(True)
view.set_vexpand(True)
columns = ["Select", " Title",
" Description", " Install Progress ", " "]
# Render menuItemStore columns
for i in range(len(columns)):
# Render first column as toggle
if i == 0:
cell = Gtk.CellRendererToggle()
cell.connect('toggled', on_cell_toggle, menuItemStore)
# ceate coloumn and get val from liststore col 0
col = Gtk.TreeViewColumn(columns[i], cell, active=i)
if i == 1 or i ==2 :
# cellrenderer to render the text
cell = Gtk.CellRendererText()
# create column
col = Gtk.TreeViewColumn(columns[i], cell, text=i)
# the text in the first column should be in boldface
if i == 1:
cell.props.weight_set=True
cell.props.weight=Pango.Weight.BOLD
# the column is created
if i == 3:
cell = Gtk.CellRendererProgress()
# progress value from liststore col 3
col = Gtk.TreeViewColumn(columns[i], cell, value=i)
# the icon column is created
if i == 4:
cell = Gtk.CellRendererPixbuf()
cell.set_fixed_size(30, -1)
# progress value from liststore col 4
col = Gtk.TreeViewColumn(columns[i], cell, pixbuf=i)
col.set_min_width(30)
#col.set_fixed_width(30)
#col.set_expand(False)
# and it is appended to the treeview
view.append_column(col)
# Create an logo Image
#logoImage = Gtk.Image()
# set the content of the image as the file filename.png
#logoImage.set_from_file(iconPath)
# Create Label
label = Gtk.Label()
label.set_text("Selecione os Softwares que deseja instalar")
# Create progress bar
progressBar = Gtk.ProgressBar()
#progressBar.set_text("")
progressBar.set_show_text(True)
progressBar.set_fraction(0.0)
progressBar.set_text(' ')
# Create Spinner
spinner = Gtk.Spinner()
# Create Install Button
installButton = Gtk.Button("Install")
installButton.connect("clicked", on_install_thread, menuItemStore)
# Create Cancel Button
cancelButton = Gtk.Button("_Cancel", use_underline=True)
cancelButton.connect("clicked", on_cancel_button_clicked)
# CALLBACK for all changes item selected in select list
view.get_selection().connect("changed", on_changed, label)
# create scrollwindow
scrollTree = Gtk.ScrolledWindow()
scrollTree.set_min_content_width(560)
scrollTree.set_min_content_height(300)
scrollTree.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
# Put liststore view in scroll window
scrollTree.add(view)
# a grid to attach the elements
grid = Gtk.Grid()
grid.set_row_spacing(15)
grid.attach(label, 0, 0, 3, 1)
grid.attach(progressBar, 0, 1, 3, 1)
grid.attach(scrollTree, 0, 3, 3, 1)
grid.attach(cancelButton, 1, 4, 1, 1)
grid.attach_next_to(spinner, cancelButton, Gtk.PositionType.LEFT, 1, 1)
grid.attach_next_to(installButton, cancelButton, Gtk.PositionType.RIGHT, 1, 1)
#grid.attach(spinner, 2, 0, 1, 1)
#grid.attach_next_to(spinner, label, Gtk.PositionType.RIGHT, 1, 1)
# attach the grid to the window
mainWindow.add(grid)
appendToLog('Main display rendered')
# Render Main Window
mainWindow.show_all()
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def checkInstall(software):
# Convert software list into array
software = software.split(' ')
# Check each item and if one is missing assume not installed
for item in software:
try:
isInstalled = cache[item].is_installed # Evaluates true if git is installed
except:
isInstalled = False
# if any item on install list is False return False
if isInstalled == False:
return isInstalled
# if installed return True as False would already been returned
return isInstalled
############################# Main Loop
#
if __name__ == "__main__":
# Vars
verboseDebug = True
# Main Env Vars
appName = 'Kefir After Install'
procName = 'Kefir-after-install'
appVersion = 'Alpha'
userHome = os.getenv("HOME")
timeStamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
installDir = './'
logFile = '/var/log/kefir-after-install.log'
lockFilePath = os.path.join('/tmp/ubuntu-after-install.lock')
iconPath = os.path.join(installDir, 'kefir-after-install.png')
iconPathError = os.path.join(installDir, 'lib', 'icons', 'empathy-busy.png')
iconPathReinstall = os.path.join(installDir, 'lib', 'icons', 'empathy-orange.png')
iconPathOk = os.path.join(installDir, 'lib', 'icons', 'empathy-available.png')
iconPathBlank = os.path.join(installDir, 'lib', 'icons', 'empathy-offline.png')
xmlFilename = 'kefir-after-install.xml'
xmlPath = os.path.join(installDir, xmlFilename)
VersionFilename = ''
localVersionPath = os.path.join(installDir, VersionFilename)
remoteVersionDir = 'https://'
remoteVersionPath = os.path.join(remoteVersionDir, VersionFilename)
remoteXmlPath = os.path.join(remoteVersionDir, xmlFilename)
aptListPath = '/etc/apt/sources.list.d/'
# App Vars
connectionStatus = False
installStatus = ''
pulseTimer = 0
timerAsync = 0
progressTimer = 0
p1 = ''
# Set running process title using libc
set_proc_name(procName)