Beispiel #1
0
def _remove_older_files(directory, moment, clean_cloud_info, exts):
    removed_folder_count = 0
    for (full_path, st) in _list_media_files(directory, exts):
        file_moment = datetime.datetime.fromtimestamp(st.st_mtime)
        if file_moment < moment:
            logging.debug('removing file %(path)s...' % {'path': full_path})

            # remove the file itself
            try:
                os.remove(full_path)

            except OSError as e:
                if e.errno == errno.ENOENT:
                    pass  # the file might have been removed in the meantime

                else:
                    logging.error('failed to remove %s: %s' % (full_path, e))

            # remove the parent directories if empty or contain only thumb files
            dir_path = os.path.dirname(full_path)
            if not os.path.exists(dir_path):
                continue

            listing = os.listdir(dir_path)
            thumbs = [l for l in listing if l.endswith('.thumb')]

            if len(listing) == len(thumbs):  # only thumbs
                for p in thumbs:
                    try:
                        os.remove(os.path.join(dir_path, p))

                    except:
                        logging.error('failed to remove %s: %s' % (p, e))

            if not listing or len(listing) == len(thumbs):
                # this will possibly cause following paths that are in the media files for loop
                # to be removed in advance; the os.remove call will raise ENOENT which is silently ignored
                logging.debug('removing empty directory %(path)s...' %
                              {'path': dir_path})
                try:
                    os.removedirs(dir_path)
                    removed_folder_count += 1

                except:
                    logging.error('failed to remove %s: %s' % (dir_path, e))

    if clean_cloud_info and removed_folder_count > 0:
        uploadservices.clean_cloud(directory, {}, clean_cloud_info)
import uploadservices

#assemes camera_id is 1 and service_name is 'gdrive'
uploadservices.clean_cloud(1, 'gdrive', {})