forked from anticapitalista/apt-notifier
-
Notifications
You must be signed in to change notification settings - Fork 10
/
apt-notifier.py
executable file
·2317 lines (2050 loc) · 98.4 KB
/
apt-notifier.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/python3
# -*- coding: utf-8 -*-
import subprocess
import sys
import os
import dbus
import tempfile
from os import environ
import notify2
from PyQt5 import QtWidgets, QtGui
from PyQt5 import QtCore
from distutils import spawn
from time import sleep
# check version_at_start
cmd = "dpkg-query -f ${Version} -W apt-notifier"
version_at_start = subprocess.run(cmd.split(), capture_output=True, universal_newlines=True).stdout.strip()
rc_file_name = environ.get('HOME') + '/.config/apt-notifierrc'
message_status = "not displayed"
notify2.init("MX Updater")
def fix_fluxbox_startup():
cmd = "[ -f ~/.fluxbox/startup ]"
cmd += " && sed -i -r -e '\![[:space:]]*(/usr/bin/)?python[23]?[[:space:]]+(/usr/bin/)?apt-notifier.py.*!s![[:space:]]*(/usr/bin/)?python[23]?[[:space:]]+! !' ~/.fluxbox/startup;"
run = subprocess.run(cmd, shell=True, executable="/bin/bash")
def package_manager():
global package_manager
global package_manager_name
global package_manager_exec
if spawn.find_executable("synaptic-pkexec"):
package_manager = "synaptic"
package_manager_exec = "synaptic-pkexec"
package_manager_name = "Synaptic"
elif spawn.find_executable("muon"):
package_manager = "muon"
package_manager_name = "Muon"
if spawn.find_executable("muon-pkexec"):
package_manager_exec = "muon-pkexec"
elif spawn.find_executable("mx-pkexec"):
package_manager_exec = "mx-pkexec muon"
else:
package_manager_exec = "su-to-root -X -c muon"
else:
package_manager = None
sys.exit("Error: No package manager found! Synaptic or Muon are required.")
package_manager()
# ~~~ Localize 0 ~~~
# Use gettext and specify translation file locations
import gettext
gettext.bindtextdomain('apt-notifier', '/usr/share/locale')
gettext.textdomain('apt-notifier')
_ = gettext.gettext
gettext.install('apt-notifier.py')
from string import Template # for simple string substitution (popup_msg...)
def set_translations():
global tooltip_0_updates_available
global tooltip_1_new_update_available
global tooltip_multiple_new_updates_available
global popup_title
global popup_msg_1_new_update_available
global popup_msg_multiple_new_updates_available
global Upgrade_using_package_manager
global View_and_Upgrade
global Hide_until_updates_available
global Quit_Apt_Notifier
global Apt_Notifier_Help
global Package_Manager_Help
global Apt_Notifier_Preferences
global Apt_History
global View_Auto_Updates_Logs
global View_Auto_Updates_Dpkg_Logs
global Check_for_Updates
global Force_Check_Counter
Force_Check_Counter = 0
global About
global Check_for_Updates_by_User
Check_for_Updates_by_User = 'false'
global ignoreClick
ignoreClick = '0'
global WatchedFilesAndDirsHashNow
WatchedFilesAndDirsHashNow = ''
global WatchedFilesAndDirsHashPrevious
WatchedFilesAndDirsHashPrevious = ''
global AvailableUpdates
AvailableUpdates = ''
global MX_Package_Installer
# ~~~ Localize 1 ~~~
tooltip_0_updates_available = str (_("0 updates available") )
tooltip_0_updates_available = str (_("No updates available") )
tooltip_1_new_update_available = str (_("1 new update available") )
tooltip_multiple_new_updates_available = str (_("$count new updates available") )
popup_title = str (_("Updates") )
popup_msg_1_new_update_available = str (_("You have 1 new update available") )
popup_msg_multiple_new_updates_available = str (_("You have $count new updates available") )
Upgrade_using_package_manager = str (_("Upgrade using Synaptic") )
Upgrade_using_package_manager = Upgrade_using_package_manager.replace('Synaptic', package_manager_name)
View_and_Upgrade = str (_("View and Upgrade") )
Hide_until_updates_available = str (_("Hide until updates available") )
Quit_Apt_Notifier = str (_("Quit") )
Apt_Notifier_Help = str (_("MX Updater Help") )
Package_Manager_Help = str (_("Synaptic Help") )
Package_Manager_Help = Package_Manager_Help.replace("Synaptic", package_manager_name)
Apt_Notifier_Preferences = str (_("Preferences") )
Apt_History = str (_("History") )
View_Auto_Updates_Logs = str (_("Auto-update log(s)") )
View_Auto_Updates_Dpkg_Logs = str (_("Auto-update dpkg log(s)") )
Check_for_Updates = str (_("Check for Updates") )
About = str (_("About") )
MX_Package_Installer = str (_("MX Package Installer") )
# Check for updates, using subprocess.Popen
def check_updates():
global message_status
global AvailableUpdates
global WatchedFilesAndDirsHashNow
global WatchedFilesAndDirsHashPrevious
global Check_for_Updates_by_User
global Force_Check_Counter
"""
Don't bother checking for updates when /var/lib/apt/periodic/update-stamp
isn't present. This should only happen in a Live session before the repository
lists have been loaded for the first time.
"""
cmd = "[ ! -e /var/lib/apt/periodic/update-stamp ] && [ ! -e /var/lib/apt/lists/lock ]"
run = subprocess.run(cmd, shell=True)
if run.returncode == 0:
if AvailableUpdates == '':
AvailableUpdates = '0'
message_status = "not displayed" # Resets flag once there are no more updates
add_hide_action()
if icon_config != "show":
AptIcon.hide()
else:
AptIcon.setIcon(NoUpdatesIcon)
cmd = 'U=0; eval $(apt-config shell U APT::Periodic::Unattended-Upgrade); [ "${U}" != "0" ]'
run = subprocess.run(cmd, shell=True)
if run.returncode == 0:
AptIcon.setToolTip("")
else:
AptIcon.setToolTip(tooltip_0_updates_available)
return
"""
Don't bother checking for updates if processes for other package management tools
appear to be runninng. For unattended-upgrade, use '/usr/bin/unattended-upgrade'
to avoid getting a hit on /usr/share/unattended-upgrades/unattended-upgrade-shutdown
which appears to be started automatically when using systemd as init.
"""
cmd = "sudo lsof "
cmd+= "/var/lib/dpkg/lock "
cmd+= "/var/lib/dpkg/lock-frontend "
cmd+= "/var/lib/apt/lists/lock "
cmd+= "/var/cache/apt/archives/lock "
cmd+= "2>/dev/null "
cmd+= "| grep -qE 'lock$|lock-frontend$'"
ret = subprocess.run(cmd, shell=True).returncode
if ret == 0:
Force_Check_Counter = 5
return
"""
Get a hash of files and directories we are watching
"""
script = '''#!/bin/bash
WatchedFilesAndDirs=(
/etc/apt/apt.conf*
/etc/apt/preferences*
/var/lib/apt*
/var/lib/apt/lists
/var/lib/apt/lists/partial
/var/lib/dpkg
/var/cache/apt
/var/lib/synaptic/preferences
)
stat -c %Y,%Z ${WatchedFilesAndDirs[*]} 2>/dev/null | md5sum
'''
cmd = script
run = subprocess.run(cmd, capture_output=True, shell=True, text=True, executable="/bin/bash")
WatchedFilesAndDirsHashNow = run.stdout.strip()
"""
If
no changes in hash of files and directories being watched since last checked
AND
the call to check_updates wasn't initiated by user
then don't bother checking for updates.
"""
if WatchedFilesAndDirsHashNow == WatchedFilesAndDirsHashPrevious:
if Check_for_Updates_by_User == 'false':
if Force_Check_Counter < 5:
Force_Check_Counter = Force_Check_Counter + 1
if AvailableUpdates == '':
AvailableUpdates = '0'
return
WatchedFilesAndDirsHashPrevious = WatchedFilesAndDirsHashNow
WatchedFilesAndDirsHashNow = ''
Force_Check_Counter = 1
Check_for_Updates_by_User = 'false'
#Create an inline script (what used to be /usr/bin/apt-notifier-check-Updates)
#and then run it to get the number of updates.
script = '''#!/bin/bash
# prepare pinned package handling
AptPref_Opts=""
AptPreferences=""
PinnedPreferences=""
[ "${PATH%%/sbin*}" = "$PATH" ] && PATH="$PATH:/sbin:/usr/sbin"
if grep -sq "^Package" /var/lib/synaptic/preferences && which synaptic >/dev/null; then
PinnedPreferences=/var/lib/synaptic/preferences
fi
if [ -n "$PinnedPreferences" ] && [ -r "$PinnedPreferences" ]; then
eval $(apt-config shell AptPreferences Dir::Etc::preferences)
[ -n "${AptPreferences%%/*}" ] && AptPreferences=/etc/apt/${AptPreferences}
if [ ! -f ${AptPreferences} ]; then
AptPref_Opts=" -o Dir::Etc::preferences=${PinnedPreferences}"
else
tmp_apt_pref=$(mktemp -t apt_tmp_preferences.XXXXXXXXXXXX)
chmod 644 $tmp_apt_pref
trap "rm -f $tmp_apt_pref" EXIT
cat ${AptPreferences} >> $tmp_apt_pref
echo "" >> $tmp_apt_pref
cat $PinnedPreferences >> $tmp_apt_pref
AptPref_Opts=" -o Dir::Etc::preferences=${tmp_apt_pref}"
fi
fi
# UpgradeType: dist-upgrade or upgrade
UpgradeCounts=""
DistUpgradeCounts=""
UpgradeType=$(grep -m1 ^UpgradeType ~/.config/apt-notifierrc | cut -f2 -d=)
case $UpgradeType in
upgrade) UpgradeCounts=$(LANG=C apt-get -s $AptPref_Opts upgrade | grep -c '^Inst ')
;;
*) DistUpgradeCounts=$(LANG=C apt-get -s $AptPref_Opts dist-upgrade | grep -c '^Inst ')
;;
esac
#Suppress 'updates available' notification if Unattended-Upgrades are enabled (>=1) AND apt-get upgrade & dist-upgrade output are the same
Unattended_Upgrade=0
eval $(apt-config shell Unattended_Upgrade APT::Periodic::Unattended-Upgrade)
if [ $Unattended_Upgrade != 0 ]; then
if [ -z "$UpgradeCounts" ]; then
UpgradeCounts=$(LANG=C apt-get -s $AptPref_Opts upgrade | grep -c '^Inst ')
fi
if [ -z "$DistUpgradeCounts" ]; then
DistUpgradeCounts=$( LANG=C apt-get -s $AptPref_Opts dist-upgrade | grep -c '^Inst ')
fi
if [ "$UpgradeCounts" = "$DistUpgradeCounts" ]; then
echo 0
exit
fi
fi
case $UpgradeType in
upgrade) echo $UpgradeCounts
;;
*) echo $DistUpgradeCounts
;;
esac
exit
# commented out to enable backports-upgrade: B/c backports do have
# NotAutomatic=yes and ButAutomaticaUpgrades=yes
#
#Suppress the 'updates available' notification if all of the updates are from a backports repo (jessie-backports, stretch-backports, etc.)
#if [ "$(grep " => " <<<"$Updates" | wc -l)" = "$(grep " => " <<<"$Updates" | grep -E ~bpo[0-9]+[+][0-9]+[\)]$ | wc -l)" ]
# then
# echo 0
# exit
#fi
'''
run = subprocess.run(script, capture_output=True, shell=True, text=True, executable="/bin/bash")
# Read the output into a text string
AvailableUpdates = run.stdout.strip()
# Alter both Icon and Tooltip, depending on updates available or not
if AvailableUpdates == "":
AvailableUpdates = "0"
if AvailableUpdates == "0":
message_status = "not displayed" # Resets flag once there are no more updates
add_hide_action()
if icon_config != "show":
AptIcon.hide()
else:
AptIcon.setIcon(NoUpdatesIcon)
cmd = 'U=0; eval $(apt-config shell U APT::Periodic::Unattended-Upgrade); [ "${U}" != "0" ]'
run = subprocess.run(cmd, shell=True)
if run.returncode == 0:
AptIcon.setToolTip("")
else:
AptIcon.setToolTip(tooltip_0_updates_available)
else:
if AvailableUpdates == "1":
AptIcon.setIcon(NewUpdatesIcon)
AptIcon.show()
AptIcon.setToolTip(tooltip_1_new_update_available)
add_rightclick_actions()
# Shows the pop up message only if not displayed before
if message_status == "not displayed":
cmd = "for WID in $(wmctrl -l | cut -d' ' -f1); do xprop -id $WID | grep 'NET_WM_STATE(ATOM)'; done | grep -sq _NET_WM_STATE_FULLSCREEN"
run = subprocess.run(cmd, shell=True)
if run.returncode == 1:
cmd = "sed -n -r s/./\L&/g;s/^usenotifier=(..)/\\1/p"
cmd = cmd.split() + [rc_file_name]
run = subprocess.run(cmd, capture_output=True, universal_newlines=True)
UseNotifier = run.stdout.strip()
if UseNotifier == "":
ret = subprocess.run("pgrep -x xfdesktop".split(), stdout=subprocess.DEVNULL).returncode
if ret == 0:
running_in_xfce = True
else:
running_in_xfce = False
if running_in_xfce:
run = subprocess.run("dpkg-query -f ${Version} -W xfdesktop4".split(), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True)
xfce_version = run.stdout.strip()
if xfce_version.startswith("4.16"):
UseNotifier = "py"
else:
UseNotifier = "qt"
if UseNotifier == "":
UseNotifier = "qt"
print( "UseNotifier:" + UseNotifier)
if UseNotifier.startswith("qt"):
def show_message():
AptIcon.showMessage(popup_title, popup_msg_1_new_update_available)
Timer.singleShot(1000, show_message)
else:
ICON_PATH = "/usr/share/icons/hicolor/scalable/mx-updater.svg"
notify = notify2.Notification(None, icon = ICON_PATH)
notify.timeout = 10000
notify.update(popup_title, popup_msg_1_new_update_available)
notify.show()
message_status = "displayed"
else:
AptIcon.setIcon(NewUpdatesIcon)
AptIcon.show()
tooltip_template=Template(tooltip_multiple_new_updates_available)
tooltip_with_count=tooltip_template.substitute(count=AvailableUpdates)
AptIcon.setToolTip(tooltip_with_count)
add_rightclick_actions()
# Shows the pop up message only if not displayed before
if message_status == "not displayed":
cmd = "for WID in $(wmctrl -l | cut -d' ' -f1); do xprop -id $WID | grep 'NET_WM_STATE(ATOM)'; done | grep -sq _NET_WM_STATE_FULLSCREEN"
run = subprocess.run(cmd, shell=True)
if run.returncode == 1:
# ~~~ Localize 1b ~~~
# Use embedded count placeholder.
popup_template=Template(popup_msg_multiple_new_updates_available)
popup_with_count=popup_template.substitute(count=AvailableUpdates)
cmd = "sed -n -r s/./\L&/g;s/^usenotifier=(..)/\\1/p"
cmd = cmd.split() + [rc_file_name]
run = subprocess.run(cmd, capture_output=True, universal_newlines=True)
UseNotifier = run.stdout.strip()
if UseNotifier == "":
ret = subprocess.run("pgrep -x xfdesktop".split(), stdout=subprocess.DEVNULL).returncode
if ret == 0:
running_in_xfce = True
else:
running_in_xfce = False
if running_in_xfce:
run = subprocess.run("dpkg-query -f ${Version} -W xfdesktop4".split(), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True)
xfce_version = run.stdout.strip()
if xfce_version.startswith("4.16"):
UseNotifier = "py"
else:
UseNotifier = "qt"
if UseNotifier == "":
UseNotifier = "qt"
print( "UseNotifier:" + UseNotifier)
if UseNotifier.startswith("qt"):
def show_message():
#AptIcon.showMessage(popup_title, popup_msg_multiple_new_updates_available_begin + AvailableUpdates + popup_msg_multiple_new_updates_available_end)
AptIcon.showMessage(popup_title, popup_with_count)
Timer.singleShot(1000, show_message)
else:
ICON_PATH = "/usr/share/icons/hicolor/scalable/mx-updater.svg"
notify = notify2.Notification(None, icon=ICON_PATH)
notify.timeout = 10000
notify.update(popup_title, popup_with_count)
notify.show()
message_status = "displayed"
def start_package_manager():
global Check_for_Updates_by_User
cmd = "pgrep -x plasmashell >/dev/null && exit 1 || exit 0"
running_in_plasma = subprocess.run(cmd, shell=True).returncode
if running_in_plasma:
systray_icon_hide()
cmd = package_manager_exec + "; ionice -c3 nice -n19 /usr/bin/apt-notifier.py & disown -h;"
run = subprocess.Popen(cmd, shell=True, executable="/bin/bash")
AptIcon.hide()
sleep(1);
sys.exit(0)
else:
run = subprocess.run(package_manager_exec)
cmd = "dpkg-query -f ${Version} -W apt-notifier"
version_installed = subprocess.run(cmd.split(), capture_output=True, universal_newlines=True).stdout.strip()
if version_installed != version_at_start:
cmd = "apt-notifier-unhide-Icon & disown -h >/dev/null 2>/dev/null"
run = subprocess.run(cmd, shell=True, executable="/bin/bash")
sleep(2)
Check_for_Updates_by_User = 'true'
check_updates()
def viewandupgrade():
global Check_for_Updates_by_User
systray_icon_hide()
initialize_aptnotifier_prefs()
# ~~~ Localize 2 ~~~
# Accommodations for transformation from Python literals to Bash literals:
# t10: \\n will convert to \n
# t12: \\n will convert to \n
# t16: '( and )' moved outside of translatable string to protect from potential translator's typo
# t18: \\\"n\\\" will convert to \"n\" which will become "n" in shell (to avoid concatenating shell strings)
# t01 thru t12, Yad 'View and Upgrade' strings
t01 = _("MX Updater--View and Upgrade, previewing: basic upgrade")
t02 = _("MX Updater--View and Upgrade, previewing: full upgrade")
#t03 = _("Automatically answer 'yes' to all prompts during full/basic upgrade")
t03 = _("Automatically answer 'yes' to all prompts during upgrade")
#t04 = _("automatically close terminal window when basic upgrade complete")
t04 = _("automatically close terminal window when upgrade complete")
#t05 = _("automatically close terminal window when full upgrade complete")
t05 = _("automatically close terminal window when upgrade complete")
t06 = _("basic upgrade")
t07 = _("full upgrade")
t08 = _("switch to basic upgrade")
t09 = _("switch to full upgrade")
t10 = _("Switches the type of Upgrade that will be performed, alternating back and forth between 'full upgrade' and 'basic upgrade'.")
t11 = _("Reload")
t12 = _("Reload the package information to become informed about new, removed or upgraded software packages. (apt-get update)")
# t14 thru t19, strings for the upgrade (basic) / dist-upgrade (full) script that runs in the terminal window
t14 = _("basic upgrade complete (or was canceled)")
t15 = _("full upgrade complete (or was canceled)")
t16 = _("this terminal window can now be closed")
t17 = "'(" + _("press any key to close") + ")'"
t18 = _("Unneeded packages are installed that can be removed.")
t19 = _("Running apt-get autoremove, if you are unsure type 'n'.")
t20 = _("upgrade")
t21 = _("Using full upgrade")
t22 = _("Using basic upgrade (not recommended)")
t23 = _("press any key to close")
shellvar = (
' window_title_basic="' + t01 + '"\n'
' window_title_full="' + t02 + '"\n'
' use_apt_get_dash_dash_yes="' + t03 + '"\n'
' auto_close_window_basic="' + t04 + '"\n'
' auto_close_window_full="' + t05 + '"\n'
' basic_upgrade="' + t06 + '"\n'
' full_upgrade="' + t07 + '"\n'
' switch_to_basic_upgrade="' + t08 + '"\n'
' switch_to_full_upgrade="' + t09 + '"\n'
' switch_tooltip="' + t10 + '"\n'
' reload="' + t11 + '"\n'
' reload_tooltip="' + t12 + '"\n'
' done1basic="' + t14 + '"\n'
' done1full="' + t15 + '"\n'
' done2="' + t16 + '"\n'
' done3="' + t17 + '"\n'
' autoremovable_packages_msg1="' + t18 + '"\n'
' autoremovable_packages_msg2="' + t19 + '"\n'
' upgrade="' + t20 + '"\n'
' upgrade_tooltip_full="' + t21 + '"\n'
' upgrade_tooltip_basic="' + t22 + '"\n'
' PressAnyKey="' + t23 + '"\n'
)
script = '''#!/bin/bash
#cancel updates available indication if 2 or more Release.reverify entries found
#if [ $(ls -1 /var/lib/apt/lists/partial/ | grep Release.reverify$ | wc -l) -ge 2 ]; then exit; fi
''' + shellvar + '''
RunAptScriptInTerminal(){
#for MEPIS remove "MX" branding from the $window_title string
window_title_term=$window_title
window_title_term=$(echo "$2"|sed 's/MX /'$(grep -o MX.*[1-9][0-9] /etc/issue|cut -c1-2)" "'/')
TermXOffset="$(xwininfo -root|awk '/Width/{print $2/4}')"
TermYOffset="$(xwininfo -root|awk '/Height/{print $2/4}')"
G=" --geometry=80x25+"$TermXOffset"+"$TermYOffset
# I=" --icon=mnotify-some-""$(grep IconLook ~/.config/apt-notifierrc | cut -f2 -d=)"
I=" --icon=mx-updater"
if [ "$3" = "" ]
then T=""; I=""
else
if [ "$3" != "update" ]
then T=" --title='""$(grep -o MX.*[1-9][0-9] /etc/issue|cut -c1-2)"" Updater: "$3"'"
else T=" --title='""$(grep -o MX.*[1-9][0-9] /etc/issue|cut -c1-2)"" Updater: "$reload"'"
fi
fi
<<'Disabled'
if (xprop -root | grep -i kde > /dev/null)
then
# Running KDE
#
# Can't get su-to-root to work in newer KDE's, so use kdesu for
# authentication.
#
# If x-terminal-emulator is set to xfce4-terminal.wrapper, use
# xfce4-terminal instead because the --hold option doesn't work with
# the wrapper. Also need to enclose the apt-get command in single
# quotes.
#
# If x-terminal-emulator is set to xterm, use konsole instead, if
# it's available (it should be).
case $(readlink -e /usr/bin/x-terminal-emulator | xargs basename) in
gnome-terminal.wrapper) if [ -e /usr/bin/konsole ]
then
$(kde4-config --path libexec)kdesu -c "konsole -e $3"
sleep 5
while [ "$(ps aux | grep [0-9]' konsole -e apt-get update')" != "" ]
do
sleep 1
done
sleep 1
else
:
fi
;;
konsole) $(kde4-config --path libexec)kdesu -c "konsole -e $3"
pgrep -x plasmashell >/dev/null || sleep 5
while [ "$(ps aux | grep [0-9]' konsole -e apt-get update')" != "" ]
do
sleep 1
done
sleep 1
;;
roxterm) $(kde4-config --path libexec)kdesu -c "roxterm$G$T --separate -e $3"
;;
xfce4-terminal.wrapper) $(kde4-config --path libexec)kdesu --noignorebutton -d -c "xfce4-terminal$G$I$T -e $3"
;;
xterm) if [ -e /usr/bin/konsole ]
then
$(kde4-config --path libexec)kdesu -c "konsole -e $3"
pgrep -x plasmashell >/dev/null || sleep 5
while [ "$(ps aux | grep [0-9]' konsole -e apt-get update')" != "" ]
do
sleep 1
done
sleep 1
else
$(kde4-config --path libexec)kdesu -c "xterm -e $3"
fi
;;
*) $(kde4-config --path libexec)kdesu -c "x-terminal-emulator -e $3"
sleep 5
while [ "$(ps aux | grep [0-9]' konsole -e apt-get update')" != "" ]
do
sleep 1
done
sleep 1
;;
esac
else
# Running a non KDE desktop
#
# Use pkexec for authentication.
#
# If x-terminal-emulator is set to xfce4-terminal.wrapper, use
# xfce4-terminal instead because the --hold option doesn't work
# with the wrapper. Also need to enclose the apt-get command in
# single quotes.
#
# If x-terminal-emulator is set to xterm, use xfce4-terminal
# instead, if it's available (it is in MX)
Disabled
case $(readlink -e /usr/bin/x-terminal-emulator | xargs basename) in
gnome-terminal.wrapper) sh "$1" "gnome-terminal$G$T -e $4"
;;
konsole) sh "$1" "konsole -e $4"
pgrep -x plasmashell >/dev/null || sleep 5
while [ "$(ps aux | grep [0-9]' konsole -e apt-get update')" != "" ]
do
sleep 1
done
sleep 1
;;
roxterm) sh "$1" "roxterm$G$T --separate -e $4"
;;
xfce4-terminal.wrapper) sh "$1" "xfce4-terminal --hide-menubar $G$I$T -e $4" 2>/dev/null 1>/dev/null
;;
xterm) if [ -e /usr/bin/xfce4-terminal ]
then
sh "$1" "xfce4-terminal --hide-menubar $G$I$T -e $4"
else
sh "$1" "xterm -fa monaco -fs 12 -bg black -fg white -e $4"
fi
;;
*) sh "$1" "x-terminal-emulator -e $4"
;;
esac
#fi
}
DoUpgrade(){
case $1 in
0)
BP="1"
chmod +x $TMP/upgradeScript
T="$(grep -o MX.*[1-9][0-9] /etc/issue|cut -c1-2) Updater: $UpgradeTypeUserFriendlyName"
# I="mnotify-some-$(grep IconLook ~/.config/apt-notifierrc | cut -f2 -d=)"
I="mx-updater"
#if [[ $(find /usr/share/{icons,pixmaps} -name mx-updater.svg) ]]
# then
# if [ $(grep IconLook=wireframe ~/.config/apt-notifierrc) ]
# then
# I="/usr/share/icons/Papirus/64x64/apps/mx-updater.svg"
# fi
#fi
if [ "$(grep UpgradeType ~/.config/apt-notifierrc | cut -f2 -d=)" = "dist-upgrade" ]
then
/usr/lib/apt-notifier/pkexec-wrappers/mx-updater-full-upgrade "$T" "$I" "$TMP/upgradeScript"
else
/usr/lib/apt-notifier/pkexec-wrappers/mx-updater-basic-upgrade "$T" "$I" "$TMP/upgradeScript"
fi
if [ ! -x /usr/bin/xfce4-terminal ]; then
while [ "$(ps aux | grep -v grep | grep bash.*/usr/lib/apt-notifier/pkexec-wrappers/mx-updater-full-upgrade.*MX.*mnotify-some.*/tmp/apt-notifier.*/upgradeScript)" ]
do
sleep 1
done
sleep 1
fi
;;
2)
BP="1"
;;
4)
BP="0"
sed -i 's/UpgradeType='$UpgradeType'/UpgradeType='$OtherUpgradeType'/' ~/.config/apt-notifierrc
;;
8)
BP="0"
#chmod +x $TMP/upgradeScript
#RunAptScriptInTerminal "/usr/lib/apt-notifier/pkexec-wrappers/mx-updater-reload.sh" "" "$reload" "$TMP/upgradeScript"
#I="mnotify-some-$(grep IconLook ~/.config/apt-notifierrc | cut -f2 -d=)"
#if [[ $(find /usr/share/{icons,pixmaps} -name mx-updater.svg) ]]
# then
# if [ $(grep IconLook=wireframe ~/.config/apt-notifierrc) ]
# then
# I="/usr/share/icons/Papirus/64x64/apps/mx-updater.svg"
# fi
#fi
I="mx-updater"
/usr/lib/apt-notifier/pkexec-wrappers/mx-updater-reload.sh \
" --title=""$(grep -o MX.*[1-9][0-9] /etc/issue|cut -c1-2)"" Updater: $reload" \
" --icon=$I" \
#"$PressAnyKey"
if [ ! -x /usr/bin/xfce4-terminal ]; then
while [ "$(ps aux | grep -v grep | grep "bash -c".*"apt-get update".*"sleep".*"mx-updater_reload".*"read.*-p")" ]
do
sleep 1
done
sleep 1
fi
;;
*)
BP="1"
;;
esac
}
BP="0"
while [ $BP != "1" ]
do
UpgradeType=$(grep ^UpgradeType ~/.config/apt-notifierrc | cut -f2 -d=)
if [ "$UpgradeType" = "upgrade" ]; then
UpgradeTypeUserFriendlyName=$basic_upgrade
OtherUpgradeType="dist-upgrade"
upgrade_tooltip=$upgrade_tooltip_basic
fi
if [ "$UpgradeType" = "dist-upgrade" ]; then
UpgradeTypeUserFriendlyName=$full_upgrade
OtherUpgradeType="upgrade"
upgrade_tooltip=$upgrade_tooltip_full
fi
UpgradeAssumeYes=$(grep ^UpgradeAssumeYes ~/.config/apt-notifierrc | cut -f2 -d=)
UpgradeAutoClose=$(grep ^UpgradeAutoClose ~/.config/apt-notifierrc | cut -f2 -d=)
TMP=$(mktemp -d /tmp/apt-notifier.XXXXXX)
echo "$UpgradeTypeUserFriendlyName" > "$TMP"/upgrades
#The following 40 or so lines (down to the "APT_CONFIG" line) create a temporary etc/apt folder and subfolders
#that for the most part match the root owned /etc/apt folder and it's subfolders.
#
#A symlink to /var/synaptic/preferences symlink ("$TMP"/etc/apt/preferences.d/synaptic-pins) will be created
#if there isn't one already (note: the non-root user wouldn't be able to create one in /etc/apt/preferences.d/).
#
#With a /var/synaptic/preferences symlink in place, no longer need to remove the lines with Synaptic pinned packages
#from the "$TMP"/upgrades file to keep them from being displayed in the 'View and Upgrade' window, also no longer
#need to correct the upgrades count after removing the lines with the pinned updates.
#create the etc/apt/*.d subdirectories in the temporary directory ("$TMP")
for i in $(find /etc/apt -name *.d); do mkdir -p "$TMP"/$(echo $i | cut -f2- -d/); done
#create symlinks to the files in /etc/apt and it's subdirectories with exception of /etc/apt and /etc/apt/apt.conf
for i in $(find /etc/apt | grep -v -e .d$ -e apt.conf$ -e apt$); do ln -s $i "$TMP"/$(echo $i | cut -f2- -d/) 2>/dev/null; done
#in etc/preferences test to see if there's a symlink to /var/lib/synaptic/preferences
ls -l /etc/apt/preferences* | grep ^l | grep -m1 /var/lib/synaptic/preferences$ > /dev/null
#if there isn't, create one if there are synaptic pinned packages
if [ $? -eq 1 ]
then
if which synaptic-pkexec > /dev/null && [ -s /var/lib/synaptic/preferences ]; then
ln -s /var/lib/synaptic/preferences "$TMP"/etc/apt/preferences.d/synaptic-pins 2>/dev/null
fi
fi
#create a apt.conf in the temp directory by copying existing /etc/apt/apt.conf to it
[ ! -e /etc/apt/apt.conf ] || cp /etc/apt/apt.conf "$TMP"/apt.conf
#in apt.conf file set Dir to the path of the temp directory
echo 'Dir "'"$TMP"'/";' >> "$TMP"/apt.conf
#set Dir::State::* and Dir::Cache::* to the existing ones in /var/lib/apt, /var/lib/dpkg and /var/cache/apt
echo 'Dir::State "/var/lib/apt/";' >> "$TMP"/apt.conf
echo 'Dir::State::Lists "/var/lib/apt/lists/";' >> "$TMP"/apt.conf
echo 'Dir::State::status "/var/lib/dpkg/status";' >> "$TMP"/apt.conf
echo 'Dir::State::extended_states "/var/lib/apt/extended_states";' >> "$TMP"/apt.conf
echo 'Dir::Cache "/var/cache/apt/";' >> "$TMP"/apt.conf
echo 'Dir::Cache::Archives "/var/cache/apt/archives";' >> "$TMP"/apt.conf
echo 'Dir::Cache::srcpkgcache "/var/cache/apt/srcpkgcache.bin";' >> "$TMP"/apt.conf
echo 'Dir::Cache::pkgcache "/var/cache/apt/pkgcache.bin";' >> "$TMP"/apt.conf
APT_CONFIG="$TMP"/apt.conf apt-get -o Debug::NoLocking=true --trivial-only -V $UpgradeType 2>/dev/null >> "$TMP"/upgrades
#fix to display epochs
#for i in $(grep [[:space:]]'=>'[[:space:]] "$TMP"/upgrades | awk '{print $1}')
#do
# withoutEpoch="$(grep [[:space:]]$i[[:space:]] "$TMP"/upgrades | awk '{print $2}')"
# withEpoch="(""$(apt-cache policy $i | head -2 | tail -1 | awk '{print $NF}')"
# sed -i 's/'"$withoutEpoch"'/'"$withEpoch"'/' "$TMP"/upgrades
# withoutEpoch="$(grep [[:space:]]$i[[:space:]] "$TMP"/upgrades | awk '{print $4}')"
# withEpoch="$(apt-cache policy $i | head -3 | tail -1 | awk '{print $NF}')"")"
# sed -i 's/'"$withoutEpoch"'/'"$withEpoch"'/' "$TMP"/upgrades
#done
# ~~~ Localize 2a ~~~
# Format switch label. switch_to contains %s. eg "switch to %s" or "zu %s wechseln"
# Result output to switch_label could be eg "switch to 'apt-get upgrade'"
# or "zu 'apt-get dist-upgrade' wechseln'"
# Should be able to use statement like:
# printf -v switch_label "$switch_to" "$switch_type"
# But fails, so use sed instead.
# Format auto close message in same way.
switch_type="'""$OtherUpgradeType""'"
switch_label=$(echo "$switch_to" | sed 's/%s/'"$switch_type"'/')
auto_close_label=$(echo "$auto_close_window" | sed 's/%s/'"$UpgradeType"'/')
if [ "$UpgradeType" = "upgrade" ]
then
upgrade_label=$upgrade
switch_label=$switch_to_full_upgrade
auto_close_label=$auto_close_window_basic
window_title="$window_title_basic"
else
upgrade_label=$upgrade
switch_label=$switch_to_basic_upgrade
auto_close_label=$auto_close_window_full
window_title="$window_title_full"
fi
# IFS="x" read screenWidth screenHeight < <(xdpyinfo | grep dimensions | grep -o "[0-9x]*" | head -n 1)
read screenWidth screenHeight < <(xdotool getdisplaygeometry)
case "$(grep IconLook ~/.config/apt-notifierrc | cut -f2 -d=)" in
classic ) windowIcon=mnotify-some-classic
ButtonIcon=mnotify-some-classic
;;
pulse ) windowIcon=mnotify-some-pulse
ButtonIcon=mnotify-some-pulse
;;
wireframe|*) #if [[ $(find /usr/share/{icons,pixmaps} -name mx-updater.svg) ]]
# then
# if [[ $(xfconf-query -lvc xsettings | grep IconThemeName | grep .*Papirus.* -i) ]]
# then
# windowIcon=mx-updater
# else
# windowIcon=mnotify-some-wireframe
# fi
# ButtonIcon=/usr/share/icons/mnotify-some-wireframe.png
# else
# windowIcon=/usr/share/icons/mnotify-some-wireframe.png
# ButtonIcon=/usr/share/icons/mnotify-some-wireframe.png
#fi
#windowIcon=/usr/share/icons/mnotify-some-wireframe.png
windowIcon=mx-updater
ButtonIcon=/usr/share/icons/mnotify-some-wireframe.png
;;
esac
windowIcon=mx-updater
yad \\
--window-icon="$windowIcon" \\
--width=$(($screenWidth*2/3)) \\
--height=$(($screenHeight*2/3)) \\
--center \\
--title "$(echo "$window_title"|sed 's/MX /'$(grep -o MX.*[1-9][0-9] /etc/issue|cut -c1-2)" "'/')" \\
--form \\
--field=:TXT "$(sed 's/^/ /' $TMP/upgrades)" \\
--field="$use_apt_get_dash_dash_yes":CHK $UpgradeAssumeYes \\
--field="$auto_close_label":CHK $UpgradeAutoClose \\
--button "$reload"!reload!"$reload_tooltip":8 \\
--button ''"$upgrade_label"!"$ButtonIcon"!"$upgrade_tooltip":0 \\
--button gtk-cancel:2 \\
--buttons-layout=spread \\
2>/dev/null \\
> "$TMP"/results
echo $?>>"$TMP"/results
# if the View and Upgrade yad window was closed by one of it's 4 buttons,
# then update the UpgradeAssumeYes & UpgradeAutoClose flags in the
# ~/.config/apt-notifierrc file to match the checkboxes
if [ $(tail -n 1 "$TMP"/results) -eq 0 ]||\\
[ $(tail -n 1 "$TMP"/results) -eq 2 ]||\\
[ $(tail -n 1 "$TMP"/results) -eq 4 ]||\\
[ $(tail -n 1 "$TMP"/results) -eq 8 ];
then
if [ "$(head -n 1 "$TMP"/results | rev | awk -F \| '{ print $3}' | rev)" = "TRUE" ];
then
grep UpgradeAssumeYes=true ~/.config/apt-notifierrc > /dev/null || sed -i 's/UpgradeAssumeYes=false/UpgradeAssumeYes=true/' ~/.config/apt-notifierrc
else
grep UpgradeAssumeYes=false ~/.config/apt-notifierrc > /dev/null || sed -i 's/UpgradeAssumeYes=true/UpgradeAssumeYes=false/' ~/.config/apt-notifierrc
fi
if [ "$(head -n 1 "$TMP"/results | rev | awk -F \| '{ print $2}' | rev)" = "TRUE" ];
then
grep UpgradeAutoClose=true ~/.config/apt-notifierrc > /dev/null || sed -i 's/UpgradeAutoClose=false/UpgradeAutoClose=true/' ~/.config/apt-notifierrc
else
grep UpgradeAutoClose=false ~/.config/apt-notifierrc > /dev/null || sed -i 's/UpgradeAutoClose=true/UpgradeAutoClose=false/' ~/.config/apt-notifierrc
fi
else
:
fi
# refresh UpgradeAssumeYes & UpgradeAutoClose
UpgradeAssumeYes=$(grep ^UpgradeAssumeYes ~/.config/apt-notifierrc | cut -f2 -d=)
UpgradeAutoClose=$(grep ^UpgradeAutoClose ~/.config/apt-notifierrc | cut -f2 -d=)
#create first part of upgradeScript
cat << 'EOF' > "$TMP"/upgradeScript
#!/bin/bash
if [ ! -e /etc/apt/sources.list ]; then echo -e "#This source is empty by default in MX Linux and is only included to avoid\\n#error messages from some package install scripts that expect to find it.\\n#Sources are under /etc/apt/sources.list.d" > /etc/apt/sources.list; fi
EOF
if [ $(tail -n 1 "$TMP"/results) -eq 8 ];
then
# build a upgrade script to do a apt-get update
echo "echo 'apt-get update'">> "$TMP"/upgradeScript
echo "apt-get update">> "$TMP"/upgradeScript
else
# build a upgrade script to do the apt-get upgrade (basic upgrade) or dist-upgrade (full upgrade)
echo "echo ''"$UpgradeTypeUserFriendlyName>> "$TMP"/upgradeScript
echo 'find /etc/apt/preferences.d | grep -E synaptic-[0-9a-zA-Z]{6}-pins | xargs rm -f'>> "$TMP"/upgradeScript
echo 'if [ -f /var/lib/synaptic/preferences -a -s /var/lib/synaptic/preferences ]'>> "$TMP"/upgradeScript
echo ' then '>> "$TMP"/upgradeScript
echo ' SynapticPins=$(mktemp /etc/apt/preferences.d/synaptic-XXXXXX-pins)'>> "$TMP"/upgradeScript
echo ' ln -sf /var/lib/synaptic/preferences "$SynapticPins" 2>/dev/null'>> "$TMP"/upgradeScript
echo 'fi'>> "$TMP"/upgradeScript
echo 'file "$SynapticPins" | cut -f2- -d" " | grep -e"broken symbolic link" -e"empty" > /dev/null '>> "$TMP"/upgradeScript
echo 'if [ $? -eq 0 ]; then find /etc/apt/preferences.d | grep -E synaptic-[0-9a-zA-Z]{6}-pins | xargs rm -f; fi'>> "$TMP"/upgradeScript
if [ "$UpgradeAssumeYes" = "true" ];
then
echo "apt-get --assume-yes -V "$UpgradeType>> "$TMP"/upgradeScript
else
echo "apt-get -V "$UpgradeType>> "$TMP"/upgradeScript
fi
grep ^CheckForAutoRemovables=true ~/.config/apt-notifierrc > /dev/null
if [ $? -eq 0 ]
then
echo "echo">> "$TMP"/upgradeScript
echo 'apt-get autoremove -s | grep ^Remv > /dev/null'>> "$TMP"/upgradeScript
echo 'if [ $? -eq 0 ]; '>> "$TMP"/upgradeScript
echo ' then'>> "$TMP"/upgradeScript
echo 'echo "'"$autoremovable_packages_msg1"'"'>> "$TMP"/upgradeScript
echo 'echo "'"$autoremovable_packages_msg2"'"'>> "$TMP"/upgradeScript
echo 'apt-get autoremove -qV'>> "$TMP"/upgradeScript
echo ' else'>> "$TMP"/upgradeScript
echo ' :'>> "$TMP"/upgradeScript
echo 'fi'>> "$TMP"/upgradeScript
else
:
fi
echo "echo">> "$TMP"/upgradeScript
echo 'find /etc/apt/preferences.d | grep -E synaptic-[0-9a-zA-Z]{6}-pins | xargs rm -f'>> "$TMP"/upgradeScript
# ~~~ Localize 2b ~~~
#donetype="$UpgradeType"
#donetext=$(echo "$done1" | sed 's/%s/'"$donetype"'/')
if [ "$UpgradeType" = "upgrade" ]
then
donetext="$done1basic"
else
donetext="$done1full"
fi
echo 'echo "'"$donetext"'"'>> "$TMP"/upgradeScript
echo "echo">> "$TMP"/upgradeScript
if [ "$UpgradeAutoClose" = "true" ];
then
echo "sleep 1">> "$TMP"/upgradeScript
echo "exit 0">> "$TMP"/upgradeScript
else
echo "echo -n $done2' '">> "$TMP"/upgradeScript
echo "read -sn 1 -p $done3 -t 999999999">> "$TMP"/upgradeScript
echo "echo">> "$TMP"/upgradeScript
echo "exit 0">> "$TMP"/upgradeScript
fi
fi
DoUpgrade $(tail -n 1 "$TMP"/results)
rm -rf "$TMP"
done
#sleep 2
PID=`pidof apt-get | cut -f 1 -d " "`
if [ $PID ]; then
while (ps -p $PID > /dev/null); do
sleep 2
done
fi
'''
script_file = tempfile.NamedTemporaryFile('wt')
script_file.write(script)
script_file.flush()
cmd = "pgrep -x plasmashell >/dev/null && exit 1 || exit 0"
run = subprocess.run(cmd, shell=True)
running_in_plasma = run.returncode
if running_in_plasma:
systray_icon_hide()
#run = subprocess.Popen(["bash -c 'S=%s; N=$S.$RANDOM$RANDOM$RANDOM; cp $S $N; bash $N; rm $N; ionice -c3 nice -n19 python -m pdb /usr/bin/apt-notifier.py < <(sleep .250; echo c)& disown -h;'" % script_file.name],shell=True)