~ubuntu-core-dev/update-notifier/ubuntu

858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
1
#!/usr/bin/python3
635.1.1 by Michael Vogt
add skeleton helper
2
3
import argparse
4
import logging
635.1.5 by Michael Vogt
fixup add_cdrom support
5
import os
635.1.2 by Michael Vogt
call backend-helper instead of synaptic
6
import subprocess
635.1.1 by Michael Vogt
add skeleton helper
7
import sys
8
635.1.3 by Michael Vogt
implement the helper stuff
9
HAVE_APTDAEMON = False
10
try:
11
    import aptdaemon.gtk3widgets
888 by Brian Murray
releasing package update-notifier version 3.173
12
    HAVE_APTDAEMON = True
635.1.3 by Michael Vogt
implement the helper stuff
13
except ImportError:
14
    pass
635.1.2 by Michael Vogt
call backend-helper instead of synaptic
15
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
16
635.1.3 by Michael Vogt
implement the helper stuff
17
# show updates
635.1.1 by Michael Vogt
add skeleton helper
18
def show_updates():
635.1.3 by Michael Vogt
implement the helper stuff
19
    """ show updates using update-manager """
707.1.1 by Michael Terry
add --no-update to calls to update-manager to skip new default update-on-launch behavior
20
    cmd = ["update-manager", "--no-update"]
635.1.3 by Michael Vogt
implement the helper stuff
21
    res = subprocess.call(cmd)
22
    return (res == 0)
23
24
25
# install all updates
26
def _install_all_updates_aptdaemon():
27
    from gi.repository import Gtk
28
    from aptdaemon import client, enums
29
    from aptdaemon.gtk3widgets import AptProgressDialog
30
    client = client.AptClient()
31
    trans = client.upgrade_system(safe_mode=True)
32
    dia = AptProgressDialog(trans)
33
    dia.connect("finished", Gtk.main_quit)
34
    dia.run()
35
    Gtk.main()
36
    return trans.exit == enums.EXIT_SUCCESS
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
37
888 by Brian Murray
releasing package update-notifier version 3.173
38
767.1.1 by Michael Terry
Use synaptic-pkexec instead of gksu in backend_helper.py
39
def _install_all_updates_synaptic():
888 by Brian Murray
releasing package update-notifier version 3.173
40
    cmd = ["/usr/bin/synaptic-pkexec",
41
           "--dist-upgrade-mode",
42
           "--non-interactive",
43
           "--hide-main-window",
44
           "-o", "Synaptic::AskRelated=true",
45
           ]
635.1.2 by Michael Vogt
call backend-helper instead of synaptic
46
    return subprocess.call(cmd)
635.1.3 by Michael Vogt
implement the helper stuff
47
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
48
635.1.3 by Michael Vogt
implement the helper stuff
49
def install_all_updates():
50
    """ install all updates either with synaptic or aptdaemon """
51
    if HAVE_APTDAEMON:
52
        return _install_all_updates_aptdaemon()
53
    else:
767.1.1 by Michael Terry
Use synaptic-pkexec instead of gksu in backend_helper.py
54
        return _install_all_updates_synaptic()
635.1.3 by Michael Vogt
implement the helper stuff
55
56
57
# check updates
58
def _check_updates_aptdaemon():
59
    from gi.repository import Gtk
60
    from aptdaemon import client, enums
61
    from aptdaemon.gtk3widgets import AptProgressDialog
62
    client = client.AptClient()
63
    trans = client.update_cache()
64
    dia = AptProgressDialog(trans)
65
    dia.connect("finished", Gtk.main_quit)
66
    dia.run()
67
    Gtk.main()
68
    return trans.exit == enums.EXIT_SUCCESS
69
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
70
635.1.3 by Michael Vogt
implement the helper stuff
71
def _check_updates_gtk():
767.1.1 by Michael Terry
Use synaptic-pkexec instead of gksu in backend_helper.py
72
    cmd = ["/usr/bin/synaptic-pkexec",
73
           "--update-at-startup",
74
           "--non-interactive",
75
           "--hide-main-window",
76
           ]
635.1.2 by Michael Vogt
call backend-helper instead of synaptic
77
    subprocess.call(cmd)
635.1.3 by Michael Vogt
implement the helper stuff
78
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
79
635.1.3 by Michael Vogt
implement the helper stuff
80
def check_updates():
81
    """ check for updates either with aptdaemon or synaptic """
82
    if HAVE_APTDAEMON:
83
        return _check_updates_aptdaemon()
84
    else:
85
        return _check_updates_gtk()
86
87
88
# start packagemanager
635.1.1 by Michael Vogt
add skeleton helper
89
def start_packagemanager():
767.1.1 by Michael Terry
Use synaptic-pkexec instead of gksu in backend_helper.py
90
    if os.path.exists("/usr/bin/synaptic-pkexec"):
91
        cmd = ["/usr/bin/synaptic-pkexec"]
635.1.3 by Michael Vogt
implement the helper stuff
92
        return subprocess.call(cmd)
93
    elif os.path.exists("/usr/bin/software-center"):
94
        return subprocess.call(["/usr/bin/software-center"])
95
    else:
96
        logging.error("neither synaptic nor software-center installed")
97
98
99
# add cdrom
100
def _add_cdrom_sp(mount_path):
101
    from gi.repository import Gtk
888 by Brian Murray
releasing package update-notifier version 3.173
102
    import dbus
729.1.1 by Michael Terry
Tell python-dbus to use glib's mainloop so it doesn't throw an exception
103
    import dbus.mainloop.glib
104
    bus = dbus.SystemBus(mainloop=dbus.mainloop.glib.DBusGMainLoop())
635.1.3 by Michael Vogt
implement the helper stuff
105
    proxy = bus.get_object("com.ubuntu.SoftwareProperties", "/")
106
    backend = dbus.Interface(proxy, "com.ubuntu.SoftwareProperties")
107
    backend.AddCdromSource()
108
    backend.connect_to_signal(
109
        "SourcesListModified", Gtk.main_quit)
110
    backend.connect_to_signal(
111
        "CdromScanFailed", Gtk.main_quit)
112
    Gtk.main()
635.1.5 by Michael Vogt
fixup add_cdrom support
113
    if os.path.exists("/usr/bin/software-center"):
114
        subprocess.call(["/usr/bin/software-center"])
635.1.3 by Michael Vogt
implement the helper stuff
115
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
116
635.1.3 by Michael Vogt
implement the helper stuff
117
def _add_cdrom_synaptic(mount_path):
781 by Brian Murray
remove final reference to gksu in data/backend_helper.py
118
    cmd = ["/usr/bin/synaptic-pkexec", "--add-cdrom", mount_path]
635.1.2 by Michael Vogt
call backend-helper instead of synaptic
119
    return subprocess.call(cmd)
635.1.3 by Michael Vogt
implement the helper stuff
120
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
121
635.1.1 by Michael Vogt
add skeleton helper
122
def add_cdrom(mount_path):
767.1.1 by Michael Terry
Use synaptic-pkexec instead of gksu in backend_helper.py
123
    if os.path.exists("/usr/bin/synaptic-pkexec"):
635.1.5 by Michael Vogt
fixup add_cdrom support
124
        _add_cdrom_synaptic(mount_path)
125
    else:
635.1.3 by Michael Vogt
implement the helper stuff
126
        _add_cdrom_sp(mount_path)
127
635.1.1 by Michael Vogt
add skeleton helper
128
129
if __name__ == "__main__":
130
131
    parser = argparse.ArgumentParser(
132
        description='backend helper for update-notifier')
133
    parser.add_argument(
134
        '--debug', default=False, action="store_true",
135
        help='extra debug output')
136
    subparser = parser.add_subparsers(title="Commands")
137
    # show_update  - update-manager
138
    command = subparser.add_parser("show_updates")
139
    command.set_defaults(command="show_updates")
140
    # install_all - synaptic/aptdaemon install noninteractivly
141
    command = subparser.add_parser("install_all_updates")
142
    command.set_defaults(command="install_all_updates")
143
    # check_updates - synaptic --reload/aptdaemon reload
144
    command = subparser.add_parser("check_updates")
145
    command.set_defaults(command="check_updates")
146
    # start_pkgmanager
147
    command = subparser.add_parser("start_packagemanager")
148
    command.set_defaults(command="start_packagemanager")
149
    # add_cdrom
150
    command = subparser.add_parser("add_cdrom")
151
    command.add_argument("mount_path")
152
    command.set_defaults(command="add_cdrom")
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
153
635.1.1 by Michael Vogt
add skeleton helper
154
    args = parser.parse_args()
155
    if args.debug:
156
        logging.basicConfig(level=logging.DEBUG)
157
    else:
158
        logging.basicConfig(level=logging.INFO)
159
160
    func_name = args.command
161
    f_kwargs = {}
162
    f = globals()[func_name]
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
163
    if args.command == "add_cdrom":
635.1.1 by Michael Vogt
add skeleton helper
164
        f_kwargs["mount_path"] = args.mount_path
165
    res = f(**f_kwargs)
166
635.1.3 by Michael Vogt
implement the helper stuff
167
    if not res:
168
        sys.exit(1)