code: def recurse_tree(
      rootpath: str,
      excludes: Sequence[re.Pattern[str]],
      opts: Any,
      user_template_dir: str | None = None,
  ) -> list[str]:
      """
      Look for every file in the directory tree and create the corresponding
      ReST files.
      """
      implicit_namespaces = getattr(opts, "implicit_namespaces", False)
  
      # check if the base directory is a package and get its name
      if is_packagedir(rootpath) or implicit_namespaces:
          root_package = rootpath.split(path.sep)[-1]
      else:
          # otherwise, the base is a directory with packages
          root_package = None
  
      toplevels = []
      for root, subs, files in walk(rootpath, excludes, opts):
          is_pkg = is_packagedir(None, files)
          is_namespace = not is_pkg and implicit_namespaces
          if is_pkg:
              for f in files.copy():
                  if is_initpy(f):
                      files.remove(f)
                      files.insert(0, f)
          elif root != rootpath:
              # only accept non-package at toplevel unless using implicit namespaces
              if not implicit_namespaces:
                  subs.clear()
                  continue
  
          if is_pkg or is_namespace:
              # we are in a package with something to document
              if (
                  subs
                  or len(files) > 1
                  or not is_skipped_package(root, opts)
              ):
                  subpackage = (
                      root[len(rootpath) :]
                      .lstrip(path.sep)
                      .replace(path.sep, ".")
                  )
                  # if this is not a namespace or
                  # a namespace and there is something there to document
                  if not is_namespace or has_child_module(
                      root, excludes, opts
                  ):
                      create_package_file(
                          root,
                          root_package,
                          subpackage,
                          files,
                          opts,
                          subs,
                          is_namespace,
                          excludes,
                          user_template_dir,
                      )
                      toplevels.append(
                          module_join(root_package, subpackage)
                      )
          else:
              # if we are at the root level, we don't require it to be a package
              assert root == rootpath
              assert root_package is None
              for py_file in files:
                  if not is_skipped_module(
                      path.join(rootpath, py_file), opts, excludes
                  ):
                      module = py_file.split(".")[0]
                      create_module_file(
                          root_package, module, opts, user_template_dir
                      )
                      toplevels.append(module)
  
      return toplevels
  
imports: from __future__ import annotations
  import argparse
  import fnmatch
  import glob
  import locale
  import os
  import re
  import sys
  from copy import copy
  from importlib.machinery import EXTENSION_SUFFIXES
  from os import path
  from typing import TYPE_CHECKING, Any
  import sphinx.locale
  from sphinx import __display_version__, package_dir
  from sphinx.cmd.quickstart import EXTENSIONS
  from sphinx.locale import __
  from sphinx.util import logging
  from sphinx.util.osutil import FileAvoidWrite, ensuredir
  from sphinx.util.template import ReSTRenderer
label_desc: Use image node_id 12 for calling a global function with example usage: recurse_tree(rootpath, excludes, opts, user_template_dir) and returns: toplevels