#! /usr/bin/env python
# $Id: dpm-dpns-to-disk,v 1.1 2008/10/21 08:47:05 gcowan Exp $

__author__ = 'Greig A Cowan'
__date__ = 'April 2008'
__version = 0.1

'''Find replica filename of files in the DPM namespace.'''

import sys
try:
    import dpm
except:
    print 'Failed to import dpm API. Check your $PYTHONPATH. If this is correct, Please check the bitness of your system libraries - it may be that you have 64bit python and 32bit DPM.'
    sys.exit(1)

import stat
from optparse import OptionParser

def main():
    parser = OptionParser(
        usage = 'usage: %prog /dpm/path/to/file')
    parser.add_option('-d','--directory',dest='directory',
                      help='Analyse all files in this dir.')
    parser.add_option('-v','--verbose',dest='verbose',action='store_true',
                      help='See information about namespace entries without replicas.')
    parser.add_option('-z','--zero',dest='zero',action='store_true',
                      help='Only print out files in DPNS that have zero size.')

    (options, args) = parser.parse_args()

    if not options.directory:
        try:
            dpns_path = args[0]
        except:
            parser.print_help()

    if len(args) > 1:
        parser.error("incorrect number of arguments")

    if options.directory:
        if options.directory[-1] != '/':
            directory = options.directory + '/'
        else:
            directory = options.directory
        dir = dpm.dpns_opendirg(directory,"")
        if (dir == None) or (dir == 0):
            err_num = dpm.cvar.serrno
            err_string = dpm.sstrerror(err_num)
            print 'Error while looking for ' + name + ': Error ' \
                  + str(err_num) + ' (' + err_string + ')'
            sys.exit(1)
        
        while 1:
            read_pt = dpm.dpns_readdirxr(dir,'')
            if (read_pt == None) or (read_pt == 0):
                break
            entry, list = read_pt
            try:
                for i in range(len(list)):
                    if not options.zero:
                        print directory + entry.d_name, '\t', list[i].sfn
            except TypeError, x:
                if options.verbose or options.zero:
                    if stat.S_ISREG(entry.filemode):
                        if entry.filesize == 0:
                            print directory + entry.d_name, 'is a zero sized file and has no replicas.'
                        else:
                            if not options.zero:
                                print directory + entry.d_name, 'has no replicas.'
                    if stat.S_ISDIR(entry.filemode):
                        if not options.zero:
                            print directory + entry.d_name, 'is a directory.'
                    
        dpm.dpns_closedir(dir)
        sys.exit()

    result, replicas = dpm.dpns_getreplica(dpns_path, '', '')

    if result == 0:
        for rep in replicas:
            print  dpns_path, '\t', rep.sfn
        if len(replicas) == 0:
            print dpns_path, ': no replicas found'
    else:
        print dpns_path, ': not found'


if __name__ == '__main__':
         main()
