11from __future__ import annotations
22
3+ import argparse
4+ import sys
5+ from pathlib import Path
36from typing import (
7+ Dict ,
48 Iterable ,
59 List ,
6- Dict
710)
811
912import nox
13+ import rich .console
14+ import tomlkit
1015from nox import Session
1116
1217from exasol .toolbox .nox ._shared import python_files
1318from noxconfig import PROJECT_CONFIG
1419
15- from pathlib import Path
16- import rich .console
17- import tomlkit
18- import sys
19-
2020
2121def _pylint (session : Session , files : Iterable [str ]) -> None :
2222 session .run (
@@ -74,27 +74,28 @@ def _security_lint(session: Session, files: Iterable[str]) -> None:
7474 )
7575
7676
77+ def _import_lint (session : Session , path : Path ) -> None :
78+ session .run ("poetry" , "run" , "lint-imports" , "--config" , path )
79+
80+
7781class Dependencies :
78- def __init__ (self , illegal : Dict [str , List [str ]] | None ):
82+ def __init__ (self , illegal : dict [str , list [str ]] | None ):
7983 self ._illegal = illegal or {}
8084
8185 @staticmethod
82- def parse (pyproject_toml : str ) -> " Dependencies" :
86+ def parse (pyproject_toml : str ) -> Dependencies :
8387 def _source_filter (version ) -> bool :
84- ILLEGAL_SPECIFIERS = ['url' , 'git' , 'path' ]
85- return any (
86- specifier in version
87- for specifier in ILLEGAL_SPECIFIERS
88- )
88+ ILLEGAL_SPECIFIERS = ["url" , "git" , "path" ]
89+ return any (specifier in version for specifier in ILLEGAL_SPECIFIERS )
8990
90- def find_illegal (part ) -> List [str ]:
91+ def find_illegal (part ) -> list [str ]:
9192 return [
9293 f"{ name } = { version } "
9394 for name , version in part .items ()
9495 if _source_filter (version )
9596 ]
9697
97- illegal : Dict [str , List [str ]] = {}
98+ illegal : dict [str , list [str ]] = {}
9899 toml = tomlkit .loads (pyproject_toml )
99100 poetry = toml .get ("tool" , {}).get ("poetry" , {})
100101
@@ -114,11 +115,11 @@ def find_illegal(part) -> List[str]:
114115 return Dependencies (illegal )
115116
116117 @property
117- def illegal (self ) -> Dict [str , List [str ]]:
118+ def illegal (self ) -> dict [str , list [str ]]:
118119 return self ._illegal
119120
120121
121- def report_illegal (illegal : Dict [str , List [str ]], console : rich .console .Console ):
122+ def report_illegal (illegal : dict [str , list [str ]], console : rich .console .Console ):
122123 count = sum (len (deps ) for deps in illegal .values ())
123124 suffix = "y" if count == 1 else "ies"
124125 console .print (f"{ count } illegal dependenc{ suffix } \n " , style = "red" )
@@ -158,4 +159,35 @@ def dependency_check(session: Session) -> None:
158159 console = rich .console .Console ()
159160 if illegal := dependencies .illegal :
160161 report_illegal (illegal , console )
161- sys .exit (1 )
162+ sys .exit (1 )
163+
164+
165+ @nox .session (name = "lint:import" , python = False )
166+ def import_lint (session : Session ) -> None :
167+ """(experimental) Runs import linter on the project"""
168+ parser = argparse .ArgumentParser (
169+ usage = "nox -s import-lint -- [options]" ,
170+ description = "Runs the import linter on the project" ,
171+ )
172+ parser .add_argument (
173+ "-c" ,
174+ "--config" ,
175+ type = str ,
176+ help = "path to the configuration file for the importlinter" ,
177+ metavar = "TEXT" ,
178+ )
179+
180+ args : argparse .Namespace = parser .parse_args (args = session .posargs )
181+ file : str = args .config
182+ path : Path | None = None
183+ if file is None :
184+ path = getattr (
185+ PROJECT_CONFIG , "import_linter_config" , Path (".import_linter_config" )
186+ )
187+ else :
188+ path = Path (file )
189+ if not path .exists ():
190+ session .error (
191+ "Please make sure you have a configuration file for the importlinter"
192+ )
193+ _import_lint (session = session , path = path )
0 commit comments