Ejemplo n.º 1
0
        def function():
            hasher = hashlib.new("md5")

            args_hasher = CodeHasher("md5", hasher)
            args_hasher.update([argc, argv])
            LOGGER.debug("Hashing arguments to %s of %i bytes.", name,
                         args_hasher.size)

            args_digest_before = args_hasher.digest()

            code_hasher = CodeHasher("md5", hasher)
            code_hasher.update(func)
            LOGGER.debug("Hashing function %s in %i bytes.", name,
                         code_hasher.size)

            key = hasher.hexdigest()
            LOGGER.debug("Cache key: %s", key)

            caller_frame = inspect.currentframe().f_back
            try:
                return_value, args_mutated = _read_from_cache(
                    key, persist, ignore_hash, func, caller_frame)
            except (CacheKeyNotFoundError, CachedObjectWasMutatedError):
                return_value = func(*argc, **argv)

                args_hasher_after = CodeHasher("md5")
                args_hasher_after.update([argc, argv])
                args_mutated = args_digest_before != args_hasher_after.digest()

                _write_to_cache(key, return_value, persist, ignore_hash,
                                args_mutated)

            if args_mutated:
                st.warning(_build_args_mutated_message(func))
            return return_value
Ejemplo n.º 2
0
    def wrapped_func(*argc, **argv):
        """This function wrapper will only call the underlying function in
        the case of a cache miss. Cached objects are stored in the cache/
        directory."""
        if not config.get_option('client.caching'):
            LOGGER.debug('Purposefully skipping cache')
            return func(*argc, **argv)

        name = func.__name__

        if len(argc) == 0 and len(argv) == 0:
            message = 'Running %s().' % name
        else:
            message = 'Running %s(...).' % name
        with st.spinner(message):
            hasher = hashlib.new('md5')

            args_hasher = CodeHasher('md5', hasher)
            args_hasher.update([argc, argv])
            LOGGER.debug('Hashing arguments to %s of %i bytes.',
                         name, args_hasher.size)

            args_digest_before = args_hasher.digest()

            code_hasher = CodeHasher('md5', hasher)
            code_hasher.update(func)
            LOGGER.debug('Hashing function %s in %i bytes.',
                         name, code_hasher.size)

            key = hasher.hexdigest()
            LOGGER.debug('Cache key: %s', key)

            caller_frame = inspect.currentframe().f_back
            try:
                return_value, args_mutated = _read_from_cache(
                    key, persist, ignore_hash, func, caller_frame)
            except (CacheKeyNotFoundError, CachedObjectWasMutatedError):
                return_value = func(*argc, **argv)

                args_hasher_after = CodeHasher('md5')
                args_hasher_after.update([argc, argv])
                args_mutated = args_digest_before != args_hasher_after.digest()

                _write_to_cache(
                    key, return_value, persist, ignore_hash, args_mutated)

            if args_mutated:
                st.warning(_build_args_mutated_message(func))

        return return_value
Ejemplo n.º 3
0
        def get_or_set_cache():
            hasher = hashlib.new("md5")

            args_hasher = CodeHasher("md5", hasher)
            args_hasher.update([args, kwargs])
            LOGGER.debug("Hashing arguments to %s of %i bytes.", name,
                         args_hasher.size)

            args_digest_before = args_hasher.digest()

            code_hasher = CodeHasher("md5", hasher)
            code_hasher.update(func)
            LOGGER.debug("Hashing function %s in %i bytes.", name,
                         code_hasher.size)

            key = hasher.hexdigest()
            LOGGER.debug("Cache key: %s", key)

            caller_frame = inspect.currentframe().f_back
            try:
                return_value, args_mutated = _read_from_cache(
                    key, persist, allow_output_mutation, func, caller_frame)
            except (CacheKeyNotFoundError, CachedObjectWasMutatedError):
                with _calling_cached_function():
                    if suppress_st_warning:
                        with suppress_cached_st_function_warning():
                            return_value = func(*args, **kwargs)
                    else:
                        return_value = func(*args, **kwargs)

                args_hasher_after = CodeHasher("md5")
                args_hasher_after.update([args, kwargs])
                args_mutated = args_digest_before != args_hasher_after.digest()

                _write_to_cache(key, return_value, persist,
                                allow_output_mutation, args_mutated)

            if args_mutated:
                # If we're inside a _nested_ cached function, our
                # _within_cached_function_counter will be non-zero.
                # Suppress the warning about this.
                with suppress_cached_st_function_warning():
                    st.warning(_build_args_mutated_message(func))
            return return_value
Ejemplo n.º 4
0
def get_hash(f, context=None):
    hasher = CodeHasher("md5")
    hasher._get_main_script_directory = MagicMock()
    hasher._get_main_script_directory.return_value = os.getcwd()
    hasher.update(f, context)
    return hasher.digest()