コード例 #1
0
ファイル: utils.py プロジェクト: tanmoydeb07/text-sherlock
def safe_read_file(path, ignore_errors=settings.IGNORE_INDEXER_ERRORS,
                   encoding='utf-8'):
    """Returns the contents of the file at the specified path. Ignores any
    errors that may occur."""
    try:
        contents = read_file(path, encoding=encoding)
        return contents
    except Exception, e:
        log.error('Skipped file: %s' % path)
        if not ignore_errors:
            raise e
コード例 #2
0
ファイル: utils.py プロジェクト: pombredanne/text-sherlock
def safe_read_file(path, ignore_errors=settings.IGNORE_INDEXER_ERRORS, encoding='utf-8'):
    """Returns the contents of the file at the specified path. Ignores any
    errors that may occur
    """
    try:
        contents = read_file(path, encoding=encoding)
    except Exception, e:
        log.error('Skipped file: %s' % path)
        if not ignore_errors:
            raise e
        return None
コード例 #3
0
def is_file_updated(filepath, check_file_exists=False, update_db=False):
    """Determines if the target filepath is new or updated. Attempts to find the
    record by file path then compares the file stats.
    :return: tuple has_changed, db_record
    """
    has_file_changed = False
    record = None

    if check_file_exists:
        if not os.path.isfile(filepath):
            return has_file_changed, record

    # get file info
    try:
        file_stats = os.stat(filepath)
    except OSError:
        # file may not exist
        return has_file_changed, record
    last_mod = time.localtime(file_stats[stat.ST_MTIME])
    last_mod_dt = datetime(*last_mod[:6]) # time_struct -> datetime

    # get db record
    query = IndexerMeta.select().where(IndexerMeta.path == filepath)
    if query.exists():
        # get the one record
        record = [q for q in query][0]
        # compare mod dates
        if last_mod_dt > record.mod_date:
            # file on disk has changes after db record changed
            has_file_changed = True
        if update_db:
            record.mod_date = last_mod_dt
            record.save()
    else:
        if update_db:
            try:
                record = IndexerMeta.create(
                    path=filepath,
                    mod_date=last_mod_dt,
                    date_added=datetime.now()
                )
                return True, record
            except sqlite3.IntegrityError as e:
                # column path may not be unique
                logger.error('%s - filepath: %s' % (e, filepath))
    return has_file_changed, record