Exemplo n.º 1
0
def walkdir(i_path, i_subwalk, i_curdepth=0):
    global Progress
    global PrevFiles
    global CurFiles
    global ThumbFolderCount
    global ReportFolderCount
    global TotalSpace

    # Output current path:
    if Options.verbose > i_curdepth and i_subwalk:
        outInfo('cur_path', i_path)

    # Output report:
    if Options.report is not None:
        if i_path.find(os.path.dirname(Options.output)) == -1:
            if ReportFolderCount % Options.report == 0:
                print('REPORT: %s - %.1f GB' %
                      (i_path, TotalSpace / 1024 / 1024 / 1024))
                sys.stdout.flush()
            ReportFolderCount += 1

    out = jsonLoad(os.path.join(i_path, Options.output))
    if out is None:
        out = dict()
    checkDict(out, True)

    try:
        entries = os.listdir(i_path)
    except:
        outInfo('error_listdir', str(sys.exc_info()[1]))
        return None

    for entry in entries:

        path = os.path.join(i_path, entry)

        st = None
        try:
            st = os.lstat(path)
        except:
            outInfo('error_listdir', str(sys.exc_info()[1]))
            continue

        # We are not walking in links:
        if stat.S_ISLNK(st.st_mode):
            continue

        if stat.S_ISDIR(st.st_mode):
            out['num_folders'] += 1
            out['num_folders_total'] += 1
            size, space = getSizeSpace(st)
            out['space'] += space
            TotalSpace += space

            fout = None
            if i_subwalk:
                # Recursively walk in a subfolder:
                fout = walkdir(path, True, i_curdepth + 1)
            else:
                # Load previous walk data:
                fout = jsonLoad(os.path.join(path, Options.output))

            if fout is not None:
                checkDict(fout)

                # We do not need info for each subfolder in a child folder:
                del fout['files']
                del fout['folders']

                # Create an empty folder entry if not preset:
                if not 'folders' in out:
                    out['folders'] = dict()
                if not isinstance(out['folders'], dict):
                    out['folders'] = dict()
                if not entry in out['folders']:
                    out['folders'][entry] = dict()

                # Merge keys from subfolder:
                for key in fout.keys():
                    out['folders'][entry][key] = fout[key]

                out['num_folders_total'] += fout['num_folders_total']
                out['num_files_total'] += fout['num_files_total']
                out['size_total'] += fout['size_total']
                out['space'] += fout['space']

        if stat.S_ISREG(st.st_mode):
            CurFiles += 1
            size, space = getSizeSpace(st)
            if entry[0] != '.':
                out['num_files'] += 1
                if cgruutils.isImageExt(path):
                    if Options.thumb is not None:
                        if out['num_images'] == 0:
                            if ThumbFolderCount % Options.thumb == 0 and size < 10000000:
                                print('@IMAGE!@' + path)
                                sys.stdout.flush()
                            ThumbFolderCount += 1
                    out['num_images'] += 1
                elif cgruutils.isMovieExt(path) and Options.mediainfo:
                    obj = mediainfo.processMovie(path)
                    if obj and 'mediainfo' in obj:
                        out['files'][entry] = obj['mediainfo']
            out['num_files_total'] += 1
            out['size_total'] += size
            out['size'] += size
            out['space'] += space
            TotalSpace += space

    # Just output progress:
    if PrevFiles:
        cur_progress = int(100.0 * CurFiles / PrevFiles)
        if cur_progress != Progress:
            Progress = cur_progress
            outInfo('progress', 'PROGRESS: %d%%' % Progress)

    # Skip soting data in .rules folders, or we will create '.rules/.rules' folders
    if os.path.basename(i_path) == os.path.dirname(Options.output):
        return out

    # Store current walk data:
    filename = os.path.join(i_path, Options.output)

    if not os.path.isdir(os.path.dirname(filename)):
        try:
            os.makedirs(os.path.dirname(filename))
        except:
            outInfo('error_make_dir', str(sys.exc_info()[1]))

    if os.path.isdir(os.path.dirname(filename)):
        try:
            with open(filename, 'w') as f:
                json.dump(out, f, indent=1)
        except:
            outInfo('error_file_write', str(sys.exc_info()[1]))

    return out
Exemplo n.º 2
0
def walkdir(i_path, i_subwalk, i_curdepth=0):
    global Progress
    global PrevFiles
    global CurFiles
    global ThumbFolderCount
    global ReportFolderCount
    global TotalSpace

    # Output current path:
    if Options.verbose > i_curdepth and i_subwalk:
        outInfo('cur_path',i_path)

    # Output report:
    if Options.report is not None:
        if i_path.find( os.path.dirname( Options.output)) == -1:
            if ReportFolderCount % Options.report == 0:
                print('REPORT: %s - %.1f GB' % ( i_path, TotalSpace / 1024 / 1024 / 1024 ))
                sys.stdout.flush()
            ReportFolderCount += 1

    out = jsonLoad( os.path.join( i_path, Options.output))
    if out is None:
        out = dict()
    checkDict(out, True)

    try:
        entries = os.listdir(i_path)
    except:
        outInfo('error_listdir',str(sys.exc_info()[1]))
        return None

    for entry in entries:

        path = os.path.join(i_path, entry)

        st = None
        try:
            st = os.lstat( path)
        except:
            outInfo('error_listdir',str(sys.exc_info()[1]))
            continue

        # We are not walking in links:
        if stat.S_ISLNK( st.st_mode):
            continue

        if stat.S_ISDIR( st.st_mode):
            out['num_folders'] += 1
            out['num_folders_total'] += 1
            size, space = getSizeSpace( st)
            out['space'] += space
            TotalSpace += space

            fout = None
            if i_subwalk:
                # Recursively walk in a subfolder:
                fout = walkdir(path, True, i_curdepth + 1)
            else:
                # Load previous walk data:
                fout = jsonLoad(os.path.join(path, Options.output))

            if fout is not None:
                checkDict(fout)

                # We do not need info for each subfolder in a child folder:
                del fout['files']
                del fout['folders']

                # Create an empty folder entry if not preset:
                if not 'folders' in out:
                    out['folders'] = dict()
                if not isinstance(out['folders'], dict):
                    out['folders'] = dict()
                if not entry in out['folders']:
                    out['folders'][entry] = dict()

                # Merge keys from subfolder:
                for key in fout.keys():
                    out['folders'][entry][key] = fout[key]

                out['num_folders_total'] += fout['num_folders_total']
                out['num_files_total'] += fout['num_files_total']
                out['size_total'] += fout['size_total']
                out['space'] += fout['space']

        if stat.S_ISREG( st.st_mode):
            CurFiles += 1
            size, space = getSizeSpace( st)
            if entry[0] != '.':
                out['num_files'] += 1
                if cgruutils.isImageExt( path):
                    if Options.thumb is not None:
                        if out['num_images'] == 0:
                            if ThumbFolderCount % Options.thumb == 0 and size < 10000000:
                                print('@IMAGE!@'+path)
                                sys.stdout.flush()
                            ThumbFolderCount += 1
                    out['num_images'] += 1
                elif cgruutils.isMovieExt( path) and Options.mediainfo:
                    obj = mediainfo.processMovie( path)
                    if obj and 'mediainfo' in obj:
                        out['files'][entry] = obj['mediainfo']
            out['num_files_total'] += 1
            out['size_total'] += size
            out['size'] += size
            out['space'] += space
            TotalSpace += space

    # Just output progress:
    if PrevFiles:
        cur_progress = int(100.0 * CurFiles / PrevFiles)
        if cur_progress != Progress:
            Progress = cur_progress
            outInfo('progress','PROGRESS: %d%%' % Progress)

    # Skip soting data in .rules folders, or we will create '.rules/.rules' folders
    if os.path.basename(i_path) == os.path.dirname(Options.output):
        return out
            
    # Store current walk data:
    filename = os.path.join(i_path, Options.output)

    if not os.path.isdir(os.path.dirname(filename)):
        try:
            os.makedirs(os.path.dirname(filename))
        except:
            outInfo('error_make_dir',str(sys.exc_info()[1]))

    if os.path.isdir(os.path.dirname(filename)):
        try:
            with open(filename, 'w') as f:
                json.dump(out, f, indent=1)
        except:
            outInfo('error_file_write',str(sys.exc_info()[1]))

    return out
Exemplo n.º 3
0
OutDir = os.path.dirname(Options.output)
if OutDir != '':
    if not os.path.isdir(OutDir):
        os.makedirs(OutDir)
        if not os.path.isdir(OutDir):
            errorExit('Can`t create output folder %s' % OutDir)

Cmds = []
Thumbnails = []

if Movie is not None:

    # Try to get movie frames count:
    frame_count = 3  # < this will be the default value
    inf_obj = mediainfo.processMovie(Movie)

    if 'error' in inf_obj:
        if Options.verbose: print(inf_obj)
        if 'data' in inf_obj:
            data = inf_obj['data']
            if Options.verbose: print(data)
            data = re.findall('frame_count:\d*', data)
            if len(data):
                frame_count = int(data[0].split(':')[1])
                print('frame_count:%d' % frame_count)
        print(inf_obj['error'])
        inf_obj = None

    if inf_obj and 'mediainfo' in inf_obj and 'video' in inf_obj['mediainfo']:
        frame_count = inf_obj['mediainfo']['video']['frame_count']
Exemplo n.º 4
0
OutDir = os.path.dirname(Options.output)
if OutDir != '':
    if not os.path.isdir(OutDir):
        os.makedirs(OutDir)
        if not os.path.isdir(OutDir):
            errorExit('Can`t create output folder %s' % OutDir)

Cmds = []
Thumbnails = []

if Movie is not None:

    # Try to get movie frames count:
    frame_count = 3 # < this will be the default value
    inf_obj = mediainfo.processMovie( Movie)
    if inf_obj and 'mediainfo' in inf_obj and 'video' in inf_obj['mediainfo']:
        frame_count = inf_obj['mediainfo']['video']['frame_count']

        # Write to file (to store in walk):
        walk_obj = {}
        # Try to read existing file if any:
        walk_file = os.path.join( os.path.dirname( Movie), Options.walk)
        if os.path.isfile( walk_file):
            try:
                file = open( walk_file, 'r')
                walk_obj = json.load( file)
                file.close()
            except:
                walk_obj = {}
        # Add file to walk object files: