~brian-murray/update-manager/no-zero-byte-files

« back to all changes in this revision

Viewing changes to UpdateManager/Core/MetaRelease.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2007-02-07 18:02:35 UTC
  • Revision ID: james.westby@ubuntu.com-20070207180235-i0mgzoi54v54ezq7
Tags: 0.56
* added --proposed switch to fetch a release-upgrader from a different
  location (similar to the -proposed pocket in the archive)
* split into update-manager and update-manager-core and support
  cli release upgrades with the later
* added fdsend module to support file descriptor passing over sockets
  this will allow a better seperation between frontend and backend
  in the future

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# MetaRelease.py 
 
2
#  
 
3
#  Copyright (c) 2004,2005 Canonical
 
4
#  
 
5
#  Author: Michael Vogt <michael.vogt@ubuntu.com>
 
6
 
7
#  This program is free software; you can redistribute it and/or 
 
8
#  modify it under the terms of the GNU General Public License as 
 
9
#  published by the Free Software Foundation; either version 2 of the
 
10
#  License, or (at your option) any later version.
 
11
 
12
#  This program is distributed in the hope that it will be useful,
 
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#  GNU General Public License for more details.
 
16
 
17
#  You should have received a copy of the GNU General Public License
 
18
#  along with this program; if not, write to the Free Software
 
19
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
20
#  USA
 
21
 
 
22
import thread
 
23
import urllib2
 
24
import os
 
25
import string
 
26
import apt_pkg
 
27
import time
 
28
import rfc822
 
29
from subprocess import Popen,PIPE
 
30
 
 
31
class Dist(object):
 
32
    def __init__(self, name, version, date, supported):
 
33
        self.name = name
 
34
        self.version = version
 
35
        self.date = date
 
36
        self.supported = supported
 
37
        self.releaseNotesURI = None
 
38
        self.upgradeTool = None
 
39
        self.upgradeToolSig = None
 
40
 
 
41
class MetaReleaseCore(object):
 
42
 
 
43
    # some constants
 
44
    METARELEASE_URI = "http://changelogs.ubuntu.com/meta-release"
 
45
    METARELEASE_URI_UNSTABLE = "http://changelogs.ubuntu.com/meta-release-development"
 
46
    METARELEASE_URI_PROPOSED = "http://changelogs.ubuntu.com/meta-release-proposed"
 
47
    METARELEASE_FILE = "/var/lib/update-manager/meta-release"
 
48
 
 
49
    def __init__(self, useDevelopmentRelease=False, useProposed=False):
 
50
        # check what uri to use
 
51
        if useDevelopmentRelease:
 
52
            self.METARELEASE_URI = self.METARELEASE_URI_UNSTABLE
 
53
        elif useProposed:
 
54
            self.METARELEASE_URI = self.METARELEASE_URI_PROPOSED
 
55
        # check if we can access the METARELEASE_FILE
 
56
        if not os.access(self.METARELEASE_FILE, os.F_OK|os.W_OK|os.R_OK):
 
57
            path = os.path.expanduser("~/.update-manager/")
 
58
            if not os.path.exists(path):
 
59
                os.mkdir(path)
 
60
            self.METARELEASE_FILE = os.path.join(path,"meta-release")
 
61
        self.metarelease_information = None
 
62
        self.downloading = True
 
63
        # information about the available dists
 
64
        self.new_dist = None
 
65
        self.no_longer_supported = None
 
66
        # we start the download thread here and we have a timeout
 
67
        t=thread.start_new_thread(self.download, ())
 
68
        #t=thread.start_new_thread(self.check, ())
 
69
 
 
70
    def dist_no_longer_supported(self, dist):
 
71
        """ virtual function that is called when the distro is no longer
 
72
            supported
 
73
        """
 
74
        self.no_longer_supported = dist
 
75
    def new_dist_available(self, dist):
 
76
        """ virtual function that is called when a new distro release
 
77
            is available
 
78
        """
 
79
        self.new_dist = dist
 
80
 
 
81
    def get_dist(self):
 
82
        " return the codename of the current runing distro "
 
83
        p = Popen(["lsb_release","-c","-s"],stdout=PIPE)
 
84
        res = p.wait()
 
85
        if res != 0:
 
86
            sys.stderr.write("lsb_release returned exitcode: %i\n" % res)
 
87
        dist = string.strip(p.stdout.readline())
 
88
        return dist
 
89
    
 
90
    def parse(self):
 
91
        #print "parse"
 
92
        current_dist_name = self.get_dist()
 
93
        current_dist = None
 
94
        dists = []
 
95
 
 
96
        # parse the metarelease_information file
 
97
        index_tag = apt_pkg.ParseTagFile(self.metarelease_information)
 
98
        step_result = index_tag.Step()
 
99
        while step_result:
 
100
            if index_tag.Section.has_key("Dist"):
 
101
                name = index_tag.Section["Dist"]
 
102
                #print name
 
103
                rawdate = index_tag.Section["Date"]
 
104
                date = time.mktime(rfc822.parsedate(rawdate))
 
105
                supported = bool(index_tag.Section["Supported"])
 
106
                version = index_tag.Section["Version"]
 
107
                # add the information to a new date object
 
108
                dist = Dist(name, version, date,supported)
 
109
                if index_tag.Section.has_key("ReleaseNotes"):
 
110
                    dist.releaseNotesURI = index_tag.Section["ReleaseNotes"]
 
111
                if index_tag.Section.has_key("UpgradeTool"):
 
112
                    dist.upgradeTool =  index_tag.Section["UpgradeTool"]
 
113
                if index_tag.Section.has_key("UpgradeToolSignature"):
 
114
                    dist.upgradeToolSig =  index_tag.Section["UpgradeToolSignature"]
 
115
                dists.append(dist)
 
116
                if name == current_dist_name:
 
117
                    current_dist = dist 
 
118
            step_result = index_tag.Step()
 
119
 
 
120
        # first check if the current runing distro is in the meta-release
 
121
        # information. if not, we assume that we run on something not
 
122
        # supported and silently return
 
123
        if current_dist == None:
 
124
            print "current dist not found in meta-release file"
 
125
            return False
 
126
 
 
127
        # then see what we can upgrade to (only upgrade to supported dists)
 
128
        upgradable_to = ""
 
129
        for dist in dists:
 
130
            if dist.date > current_dist.date and dist.supported == True: 
 
131
                upgradable_to = dist
 
132
                #print "new dist: %s" % upgradable_to
 
133
                break
 
134
 
 
135
        # only warn if unsupported and a new dist is available (because 
 
136
        # the development version is also unsupported)
 
137
        if upgradable_to != "" and not current_dist.supported:
 
138
            self.dist_no_longer_supported(upgradabl_to)
 
139
        elif upgradable_to != "":
 
140
            self.new_dist_available(upgradable_to)
 
141
 
 
142
        # parsing done and sucessfully
 
143
        return True
 
144
 
 
145
    # the network thread that tries to fetch the meta-index file
 
146
    # can't touch the gui, runs as a thread
 
147
    def download(self):
 
148
        #print "download"
 
149
        lastmodified = 0
 
150
        req = urllib2.Request(self.METARELEASE_URI)
 
151
        if os.access(self.METARELEASE_FILE, os.W_OK):
 
152
            lastmodified = os.stat(self.METARELEASE_FILE).st_mtime
 
153
        if lastmodified > 0:
 
154
            req.add_header("If-Modified-Since", lastmodified)
 
155
        try:
 
156
            uri=urllib2.urlopen(req)
 
157
            if (os.path.exists(self.METARELEASE_FILE) and
 
158
                not os.access(self.METARELEASE_FILE,os.W_OK)):
 
159
                os.unlink(self.METARELEASE_FILE)
 
160
            f=open(self.METARELEASE_FILE,"w+")
 
161
            for line in uri.readlines():
 
162
                f.write(line)
 
163
            f.flush()
 
164
            f.seek(0,0)
 
165
            self.metarelease_information=f
 
166
            uri.close()
 
167
        except urllib2.URLError:
 
168
            if os.path.exists(self.METARELEASE_FILE):
 
169
                f=open(self.METARELEASE_FILE,"r")
 
170
        # now check the information we have
 
171
        self.downloading = False
 
172
        if self.metarelease_information != None:
 
173
            self.parse()