summaryrefslogtreecommitdiffstats
path: root/shared/prestoTransaction.py
blob: 8a46211ea2a613caf44e77326c7e219a9ec31e3c (plain)
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
# author: Jonathan Dieter <jdieter@gmail.com>
# 
# This program 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.
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2005 Duke University

import os
import deltarpm

def find_available_drpms(conduit, newpkg):
    """Find any applicable drpms for newpkg
       newpkg is a TransactionMember"""
    
    rpmdb = conduit.getRpmDB()
    
    # Set p_repo to be packages delta repository or set to False if
    # there is no delta repository
    try:
        p_repo = newpkg.po.repo.p_repo
        drpm_enabled = p_repo.enabled
    except:
        drpm_enabled = False
        
    chosen_drpm = None
    
    # First part of key when matching drpms
    key1 = "%s*%s*%i*%s*%s" % (newpkg.name, newpkg.arch, int(newpkg.epoch), newpkg.version, newpkg.release)
    
    # Find any installed packages that match the ones we want to download
    installed = rpmdb.searchNevra(newpkg.name, None, None, None, newpkg.arch)

    if installed == []:
        is_installed = False
    else:
        is_installed = True
        
    if os.path.exists(newpkg.po.localpath):
        is_local = True
    else:
        is_local = False
    
    if is_installed and drpm_enabled and not is_local:
        for oldpkg in installed:
            # Generate second part of key for matching drpms, then full key
            key2 = "%s*%s*%i*%s*%s" % (oldpkg.name, oldpkg.arch, int(oldpkg.epoch), oldpkg.version, oldpkg.release)
            key = "%s!!%s" % (key1, key2)
            
            # Check whether we have a matching drpm
            if p_repo.deltalist.has_key(key):
                # Check whether or not we already have a matching drpm, then choose smallest of the two if we do
                if chosen_drpm == None or p_repo.deltalist[key]['size'] < chosen_drpm['size']:
                
                    # Get sequence code for drpm
                    sequence = p_repo.deltalist[key]['sequence']
                    if int(oldpkg.epoch) == 0:
                        seq = "%s-%s-%s-%s" % (oldpkg.name, oldpkg.version, oldpkg.release, sequence)
                    else:
                        seq = "%s-%i:%s-%s-%s" % (oldpkg.name, int(oldpkg.epoch), oldpkg.version, oldpkg.release, sequence)
                    drpm = deltarpm.DeltaRpmWrapper(conduit)
                    
                    # Attempt to apply sequence code for drpm. If this fails, drpm will not apply cleanly, so
                    # don't even try to download it.
                    try:
                        drpm.verifySequence(seq)
                        chosen_drpm = p_repo.deltalist[key]
                        chosen_drpm['baseurl'] = p_repo.baseurl[0]
                    except:
                        conduit.info(5, "Verification of %s failed" % seq)
        
    return (chosen_drpm, installed, is_local)