def DoInventory()

in Providers/Scripts/3.x/Scripts/nxFileInventory.py [0:0]


def DoInventory(DestinationPath, Recurse, Links, Checksum, Type, MaxContentsReturnable, MaxOutputSize, UseSudo):
    Inventory = []
    full_path = DestinationPath.split('/')
    if full_path[-1] == '':
        full_path[-1] = '*'
    wildcard_path = False
    for p in full_path:
        if '*' in p or '?' in p:
            wildc_start=full_path.index(p)
            wildcard_path = True
            break
    if wildcard_path:
        top = '/' + os.path.join(*full_path[:wildc_start])
    else :
        top = '/' + os.path.join(*full_path)
    if not os.path.exists(top):
        print("Error: Unable to read 'DestinationPath': " + DestinationPath)
        LG().Log("ERROR","Unable to read 'DestinationPath': " + DestinationPath)
        return Inventory
    if not wildcard_path:
        if Links == 'ignore' and os.path.islink(top):
            return Inventory
        if Type != 'directory' and os.path.isfile(top): # This is s single file.
            d = GetFileInfo(top, Links, MaxContentsReturnable, Checksum)
            if 'DestinationPath' in d.keys():
                Inventory.append(copy.deepcopy(d))
            return Inventory
        if '*' not in full_path[-1] and '?' not in full_path[-1]:
            full_path.append('*') # It is a directory without the trailing '/', so add it.
    dirs = set()
    full_path_len =  len(full_path)
    for dirpath, dirnames, filenames in os.walk(top, followlinks=(Links == 'follow'), topdown=True):
        dlen = len(dirpath.split('/'))
        if dirpath.split('/')[-1] == '':
            dlen -= 1
        if wildcard_path and full_path_len >= dlen+1:
            do_wildcard = True
        else :
            do_wildcard = False
        st = os.stat(dirpath)
        scandirs = []
        if dlen+1 == full_path_len  or ( Recurse and dlen >= full_path_len ):
            for filename in filenames:
                if (dlen+1 == full_path_len  or ( Recurse and dlen >= full_path_len )) \
                       and not fnmatch.fnmatch(filename, full_path[-1]):
                    continue
                if Type != 'directory':
                    d = GetFileInfo(os.path.join(dirpath, filename),\
                                    Links, MaxContentsReturnable, Checksum)
                    if 'DestinationPath' in d.keys():
                        Inventory.append(copy.deepcopy(d))
        for dirname in dirnames:
            if not ( Recurse and dlen+1 >= full_path_len ):
                if ( do_wildcard and not fnmatch.fnmatch(dirname, full_path[dlen]) ) or \
                       ( not Recurse and dlen > full_path_len ):
                    continue
            st = os.stat(os.path.join(dirpath, dirname)) # use Lstat if follow?
            dirkey = st.st_dev, st.st_ino
            if dirkey not in dirs:
                if Recurse or (not Recurse and dlen+1 < full_path_len)  :
                    dirs.add(dirkey)
                    scandirs.append(dirname)
            if Type != 'file' and ( dlen+1 == full_path_len  or  ( Recurse and dlen >= full_path_len ) ) :
                d = GetDirInfo(os.path.join(dirpath, dirname), st, Checksum, Links)
                if 'DestinationPath' in d.keys():
                    Inventory.append(copy.deepcopy(d))
        dirnames[:] = scandirs
    return Inventory