1import glob
2import os
3import sys
4from typing import Any, Dict, Iterator, List
5from warnings import warn
6
7import setuptools  # type: ignore
8
9from . import api
10from .settings import DEFAULT_CONFIG
11
12
13class ISortCommand(setuptools.Command):  # type: ignore
14    """The :class:`ISortCommand` class is used by setuptools to perform
15    imports checks on registered modules.
16    """
17
18    description = "Run isort on modules registered in setuptools"
19    user_options: List[Any] = []
20
21    def initialize_options(self) -> None:
22        default_settings = vars(DEFAULT_CONFIG).copy()
23        for key, value in default_settings.items():
24            setattr(self, key, value)
25
26    def finalize_options(self) -> None:
27        """Get options from config files."""
28        self.arguments: Dict[str, Any] = {}  # skipcq: PYL-W0201
29        self.arguments["settings_path"] = os.getcwd()
30
31    def distribution_files(self) -> Iterator[str]:
32        """Find distribution packages."""
33        # This is verbatim from flake8
34        if self.distribution.packages:  # pragma: no cover
35            package_dirs = self.distribution.package_dir or {}
36            for package in self.distribution.packages:
37                pkg_dir = package
38                if package in package_dirs:
39                    pkg_dir = package_dirs[package]
40                elif "" in package_dirs:  # pragma: no cover
41                    pkg_dir = package_dirs[""] + os.path.sep + pkg_dir
42                yield pkg_dir.replace(".", os.path.sep)
43
44        if self.distribution.py_modules:
45            for filename in self.distribution.py_modules:
46                yield "%s.py" % filename
47        # Don't miss the setup.py file itself
48        yield "setup.py"
49
50    def run(self) -> None:
51        arguments = self.arguments
52        wrong_sorted_files = False
53        for path in self.distribution_files():
54            for python_file in glob.iglob(os.path.join(path, "*.py")):
55                try:
56                    if not api.check_file(python_file, **arguments):
57                        wrong_sorted_files = True  # pragma: no cover
58                except OSError as error:  # pragma: no cover
59                    warn(f"Unable to parse file {python_file} due to {error}")
60        if wrong_sorted_files:
61            sys.exit(1)  # pragma: no cover
62