"""Check the tree  against the version in the  backup volume, and copy
any missing files back.  Used to repair a disaster, once."""

import os, shutil

def check_dir(old_directory):

    print old_directory
    old_files = os.listdir(old_directory)
    assert re.match('/mit/python/Oldfiles', old_directory)
    new_dir = old_directory.replace('/mit/python/Oldfiles/', '/mit/python/')
    new_files = os.listdir(new_dir)
    for file in old_files:
        old_file = os.path.join(old_directory, file)
        if os.path.isdir(old_file) and (

           # Don't follow or recursively copy symlinks to directories!
           not os.path.islink(old_file)):
            if file not in new_files:
                print 'recursively copying directory %s to %s' % \
                      (old_file, new_dir)
            else:
                check_dir(old_file)
        else:
            if file not in new_files:
                print 'copying %s to %s' % (old_file, new_dir)
                # shutil.copy(old_file, new_dir)

if __name__ == '__main__': check_dir('/mit/python/Oldfiles')
