~ubuntu-branches/ubuntu/precise/unattended-upgrades/precise

« back to all changes in this revision

Viewing changes to unattended-upgrade

  • Committer: Package Import Robot
  • Author(s): Michael Vogt
  • Date: 2011-11-22 15:27:56 UTC
  • Revision ID: package-import@ubuntu.com-20111122152756-z6qcusbefxl0qhew
Tags: 0.75
* add tests for compat mode and spaces in a origin
* escape "," in the Allowed-Origins compat mode (LP: #824856)
* merged lp:~mvo/unattended-upgrades/unshadow-versions, this will
  ensure that higher versions in a non-origin branch do not "shadow"
  the versions from a desired origin (LP: #891747)

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
# set from the sigint signal handler
61
61
SIGNAL_STOP_REQUEST=False
62
62
 
 
63
class UnattendedUpgradesCache(apt.Cache):
 
64
    
 
65
    def __init__(self, rootdir, allowed_origins):
 
66
        apt.Cache.__init__(self, rootdir=rootdir)
 
67
        self.allowed_origins = allowed_origins
 
68
        # ensure we update the candidate versions 
 
69
        self.adjust_candidate_versions()
 
70
    def clear(self):
 
71
        apt.Cache.clear(self)
 
72
        # ensure we update the candidate versions 
 
73
        self.adjust_candidate_versions()
 
74
    def adjust_candidate_versions(self):
 
75
        """ Adjust candidate versions to match highest allowed origin
 
76
        
 
77
            This adjusts the origin even if the candidate has a higher
 
78
            version
 
79
        """
 
80
        for pkg in self:
 
81
            # important! this avoids downgrades below
 
82
            if not pkg.is_upgradable:
 
83
                continue
 
84
            # check if we have a version in a allowed origin that is
 
85
            # not the candidate
 
86
            new_cand = None
 
87
            for ver in pkg.versions:
 
88
                if is_allowed_origin(ver, self.allowed_origins):
 
89
                    # leave as soon as we have the highest new candidate
 
90
                    new_cand = ver
 
91
                    break
 
92
            if new_cand and new_cand != pkg.candidate:
 
93
                logging.debug("adjusting candidate version: '%s'" % new_cand)
 
94
                pkg.candidate = new_cand
 
95
 
63
96
 
64
97
class LogInstallProgress(apt.progress.base.InstallProgress):
65
98
    """ Install progress that writes to self.LOG
131
164
            (distro_id, distro_codename) = s.split(':')
132
165
        else:
133
166
            (distro_id, distro_codename) = s.split()
 
167
        # escape "," (see LP: #824856) - i wonder if there is a simpler way?
 
168
        distro_id = re.sub(r'([^\\]),', r'\1\\,', distro_id)
 
169
        distro_codename = re.sub(r'([^\\]),', r'\1\\,', distro_codename)
134
170
        # convert to new format
135
171
        allowed_origins.append("o=%s,a=%s" % (substitute(distro_id), 
136
172
                                              substitute(distro_codename)))
241
277
        # apply changes
242
278
        logging.debug("applying set %s" % smallest_partition)
243
279
        rewind_cache(cache, [cache[name] for name in smallest_partition])
 
280
 
244
281
        try:
245
282
            iprogress = LogInstallProgress()
246
283
            with Unlocked():
261
298
            break
262
299
    return True
263
300
 
264
 
def is_allowed_origin(pkg, allowed_origins):
265
 
    if not pkg.candidate:
 
301
def is_allowed_origin(ver, allowed_origins):
 
302
    if not ver:
266
303
        return False
267
 
    for origin in pkg.candidate.origins:
 
304
    for origin in ver.origins:
268
305
        for allowed in allowed_origins:
269
306
            if match_whitelist_string(allowed, origin):
270
307
                return True
278
315
            logging.debug("pkg '%s' now marked delete" % pkg.name)
279
316
            return False
280
317
        if pkg.marked_install or pkg.marked_upgrade:
281
 
            if not is_allowed_origin(pkg, allowed_origins):
 
318
            if not is_allowed_origin(pkg.candidate, allowed_origins):
282
319
                logging.debug("pkg '%s' not in allowed origin" % pkg.name)
283
320
                return False
284
321
            for blacklist_regexp in blacklist:
613
650
        sys.exit(1)
614
651
 
615
652
    # get a cache
616
 
    cache = apt.Cache(rootdir=rootdir)
 
653
    cache = UnattendedUpgradesCache(rootdir=rootdir, 
 
654
                                    allowed_origins=allowed_origins)
617
655
    if cache._depcache.broken_count > 0:
618
656
        print _("Cache has broken packages, exiting")
619
657
        logging.error(_("Cache has broken packages, exiting"))
626
664
    pkgs_kept_back = []
627
665
    pkgs_auto_removable = set([pkg.name for pkg in cache 
628
666
                               if pkg.is_auto_removable])
 
667
 
 
668
    # now do the actual upgrade
629
669
    for pkg in cache:
630
670
        if options.debug and pkg.is_upgradable:
631
671
            logging.debug("Checking: %s (%s)" % (pkg.name, map(str, pkg.candidate.origins)))
632
672
        if (pkg.is_upgradable and 
633
 
            is_allowed_origin(pkg,allowed_origins)):
 
673
            is_allowed_origin(pkg.candidate, allowed_origins)):
634
674
            try:
635
675
                pkg.mark_upgrade()
636
676
                if check_changes_for_sanity(cache, allowed_origins,