Skip to content

mime api-view-parser namespace parsing in azure-sdk-tools #41941

Closed
@scbedd

Description

@scbedd

Within tools/azure-sdk-tools/ci_tools/parsing/parse_functions.py there is a function parse_pyproject. When we calculate the name_space for the package, we simply get the name and replace - with ..

I know this is incorrect, as packages like azure-eventhub-checkpointstoreblob doesn't cleave to this. This package has:

PACKAGE_NAME = "azure-eventhub-checkpointstoreblob" and namespace_name = "azure.eventhub.extensions.checkpointstoreblob"

So that means we need to follow the __init__ ourselves to make this correct.

@swathipil wrote up a namespace discovery for a wheel package.

INIT_PY_FILE = "__init__.py"
INIT_EXTENSION_SUBSTRING = ".extend_path(__path__, __name__)"

# this is snipped out of a class definition, hence self reference which can be ignored

    def _find_modules(self, pkg_root_path):
        """Find modules within the package to import and parse
        :param str: pkg_root_path
            Package root path
        :rtype: list
        """
        modules = []
        for root, subdirs, files in os.walk(pkg_root_path):
            # Ignore any modules with name starts with "_"
            # For e.g. _generated, _shared etc
            # Ignore build, which is created when installing a package from source.
            # Ignore tests, which may have an __init__.py but is not part of the package.
            dirs_to_skip = [x for x in subdirs if x.startswith(("_", ".", "test", "build"))]
            for d in dirs_to_skip:
                logging.debug("Dirs to skip: {}".format(dirs_to_skip))
                subdirs.remove(d)
            if INIT_PY_FILE in files:
                module_name = os.path.relpath(root, pkg_root_path).replace(
                    os.path.sep, "."
                )
                # If namespace has not been set yet, try to find the first __init__.py that's not purely for extension.
                if not self.namespace:
                    self._set_root_namespace(
                        os.path.join(root, INIT_PY_FILE), module_name
                    )

                modules.append(module_name)
                # Add any public py file names as modules
                sub_modules = [
                    os.path.splitext(os.path.basename(f))[0]
                    for f in files
                    if f.endswith(".py") and not os.path.basename(f).startswith("_")
                ]
                modules.extend(["{0}.{1}".format(module_name, x) for x in sub_modules])

        logging.debug("Modules in package: {}".format(modules))
        return sorted(modules)

    def _set_root_namespace(self, init_file_path, module_name):
        with open(init_file_path, "r") as f:
            in_docstring = False
            content = []
            for line in f:
                stripped_line = line.strip()
                # If in multi-line docstring, skip following lines until end of docstring.
                # If single-line docstring, skip the docstring line.
                if stripped_line.startswith(('"""', "'''")) and not stripped_line.endswith(('"""', "'''")):
                    in_docstring = not in_docstring
                # If comment, skip line. Otherwise, add to content.
                if not in_docstring and not stripped_line.startswith("#"):
                    content.append(line)
            if len(content) > 1 or (
                len(content) == 1 and not INIT_EXTENSION_SUBSTRING in content[0]
            ):
                self.namespace = module_name

This is the exact logic that we need to use in place of name_space = name.replace('-', '.')

We should add a test to sdk/eventhub/azure-eventhub-checkpointstoreblob/ (deliberately leaving it unset whether this is a setup.py or pyproject.toml) and verifies that the name_space is azure.eventhub.extensions.checkpointstoreblob.

Metadata

Metadata

Assignees

Labels

EngSysThis issue is impacting the engineering system.

Type

No type

Projects

Status

🎊 Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions