/breezy-debian/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy-debian/unstable

« back to all changes in this revision

Viewing changes to repack_tarball.py

  • Committer: Jelmer Vernooij
  • Date: 2018-10-15 07:45:11 UTC
  • Revision ID: jelmer@jelmer.uk-20181015074511-0kwd639wr7y4w5ot
Use context managers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
    """A TgzRepacker that just gzips the input."""
83
83
 
84
84
    def repack(self, target_f):
85
 
        gz = gzip.GzipFile(mode='w', fileobj=target_f)
86
 
        try:
 
85
        with gzip.GzipFile(mode='w', fileobj=target_f) as gz:
87
86
            shutil.copyfileobj(self.source_f, gz)
88
 
        finally:
89
 
            gz.close()
90
87
 
91
88
 
92
89
class Tbz2TgzRepacker(TgzRepacker):
94
91
 
95
92
    def repack(self, target_f):
96
93
        content = bz2.decompress(self.source_f.read())
97
 
        gz = gzip.GzipFile(mode='w', fileobj=target_f)
98
 
        try:
 
94
        with gzip.GzipFile(mode='w', fileobj=target_f) as gz:
99
95
            gz.write(content)
100
 
        finally:
101
 
            gz.close()
102
96
 
103
97
 
104
98
class TarLzma2TgzRepacker(TgzRepacker):
110
104
        except ImportError as e:
111
105
            raise DependencyNotPresent('lzma', e)
112
106
        content = lzma.decompress(self.source_f.read())
113
 
        gz = gzip.GzipFile(mode='w', fileobj=target_f)
114
 
        try:
 
107
        with gzip.GzipFile(mode='w', fileobj=target_f) as gz:
115
108
            gz.write(content)
116
 
        finally:
117
 
            gz.close()
118
109
 
119
110
 
120
111
class ZipTgzRepacker(TgzRepacker):
178
169
 
179
170
def _error_if_exists(target_transport, new_name, source_name):
180
171
    source_filetype = get_filetype(source_name)
181
 
    source_f = open_file(source_name)
182
 
    try:
 
172
    with open_file(source_name) as source_f:
183
173
        source_sha = new_sha(source_f.read()).hexdigest()
184
 
    finally:
185
 
        source_f.close()
186
 
    target_f = open_file_via_transport(new_name, target_transport)
187
 
    try:
 
174
    with open_file_via_transport(new_name, target_transport) as target_f:
188
175
        target_sha = new_sha(target_f.read()).hexdigest()
189
 
    finally:
190
 
        target_f.close()
191
176
    if source_sha != target_sha:
192
177
        raise FileExists(new_name)
193
178
 
212
197
    if repacker_cls is None:
213
198
        raise UnsupportedRepackFormat(source_name)
214
199
    target_transport.ensure_base()
215
 
    target_f = target_transport.open_write_stream(new_name)
216
 
    try:
217
 
        source_f = open_file(source_name)
218
 
        try:
 
200
    with target_transport.open_write_stream(new_name) as target_f:
 
201
        with open_file(source_name) as source_f:
219
202
            repacker = repacker_cls(source_f)
220
203
            repacker.repack(target_f)
221
 
        finally:
222
 
            source_f.close()
223
 
    finally:
224
 
        target_f.close()
225
204
 
226
205
 
227
206
def repack_tarball(source_name, new_name, target_dir=None):