Éric Araujo <mer...@netwok.org> added the comment:

I have a working updated shutil module, tests pass and the documentation is 
improved.  I will make sure to make different commits for improving the tests, 
cleaning up some things, adding tarfile.compression_formats and removing 
duplication in shutil (see dep bugs), to be sure not to introduce regressions.

Before I finish and post the patch, I’d like feedback on a choice I made.  I 
don’t think it’s possible to have shutil automatically support all the formats 
that tarfile does, because of the spelling issue I mentioned.  Here’s what the 
code would look like (let me know if I should post it elsewhere or as a file to 
let you get syntax coloring):
          
  _ARCHIVE_FORMATS = {}
  _UNPACK_FORMATS = {}

  for fmt in tarfile.compression_formats:
      code = fmt + 'tar'
      ext = '.' + fmt
      desc = "%s'ed tar file" % fmt
      _ARCHIVE_FORMATS[code] = (_make_tarball, [('compress', fmt)], desc)
      _UNPACK_FORMATS[code] = ([ext], _unpack_tarfile, [], desc)

  # kludge to add alternate extension
  if 'gztar' in _ARCHIVE_FORMATS:
      _UNPACK_FORMATS['gztar'][0].append('.tgz')
      # XXX desc should say "gzip'ed tar file", not "gz'ed"

  # rectify naming incompatibility
  if 'bz2tar' in _ARCHIVE_FORMATS:
      # XXX alternative: make 'bztar' alias for 'bz2tar', but would complicate
      # manipulating the registries
      del _ARCHIVE_FORMATS['bz2tar']
      del _UNPACK_FORMATS['bz2tar']
      desc = "bzip2'ed tar file"
      _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bz2')], desc)
      _UNPACK_FORMATS['bztar'] = (['.bz2'], _unpack_tarfile, [], desc)

  # now add uncompressed tar and zip file

I really don’t like that code.  Given that tarfile is not extensible at run 
time, it is not a big deal to have to update shutil whenever we add a 
compression format to tarfile.  Therefore, I backtracked on my “automatic 
support” idea but kept a lot of cleanup in did in the code.  Here’s what the 
code looks like:

  _ARCHIVE_FORMATS = {}
  _UNPACK_FORMATS = {}

  if 'xz' in tarfile.compression_formats:
      desc = "xz'ed tar file"
      # XXX '.xz' is not great, '.tar.xz' would be better
      _ARCHIVE_FORMATS['xztar'] = (_make_tarball, [('compress', 'xz')], desc)
      _UNPACK_FORMATS['xztar'] = (['.xz'], _unpack_tarfile, [], desc)

  if 'bz2' in tarfile.compression_formats:
      desc = "bzip2'ed tar file"
      _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bz2')], desc)
      _UNPACK_FORMATS['bztar'] = (['.bz2'], _unpack_tarfile, [], desc)

  if 'gz' in tarfile.compression_formats:
      desc = "gzip'ed tar file"
      _ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gz')], desc)
      _UNPACK_FORMATS['gztar'] = (['.gz', '.tgz'], _unpack_tarfile, [], desc)

So, do you agree that “not automated but not ugly” is better than “automated 
with ugly klutches”?

----------
title: shutil should support all formats supported by tarfile automatically -> 
Add xz support to shutil

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue5411>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to