comparison mercurial/archival.py @ 44474:a23b859ad17d stable

gzip: indent the custom Gzip code We need a new conditional in the next changesets (Adding 3.8 support). We do the large code move beforehand for clarity.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 10 Mar 2020 18:53:19 +0100
parents 6c36a521572e
children b7ca03dff14c
comparison
equal deleted inserted replaced
44473:6c36a521572e 44474:a23b859ad17d
133 133
134 class tarit(object): 134 class tarit(object):
135 '''write archive to tar file or stream. can write uncompressed, 135 '''write archive to tar file or stream. can write uncompressed,
136 or compress with gzip or bzip2.''' 136 or compress with gzip or bzip2.'''
137 137
138 class GzipFileWithTime(gzip.GzipFile): 138 if True:
139 def __init__(self, *args, **kw): 139
140 timestamp = None 140 class GzipFileWithTime(gzip.GzipFile):
141 if 'mtime' in kw: 141 def __init__(self, *args, **kw):
142 timestamp = kw.pop('mtime') 142 timestamp = None
143 if timestamp is None: 143 if 'mtime' in kw:
144 self.timestamp = time.time() 144 timestamp = kw.pop('mtime')
145 else: 145 if timestamp is None:
146 self.timestamp = timestamp 146 self.timestamp = time.time()
147 gzip.GzipFile.__init__(self, *args, **kw) 147 else:
148 148 self.timestamp = timestamp
149 def _write_gzip_header(self): 149 gzip.GzipFile.__init__(self, *args, **kw)
150 self.fileobj.write(b'\037\213') # magic header 150
151 self.fileobj.write(b'\010') # compression method 151 def _write_gzip_header(self):
152 fname = self.name 152 self.fileobj.write(b'\037\213') # magic header
153 if fname and fname.endswith(b'.gz'): 153 self.fileobj.write(b'\010') # compression method
154 fname = fname[:-3] 154 fname = self.name
155 flags = 0 155 if fname and fname.endswith(b'.gz'):
156 if fname: 156 fname = fname[:-3]
157 flags = gzip.FNAME # pytype: disable=module-attr 157 flags = 0
158 self.fileobj.write(pycompat.bytechr(flags)) 158 if fname:
159 gzip.write32u( # pytype: disable=module-attr 159 flags = gzip.FNAME # pytype: disable=module-attr
160 self.fileobj, int(self.timestamp) 160 self.fileobj.write(pycompat.bytechr(flags))
161 ) 161 gzip.write32u( # pytype: disable=module-attr
162 self.fileobj.write(b'\002') 162 self.fileobj, int(self.timestamp)
163 self.fileobj.write(b'\377') 163 )
164 if fname: 164 self.fileobj.write(b'\002')
165 self.fileobj.write(fname + b'\000') 165 self.fileobj.write(b'\377')
166 if fname:
167 self.fileobj.write(fname + b'\000')
166 168
167 def __init__(self, dest, mtime, kind=b''): 169 def __init__(self, dest, mtime, kind=b''):
168 self.mtime = mtime 170 self.mtime = mtime
169 self.fileobj = None 171 self.fileobj = None
170 172