@@ -751,26 +751,46 @@ def hadolint(context):
751751 run_command (context , command )
752752
753753
754- @task
755- def pylint (context ):
754+ @task (
755+ help = {
756+ "target" : "Module or file or directory to inspect, repeatable (default: app package)" ,
757+ "recursive" : "Must be set if target is a directory rather than a module or file name" ,
758+ },
759+ iterable = ["target" ],
760+ )
761+ def pylint (context , target = None , recursive = False ):
756762 """Run pylint code analysis."""
757763 exit_code = 0
758764
759765 base_pylint_command = 'pylint --verbose --init-hook "import nautobot; nautobot.setup()" --rcfile pyproject.toml'
760- command = f"{ base_pylint_command } nautobot_golden_config"
766+ command = base_pylint_command
767+ if recursive :
768+ command += " --recursive=y"
769+ command += f" { ' ' .join (target ) if target else 'nautobot_golden_config' } "
761770 if not run_command (context , command , warn = True ):
762771 exit_code = 1
763772
764773 # run the pylint_django migrations checkers on the migrations directory, if one exists
765- migrations_dir = Path (__file__ ).absolute ().parent / Path ("nautobot_golden_config" ) / Path ("migrations" )
774+ app_dir = Path (__file__ ).absolute ().parent / Path ("nautobot_golden_config" )
775+ migrations_dir = app_dir / Path ("migrations" )
776+ migrations_target_module = "nautobot_golden_config.migrations"
777+ run_migrations_check = target is None
778+ if target is not None :
779+ for target_item in target :
780+ target_item_normalized = Path (target_item ).resolve ()
781+ if target_item_normalized in (app_dir , migrations_dir ) or target_item == migrations_target_module :
782+ run_migrations_check = True
783+ break
784+
766785 if migrations_dir .is_dir ():
767- migrations_pylint_command = (
768- f"{ base_pylint_command } --load-plugins=pylint_django.checkers.migrations"
769- " --disable=all --enable=fatal,new-db-field-with-default,missing-backwards-migration-callable"
770- " nautobot_golden_config.migrations"
771- )
772- if not run_command (context , migrations_pylint_command , warn = True ):
773- exit_code = 1
786+ if run_migrations_check :
787+ migrations_pylint_command = (
788+ f"{ base_pylint_command } --load-plugins=pylint_django.checkers.migrations"
789+ " --disable=all --enable=fatal,new-db-field-with-default,missing-backwards-migration-callable"
790+ " nautobot_golden_config.migrations"
791+ )
792+ if not run_command (context , migrations_pylint_command , warn = True ):
793+ exit_code = 1
774794 else :
775795 print ("No migrations directory found, skipping migrations checks." )
776796
@@ -790,11 +810,12 @@ def autoformat(context):
790810 "action" : "Available values are `['lint', 'format']`. Can be used multiple times. (default: `--action lint --action format`)" ,
791811 "target" : "File or directory to inspect, repeatable (default: all files in the project will be inspected)" ,
792812 "fix" : "Automatically fix selected actions. May not be able to fix all issues found. (default: False)" ,
813+ "diff" : "Show diffs of changes. (default: False)" ,
793814 "output_format" : "See https://docs.astral.sh/ruff/settings/#output-format for details. (default: `concise`)" ,
794815 },
795816 iterable = ["action" , "target" ],
796817)
797- def ruff (context , action = None , target = None , fix = False , output_format = "concise" ):
818+ def ruff (context , action = None , target = None , fix = False , diff = False , output_format = "concise" ): # noqa: PLR0913
798819 """Run ruff to perform code formatting and/or linting."""
799820 if not action :
800821 action = ["lint" , "format" ]
@@ -807,6 +828,8 @@ def ruff(context, action=None, target=None, fix=False, output_format="concise"):
807828 command = "ruff format "
808829 if not fix :
809830 command += "--check "
831+ if diff :
832+ command += "--diff "
810833 command += " " .join (target )
811834 if not run_command (context , command , warn = True ):
812835 exit_code = 1
@@ -815,6 +838,8 @@ def ruff(context, action=None, target=None, fix=False, output_format="concise"):
815838 command = "ruff check "
816839 if fix :
817840 command += "--fix "
841+ elif diff :
842+ command += "--diff "
818843 command += f"--output-format { output_format } "
819844 command += " " .join (target )
820845 if not run_command (context , command , warn = True ):
0 commit comments