~mvo/unattended-upgrades/mvo

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
#!/usr/bin/python
# Copyright (c) 2009 Canonical Ltd
#
# AUTHOR:
# Michael Vogt <mvo@ubuntu.com>
#
# This file is part of unattended-upgrades
#
# unattended-upgrades is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# unattended-upgrades 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with unattended-upgrades; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import apt_inst
import apt_pkg

import sys
import os
import string
import datetime
import logging

from StringIO import StringIO
from optparse import OptionParser
from subprocess import Popen, call, PIPE

import apt

import gettext
from gettext import gettext as _

from UnattendedUpgrades.UnattendedUpgrades import UnattendedUpgrades
from UnattendedUpgrades.GtkUI import GtkUI


if __name__ == "__main__":
    localesApp="unattended-upgrades"
    localesDir="/usr/share/locale"
    gettext.bindtextdomain(localesApp, localesDir)
    gettext.textdomain(localesApp)

    # init the options
    parser = OptionParser()
    parser.add_option("", "--debug",
                      action="store_true", default=False)
    parser.add_option("", "--ignore-origins",
                      action="store_true", default=False)
    parser.add_option("", "--fullscreen",
                      action="store_true", default=False)
    parser.add_option("", "--dry-run",
                      action="store_true", default=False,
                      help=_("Simulation, download but do not install"))
    (options, args) = parser.parse_args()

    # check if we run as root and restart if we don't run as root
    # and if there are upgradable packages
    if os.getuid() != 0:
        cache = apt.Cache()
        for pkg in cache:
            if pkg.isUpgradable:
                logging.info("Switching to install mode")
                call(["gksu", "--"]+sys.argv)
                sys.exit(0)
        sys.exit(1)

    # get UI
    ui = GtkUI()
    ui.show()

    if options.debug:
        logging.basicConfig(level=logging.DEBUG)
    
    if options.fullscreen:
        ui.window_main.fullscreen()

    # check if we are in dry-run mode
    if options.dry_run:
        ui.info("Option --dry-run given, *not* performing real actions")
        apt_pkg.Config.Set("Debug::pkgDPkgPM","1")

    # get the controller object
    uu = UnattendedUpgrades(ui)
    # all origins are allowed for updates on shutdown
    if options.ignore_origins:
        uu.is_allowed_origin = lambda p: True

    # update first
    try:
        # pulse intervall in milliseconds
        uu.update(pulseInterval=5000)
    except SystemError, e:
        # only exit on error, otherwise ignore
        if ui.cancel == True:
            sys.exit(1)
        # log the problem
        logging.warn("Error during update: '%s'" % e)

    # now run the full thing
    if not uu.run():
        sys.exit(1)