info = { 'icon' : 'PACKAGE', 'description' : 'pack all videos', 'dryrun' : False } import os, re, shutil from os.path import dirname, basename, join, abspath, splitext, exists, isfile, isdir from os import listdir import datetime dryrun = info['dryrun'] resid = re.compile(r'\d{2}-\d{3}-\d{2}$') shots = '/z/___SERIE/WIND-UPS/episodes/00/shots/' packzone = '/z/___SERIE/WIND-UPS/medias/pack' old_folder = join(packzone, '_old') new_list = [] warning = [] extension_filter = ('mp4', 'mov',)#, 'mkv' exclusion_words = ('test', 'old',)#use this for loose word search rexclude = re.compile(r'[ _-]old|test')#old if after separator (' _-') and word test anywhere (might be too loose) for f in os.scandir(shots): if not resid.match(f.name):continue imgfp = f'/z/___SERIE/WIND-UPS/episodes/00/shots/{f.name}/lighting/images' if not exists(imgfp):continue #need to be video file type all_videos = [v for v in os.scandir(imgfp) if v.name.lower().endswith(extension_filter) and not rexclude.search(v.name)]#any(x in v.name for x in exclusion_words) if not all_videos:continue print(f'\n{f.name}') bytime = sorted(all_videos, key=lambda x: x.stat().st_mtime) byname = sorted(all_videos, key=lambda x: x.name) # print('bytime: ', bytime[-1].name) # print('byname: ', byname[-1].name) if bytime[-1] != byname[-1]: warn = f'''/!\\ {f.name} last by name and by modif time different in {imgfp} by time : {bytime[-1].name} ({datetime.datetime.fromtimestamp(bytime[-1].stat().st_mtime)}) by name : {byname[-1].name} ({datetime.datetime.fromtimestamp(byname[-1].stat().st_mtime)}) ''' print(warn) warning.append(warn) good_file = bytime[-1] topack = good_file.path topackname = good_file.name dest = join(packzone, topackname) # run checks and copy if needed if exists(dest): dup = [basename(dest)] else: print(f"compare with {'_'.join(splitext(good_file.name)[0].split('_')[:3])}") # ex : twu_00-008-01_lighting_v001.mp4 (split on third to) dup = [i for i in listdir(packzone) if isfile(join(packzone, i)) and '_'.join(splitext(i)[0].split('_')[:3]) == '_'.join(splitext(good_file.name)[0].split('_')[:3])] # check duplication against this file if dup: print(f'same shot already exists:{dup}') newer = True for i in dup: dfp = join(packzone, i) # print(f'{os.stat(dfp).st_mtime} VS file {good_file.stat().st_mtime}') if os.stat(dfp).st_mtime >= good_file.stat().st_mtime: # print(f'{i} is same or most recent') newer = False if not newer: print(f'{good_file.name} is not newer, skip') continue #src is most recent, do the copy and erase/move older for d in dup: if not exists(old_folder):os.mkdir(old_folder) old_src = join(packzone, d) old_dest = join(old_folder, d) if exists(old_dest): print(f' - deleting {d} (already in _old)') if not dryrun: os.remove(old_src) else: print(f' - moving {d} to _old') if not dryrun: shutil.move(old_src, old_dest) # if made it here, safely do the copy print(f'- packing new file : {topack}') if not dryrun: shutil.copy2(topack, dest) new_list.append(f'{topackname} : {topack}') print('DONE\n----') if warning: print('\nWarnings') for w in warning: print(w) if new_list: print(f'\n{len(new_list)} New:') for n in new_list: print(n)