backend_helper.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/python3
  2. import argparse
  3. import logging
  4. import os
  5. import subprocess
  6. import sys
  7. HAVE_APTDAEMON = False
  8. try:
  9. import aptdaemon.gtk3widgets
  10. HAVE_APTDAEMON = True
  11. except ImportError:
  12. pass
  13. # show updates
  14. def show_updates():
  15. """ show updates using update-manager """
  16. cmd = ["update-manager", "--no-update"]
  17. res = subprocess.call(cmd)
  18. return (res == 0)
  19. # install all updates
  20. def _install_all_updates_aptdaemon():
  21. from gi.repository import Gtk
  22. from aptdaemon import client, enums
  23. from aptdaemon.gtk3widgets import AptProgressDialog
  24. client = client.AptClient()
  25. trans = client.upgrade_system(safe_mode=True)
  26. dia = AptProgressDialog(trans)
  27. dia.connect("finished", Gtk.main_quit)
  28. dia.run()
  29. Gtk.main()
  30. return trans.exit == enums.EXIT_SUCCESS
  31. def _install_all_updates_synaptic():
  32. cmd = ["/usr/bin/synaptic-pkexec",
  33. "--dist-upgrade-mode",
  34. "--non-interactive",
  35. "--hide-main-window",
  36. "-o", "Synaptic::AskRelated=true",
  37. ]
  38. return subprocess.call(cmd)
  39. def install_all_updates():
  40. """ install all updates either with synaptic or aptdaemon """
  41. if HAVE_APTDAEMON:
  42. return _install_all_updates_aptdaemon()
  43. else:
  44. return _install_all_updates_synaptic()
  45. # check updates
  46. def _check_updates_aptdaemon():
  47. from gi.repository import Gtk
  48. from aptdaemon import client, enums
  49. from aptdaemon.gtk3widgets import AptProgressDialog
  50. client = client.AptClient()
  51. trans = client.update_cache()
  52. dia = AptProgressDialog(trans)
  53. dia.connect("finished", Gtk.main_quit)
  54. dia.run()
  55. Gtk.main()
  56. return trans.exit == enums.EXIT_SUCCESS
  57. def _check_updates_gtk():
  58. cmd = ["/usr/bin/synaptic-pkexec",
  59. "--update-at-startup",
  60. "--non-interactive",
  61. "--hide-main-window",
  62. ]
  63. subprocess.call(cmd)
  64. def check_updates():
  65. """ check for updates either with aptdaemon or synaptic """
  66. if HAVE_APTDAEMON:
  67. return _check_updates_aptdaemon()
  68. else:
  69. return _check_updates_gtk()
  70. # start packagemanager
  71. def start_packagemanager():
  72. if os.path.exists("/usr/bin/synaptic-pkexec"):
  73. cmd = ["/usr/bin/synaptic-pkexec"]
  74. return subprocess.call(cmd)
  75. elif os.path.exists("/usr/bin/software-center"):
  76. return subprocess.call(["/usr/bin/software-center"])
  77. else:
  78. logging.error("neither synaptic nor software-center installed")
  79. # add cdrom
  80. def _add_cdrom_sp(mount_path):
  81. from gi.repository import Gtk
  82. import dbus
  83. import dbus.mainloop.glib
  84. bus = dbus.SystemBus(mainloop=dbus.mainloop.glib.DBusGMainLoop())
  85. proxy = bus.get_object("com.ubuntu.SoftwareProperties", "/")
  86. backend = dbus.Interface(proxy, "com.ubuntu.SoftwareProperties")
  87. backend.AddCdromSource()
  88. backend.connect_to_signal(
  89. "SourcesListModified", Gtk.main_quit)
  90. backend.connect_to_signal(
  91. "CdromScanFailed", Gtk.main_quit)
  92. Gtk.main()
  93. if os.path.exists("/usr/bin/software-center"):
  94. subprocess.call(["/usr/bin/software-center"])
  95. def _add_cdrom_synaptic(mount_path):
  96. cmd = ["/usr/bin/synaptic-pkexec", "--add-cdrom", mount_path]
  97. return subprocess.call(cmd)
  98. def add_cdrom(mount_path):
  99. if os.path.exists("/usr/bin/synaptic-pkexec"):
  100. _add_cdrom_synaptic(mount_path)
  101. else:
  102. _add_cdrom_sp(mount_path)
  103. if __name__ == "__main__":
  104. parser = argparse.ArgumentParser(
  105. description='backend helper for update-notifier')
  106. parser.add_argument(
  107. '--debug', default=False, action="store_true",
  108. help='extra debug output')
  109. subparser = parser.add_subparsers(title="Commands")
  110. # show_update - update-manager
  111. command = subparser.add_parser("show_updates")
  112. command.set_defaults(command="show_updates")
  113. # install_all - synaptic/aptdaemon install noninteractivly
  114. command = subparser.add_parser("install_all_updates")
  115. command.set_defaults(command="install_all_updates")
  116. # check_updates - synaptic --reload/aptdaemon reload
  117. command = subparser.add_parser("check_updates")
  118. command.set_defaults(command="check_updates")
  119. # start_pkgmanager
  120. command = subparser.add_parser("start_packagemanager")
  121. command.set_defaults(command="start_packagemanager")
  122. # add_cdrom
  123. command = subparser.add_parser("add_cdrom")
  124. command.add_argument("mount_path")
  125. command.set_defaults(command="add_cdrom")
  126. args = parser.parse_args()
  127. if args.debug:
  128. logging.basicConfig(level=logging.DEBUG)
  129. else:
  130. logging.basicConfig(level=logging.INFO)
  131. func_name = args.command
  132. f_kwargs = {}
  133. f = globals()[func_name]
  134. if args.command == "add_cdrom":
  135. f_kwargs["mount_path"] = args.mount_path
  136. res = f(**f_kwargs)
  137. if not res:
  138. sys.exit(1)