__init__.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (c) 2006-2010, 2012-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2008 pyves@crater.logilab.fr <pyves@crater.logilab.fr>
  3. # Copyright (c) 2013 Google, Inc.
  4. # Copyright (c) 2013 John McGehee <jmcgehee@altera.com>
  5. # Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com>
  6. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  7. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  8. # Copyright (c) 2015 Aru Sahni <arusahni@gmail.com>
  9. # Copyright (c) 2015 John Kirkham <jakirkham@gmail.com>
  10. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  11. # Copyright (c) 2016 Erik <erik.eriksson@yahoo.com>
  12. # Copyright (c) 2016 Alexander Todorov <atodorov@otb.bg>
  13. # Copyright (c) 2016 Moises Lopez <moylop260@vauxoo.com>
  14. # Copyright (c) 2017, 2020 hippo91 <guillaume.peillex@gmail.com>
  15. # Copyright (c) 2017-2019 Ville Skyttä <ville.skytta@iki.fi>
  16. # Copyright (c) 2017 ahirnish <ahirnish@gmail.com>
  17. # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com>
  18. # Copyright (c) 2018, 2020 Anthony Sottile <asottile@umich.edu>
  19. # Copyright (c) 2018 Jim Robertson <jrobertson98atx@gmail.com>
  20. # Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com>
  21. # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  22. # Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>
  23. # Copyright (c) 2018 Gary Tyler McLeod <mail@garytyler.com>
  24. # Copyright (c) 2018 Konstantin <Github@pheanex.de>
  25. # Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
  26. # Copyright (c) 2019-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  27. # Copyright (c) 2019, 2021 Ashley Whetter <ashley@awhetter.co.uk>
  28. # Copyright (c) 2019 Janne Rönkkö <jannero@users.noreply.github.com>
  29. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  30. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  31. # Copyright (c) 2021 Eisuke Kawashima <e-kwsm@users.noreply.github.com>
  32. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  33. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  34. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  35. import os
  36. import pathlib
  37. import pickle
  38. import sys
  39. from datetime import datetime
  40. from pylint.config.configuration_mixin import ConfigurationMixIn
  41. from pylint.config.find_default_config_files import (
  42. find_default_config_files,
  43. find_pylintrc,
  44. )
  45. from pylint.config.man_help_formatter import _ManHelpFormatter
  46. from pylint.config.option import Option
  47. from pylint.config.option_manager_mixin import OptionsManagerMixIn
  48. from pylint.config.option_parser import OptionParser
  49. from pylint.config.options_provider_mixin import OptionsProviderMixIn, UnsupportedAction
  50. from pylint.constants import DEFAULT_PYLINT_HOME, OLD_DEFAULT_PYLINT_HOME
  51. from pylint.utils import LinterStats
  52. __all__ = [
  53. "ConfigurationMixIn",
  54. "find_default_config_files",
  55. "_ManHelpFormatter",
  56. "Option",
  57. "OptionsManagerMixIn",
  58. "OptionParser",
  59. "OptionsProviderMixIn",
  60. "UnsupportedAction",
  61. ]
  62. USER_HOME = os.path.expanduser("~")
  63. if "PYLINTHOME" in os.environ:
  64. PYLINT_HOME = os.environ["PYLINTHOME"]
  65. if USER_HOME == "~":
  66. USER_HOME = os.path.dirname(PYLINT_HOME)
  67. elif USER_HOME == "~":
  68. PYLINT_HOME = OLD_DEFAULT_PYLINT_HOME
  69. else:
  70. PYLINT_HOME = DEFAULT_PYLINT_HOME
  71. # The spam prevention is due to pylint being used in parallel by
  72. # pre-commit, and the message being spammy in this context
  73. # Also if you work with old version of pylint that recreate the
  74. # old pylint home, you can get the old message for a long time.
  75. prefix_spam_prevention = "pylint_warned_about_old_cache_already"
  76. spam_prevention_file = os.path.join(
  77. PYLINT_HOME,
  78. datetime.now().strftime(prefix_spam_prevention + "_%Y-%m-%d.temp"),
  79. )
  80. old_home = os.path.join(USER_HOME, OLD_DEFAULT_PYLINT_HOME)
  81. if os.path.exists(old_home) and not os.path.exists(spam_prevention_file):
  82. print(
  83. f"PYLINTHOME is now '{PYLINT_HOME}' but obsolescent '{old_home}' is found; "
  84. "you can safely remove the latter",
  85. file=sys.stderr,
  86. )
  87. # Remove old spam prevention file
  88. if os.path.exists(PYLINT_HOME):
  89. for filename in os.listdir(PYLINT_HOME):
  90. if prefix_spam_prevention in filename:
  91. try:
  92. os.remove(os.path.join(PYLINT_HOME, filename))
  93. except OSError:
  94. pass
  95. # Create spam prevention file for today
  96. try:
  97. pathlib.Path(PYLINT_HOME).mkdir(parents=True, exist_ok=True)
  98. with open(spam_prevention_file, "w", encoding="utf8") as f:
  99. f.write("")
  100. except Exception: # pylint: disable=broad-except
  101. # Can't write in PYLINT_HOME ?
  102. print(
  103. "Can't write the file that was supposed to "
  104. f"prevent pylint.d deprecation spam in {PYLINT_HOME}."
  105. )
  106. def _get_pdata_path(base_name, recurs):
  107. base_name = base_name.replace(os.sep, "_")
  108. return os.path.join(PYLINT_HOME, f"{base_name}{recurs}.stats")
  109. def load_results(base):
  110. data_file = _get_pdata_path(base, 1)
  111. try:
  112. with open(data_file, "rb") as stream:
  113. data = pickle.load(stream)
  114. if not isinstance(data, LinterStats):
  115. raise TypeError
  116. return data
  117. except Exception: # pylint: disable=broad-except
  118. return None
  119. def save_results(results, base):
  120. if not os.path.exists(PYLINT_HOME):
  121. try:
  122. os.makedirs(PYLINT_HOME)
  123. except OSError:
  124. print(f"Unable to create directory {PYLINT_HOME}", file=sys.stderr)
  125. data_file = _get_pdata_path(base, 1)
  126. try:
  127. with open(data_file, "wb") as stream:
  128. pickle.dump(results, stream)
  129. except OSError as ex:
  130. print(f"Unable to create file {data_file}: {ex}", file=sys.stderr)
  131. PYLINTRC = find_pylintrc()