~ubuntu-core-dev/ubuntu/xenial/ubuntu-release-upgrader/xenial

« back to all changes in this revision

Viewing changes to DistUpgrade/DistUpgradeAptCdrom.py

  • Committer: Dimitri John Ledkov
  • Date: 2015-01-20 22:30:06 UTC
  • mto: This revision was merged to the branch mainline in revision 2859.
  • Revision ID: dimitri.j.ledkov@intel.com-20150120223006-ba30fbqtp4x2n0og
Fix resource warnings & logging.warn deprecation warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
        diskname = self._readDiskName()
64
64
        pentry = self._generateSourcesListLine(diskname, self.packages)
65
65
        sourceslist = apt_pkg.config.find_file("Dir::Etc::sourcelist")
66
 
        content = open(sourceslist).read()
 
66
        with open(sourceslist) as f:
 
67
            content = f.read()
67
68
        content = content.replace(pentry, "# %s" % pentry)
68
 
        open(sourceslist, "w").write(content)
 
69
        with open(sourceslist, "w") as f:
 
70
            f.write(content)
69
71
 
70
72
    def _scanCD(self):
71
73
        """ 
106
108
        cdrom = apt_pkg.Cdrom()
107
109
        id = cdrom.ident(apt.progress.base.CdromProgress())
108
110
        label = self._readDiskName()
109
 
        out = open(dbfile, "a")
110
 
        out.write('CD::%s "%s";\n' % (id, label))
111
 
        out.write('CD::%s::Label "%s";\n' % (id, label))
 
111
        with open(dbfile, "a") as out:
 
112
            out.write('CD::%s "%s";\n' % (id, label))
 
113
            out.write('CD::%s::Label "%s";\n' % (id, label))
112
114
 
113
115
    def _dropArch(self, packages):
114
116
        """ drop architectures that are not ours """
127
129
        diskname = self.cdrompath
128
130
        info = os.path.join(self.cdrompath, ".disk", "info")
129
131
        if os.path.exists(info):
130
 
            diskname = open(info).read()
 
132
            with open(info) as f:
 
133
                diskname = f.read()
131
134
            for special in ('"', ']', '[', '_'):
132
135
                diskname = diskname.replace(special, '_')
133
136
        return diskname
162
165
                "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):]))
163
166
            outf = os.path.join(targetdir, os.path.splitext(fname)[0])
164
167
            if f.endswith(".gz"):
165
 
                g = gzip.open(f)
166
 
                try:
167
 
                    with open(outf, "wb") as out:
168
 
                        # uncompress in 64k chunks
169
 
                        while True:
170
 
                            s = g.read(64000)
171
 
                            out.write(s)
172
 
                            if s == b"":
173
 
                                break
174
 
                finally:
175
 
                    g.close()
 
168
                with gzip.open(f) as g, open(outf, "wb") as out:
 
169
                    # uncompress in 64k chunks
 
170
                    while True:
 
171
                        s = g.read(64000)
 
172
                        out.write(s)
 
173
                        if s == b"":
 
174
                            break
176
175
            else:
177
176
                shutil.copy(f, outf)
178
177
        return True
187
186
                "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):]))
188
187
            outf = os.path.join(targetdir, os.path.splitext(fname)[0])
189
188
            if f.endswith(".gz"):
190
 
                g = gzip.open(f)
191
 
                try:
192
 
                    with open(outf, "wb") as out:
193
 
                        # uncompress in 64k chunks
194
 
                        while True:
195
 
                            s = g.read(64000)
196
 
                            out.write(s)
197
 
                            if s == b"":
198
 
                                break
199
 
                finally:
200
 
                    g.close()
 
189
                with gzip.open(f) as g, open(outf, "wb") as out:
 
190
                    # uncompress in 64k chunks
 
191
                    while True:
 
192
                        s = g.read(64000)
 
193
                        out.write(s)
 
194
                        if s == b"":
 
195
                            break
201
196
            else:
202
197
                shutil.copy(f, outf)
203
198
        return True
218
213
            if not (ret == 0):
219
214
                return False
220
215
            # now do the hash sum checks
221
 
            t = apt_pkg.TagFile(open(releasef))
222
 
            t.step()
223
 
            for entry in t.section["SHA256"].split("\n"):
 
216
            with open(releasef) as f:
 
217
                t = apt_pkg.TagFile(f)
 
218
                t.step()
 
219
                sha256_section = t.section["SHA256"]
 
220
            for entry in sha256_section.split("\n"):
224
221
                (hash, size, name) = entry.split()
225
222
                f = os.path.join(basepath, name)
226
223
                if not os.path.exists(f):
227
224
                    logging.info("ignoring missing '%s'" % f)
228
225
                    continue
229
 
                sum = apt_pkg.sha256sum(open(f))
 
226
                with open(f) as fp:
 
227
                    sum = apt_pkg.sha256sum(open(fp))
230
228
                if not (sum == hash):
231
229
                    logging.error(
232
230
                        "hash sum mismatch expected %s but got %s" % (
279
277
        
280
278
        # prepend to the sources.list
281
279
        sourceslist = apt_pkg.config.find_file("Dir::Etc::sourcelist")
282
 
        content = open(sourceslist).read()
283
 
        open(sourceslist, "w").write(
 
280
        with open(sourceslist) as f:
 
281
            content = f.read()
 
282
        with open(sourceslist, "w") as f:
 
283
            f.write(
284
284
            "# added by the release upgrader\n%s\n%s" % (debline, content))
285
285
        self._writeDatabase()
286
286