Searches for newer files present in projects, replaces their older versions present in library.
Searches for older files present in projects, replaces them with newer files from library.
First parameter - projects root directory, second - libraries root directory, third - files patterns, separated by semicolon.
#!/usr/bin/python # -*- coding: utf_8 -*- ''' Libraries files synchronizer. Searches for newer files present in projects, replaces older files in library. Searches for older files present in projects, replaces with newer files from library. First parameter - projects root directory, second - libraries root directory, third - files patterns, separated by semicolon. ''' import os, os.path, sys, fnmatch, shutil, time path_root_projects = '../' # projects root dir path_root_libs = '.' # libs root dir # default files patterns files_pats = 'ul_*.c;ul_*.h;ul_*.cxx;ul_*.cpp;ul_*.py;ul_*.rb;ul_*.lua;ul_*.js;ul_*.css' # returns True if file name belongs to patterns, delimited by patterns_separator def file_in_patterns(file_name, patterns, patterns_separator = ';') : ''' returns True if file_name satisfies patterns delimited by patterns_splitter ''' return True in [fnmatch.fnmatch(file_name, pat) for pat in patterns.split(patterns_separator)] # none parameters - print help if len(sys.argv) <= 1 : print 'Files synchronizer.' print 'Usage : ul_py_sync projects_root [libs_root [files_masks]]' print ' where' print ' projects_root - root directory of projects,' print ' libs_root - root directory of libraries,' print ' files_masks - files masks, splitted by semicolon' raw_input() exit() # projects root dir path_root_projects = os.path.abspath(sys.argv[1]) # libs root dir if len(sys.argv) <= 2 : path_root_libs = os.path.split(sys.argv[0])[0] else : path_root_libs = sys.argv[2] path_root_libs = os.path.abspath(path_root_libs) # patterns given if len(sys.argv) > 3 : files_pats = sys.argv[3] print print 'Libraries root : ', path_root_libs print 'Projects root : ', path_root_projects print 'Files patterns to search : ', files_pats print # build library files list lib_files = {} for wroot, wdirs, wfiles in os.walk(path_root_libs) : for wfile in wfiles : if file_in_patterns(wfile, files_pats) : # remember file modification date and path in dict lib_files[wfile] = [os.path.getmtime(os.path.join(wroot, wfile)), os.path.join(wroot, wfile), None, None] print 'Found : ', len(lib_files), ' library files.' print #search for newer files in projects files_cnt = 0 print 'Newer files in projects :' for wroot, wdirs, wfiles in os.walk(path_root_projects) : for wfile in wfiles : files_cnt += 1 # if file name present in library and is newer if wfile in lib_files : if lib_files[wfile][2] == None : old_date = lib_files[wfile][0] else : old_date = lib_files[wfile][2] if old_date < os.path.getmtime(os.path.join(wroot, wfile)) : # remember newer file path and date lib_files[wfile][2] = os.path.getmtime(os.path.join(wroot, wfile)) lib_files[wfile][3] = os.path.join(wroot, wfile) # search for newer files in projects files_lib_newer_cnt = 0 for wfile in lib_files : if lib_files[wfile][2] != None : # found newer file in projects print ' ', wfile, ' : ', os.path.split(lib_files[wfile][3])[0] print ' ', time.ctime(lib_files[wfile][0]), ' < ', time.ctime(lib_files[wfile][2]) files_lib_newer_cnt += 1 print print 'Total files scanned : ', files_cnt print 'Newer files found in projects : ', files_lib_newer_cnt print # update files in library if files_lib_newer_cnt > 0 : print 'Update (y/n) ?', yn = raw_input() print if yn[0].lower() == 'Y' : print 'Updating :' for wfile in lib_files : if lib_files[wfile][2] != None : # found newer file in projects print ' ', wfile, ' : ', os.path.split(lib_files[wfile][3])[0] shutil.copy2(lib_files[wfile][3], lib_files[wfile][1]) # update older library file lib_files[wfile][0] = lib_files[wfile][2] # remember its modification date #renew project files files_proj_newer_cnt = 0 print 'Older files in projects : ' for wroot, wdirs, wfiles in os.walk(path_root_projects) : for wfile in wfiles : #if file name present in library and is newer, update it if wfile in lib_files : if (lib_files[wfile][0] > os.path.getmtime(os.path.join(wroot, wfile))) : print ' ', os.path.join(wroot, wfile) lib_files[wfile][2] = os.path.getmtime(os.path.join(wroot, wfile)) lib_files[wfile][3] = os.path.join(wroot, wfile) files_proj_newer_cnt += 1 else : lib_files[wfile][2] = lib_files[wfile][3] = None print print 'Newer files in libs : ', files_proj_newer_cnt print # update files in library if files_proj_newer_cnt > 0 : print 'Update (y/n) ?', yn = raw_input() print if yn[0].lower() == 'Y' : print 'Updating :' for wfile in lib_files : if lib_files[wfile][2] != None : # found newer file in projects print ' ', wfile, ' : ', os.path.split(lib_files[wfile][3])[0] shutil.copy2(lib_files[wfile][1], lib_files[wfile][3]) # update older project file #raw_input()