<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def stamp_excerpt(kb_app: kb, sphinx_app: Sphinx, doctree: doctree):
 """ Walk the tree and extract excert into resource.excerpt """


    # First, find out which resource this is. Won't be easy.
    resources = sphinx_app.env.resources
    confdir = sphinx_app.confdir
    source = PurePath(doctree.attributes['source'])

    # Get the relative path inside the docs dir, without .rst, then
    # get the resource
    docname = str(source.relative_to(confdir)).split('.rst')[0]
    resource = resources.get(docname)

    if resource:
        # Stamp the excerpt on the resource
        excerpt = getattr(resource.props, 'excerpt', False)
        auto_excerpt = getattr(resource.props, 'auto_excerpt', False)
        if excerpt:
            resource.excerpt = excerpt
        elif not auto_excerpt:
            resource.excerpt = None
        else:
            # Extract the excerpt based on the number of paragraphs
            # in auto_excerpt
            resource.excerpt = get_rst_excerpt(doctree, auto_excerpt)