mercurial/archival.py
changeset 44577 f8427841c8fc
parent 44284 e786d69c665d
parent 44475 b7ca03dff14c
child 45048 2c0043977b6d
child 45072 a56ba57c837d
equal deleted inserted replaced
44576:2ec6160449aa 44577:f8427841c8fc
   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 pycompat.ispy3:
   139         def __init__(self, *args, **kw):
   139         GzipFileWithTime = gzip.GzipFile  # camelcase-required
   140             timestamp = None
   140     else:
   141             if 'timestamp' in kw:
   141 
   142                 timestamp = kw.pop('timestamp')
   142         class GzipFileWithTime(gzip.GzipFile):
   143             if timestamp is None:
   143             def __init__(self, *args, **kw):
   144                 self.timestamp = time.time()
   144                 timestamp = None
   145             else:
   145                 if 'mtime' in kw:
   146                 self.timestamp = timestamp
   146                     timestamp = kw.pop('mtime')
   147             gzip.GzipFile.__init__(self, *args, **kw)
   147                 if timestamp is None:
   148 
   148                     self.timestamp = time.time()
   149         def _write_gzip_header(self):
   149                 else:
   150             self.fileobj.write(b'\037\213')  # magic header
   150                     self.timestamp = timestamp
   151             self.fileobj.write(b'\010')  # compression method
   151                 gzip.GzipFile.__init__(self, *args, **kw)
   152             fname = self.name
   152 
   153             if fname and fname.endswith(b'.gz'):
   153             def _write_gzip_header(self):
   154                 fname = fname[:-3]
   154                 self.fileobj.write(b'\037\213')  # magic header
   155             flags = 0
   155                 self.fileobj.write(b'\010')  # compression method
   156             if fname:
   156                 fname = self.name
   157                 flags = gzip.FNAME  # pytype: disable=module-attr
   157                 if fname and fname.endswith(b'.gz'):
   158             self.fileobj.write(pycompat.bytechr(flags))
   158                     fname = fname[:-3]
   159             gzip.write32u(  # pytype: disable=module-attr
   159                 flags = 0
   160                 self.fileobj, int(self.timestamp)
   160                 if fname:
   161             )
   161                     flags = gzip.FNAME  # pytype: disable=module-attr
   162             self.fileobj.write(b'\002')
   162                 self.fileobj.write(pycompat.bytechr(flags))
   163             self.fileobj.write(b'\377')
   163                 gzip.write32u(  # pytype: disable=module-attr
   164             if fname:
   164                     self.fileobj, int(self.timestamp)
   165                 self.fileobj.write(fname + b'\000')
   165                 )
       
   166                 self.fileobj.write(b'\002')
       
   167                 self.fileobj.write(b'\377')
       
   168                 if fname:
       
   169                     self.fileobj.write(fname + b'\000')
   166 
   170 
   167     def __init__(self, dest, mtime, kind=b''):
   171     def __init__(self, dest, mtime, kind=b''):
   168         self.mtime = mtime
   172         self.mtime = mtime
   169         self.fileobj = None
   173         self.fileobj = None
   170 
   174 
   176                 gzfileobj = self.GzipFileWithTime(
   180                 gzfileobj = self.GzipFileWithTime(
   177                     name,
   181                     name,
   178                     pycompat.sysstr(mode + b'b'),
   182                     pycompat.sysstr(mode + b'b'),
   179                     zlib.Z_BEST_COMPRESSION,
   183                     zlib.Z_BEST_COMPRESSION,
   180                     fileobj,
   184                     fileobj,
   181                     timestamp=mtime,
   185                     mtime=mtime,
   182                 )
   186                 )
   183                 self.fileobj = gzfileobj
   187                 self.fileobj = gzfileobj
   184                 return tarfile.TarFile.taropen(  # pytype: disable=attribute-error
   188                 return tarfile.TarFile.taropen(  # pytype: disable=attribute-error
   185                     name, pycompat.sysstr(mode), gzfileobj
   189                     name, pycompat.sysstr(mode), gzfileobj
   186                 )
   190                 )