lint-python-dead-code.py raw

   1  #!/usr/bin/env python3
   2  #
   3  # Copyright (c) 2022 The Limenka developers
   4  # Distributed under the MIT software license, see the accompanying
   5  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   6  
   7  """
   8  Find dead Python code.
   9  """
  10  
  11  from subprocess import check_output, STDOUT, CalledProcessError
  12  
  13  FILES_ARGS = ['git', 'ls-files', '--', '*.py']
  14  
  15  
  16  def check_vulture_install():
  17      try:
  18          check_output(["vulture", "--version"])
  19      except FileNotFoundError:
  20          print("Skipping Python dead code linting since vulture is not installed. Install by running \"pip3 install vulture\"")
  21          exit(0)
  22  
  23  
  24  def main():
  25      check_vulture_install()
  26  
  27      files = check_output(FILES_ARGS).decode("utf-8").splitlines()
  28      # --min-confidence 100 will only report code that is guaranteed to be unused within the analyzed files.
  29      # Any value below 100 introduces the risk of false positives, which would create an unacceptable maintenance burden.
  30      vulture_args = ['vulture', '--min-confidence=100'] + files
  31  
  32      try:
  33          check_output(vulture_args, stderr=STDOUT)
  34      except CalledProcessError as e:
  35          print(e.output.decode("utf-8"), end="")
  36          print("Python dead code detection found some issues")
  37  
  38  
  39  if __name__ == "__main__":
  40      main()
  41