def upload_file(flickr, path): real_sha1 = sha1sum(path) real_md5 = md5sum(path) tags = sha1_machine_tag_prefix + real_sha1 + " " +\ md5_machine_tag_prefix + real_md5 result = flickr.upload(filename=path, title=os.path.basename(path), tags=tags, async=1)
def decisionlogic(): """Main decision/summing loop. Returns False if no more actions to perform. """ global FilesActionMap global FilesHashMap global FilesSyncQueue global logger # reread fresh status on every run FilesActionMap = read_atomic(FILES_STATUS_FILE) # ignore if no actions pending if len(FilesActionMap.keys()) == 0: return False # random choice to avoid checksumming the same file over and over if # it changes often myfile = random.choice(FilesActionMap.keys()) monitor_action, monitor_timestamp = FilesActionMap[myfile] # by default don't resync nor remote remove files sync_action = None myperm = None # file is freshly created or changed if monitor_action == 'changed' or monitor_action == 'created' or \ monitor_action == 'attrib': # calculate checksum try: mysha1sum = sha1sum(myfile) except (IOError, OSError): logger.info('Could not checksum file %s. Ignoring.' % myfile) check_updated(None, monitor_timestamp, myfile) return True # get permissions try: myperm = oct(stat.S_IMODE(os.stat(myfile)[stat.ST_MODE])) except (IOError, OSError): logger.info('Could not get permissions for file %s. Ignoring.' % myfile) check_updated(None, monitor_timestamp, myfile) return True # already known file if myfile in FilesHashMap: mysha1sumold, mypermold = FilesHashMap[myfile] # if checksum is different, resync is mandatory if mysha1sumold != mysha1sum: sync_action = 'sync' # else if just mode changed, change remote mode elif myperm != mypermold: sync_action = 'change_perm' # first time seen file (no checksum and no mode) else: sync_action = 'sync' FilesHashMap[myfile] = mysha1sum, myperm # deleted file elif monitor_action == 'deleted': if myfile in FilesHashMap: del FilesHashMap[myfile] sync_action = 'remove' # created directory elif monitor_action == 'created_dir': try: myperm = oct(stat.S_IMODE(os.stat(myfile)[stat.ST_MODE])) except (IOError, OSError): logger.info('Could not get permissions for directory %s. ' 'Ignoring.' % myfile) check_updated(None, monitor_timestamp, myfile) return True sync_action = 'make_dir' # deleted directory elif monitor_action == 'deleted_dir': sync_action = 'remove_dir' # permissions change for directory elif monitor_action == 'attrib_dir': # get permissions try: myperm = oct(stat.S_IMODE(os.stat(myfile)[stat.ST_MODE])) except (IOError, OSError): logger.info('Could not get permissions for directory %s. ' 'Ignoring.' % myfile) check_updated(None, monitor_timestamp, myfile) return True sync_action = 'change_perm' # write hash file.. write_atomic(FILES_HASH_FILE, FilesHashMap) logger.debug('Hash file status: %s.' % FilesHashMap) # check if file/directory has been updated in the meantime check_updated(sync_action, monitor_timestamp, myfile) # resync or remove remote files logger.debug('Pending action %s for file %s.' % (sync_action, myfile)) if sync_action: FilesSyncQueue = read_atomic(FILES_SYNC_FILE) FilesSyncQueue.append((myfile, sync_action, myperm)) write_atomic(FILES_SYNC_FILE, FilesSyncQueue) logger.debug('Pending sync queue: %s.' % FilesSyncQueue) return True