summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2020-01-10 08:35:47 -0800
committerGitHub <noreply@github.com>2020-01-10 08:35:47 -0800
commit44f0f8d2575afe110864f02f62ce3e74eb2456ac (patch)
treef9bcdc380b2d0c63efe1366f4ad0a85b199d58aa
parent41a12c2c20bfe8c0c90d4f02f6354436f7c0abed (diff)
parent1662754ac4a5d3bf3ec1059810e828da89ea788a (diff)
downloadisort-44f0f8d2575afe110864f02f62ce3e74eb2456ac.tar.gz
Merge pull request #1096 from timothycrosley/feature/fix-issue-1088
Fix issue #1095
-rw-r--r--isort/main.py65
-rw-r--r--isort/setuptools_commands.py65
-rw-r--r--tests/test_main.py5
3 files changed, 76 insertions, 59 deletions
diff --git a/isort/main.py b/isort/main.py
index 27d01cc1..ca94c4ac 100644
--- a/isort/main.py
+++ b/isort/main.py
@@ -1,7 +1,6 @@
"""Tool for sorting imports alphabetically, and automatically separated into sections."""
import argparse
import functools
-import glob
import os
import re
import stat
@@ -11,12 +10,15 @@ from pprint import pprint
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence
from warnings import warn
-import setuptools
-
from . import SortImports, __version__, sections
from .logo import ASCII_ART
from .profiles import profiles
-from .settings import DEFAULT_CONFIG, SUPPORTED_EXTENSIONS, VALID_PY_TARGETS, Config, WrapModes
+from .settings import SUPPORTED_EXTENSIONS, VALID_PY_TARGETS, Config, WrapModes
+
+try:
+ from .setuptools_commands import ISortCommand
+except ImportError:
+ pass
shebang_re = re.compile(br"^#!.*\bpython[23w]?\b")
QUICK_GUIDE = f"""
@@ -97,61 +99,6 @@ def iter_source_code(paths: Iterable[str], config: Config, skipped: List[str]) -
yield path
-class ISortCommand(setuptools.Command):
- """The :class:`ISortCommand` class is used by setuptools to perform
- imports checks on registered modules.
- """
-
- description = "Run isort on modules registered in setuptools"
- user_options: List[Any] = []
-
- def initialize_options(self) -> None:
- default_settings = vars(DEFAULT_CONFIG).copy()
- for key, value in default_settings.items():
- setattr(self, key, value)
-
- def finalize_options(self) -> None:
- "Get options from config files."
- self.arguments: Dict[str, Any] = {}
- computed_settings = vars(Config(directory=os.getcwd()))
- for key, value in computed_settings.items():
- self.arguments[key] = value
-
- def distribution_files(self) -> Iterator[str]:
- """Find distribution packages."""
- # This is verbatim from flake8
- if self.distribution.packages:
- package_dirs = self.distribution.package_dir or {}
- for package in self.distribution.packages:
- pkg_dir = package
- if package in package_dirs:
- pkg_dir = package_dirs[package]
- elif "" in package_dirs:
- pkg_dir = package_dirs[""] + os.path.sep + pkg_dir
- yield pkg_dir.replace(".", os.path.sep)
-
- if self.distribution.py_modules:
- for filename in self.distribution.py_modules:
- yield "%s.py" % filename
- # Don't miss the setup.py file itself
- yield "setup.py"
-
- def run(self) -> None:
- arguments = self.arguments
- wrong_sorted_files = False
- arguments["check"] = True
- for path in self.distribution_files():
- for python_file in glob.iglob(os.path.join(path, "*.py")):
- try:
- incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
- if incorrectly_sorted:
- wrong_sorted_files = True
- except OSError as error:
- warn(f"Unable to parse file {python_file} due to {error}")
- if wrong_sorted_files:
- sys.exit(1)
-
-
def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
parser = argparse.ArgumentParser(
description="Sort Python import definitions alphabetically "
diff --git a/isort/setuptools_commands.py b/isort/setuptools_commands.py
new file mode 100644
index 00000000..8da5c7f6
--- /dev/null
+++ b/isort/setuptools_commands.py
@@ -0,0 +1,65 @@
+import glob
+import os
+import sys
+from typing import Any, Dict, Iterator, List
+from warnings import warn
+
+import setuptools
+
+from . import SortImports
+from .settings import DEFAULT_CONFIG, Config
+
+
+class ISortCommand(setuptools.Command):
+ """The :class:`ISortCommand` class is used by setuptools to perform
+ imports checks on registered modules.
+ """
+
+ description = "Run isort on modules registered in setuptools"
+ user_options: List[Any] = []
+
+ def initialize_options(self) -> None:
+ default_settings = vars(DEFAULT_CONFIG).copy()
+ for key, value in default_settings.items():
+ setattr(self, key, value)
+
+ def finalize_options(self) -> None:
+ "Get options from config files."
+ self.arguments: Dict[str, Any] = {}
+ computed_settings = vars(Config(directory=os.getcwd()))
+ for key, value in computed_settings.items():
+ self.arguments[key] = value
+
+ def distribution_files(self) -> Iterator[str]:
+ """Find distribution packages."""
+ # This is verbatim from flake8
+ if self.distribution.packages:
+ package_dirs = self.distribution.package_dir or {}
+ for package in self.distribution.packages:
+ pkg_dir = package
+ if package in package_dirs:
+ pkg_dir = package_dirs[package]
+ elif "" in package_dirs:
+ pkg_dir = package_dirs[""] + os.path.sep + pkg_dir
+ yield pkg_dir.replace(".", os.path.sep)
+
+ if self.distribution.py_modules:
+ for filename in self.distribution.py_modules:
+ yield "%s.py" % filename
+ # Don't miss the setup.py file itself
+ yield "setup.py"
+
+ def run(self) -> None:
+ arguments = self.arguments
+ wrong_sorted_files = False
+ arguments["check"] = True
+ for path in self.distribution_files():
+ for python_file in glob.iglob(os.path.join(path, "*.py")):
+ try:
+ incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
+ if incorrectly_sorted:
+ wrong_sorted_files = True
+ except OSError as error:
+ warn(f"Unable to parse file {python_file} due to {error}")
+ if wrong_sorted_files:
+ sys.exit(1)
diff --git a/tests/test_main.py b/tests/test_main.py
index 300108a0..f8447a37 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -19,3 +19,8 @@ def test_is_python_file_fifo(tmpdir):
fifo_file = os.path.join(tmpdir, "fifo_file")
os.mkfifo(fifo_file)
assert not main.is_python_file(fifo_file)
+
+
+def test_isort_command():
+ """Ensure ISortCommand got registered, otherwise setuptools error must have occured"""
+ assert main.ISortCommand