[ale] Archiving directories/files with "compressed" mirror version

Brian Pitts brian at polibyte.com
Wed Aug 13 16:42:46 EDT 2008


Chris Woodfield wrote:
> I'm handling a request that I'd be surprised if no one's run into  
> before, so I figured I'd
> 
> The request is to be able to walk a directory tree and make a mirrored  
> copy of the directory, but with every file compressed. Not make a  
> single archive of the dir - the end result will have all directories  
> with the same names, perms, etc, but each file will be a bzip'ed  
> version of the original.
> 
[snip]
> Anyone out there have working code to do something like this? Perl  
> preferred...

Python provided. ;-) It worked for the one test I ran.

#!/usr/bin/env python

import sys, os, os.path, bz2

if len(sys.argv) != 2:
  print 'Specify one source directory'
  sys.exit()

source = os.path.abspath(sys.argv[1])
dest = source + '.archive'
os.mkdir(dest)

for root, dirs, files in os.walk(source, topdown=True):
  for name in dirs:
    newroot = root.replace(source, dest)
    statinfo = os.stat(os.path.join(root, name))
    os.mkdir(os.path.join(newroot, name), statinfo.st_mode)
  for name in files:
    newroot = root.replace(source, dest)
    newname = name + '.bz2'
    ifile = open(os.path.join(root, name))
    ofile = bz2.BZ2File(os.path.join(newroot, newname), 'w')
    ofile.writelines(ifile)
    ofile.close()
    ifile.close()


More information about the Ale mailing list