pax_global_header00006660000000000000000000000064134755501670014527gustar00rootroot0000000000000052 comment=283ecc4258ad8dd5da48856825ce68a849c030ea salt-pylint-2019.6.7/000077500000000000000000000000001347555016700142555ustar00rootroot00000000000000salt-pylint-2019.6.7/.gitignore000066400000000000000000000012431347555016700162450ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .cache nosetests.xml coverage.xml # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ salt-pylint-2019.6.7/.pylintrc000066400000000000000000000311501347555016700161220ustar00rootroot00000000000000[MASTER] # Specify a configuration file. #rcfile= # Python code to execute, usually for sys.path manipulation such as # pygtk.require(). #init-hook= # Profiled execution. profile=no # Add files or directories to the blacklist. They should be base names, not # paths. ignore=CVS # Pickle collected data for later comparisons. persistent=yes # List of plugins (as comma separated values of python modules names) to load, # usually to register additional checkers. load-plugins=saltpylint.pep8, saltpylint.pep263, saltpylint.strings, saltpylint.fileperms, saltpylint.py3modernize, saltpylint.smartup, saltpylint.minpyver # Use multiple processes to speed up Pylint. jobs=1 # Allow loading of arbitrary C extensions. Extensions are imported into the # active Python interpreter and may run arbitrary code. unsafe-load-any-extension=no # A comma-separated list of package or module names from where C extensions may # be loaded. Extensions are loading into the active Python interpreter and may # run arbitrary code extension-pkg-whitelist= # Allow optimization of some AST trees. This will activate a peephole AST # optimizer, which will apply various small optimizations. For instance, it can # be used to obtain the result of joining multiple strings with the addition # operator. Joining a lot of strings can lead to a maximum recursion error in # Pylint and this flag can prevent that. It has one side effect, the resulting # AST will be different than the one from reality. optimize-ast=no # Fileperms Lint Plugin Settings fileperms-default = 0644 fileperms-ignore-paths = setup.py # Py3 Modernize PyLint Plugin Settings modernize-nofix = libmodernize.fixes.fix_dict_six # Minimum Python Version To Enforce minimum-python-version = 3.4 [REPORTS] # Set the output format. Available formats are text, parseable, colorized, msvs # (visual studio) and html. You can also give a reporter class, eg # mypackage.mymodule.MyReporterClass. output-format=text # Put messages in a separate file for each module / package specified on the # command line instead of printing them on stdout. Reports (if any) will be # written in a file name "pylint_global.[txt|html]". files-output=no # Tells whether to display a full report or only the messages reports=yes # Python expression which should return a note less than 10 (10 is the highest # note). You have access to the variables errors warning, statement which # respectively contain the number of errors / warnings messages and the total # number of statements analyzed. This is used by the global evaluation report # (RP0004). evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) # Add a comment according to your evaluation note. This is used by the global # evaluation report (RP0004). comment=no # Template used to display messages. This is a python new-style format string # used to format the message information. See doc for all details #msg-template= [MESSAGES CONTROL] # Only show warnings with the listed confidence levels. Leave empty to show # all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED confidence= # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option # multiple time. See also the "--disable" option for examples. #enable= # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this # option multiple times (only on the command line, not in the configuration # file where it should appear only once).You can also use "--disable=all" to # disable everything first and then reenable specific checks. For example, if # you want to run only the similarities checker, you can use "--disable=all # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" #disable= disable=W0142, C0330, I0011, I0012, W1202, E8402, E8731 # E8121, # E8122, # E8123, # E8124, # E8125, # E8126, # E8127, # E8128 # Disabled Checks # # W0142 (star-args) # W1202 (logging-format-interpolation) Use % formatting in logging functions but pass the % parameters as arguments # E812* All PEP8 E12* # E8402 module level import not at top of file # E8501 PEP8 line too long # E8731 do not assign a lambda expression, use a def # C0330 (bad-continuation) # I0011 (locally-disabling) # I0012 (locally-enabling) # W1202 (logging-format-interpolation) [FORMAT] # Maximum number of characters on a single line. max-line-length=120 # Regexp for a line that is allowed to be longer than the limit. ignore-long-lines=^\s*(# )??$ # Allow the body of an if to be on the same line as the test if there is no # else. single-line-if-stmt=no # List of optional constructs for which whitespace checking is disabled no-space-check=trailing-comma,dict-separator # Maximum number of lines in a module max-module-lines=1000 # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 # tab). indent-string=' ' # Number of spaces of indent required inside a hanging or continued line. indent-after-paren=4 # Expected format of line ending, e.g. empty (any line ending), LF or CRLF. expected-line-ending-format=LF [LOGGING] # Logging modules to check that the string format arguments are in logging # function parameter format logging-modules=logging [TYPECHECK] # Tells whether missing members accessed in mixin class should be ignored. A # mixin class is detected if its name ends with "mixin" (case insensitive). ignore-mixin-members=yes # List of module names for which member attributes should not be checked # (useful for modules/projects where namespaces are manipulated during runtime # and thus existing member attributes cannot be deduced by static analysis ignored-modules= # List of classes names for which member attributes should not be checked # (useful for classes with attributes dynamically set). ignored-classes=SQLObject # When zope mode is activated, add a predefined set of Zope acquired attributes # to generated-members. zope=no # List of members which are set dynamically and missed by pylint inference # system, and so shouldn't trigger E0201 when accessed. Python regular # expressions are accepted. generated-members=REQUEST,acl_users,aq_parent [MISCELLANEOUS] # List of note tags to take in consideration, separated by a comma. notes=FIXME,XXX,TODO [SIMILARITIES] # Minimum lines number of a similarity. min-similarity-lines=4 # Ignore comments when computing similarities. ignore-comments=yes # Ignore docstrings when computing similarities. ignore-docstrings=yes # Ignore imports when computing similarities. ignore-imports=no [VARIABLES] # Tells whether we should check for unused import in __init__ files. init-import=no # A regular expression matching the name of dummy variables (i.e. expectedly # not used). dummy-variables-rgx=_$|dummy # List of additional names supposed to be defined in builtins. Remember that # you should avoid to define new builtins when possible. additional-builtins=__opts__ # List of strings which can identify a callback function by name. A callback # name must start or end with one of those strings. callbacks=cb_,_cb [SPELLING] # Spelling dictionary name. Available dictionaries: none. To make it working # install python-enchant package. spelling-dict= # List of comma separated words that should not be checked. spelling-ignore-words= # A path to a file that contains private dictionary; one word per line. spelling-private-dict-file= # Tells whether to store unknown words to indicated private dictionary in # --spelling-private-dict-file option instead of raising a message. spelling-store-unknown-words=no [BASIC] # Required attributes for module, separated by a comma # DEPRECATED in PyLint 2.0 #required-attributes= # List of builtins function names that should not be used, separated by a comma bad-functions=map,filter # Good variable names which should always be accepted, separated by a comma good-names=i,j,k,ex,Run,_,log,db # Bad variable names which should always be refused, separated by a comma bad-names=foo,bar,baz,toto,tutu,tata # Colon-delimited sets of names that determine each other's naming style when # the name regexes allow several styles. name-group= # Include a hint for the correct naming format with invalid-name include-naming-hint=no # Regular expression matching correct attribute names attr-rgx=[a-z_][a-z0-9_]{2,30}$ # Naming hint for attribute names attr-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct variable names variable-rgx=[a-z_][a-z0-9_]{2,30}$ # Naming hint for variable names variable-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct function names function-rgx=[a-z_][a-z0-9_]{2,30}$ # Naming hint for function names function-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct constant names const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ # Naming hint for constant names const-name-hint=(([A-Z_][A-Z0-9_]*)|(__.*__))$ # Regular expression matching correct module names module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ # Naming hint for module names module-name-hint=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ # Regular expression matching correct argument names argument-rgx=[a-z_][a-z0-9_]{2,30}$ # Naming hint for argument names argument-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct inline iteration names inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$ # Naming hint for inline iteration names inlinevar-name-hint=[A-Za-z_][A-Za-z0-9_]*$ # Regular expression matching correct class attribute names class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ # Naming hint for class attribute names class-attribute-name-hint=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ # Regular expression matching correct method names method-rgx=[a-z_][a-z0-9_]{2,30}$ # Naming hint for method names method-name-hint=[a-z_][a-z0-9_]{2,30}$ # Regular expression matching correct class names class-rgx=[A-Z_][a-zA-Z0-9]+$ # Naming hint for class names class-name-hint=[A-Z_][a-zA-Z0-9]+$ # Regular expression which should only match function or class names that do # not require a docstring. no-docstring-rgx=__.*__ # Minimum line length for functions/classes that require docstrings, shorter # ones are exempt. docstring-min-length=-1 [CLASSES] # List of interface methods to ignore, separated by a comma. This is used for # instance to not check methods defines in Zope's Interface base class. # DEPRECATED in PyLint 2.0 #ignore-iface-methods=isImplementedBy,deferred,extends,names,namesAndDescriptions,queryDescriptionFor,getBases,getDescriptionFor,getDoc,getName,getTaggedValue,getTaggedValueTags,isEqualOrExtendedBy,setTaggedValue,isImplementedByInstancesOf,adaptWith,is_implemented_by # List of method names used to declare (i.e. assign) instance attributes. defining-attr-methods=__init__,__new__,setUp # List of valid names for the first argument in a class method. valid-classmethod-first-arg=cls # List of valid names for the first argument in a metaclass class method. valid-metaclass-classmethod-first-arg=mcs # List of member names, which should be excluded from the protected access # warning. exclude-protected=_asdict,_fields,_replace,_source,_make [IMPORTS] # Deprecated modules which should not be used, separated by a comma deprecated-modules=stringprep,optparse # Create a graph of every (i.e. internal and external) dependencies in the # given file (report RP0402 must not be disabled) import-graph= # Create a graph of external dependencies in the given file (report RP0402 must # not be disabled) ext-import-graph= # Create a graph of internal dependencies in the given file (report RP0402 must # not be disabled) int-import-graph= [DESIGN] # Maximum number of arguments for function / method max-args=5 # Argument names that match this expression will be ignored. Default to name # with leading underscore ignored-argument-names=_.* # Maximum number of locals for function / method body max-locals=15 # Maximum number of return / yield for function / method body max-returns=6 # Maximum number of branch for function / method body max-branches=12 # Maximum number of statements in function / method body max-statements=50 # Maximum number of parents for a class (see R0901). max-parents=7 # Maximum number of attributes for a class (see R0902). max-attributes=7 # Minimum number of public methods for a class (see R0903). min-public-methods=2 # Maximum number of public methods for a class (see R0904). max-public-methods=20 [EXCEPTIONS] # Exceptions that will emit a warning when being caught. Defaults to # "Exception" overgeneral-exceptions=Exception salt-pylint-2019.6.7/.testing.pylintrc000066400000000000000000000100671347555016700176020ustar00rootroot00000000000000[MASTER] # Specify a configuration file. rcfile=.pylintrc # Pickle collected data for later comparisons. persistent=no # List of plugins (as comma separated values of python modules names) to load, # usually to register additional checkers. load-plugins=saltpylint.pep8, saltpylint.pep263, saltpylint.strings, saltpylint.fileperms, saltpylint.py3modernize, saltpylint.smartup, saltpylint.minpyver # Fileperms Lint Plugin Settings fileperms-default = 0644 fileperms-ignore-paths = setup.py # Py3 Modernize PyLint Plugin Settings modernize-nofix = libmodernize.fixes.fix_dict_six # Minimum Python Version To Enforce minimum-python-version = 3.4 [MESSAGES CONTROL] # Only show warnings with the listed confidence levels. Leave empty to show # all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED confidence= # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option # multiple time. See also the "--disable" option for examples. #enable= # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this # option multiple times (only on the command line, not in the configuration # file where it should appear only once).You can also use "--disable=all" to # disable everything first and then reenable specific checks. For example, if # you want to run only the similarities checker, you can use "--disable=all # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" #disable= disable=R, I0011, I0012, I0013, E1101, E1103, C0102, C0103, C0111, C0203, C0204, C0301, C0302, C0330, W0110, W0122, W0142, W0201, W0212, W0404, W0511, W0603, W0612, W0613, W0621, W0622, W0631, W0704, W1202, W1307, F0220, F0401, E8501, E8116, E8121, E8122, E8123, E8124, E8125, E8126, E8127, E8128, E8129, E8131, E8265, E8266, E8402, E8731 # Disabled: # R* [refactoring suggestions & reports] # I0011 (locally-disabling) # I0012 (locally-enabling) # I0013 (file-ignored) # E1101 (no-member) [pylint isn't smart enough] # E1103 (maybe-no-member) # C0102 (blacklisted-name) [because it activates C0103 too] # C0103 (invalid-name) # C0111 (missing-docstring) # C0203 (bad-mcs-method-argument) # C0204 (bad-mcs-classmethod-argument) # C0301 (line-too-long) # C0302 (too-many-lines) # C0330 (bad-continuation) # W0110 (deprecated-lambda) # W0122 (exec-statement) # W0142 (star-args) # W0201 (attribute-defined-outside-init) [done in several places in the codebase] # W0212 (protected-access) # W0404 (reimported) [done intentionally for legit reasons] # W0511 (fixme) [several outstanding instances currently in the codebase] # W0603 (global-statement) # W0612 (unused-variable) [unused return values] # W0613 (unused-argument) # W0621 (redefined-outer-name) # W0622 (redefined-builtin) [many parameter names shadow builtins] # W0631 (undefined-loop-variable) [~3 instances, seem to be okay] # W0704 (pointless-except) [misnomer; "ignores the exception" rather than "pointless"] # F0220 (unresolved-interface) # F0401 (import-error) # W1202 (logging-format-interpolation) Use % formatting in logging functions but pass the % parameters as arguments # W1307 (invalid-format-index) Using invalid lookup key '%s' in format specifier "0['%s']" # # E8116 PEP8 E116: unexpected indentation (comment) # E812* All PEP8 E12* # E8265 PEP8 E265 - block comment should start with "# " # E8266 PEP8 E266 - too many leading '#' for block comment # E8501 PEP8 line too long # E8402 module level import not at top of file # E8731 do not assign a lambda expression, use a def [REPORTS] # Tells whether to display a full report or only the messages reports=no # Template used to display messages. This is a python new-style format string # used to format the message information. See doc for all details msg-template='{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}' salt-pylint-2019.6.7/LICENSE000066400000000000000000000260751347555016700152740ustar00rootroot00000000000000Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. salt-pylint-2019.6.7/requirements.txt000066400000000000000000000000551347555016700175410ustar00rootroot00000000000000PyLint pycodestyle >= 2.4.0 modernize == 0.5 salt-pylint-2019.6.7/saltpylint/000077500000000000000000000000001347555016700164605ustar00rootroot00000000000000salt-pylint-2019.6.7/saltpylint/__init__.py000066400000000000000000000000301347555016700205620ustar00rootroot00000000000000# -*- coding: utf-8 -*- salt-pylint-2019.6.7/saltpylint/blacklist.py000066400000000000000000000621001347555016700210010ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2017 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. saltpylint.blacklist ~~~~~~~~~~~~~~~~~~~~ Checks blacklisted imports and code usage on salt ''' # Import python libs from __future__ import absolute_import import os import fnmatch # Import pylint libs import astroid from saltpylint.checkers import BaseChecker, utils from pylint.interfaces import IAstroidChecker BLACKLISTED_IMPORTS_MSGS = { 'E9402': ('Uses of a blacklisted module %r: %s', 'blacklisted-module', 'Used a module marked as blacklisted is imported.'), 'E9403': ('Uses of a blacklisted external module %r: %s', 'blacklisted-external-module', 'Used a module marked as blacklisted is imported.'), 'E9404': ('Uses of a blacklisted import %r: %s', 'blacklisted-import', 'Used an import marked as blacklisted.'), 'E9405': ('Uses of an external blacklisted import %r: %s', 'blacklisted-external-import', 'Used an external import marked as blacklisted.'), 'E9406': ('Uses of blacklisted test module execution code: %s', 'blacklisted-test-module-execution', 'Uses of blacklisted test module execution code.'), 'E9407': ('Uses of blacklisted sys.path updating through \'ensure_in_syspath\'. ' 'Please remove the import and any calls to \'ensure_in_syspath()\'.', 'blacklisted-syspath-update', 'Uses of blacklisted sys.path updating through ensure_in_syspath.'), } class BlacklistedImportsChecker(BaseChecker): __implements__ = IAstroidChecker name = 'blacklisted-imports' msgs = BLACKLISTED_IMPORTS_MSGS priority = -2 def open(self): self.blacklisted_modules = ('salttesting', 'integration', 'unit', 'mock', 'six', 'distutils.version', 'unittest', 'unittest2') @utils.check_messages('blacklisted-imports') def visit_import(self, node): '''triggered when an import statement is seen''' module_filename = node.root().file if fnmatch.fnmatch(module_filename, '__init__.py*') and \ not fnmatch.fnmatch(module_filename, 'test_*.py*'): return modnode = node.root() names = [name for name, _ in node.names] for name in names: self._check_blacklisted_module(node, name) @utils.check_messages('blacklisted-imports') def visit_importfrom(self, node): '''triggered when a from statement is seen''' module_filename = node.root().file if fnmatch.fnmatch(module_filename, '__init__.py*') and \ not fnmatch.fnmatch(module_filename, 'test_*.py*'): return basename = node.modname self._check_blacklisted_module(node, basename) def _check_blacklisted_module(self, node, mod_path): '''check if the module is blacklisted''' for mod_name in self.blacklisted_modules: if mod_path == mod_name or mod_path.startswith(mod_name + '.'): names = [] for name, name_as in node.names: if name_as: names.append('{0} as {1}'.format(name, name_as)) else: names.append(name) try: import_from_module = node.modname if import_from_module == 'salttesting.helpers': for name in names: if name == 'ensure_in_syspath': self.add_message('blacklisted-syspath-update', node=node) continue msg = 'Please use \'from tests.support.helpers import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module in ('salttesting.mock', 'mock', 'unittest.mock', 'unittest2.mock'): for name in names: msg = 'Please use \'from tests.support.mock import {0}\''.format(name) if import_from_module in ('salttesting.mock', 'unittest.mock', 'unittest2.mock'): message_id = 'blacklisted-module' else: message_id = 'blacklisted-external-module' self.add_message(message_id, node=node, args=(mod_path, msg)) continue if import_from_module == 'salttesting.parser': for name in names: msg = 'Please use \'from tests.support.parser import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module == 'salttesting.case': for name in names: msg = 'Please use \'from tests.support.case import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module == 'salttesting.unit': for name in names: msg = 'Please use \'from tests.support.unit import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module.startswith(('unittest', 'unittest2')): for name in names: msg = 'Please use \'from tests.support.unit import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module == 'salttesting.mixins': for name in names: msg = 'Please use \'from tests.support.mixins import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module == 'six': for name in names: msg = 'Please use \'from salt.ext.six import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if import_from_module == 'distutils.version': for name in names: msg = 'Please use \'from salt.utils.versions import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if names: for name in names: if name in ('TestLoader', 'TextTestRunner', 'TestCase', 'expectedFailure', 'TestSuite', 'skipIf', 'TestResult'): msg = 'Please use \'from tests.support.unit import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if name in ('SaltReturnAssertsMixin', 'SaltMinionEventAssertsMixin'): msg = 'Please use \'from tests.support.mixins import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if name in ('ModuleCase', 'SyndicCase', 'ShellCase', 'SSHCase'): msg = 'Please use \'from tests.support.case import {0}\''.format(name) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) continue if name == 'run_tests': msg = 'Please remove the \'if __name__ == "__main__":\' section from the end of the module' self.add_message('blacklisted-test-module-execution', node=node, args=msg) continue if mod_name in ('integration', 'unit'): if name in ('SYS_TMP_DIR', 'TMP', 'FILES', 'PYEXEC', 'MOCKBIN', 'SCRIPT_DIR', 'TMP_STATE_TREE', 'TMP_PRODENV_STATE_TREE', 'TMP_CONF_DIR', 'TMP_SUB_MINION_CONF_DIR', 'TMP_SYNDIC_MINION_CONF_DIR', 'TMP_SYNDIC_MASTER_CONF_DIR', 'CODE_DIR', 'TESTS_DIR', 'CONF_DIR', 'PILLAR_DIR', 'TMP_SCRIPT_DIR', 'ENGINES_DIR', 'LOG_HANDLERS_DIR', 'INTEGRATION_TEST_DIR'): msg = 'Please use \'from tests.support.paths import {0}\''.format(name) self.add_message('blacklisted-import', node=node, args=(mod_path, msg)) continue msg = 'Please use \'from tests.{0} import {1}\''.format(mod_path, name) self.add_message('blacklisted-import', node=node, args=(mod_path, msg)) continue msg = 'Please report this error to SaltStack so we can fix it: Trying to import {0} from {1}'.format(name, mod_path) self.add_message('blacklisted-module', node=node, args=(mod_path, msg)) except AttributeError: if mod_name in ('integration', 'unit', 'mock', 'six', 'distutils.version', 'unittest', 'unittest2'): if mod_name in ('integration', 'unit'): msg = 'Please use \'import tests.{0} as {0}\''.format(mod_name) message_id = 'blacklisted-import' elif mod_name == 'mock': msg = 'Please use \'import tests.support.{0} as {0}\''.format(mod_name) message_id = 'blacklisted-external-import' elif mod_name == 'six': msg = 'Please use \'import salt.ext.{0} as {0}\''.format(name) message_id = 'blacklisted-external-import' elif mod_name == 'distutils.version': msg = 'Please use \'import salt.utils.versions\' instead' message_id = 'blacklisted-import' elif mod_name.startswith(('unittest', 'unittest2')): msg = 'Please use \'import tests.support.unit as {}\' instead'.format(mod_name) message_id = 'blacklisted-import' self.add_message(message_id, node=node, args=(mod_path, msg)) continue msg = 'Please report this error to SaltStack so we can fix it: Trying to import {0}'.format(mod_path) self.add_message('blacklisted-import', node=node, args=(mod_path, msg)) BLACKLISTED_LOADER_USAGE_MSGS = { 'E9501': ('Blacklisted salt loader dunder usage. Setting dunder attribute %r to module %r. ' 'Use \'salt.support.mock\' and \'patch.dict()\' instead.', 'unmocked-patch-dunder', 'Uses a blacklisted salt loader dunder usage in tests.'), 'E9502': ('Blacklisted salt loader dunder usage. Setting attribute %r to module %r. ' 'Use \'salt.support.mock\' and \'patch()\' instead.', 'unmocked-patch', 'Uses a blacklisted salt loader dunder usage in tests.'), 'E9503': ('Blacklisted salt loader dunder usage. Updating dunder attribute %r on module %r. ' 'Use \'salt.support.mock\' and \'patch.dict()\' instead.', 'unmocked-patch-dunder-update', 'Uses a blacklisted salt loader dunder usage in tests.'), } class BlacklistedLoaderModulesUsageChecker(BaseChecker): __implements__ = IAstroidChecker name = 'blacklisted-unmocked-patching' msgs = BLACKLISTED_LOADER_USAGE_MSGS priority = -2 def open(self): self.process_module = False self.salt_dunders = ( '__opts__', '__salt__', '__runner__', '__context__', '__utils__', '__ext_pillar__', '__thorium__', '__states__', '__serializers__', '__ret__', '__grains__', '__pillar__', '__sdb__', '__proxy__', '__low__', '__orchestration_jid__', '__running__', '__intance_id__', '__lowstate__', '__env__' ) self.imported_salt_modules = {} def close(self): self.process_module = False self.imported_salt_modules = {} @utils.check_messages('blacklisted-unmocked-patching') def visit_module(self, node): module_filename = node.root().file if not fnmatch.fnmatch(os.path.basename(module_filename), 'test_*.py*'): return self.process_module = True @utils.check_messages('blacklisted-unmocked-patching') def leave_module(self, node): if self.process_module: # Reset self.process_module = False self.imported_salt_modules = {} @utils.check_messages('blacklisted-unmocked-patching') def visit_import(self, node): '''triggered when an import statement is seen''' if self.process_module: # Store salt imported modules for module, import_as in node.names: if not module.startswith('salt'): continue if import_as and import_as not in self.imported_salt_modules: self.imported_salt_modules[import_as] = module continue if module not in self.imported_salt_modules: self.imported_salt_modules[module] = module @utils.check_messages('blacklisted-unmocked-patching') def visit_importfrom(self, node): '''triggered when a from statement is seen''' if self.process_module: if not node.modname.startswith('salt'): return # Store salt imported modules for module, import_as in node.names: if import_as and import_as not in self.imported_salt_modules: self.imported_salt_modules[import_as] = import_as continue if module not in self.imported_salt_modules: self.imported_salt_modules[module] = module @utils.check_messages('blacklisted-loader-usage') def visit_assign(self, node, *args): if not self.process_module: return node_left = node.targets[0] if isinstance(node_left, astroid.Subscript): # Were're changing an existing attribute if not isinstance(node_left.value, astroid.Attribute): return if node_left.value.attrname in self.salt_dunders: self.add_message( 'unmocked-patch-dunder-update', node=node, args=(node_left.value.attrname, self.imported_salt_modules[node_left.value.expr.name]) ) return if not isinstance(node_left, astroid.AssignAttr): return try: if node_left.expr.name not in self.imported_salt_modules: # If attributes are not being set on salt's modules, # leave it alone, for now! return except AttributeError: # This mmight not be what we're looking for return # we're assigning to an imported salt module! if node_left.attrname in self.salt_dunders: # We're changing salt dunders self.add_message( 'unmocked-patch-dunder', node=node, args=(node_left.attrname, self.imported_salt_modules[node_left.expr.name]) ) return # Changing random attributes self.add_message( 'unmocked-patch', node=node, args=(node_left.attrname, self.imported_salt_modules[node_left.expr.name]) ) RESOURCE_LEAKAGE_MSGS = { 'W8470': ('Resource leakage detected. %s ', 'resource-leakage', 'Resource leakage detected.'), } class ResourceLeakageChecker(BaseChecker): __implements__ = IAstroidChecker name = 'resource-leakage' msgs = RESOURCE_LEAKAGE_MSGS priority = -2 def open(self): self.inside_with_ctx = False def close(self): self.inside_with_ctx = False def visit_with(self, node): self.inside_with_ctx = True def leave_with(self, node): self.inside_with_ctx = False def visit_call(self, node): if isinstance(node.func, astroid.Attribute): if node.func.attrname == 'fopen' and self.inside_with_ctx is False: msg = ('Please call \'salt.utils.fopen\' using the \'with\' context ' 'manager, otherwise the file handle won\'t be closed and ' 'resource leakage will occur.') self.add_message('resource-leakage', node=node, args=(msg,)) elif isinstance(node.func, astroid.Name): if utils.is_builtin(node.func.name) and node.func.name == 'open': if self.inside_with_ctx: msg = ('Please use \'with salt.utils.fopen()\' instead of ' '\'with open()\'. It assures salt does not leak ' 'file handles.') else: msg = ('Please use \'salt.utils.fopen()\' instead of \'open()\' ' 'using the \'with\' context manager, otherwise the file ' 'handle won\'t be closed and resource leakage will occur.') self.add_message('resource-leakage', node=node, args=(msg,)) MOVED_TEST_CASE_CLASSES_MSGS = { 'E9490': ('Moved test case base class detected. %s', 'moved-test-case-class', 'Moved test case base class detected.'), 'E9491': ('Moved test case mixin class detected. %s', 'moved-test-case-mixin', 'Moved test case mixin class detected.'), } class MovedTestCaseClassChecker(BaseChecker): __implements__ = IAstroidChecker name = 'moved-test-case-class' msgs = MOVED_TEST_CASE_CLASSES_MSGS priority = -2 def open(self): self.process_module = False def close(self): self.process_module = False @utils.check_messages('moved-test-case-class') def visit_module(self, node): module_filename = node.root().file if not fnmatch.fnmatch(os.path.basename(module_filename), 'test_*.py*'): return self.process_module = True @utils.check_messages('moved-test-case-class') def leave_module(self, node): if self.process_module: # Reset self.process_module = False @utils.check_messages('moved-test-case-class') def visit_importfrom(self, node): '''triggered when a from statement is seen''' if self.process_module: if not node.modname.startswith('tests.integration'): return # Store salt imported modules for module, import_as in node.names: if import_as: self._check_moved_imports(node, module, import_as) continue self._check_moved_imports(node, module) @utils.check_messages('moved-test-case-class') def visit_classdef(self, node): for base in node.bases: if not hasattr(base, 'attrname'): continue if base.attrname in ('TestCase',): msg = 'Please use \'from tests.support.unit import {0}\''.format(base.attrname) self.add_message('moved-test-case-class', node=node, args=(msg,)) if base.attrname in ('ModuleCase', 'SyndicCase', 'ShellCase', 'SSHCase'): msg = 'Please use \'from tests.support.case import {0}\''.format(base.attrname) self.add_message('moved-test-case-class', node=node, args=(msg,)) if base.attrname in ('AdaptedConfigurationTestCaseMixin', 'ShellCaseCommonTestsMixin', 'SaltMinionEventAssertsMixin'): msg = 'Please use \'from tests.support.mixins import {0}\''.format(base.attrname) self.add_message('moved-test-case-mixin', node=node, args=(msg,)) def _check_moved_imports(self, node, module, import_as=None): names = [] for name, name_as in node.names: if name not in ('ModuleCase', 'SyndicCase', 'ShellCase', 'SSHCase'): continue if name_as: msg = 'Please use \'from tests.support.case import {0} as {1}\''.format(name, name_as) else: msg = 'Please use \'from tests.support.case import {0}\''.format(name) self.add_message('moved-test-case-class', node=node, args=(msg,)) BLACKLISTED_FUNCTIONS_MSGS = { 'E9601': ('Use of blacklisted function %s (use %s instead)', 'blacklisted-function', 'Used a function marked as blacklisted'), } class BlacklistedFunctionsChecker(BaseChecker): __implements__ = IAstroidChecker name = 'blacklisted-functions' msgs = BLACKLISTED_FUNCTIONS_MSGS priority = -2 max_depth = 20 options = ( ('blacklisted-functions', {'default': '', 'type': 'string', 'metavar': 'bad1=good1,bad2=good2', 'help': 'List of blacklisted functions and their recommended ' 'replacements'}), ) def open(self): self.blacklisted_functions = {} blacklist = [ x.strip() for x in self.config.blacklisted_functions.split(',')] for item in [x.strip() for x in self.config.blacklisted_functions.split(',')]: try: key, val = [x.strip() for x in item.split('=')] except ValueError: pass else: self.blacklisted_functions[key] = val def _get_full_name(self, node): try: func = utils.safe_infer(node.func) if func.name.__str__() == 'Uninferable': return except Exception: func = None if func is None: return ret = [] depth = 0 while func is not None: depth += 1 if depth > self.max_depth: # Prevent endless loop return try: ret.append(func.name) except AttributeError: return func = func.parent # ret will contain the levels of the function from last to first (e.g. # ['walk', 'os']. Reverse it and join with dots to get the correct # full name for the function. return '.'.join(ret[::-1]) @utils.check_messages('blacklisted-functions') def visit_call(self, node): if self.blacklisted_functions: full_name = self._get_full_name(node) if full_name is not None: try: self.add_message( 'blacklisted-function', node=node, args=(full_name, self.blacklisted_functions[full_name]) ) except KeyError: # Not blacklisted pass def register(linter): ''' Required method to auto register this checker ''' linter.register_checker(ResourceLeakageChecker(linter)) linter.register_checker(BlacklistedImportsChecker(linter)) linter.register_checker(MovedTestCaseClassChecker(linter)) linter.register_checker(BlacklistedLoaderModulesUsageChecker(linter)) linter.register_checker(BlacklistedFunctionsChecker(linter)) salt-pylint-2019.6.7/saltpylint/checkers.py000066400000000000000000000012061347555016700206200ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' saltpylint.checkers ~~~~~~~~~~~~~~~~~~~~ Works around older astroid versions ''' # Import python libs from __future__ import absolute_import # Import pylint libs import astroid from pylint.checkers import BaseChecker as _BaseChecker # Imported to avoid needing a separate import from pylint.checkers from pylint.checkers import utils class BaseChecker(_BaseChecker): def __init__(self, *args, **kwargs): super(BaseChecker, self).__init__(*args, **kwargs) if hasattr(self, 'visit_call') and not hasattr(astroid, 'Call'): setattr(self, 'visit_callfunc', self.visit_call) salt-pylint-2019.6.7/saltpylint/dunder_del.py000066400000000000000000000027661347555016700211520ustar00rootroot00000000000000# -*- coding: utf-8 -*- # Import Python libs from __future__ import absolute_import # Import PyLint libs try: # >= pylint 1.0 import astroid except ImportError: # pylint < 1.0 from logilab import astng as astroid # pylint: disable=no-name-in-module from saltpylint.checkers import BaseChecker, utils try: # >= pylint 1.0 from pylint.interfaces import IAstroidChecker except ImportError: # < pylint 1.0 from pylint.interfaces import IASTNGChecker as IAstroidChecker # pylint: disable=no-name-in-module try: from astroid.exceptions import NameInferenceError except ImportError: class NameInferenceError(Exception): pass WARNING_CODE = 'W1701' class DunderDelChecker(BaseChecker): ''' info: This class is used by pylint that checks if "__del__" is not used if "__del__" is used then a warning will be raised ''' __implements__ = IAstroidChecker name = 'dunder-del' priority = -1 msgs = { WARNING_CODE: ( '"__del__" is not allowed!', 'no-dunder-del', '"__del__" is not allowed! A "with" block could be a good solution' ), } def visit_functiondef(self, node): ''' :param node: info about a function or method :return: None ''' if node.name == "__del__": self.add_message(WARNING_CODE, node=node) def register(linter): '''required method to auto register this checker ''' linter.register_checker(DunderDelChecker(linter)) salt-pylint-2019.6.7/saltpylint/ext/000077500000000000000000000000001347555016700172605ustar00rootroot00000000000000salt-pylint-2019.6.7/saltpylint/ext/__init__.py000066400000000000000000000000301347555016700213620ustar00rootroot00000000000000# -*- coding: utf-8 -*- salt-pylint-2019.6.7/saltpylint/ext/pyqver2.py000066400000000000000000000314471347555016700212530ustar00rootroot00000000000000# -*- coding: utf-8 -*- # This software is provided 'as-is', without any express or implied # warranty. In no event will the author be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. # # Copyright (c) 2009-2013 Greg Hewgill http://hewgill.com # # # ############################################################################ # # Changed the code in order for it not to process sys.argv # # ############################################################################ # pylint: skip-file from __future__ import print_function import compiler import platform import sys StandardModules = { "__future__": (2, 1), "abc": (2, 6), "argparse": (2, 7), "ast": (2, 6), "atexit": (2, 0), "bz2": (2, 3), "cgitb": (2, 2), "collections": (2, 4), "contextlib": (2, 5), "cookielib": (2, 4), "cProfile": (2, 5), "csv": (2, 3), "ctypes": (2, 5), "datetime": (2, 3), "decimal": (2, 4), "difflib": (2, 1), "DocXMLRPCServer": (2, 3), "dummy_thread": (2, 3), "dummy_threading": (2, 3), "email": (2, 2), "fractions": (2, 6), "functools": (2, 5), "future_builtins": (2, 6), "hashlib": (2, 5), "heapq": (2, 3), "hmac": (2, 2), "hotshot": (2, 2), "HTMLParser": (2, 2), "importlib": (2, 7), "inspect": (2, 1), "io": (2, 6), "itertools": (2, 3), "json": (2, 6), "logging": (2, 3), "modulefinder": (2, 3), "msilib": (2, 5), "multiprocessing": (2, 6), "netrc": (1, 5, 2), "numbers": (2, 6), "optparse": (2, 3), "ossaudiodev": (2, 3), "pickletools": (2, 3), "pkgutil": (2, 3), "platform": (2, 3), "pydoc": (2, 1), "runpy": (2, 5), "sets": (2, 3), "shlex": (1, 5, 2), "SimpleXMLRPCServer": (2, 2), "spwd": (2, 5), "sqlite3": (2, 5), "ssl": (2, 6), "stringprep": (2, 3), "subprocess": (2, 4), "sysconfig": (2, 7), "tarfile": (2, 3), "textwrap": (2, 3), "timeit": (2, 3), "unittest": (2, 1), "uuid": (2, 5), "warnings": (2, 1), "weakref": (2, 1), "winsound": (1, 5, 2), "wsgiref": (2, 5), "xml.dom": (2, 0), "xml.dom.minidom": (2, 0), "xml.dom.pulldom": (2, 0), "xml.etree.ElementTree": (2, 5), "xml.parsers.expat":(2, 0), "xml.sax": (2, 0), "xml.sax.handler": (2, 0), "xml.sax.saxutils": (2, 0), "xml.sax.xmlreader":(2, 0), "xmlrpclib": (2, 2), "zipfile": (1, 6), "zipimport": (2, 3), "_ast": (2, 5), "_winreg": (2, 0), } Functions = { "all": (2, 5), "any": (2, 5), "collections.Counter": (2, 7), "collections.defaultdict": (2, 5), "collections.OrderedDict": (2, 7), "enumerate": (2, 3), "frozenset": (2, 4), "itertools.compress": (2, 7), "math.erf": (2, 7), "math.erfc": (2, 7), "math.expm1": (2, 7), "math.gamma": (2, 7), "math.lgamma": (2, 7), "memoryview": (2, 7), "next": (2, 6), "os.getresgid": (2, 7), "os.getresuid": (2, 7), "os.initgroups": (2, 7), "os.setresgid": (2, 7), "os.setresuid": (2, 7), "reversed": (2, 4), "set": (2, 4), "subprocess.check_call": (2, 5), "subprocess.check_output": (2, 7), "sum": (2, 3), "symtable.is_declared_global": (2, 7), "weakref.WeakSet": (2, 7), } Identifiers = { "False": (2, 2), "True": (2, 2), } def uniq(a): if len(a) == 0: return [] else: return [a[0]] + uniq([x for x in a if x != a[0]]) class NodeChecker(object): def __init__(self): self.vers = dict() self.vers[(2,0)] = [] def add(self, node, ver, msg): if ver not in self.vers: self.vers[ver] = [] self.vers[ver].append((node.lineno, msg)) def default(self, node): for child in node.getChildNodes(): self.visit(child) def visitCallFunc(self, node): def rollup(n): if isinstance(n, compiler.ast.Name): return n.name elif isinstance(n, compiler.ast.Getattr): r = rollup(n.expr) if r: return r + "." + n.attrname name = rollup(node.node) if name: v = Functions.get(name) if v is not None: self.add(node, v, name) self.default(node) def visitClass(self, node): if node.bases: self.add(node, (2,2), "new-style class") if node.decorators: self.add(node, (2,6), "class decorator") self.default(node) def visitDictComp(self, node): self.add(node, (2,7), "dictionary comprehension") self.default(node) def visitFloorDiv(self, node): self.add(node, (2,2), "// operator") self.default(node) def visitFrom(self, node): v = StandardModules.get(node.modname) if v is not None: self.add(node, v, node.modname) for n in node.names: name = node.modname + "." + n[0] v = Functions.get(name) if v is not None: self.add(node, v, name) def visitFunction(self, node): if node.decorators: self.add(node, (2,4), "function decorator") self.default(node) def visitGenExpr(self, node): self.add(node, (2,4), "generator expression") self.default(node) def visitGetattr(self, node): if (isinstance(node.expr, compiler.ast.Const) and isinstance(node.expr.value, str) and node.attrname == "format"): self.add(node, (2,6), "string literal .format()") self.default(node) def visitIfExp(self, node): self.add(node, (2,5), "inline if expression") self.default(node) def visitImport(self, node): for n in node.names: v = StandardModules.get(n[0]) if v is not None: self.add(node, v, n[0]) self.default(node) def visitName(self, node): v = Identifiers.get(node.name) if v is not None: self.add(node, v, node.name) self.default(node) def visitSet(self, node): self.add(node, (2,7), "set literal") self.default(node) def visitSetComp(self, node): self.add(node, (2,7), "set comprehension") self.default(node) def visitTryFinally(self, node): # try/finally with a suite generates a Stmt node as the body, # but try/except/finally generates a TryExcept as the body if isinstance(node.body, compiler.ast.TryExcept): self.add(node, (2,5), "try/except/finally") self.default(node) def visitWith(self, node): if isinstance(node.body, compiler.ast.With): self.add(node, (2,7), "with statement with multiple contexts") else: self.add(node, (2,5), "with statement") self.default(node) def visitYield(self, node): self.add(node, (2,2), "yield expression") self.default(node) def get_versions(source): """Return information about the Python versions required for specific features. The return value is a dictionary with keys as a version number as a tuple (for example Python 2.6 is (2,6)) and the value are a list of features that require the indicated Python version. """ tree = compiler.parse(source) checker = compiler.walk(tree, NodeChecker()) return checker.vers def v27(source): if sys.version_info >= (2, 7): return qver(source) else: print("Not all features tested, run --test with Python 2.7", file=sys.stderr) return (2, 7) def qver(source): """Return the minimum Python version required to run a particular bit of code. >>> qver('print "hello world"') (2, 0) >>> qver('class test(object): pass') (2, 2) >>> qver('yield 1') (2, 2) >>> qver('a // b') (2, 2) >>> qver('True') (2, 2) >>> qver('enumerate(a)') (2, 3) >>> qver('total = sum') (2, 0) >>> qver('sum(a)') (2, 3) >>> qver('(x*x for x in range(5))') (2, 4) >>> qver('class C:\\n @classmethod\\n def m(): pass') (2, 4) >>> qver('y if x else z') (2, 5) >>> qver('import hashlib') (2, 5) >>> qver('from hashlib import md5') (2, 5) >>> qver('import xml.etree.ElementTree') (2, 5) >>> qver('try:\\n try: pass;\\n except: pass;\\nfinally: pass') (2, 0) >>> qver('try: pass;\\nexcept: pass;\\nfinally: pass') (2, 5) >>> qver('from __future__ import with_statement\\nwith x: pass') (2, 5) >>> qver('collections.defaultdict(list)') (2, 5) >>> qver('from collections import defaultdict') (2, 5) >>> qver('"{0}".format(0)') (2, 6) >>> qver('memoryview(x)') (2, 7) >>> v27('{1, 2, 3}') (2, 7) >>> v27('{x for x in s}') (2, 7) >>> v27('{x: y for x in s}') (2, 7) >>> qver('from __future__ import with_statement\\nwith x:\\n with y: pass') (2, 5) >>> v27('from __future__ import with_statement\\nwith x, y: pass') (2, 7) >>> qver('@decorator\\ndef f(): pass') (2, 4) >>> qver('@decorator\\nclass test:\\n pass') (2, 6) #>>> qver('0o0') #(2, 6) #>>> qver('@foo\\nclass C: pass') #(2, 6) """ return max(get_versions(source).keys()) if __name__ == '__main__': Verbose = False MinVersion = (2, 3) Lint = False files = [] i = 1 while i < len(sys.argv): a = sys.argv[i] if a == "--test": import doctest doctest.testmod() sys.exit(0) if a == "-v" or a == "--verbose": Verbose = True elif a == "-l" or a == "--lint": Lint = True elif a == "-m" or a == "--min-version": i += 1 MinVersion = tuple(map(int, sys.argv[i].split("."))) else: files.append(a) i += 1 if not files: print("""Usage: %s [options] source ... Report minimum Python version required to run given source files. -m x.y or --min-version x.y (default 2.3) report version triggers at or above version x.y in verbose mode -v or --verbose print more detailed report of version triggers for each version """ % sys.argv[0], file=sys.stderr) sys.exit(1) for fn in files: try: f = open(fn) source = f.read() f.close() ver = get_versions(source) if Verbose: print(fn) for v in sorted([k for k in ver.keys() if k >= MinVersion], reverse=True): reasons = [x for x in uniq(ver[v]) if x] if reasons: # each reason is (lineno, message) print("\t%s\t%s" % (".".join(map(str, v)), ", ".join([x[1] for x in reasons]))) elif Lint: for v in sorted([k for k in ver.keys() if k >= MinVersion], reverse=True): reasons = [x for x in uniq(ver[v]) if x] for r in reasons: # each reason is (lineno, message) print("%s:%s: %s %s" % (fn, r[0], ".".join(map(str, v)), r[1])) else: print("%s\t%s" % (".".join(map(str, max(ver.keys()))), fn)) except SyntaxError as x: print("%s: syntax error compiling with Python %s: %s" % (fn, platform.python_version(), x)) salt-pylint-2019.6.7/saltpylint/fileperms.py000066400000000000000000000117321347555016700210240ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2015 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. ==================================== PyLint File Permissions Check Plugin ==================================== PyLint plugin which checks for specific file permissions ''' # Import Python libs from __future__ import absolute_import import os import sys import glob import stat # Import PyLint libs from pylint.interfaces import IRawChecker from saltpylint.checkers import BaseChecker class FilePermsChecker(BaseChecker): ''' Check for files with undesirable permissions ''' __implements__ = IRawChecker name = 'fileperms' msgs = {'E0599': ('Module file has the wrong file permissions(expected %s): %s', 'file-perms', ('Wrong file permissions')), } priority = -1 options = (('fileperms-default', {'default': '0644', 'type': 'string', 'metavar': 'ZERO_PADDED_PERM', 'help': 'Desired file permissons. Default: 0644'} ), ('fileperms-ignore-paths', {'default': (), 'type': 'csv', 'metavar': '', 'help': 'File paths to ignore file permission. Glob patterns allowed.'} ) ) def process_module(self, node): ''' process a module ''' for listing in self.config.fileperms_ignore_paths: if node.file.split('{0}/'.format(os.getcwd()))[-1] in glob.glob(listing): # File is ignored, no checking should be done return desired_perm = self.config.fileperms_default if '-' in desired_perm: desired_perm = desired_perm.split('-') else: desired_perm = [desired_perm] if len(desired_perm) > 2: raise RuntimeError('Permission ranges should be like XXXX-YYYY') for idx, perm in enumerate(desired_perm): desired_perm[idx] = desired_perm[idx].strip('"').strip('\'').lstrip('0').zfill(4) if desired_perm[idx][0] != '0': # Always include a leading zero desired_perm[idx] = '0{0}'.format(desired_perm[idx]) if sys.version_info > (3,): # The octal representation in python 3 has changed to 0o644 instead of 0644 if desired_perm[idx][1] != 'o': desired_perm[idx] = '0o' + desired_perm[idx][1:] if sys.platform.startswith('win'): # Windows does not distinguish between user/group/other. # They must all be the same. Also, Windows will automatically # set the execution bit on files with a known extension # (eg .exe, .bat, .com). So we cannot reliably test the # execution bit on other files such as .py files. user_perm_noexec = int(desired_perm[idx][-3]) if user_perm_noexec % 2 == 1: user_perm_noexec -= 1 desired_perm[idx] = desired_perm[idx][:-3] + (str(user_perm_noexec) * 3) module_perms = oct(stat.S_IMODE(os.stat(node.file).st_mode)) if sys.version_info < (3,): module_perms = str(module_perms) if len(desired_perm) == 1: if module_perms != desired_perm[0]: if sys.platform.startswith('win'): # Check the variant with execution bit set due to the # unreliability of checking the execution bit on Windows. user_perm_noexec = int(desired_perm[0][-3]) desired_perm_exec = desired_perm[0][:-3] + (str(user_perm_noexec + 1) * 3) if module_perms == desired_perm_exec: return self.add_message('E0599', line=1, args=(desired_perm[0], module_perms)) else: if module_perms < desired_perm[0] or module_perms > desired_perm[1]: if sys.platform.startswith('win'): # Check the variant with execution bit set due to the # unreliability of checking the execution bit on Windows. user_perm_noexec0 = int(desired_perm[0][-3]) desired_perm_exec0 = desired_perm[0][:-3] + (str(user_perm_noexec0 + 1) * 3) user_perm_noexec1 = int(desired_perm[1][-3]) desired_perm_exec1 = desired_perm[1][:-3] + (str(user_perm_noexec1 + 1) * 3) if desired_perm_exec0 <= module_perms <= desired_perm_exec1: return desired_perm = '>= {0} OR <= {1}'.format(*desired_perm) self.add_message('E0599', line=1, args=(desired_perm, module_perms)) def register(linter): ''' required method to auto register this checker ''' linter.register_checker(FilePermsChecker(linter)) salt-pylint-2019.6.7/saltpylint/minpyver.py000066400000000000000000000046651347555016700207160ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2015 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. =============================================== PyLint Minimum Python Version Enforcing Checker =============================================== PyLint plugin which checks if the code meet the requirements of a targeted minimal version. ''' # Import Python libs from __future__ import absolute_import import sys # Import PyLint libs from pylint.interfaces import IRawChecker from saltpylint.checkers import BaseChecker # Import 3rd-party libs try: from saltpylint.ext import pyqver2 HAS_PYQVER = True except ImportError: if sys.version_info[0] == 2: import warnings # pylint: disable=wrong-import-order warnings.warn( 'Unable to import saltpylint.ext.pyqver2. Checker skipped.', RuntimeWarning ) HAS_PYQVER = False class MininumPythonVersionChecker(BaseChecker): ''' Check the minimal required python version ''' __implements__ = IRawChecker name = 'mininum-python-version' msgs = {'E0598': ('Incompatible Python %s code found: %s', 'minimum-python-version', 'The code does not meet the required minimum python version'), } priority = -1 options = (('minimum-python-version', {'default': '2.6', 'type': 'string', 'metavar': 'MIN_PYTHON_VERSION', 'help': 'The desired minimum python version to enforce. Default: 2.6'} ), ) def process_module(self, node): ''' process a module ''' if not HAS_PYQVER: return minimum_version = tuple([int(x) for x in self.config.minimum_python_version.split('.')]) with open(node.path, 'r') as rfh: for version, reasons in pyqver2.get_versions(rfh.read()).iteritems(): if version > minimum_version: for lineno, msg in reasons: self.add_message( 'E0598', line=lineno, args=(self.config.minimum_python_version, msg) ) def register(linter): ''' required method to auto register this checker ''' linter.register_checker(MininumPythonVersionChecker(linter)) salt-pylint-2019.6.7/saltpylint/pep263.py000066400000000000000000000076711347555016700200640ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Henrik Holmboe (henrik@holmboe.se)` :copyright: © 2013 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. ====================== PEP-263 PyLint Checker ====================== ''' # Import Python libs from __future__ import absolute_import import re import itertools # Import PyLint libs from pylint.interfaces import IRawChecker from saltpylint.checkers import BaseChecker # Import 3rd-party libs import six class FileEncodingChecker(BaseChecker): ''' Check for PEP263 compliant file encoding in file. ''' __implements__ = IRawChecker name = 'pep263' msgs = {'W9901': ('PEP263: Multiple file encodings', 'multiple-encoding-in-file', ('There are multiple encodings in file.')), 'W9902': ('PEP263: Parser and PEP263 encoding mismatch', 'encoding-mismatch-in-file', ('The pylint parser and the PEP263 file encoding in file ' 'does not match.')), 'W9903': ('PEP263: Use UTF-8 file encoding', 'no-encoding-in-file', ('There is no PEP263 compliant file encoding in file.')), 'W9904': ('PEP263: Use UTF-8 file encoding', 'wrongly-encoded-file', ('Change file encoding and PEP263 header in file.')), 'W9905': ('PEP263: Use UTF-8 file encoding', 'no-encoding-in-empty-file', ('There is no PEP263 compliant file encoding in file.')), } priority = -1 options = () RE_PEP263 = r'coding[:=]\s*([-\w.]+)' REQ_ENCOD = six.b('utf-8') def process_module(self, node): ''' process a module the module's content is accessible via node.file_stream object ''' pep263 = re.compile(six.b(self.RE_PEP263)) try: file_stream = node.file_stream except AttributeError: # Pylint >= 1.8.1 file_stream = node.stream() # Store a reference to the node's file stream position current_stream_position = file_stream.tell() # Go to the start of stream to achieve our logic file_stream.seek(0) # Grab the first two lines twolines = list(itertools.islice(file_stream, 2)) pep263_encoding = [m.group(1).lower() for l in twolines for m in [pep263.search(l)] if m] multiple_encodings = len(pep263_encoding) > 1 file_empty = len(twolines) == 0 # Reset the node's file stream position file_stream.seek(current_stream_position) # - If the file has an UTF-8 BOM and yet uses any other # encoding, it will be caught by F0002 # - If the file has a PEP263 UTF-8 encoding and yet uses any # other encoding, it will be caught by W0512 # - If there are non-ASCII characters and no PEP263, or UTF-8 # BOM, it will be caught by W0512 # - If there are ambiguous PEP263 encodings it will be caught # by E0001, we still test for this if multiple_encodings: self.add_message('W9901', line=1) if node.file_encoding: pylint_encoding = node.file_encoding.lower() if six.PY3: pylint_encoding = pylint_encoding.encode('utf-8') if pep263_encoding and pylint_encoding not in pep263_encoding: self.add_message('W9902', line=1) if not pep263_encoding: if file_empty: self.add_message('W9905', line=1) else: self.add_message('W9903', line=1) elif self.REQ_ENCOD not in pep263_encoding: self.add_message('W9904', line=1) def register(linter): ''' required method to auto register this checker ''' linter.register_checker(FileEncodingChecker(linter)) salt-pylint-2019.6.7/saltpylint/pep8.py000066400000000000000000000357441347555016700177230ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2013-2018 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. =================== PEP-8 PyLint Plugin =================== A bridge between the `pep8`_ library and PyLint .. _`pep8`: http://pep8.readthedocs.org ''' # Import Python libs from __future__ import absolute_import import sys import logging import warnings # Import 3rd-party libs import six # Import PyLint libs from pylint.interfaces import IRawChecker from pylint.__pkginfo__ import numversion as pylint_version_info from saltpylint.checkers import BaseChecker # Import PEP8 libs try: from pycodestyle import StyleGuide, BaseReport HAS_PEP8 = True except ImportError: HAS_PEP8 = False warnings.warn( 'No pycodestyle library could be imported. No PEP8 check\'s will be done', RuntimeWarning ) _PROCESSED_NODES = {} _KNOWN_PEP8_IDS = [] _UNHANDLED_PEP8_IDS = [] if HAS_PEP8 is True: class PyLintPEP8Reporter(BaseReport): def __init__(self, options): super(PyLintPEP8Reporter, self).__init__(options) self.locations = [] def error(self, line_number, offset, text, check): code = super(PyLintPEP8Reporter, self).error( line_number, offset, text, check ) if code: # E123, at least, is not reporting it's code in the above call, # don't want to bother about that now self.locations.append((code, line_number, text.split(code, 1)[-1].strip())) class _PEP8BaseChecker(BaseChecker): __implements__ = IRawChecker name = 'pep8' priority = -1 options = () msgs = None _msgs = {} msgs_map = {} def __init__(self, linter=None): # To avoid PyLints deprecation about a missing symbolic name and # because I don't want to add descriptions, let's make the descriptions # equal to the messages. if self.msgs is None: self.msgs = {} for code, (message, symbolic) in six.iteritems(self._msgs): self.msgs[code] = (message, symbolic, message) BaseChecker.__init__(self, linter=linter) def process_module(self, node): ''' process a module the module's content is accessible via node.file_stream object ''' nodepaths = [] if not isinstance(node.path, list): nodepaths = [node.path] else: nodepaths = node.path for node_path in nodepaths: if node_path not in _PROCESSED_NODES: stylechecker = StyleGuide( parse_argv=False, config_file=False, quiet=2, reporter=PyLintPEP8Reporter ) _PROCESSED_NODES[node_path] = stylechecker.check_files([node_path]) for code, lineno, text in _PROCESSED_NODES[node_path].locations: pylintcode = '{0}8{1}'.format(code[0], code[1:]) if pylintcode in self.msgs_map: # This will be handled by PyLint itself, skip it continue if pylintcode not in _KNOWN_PEP8_IDS: if pylintcode not in _UNHANDLED_PEP8_IDS: _UNHANDLED_PEP8_IDS.append(pylintcode) msg = 'The following code, {0}, was not handled by the PEP8 plugin'.format(pylintcode) if logging.root.handlers: logging.getLogger(__name__).warning(msg) else: sys.stderr.write('{0}\n'.format(msg)) continue if pylintcode not in self._msgs: # Not for our class implementation to handle continue if code in ('E111', 'E113'): if _PROCESSED_NODES[node_path].lines[lineno-1].strip().startswith('#'): # If E111 is triggered in a comment I consider it, at # least, bad judgement. See https://github.com/jcrocholl/pep8/issues/300 # If E113 is triggered in comments, which I consider a bug, # skip it. See https://github.com/jcrocholl/pep8/issues/274 continue try: self.add_message(pylintcode, line=lineno, args=(code, text)) except TypeError as exc: if 'not all arguments' not in str(exc): raise # Message does not support being passed the text arg self.add_message(pylintcode, line=lineno, args=(code,)) class PEP8Indentation(_PEP8BaseChecker): ''' Process PEP8 E1 codes ''' _msgs = { 'E8101': ('PEP8 %s: %s', 'indentation-contains-mixed-spaces-and-tabs'), 'E8111': ('PEP8 %s: %s', 'indentation-is-not-a-multiple-of-four'), 'E8112': ('PEP8 %s: %s', 'expected-an-indented-block'), 'E8113': ('PEP8 %s: %s', 'unexpected-indentation'), 'E8114': ('PEP8 %s: %s', 'indentation-is-not-a-multiple-of-four-comment'), 'E8115': ('PEP8 %s: %s', 'expected-an-indented-block-comment'), 'E8116': ('PEP8 %s: %s', 'unexpected-indentation-comment'), 'E8121': ('PEP8 %s: %s', 'continuation-line-indentation-is-not-a-multiple-of-four'), 'E8122': ('PEP8 %s: %s', 'continuation-line-missing-indentation-or-outdented'), 'E8123': ('PEP8 %s: %s', 'closing-bracket-does-not-match-indentation-of-opening-brackets-line'), 'E8124': ('PEP8 %s: %s', 'closing-bracket-does-not-match-visual-indentation'), 'E8125': ('PEP8 %s: %s', 'continuation-line-does-not-distinguish-itself-from-next-logical-line'), 'E8126': ('PEP8 %s: %s', 'continuation-line-over-indented-for-hanging-indent'), 'E8127': ('PEP8 %s: %s', 'continuation-line-over-indented-for-visual-indent'), 'E8128': ('PEP8 %s: %s', 'continuation-line-under-indented-for-visual-indent'), 'E8129': ('PEP8 %s: %s', 'visually-indented-line-with-same-indent-as-next-logical-line'), 'E8131': ('PEP8 %s: %s', 'unaligned-for-hanging-indent'), 'E8133': ('PEP8 %s: %s', 'closing-bracket-is-missing-indentation'), } msgs_map = { 'E8126': 'C0330' } class PEP8Whitespace(_PEP8BaseChecker): ''' Process PEP8 E2 codes ''' _msgs = { 'E8201': ('PEP8 %s: %s', 'whitespace-after-left-parenthesis'), 'E8202': ('PEP8 %s: %s', 'whitespace-before-right-parenthesis'), 'E8203': ('PEP8 %s: %s', 'whitespace-before-colon'), 'E8211': ('PEP8 %s: %s', 'whitespace-before-left-parenthesis'), 'E8221': ('PEP8 %s: %s', 'multiple-spaces-before-operator'), 'E8222': ('PEP8 %s: %s', 'multiple-spaces-after-operator'), 'E8223': ('PEP8 %s: %s', 'tab-before-operator'), 'E8224': ('PEP8 %s: %s', 'tab-after-operator'), 'E8225': ('PEP8 %s: %s', 'missing-whitespace-around-operator'), 'E8226': ('PEP8 %s: %s', 'missing-whitespace-around-arithmetic-operator'), 'E8227': ('PEP8 %s: %s', 'missing-whitespace-around-bitwise-or-shift-operator'), 'E8228': ('PEP8 %s: %s', 'missing-whitespace-around-modulo-operator'), 'E8231': ('PEP8 %s: %s', 'missing-whitespace-after-comma'), 'E8241': ('PEP8 %s: %s', 'multiple-spaces-after-comma'), 'E8242': ('PEP8 %s: %s', 'tab-after-comma'), 'E8251': ('PEP8 %s: %s', 'unexpected-spaces-around-keyword-or-parameter-equals'), 'E8261': ('PEP8 %s: %s', 'at-least-two-spaces-before-inline-comment'), 'E8262': ('PEP8 %s: %s', 'inline-comment-should-start-with-cardinal-space'), 'E8265': ('PEP8 %s: %s', 'block-comment-should-start-with-cardinal-space'), 'E8266': ('PEP8 %s: %s', 'too-many-leading-hastag-for-block-comment'), 'E8271': ('PEP8 %s: %s', 'multiple-spaces-after-keyword'), 'E8272': ('PEP8 %s: %s', 'multiple-spaces-before-keyword'), 'E8273': ('PEP8 %s: %s', 'tab-after-keyword'), 'E8274': ('PEP8 %s: %s', 'tab-before-keyword'), 'E8275': ('PEP8 %s: %s', 'pep8-missing-whitespace-after-keyword') } msgs_map = { 'E8222': 'C0326', 'E8225': 'C0326', 'E8251': 'C0326' } class PEP8BlankLine(_PEP8BaseChecker): ''' Process PEP8 E3 codes ''' _msgs = { 'E8301': ('PEP8 %s: %s', 'expected-1-blank-line-found-0'), 'E8302': ('PEP8 %s: %s', 'expected-2-blank-lines-found-0'), 'E8303': ('PEP8 %s: %s', 'too-many-blank-lines'), 'E8304': ('PEP8 %s: %s', 'blank-lines-found-after-function-decorator'), 'E8305': ('PEP8 %s: %s', 'blank-lines-found-after-class-or-function-decorator'), 'E8306': ('PEP8 %s: %s', 'pep8-blank-lines-before-nested-definition'), } class PEP8Import(_PEP8BaseChecker): ''' Process PEP8 E4 codes ''' _msgs = { 'E8401': ('PEP8 %s: %s', 'multiple-imports-on-one-line'), 'E8402': ('PEP8 %s: %s', 'module-level-import-not-at-top-of-file') } class PEP8LineLength(_PEP8BaseChecker): ''' Process PEP8 E5 codes ''' _msgs = { 'E8501': ('PEP8 %s: %s', 'line-too-long)'), 'E8502': ('PEP8 %s: %s', 'the-backslash-is-redundant-between-brackets') } msgs_map = { 'E8501': 'C0301' } class PEP8Statement(_PEP8BaseChecker): ''' Process PEP8 E7 codes ''' _msgs = { 'E8701': ('PEP8 %s: %s', 'multiple-statements-on-one-line-colon'), 'E8702': ('PEP8 %s: %s', 'multiple-statements-on-one-line-semicolon'), 'E8703': ('PEP8 %s: %s', 'statement-ends-with-a-semicolon'), 'E8704': ('PEP8 %s: %s', 'pep8-multiple-statements-on-one-line'), 'E8711': ('PEP8 %s: %s', 'comparison-to-None-should-be-if-cond-is-None'), 'E8712': ('PEP8 %s: %s', 'comparison-to-True-should-be-if-cond-is-True-or-if-cond'), 'E8713': ('PEP8 %s: %s', 'test-for-membership-should-be-not-in'), 'E8714': ('PEP8 %s: %s', 'test-for-object-identity-should-be-is-not'), 'E8721': ('PEP8 %s: %s', 'do-not-compare-types-use-isinstance'), 'E8722': ('PEP8 %s: %s', 'pep8-bare-except'), 'E8731': ('PEP8 %s: %s', 'do-not-assign-a-lambda-expression-use-a-def'), 'E8741': ('PEP8 %s: %s', 'bad-variable-identifier-name'), 'E8742': ('PEP8 %s: %s', 'bad-class-identifier-name'), 'E8743': ('PEP8 %s: %s', 'bad-funtion-identifier-name'), } msgs_map = { 'E8722': 'W0702', 'E8741': 'C0103' } class PEP8Runtime(_PEP8BaseChecker): ''' Process PEP8 E9 codes ''' _msgs = { 'E8901': ('PEP8 %s: %s', 'SyntaxError-or-IndentationError'), 'E8902': ('PEP8 %s: %s', 'IOError'), } class PEP8IndentationWarning(_PEP8BaseChecker): ''' Process PEP8 W1 codes ''' _msgs = { 'W8191': ('PEP8 %s: %s', 'indentation-contains-tabs'), } class PEP8WhitespaceWarning(_PEP8BaseChecker): ''' Process PEP8 W2 codes ''' _msgs = { 'W8291': ('PEP8 %s: %s', 'trailing-whitespace' if pylint_version_info < (1, 0) else 'pep8-trailing-whitespace'), 'W8292': ('PEP8 %s: %s', 'no-newline-at-end-of-file'), 'W8293': ('PEP8 %s: %s', 'blank-line-contains-whitespace'), } msgs_map = { 'W8291': 'C0303', 'W8293': 'C0303' } class PEP8BlankLineWarning(_PEP8BaseChecker): ''' Process PEP8 W3 codes ''' _msgs = { 'W8391': ('PEP8 %s: %s', 'blank-line-at-end-of-file'), } class BinaryOperatorLineBreaks(_PEP8BaseChecker): ''' Process PEP8 W5 codes ''' _msgs = { 'W8503': ('PEP8 %s: %s', 'line-break-before-binary-operator'), 'W8504': ('PEP8 %s: %s', 'pep8-line-break-after-binary-operator'), 'W8505': ('PEP8 %s: %s', 'pep8-line-doc-too-long'), } class PEP8DeprecationWarning(_PEP8BaseChecker): ''' Process PEP8 W6 codes ''' _msgs = { 'W8601': ('PEP8 %s: %s', '.has_key-is-deprecated-use-in'), 'W8602': ('PEP8 %s: %s', 'deprecated-form-of-raising-exception'), 'W8603': ('PEP8 %s: %s', 'less-or-more-is-deprecated-use-no-equal'), 'W8604': ('PEP8 %s: %s', 'backticks-are-deprecated-use-repr'), 'W8605': ('PEP8 %s: %s', 'pep8-invalid-escape-sequence'), 'W8606': ('PEP8 %s: %s', 'pep8-reserved-keywords') } msgs_map = { 'W8605': 'W1401' } # ----- Keep Track Of Handled PEP8 MSG IDs --------------------------------------------------------------------------> for checker in list(locals().values()): try: if issubclass(checker, _PEP8BaseChecker): _KNOWN_PEP8_IDS.extend(checker._msgs.keys()) except TypeError: # Not class continue # <---- Keep Track Of Handled PEP8 MSG IDs --------------------------------------------------------------------------- def register(linter): ''' required method to auto register this checker ''' if HAS_PEP8 is False: return linter.register_checker(PEP8Indentation(linter)) linter.register_checker(PEP8Whitespace(linter)) linter.register_checker(PEP8BlankLine(linter)) linter.register_checker(PEP8Import(linter)) linter.register_checker(PEP8LineLength(linter)) linter.register_checker(PEP8Statement(linter)) linter.register_checker(PEP8Runtime(linter)) linter.register_checker(PEP8IndentationWarning(linter)) linter.register_checker(PEP8WhitespaceWarning(linter)) linter.register_checker(PEP8BlankLineWarning(linter)) linter.register_checker(PEP8DeprecationWarning(linter)) salt-pylint-2019.6.7/saltpylint/py3modernize/000077500000000000000000000000001347555016700211105ustar00rootroot00000000000000salt-pylint-2019.6.7/saltpylint/py3modernize/__init__.py000066400000000000000000000216541347555016700232310ustar00rootroot00000000000000# -*- coding: utf-8 -*- # Import Python libs from __future__ import absolute_import import difflib import warnings import logging try: from lib2to3 import fixer_util, refactor, pgen2 from lib2to3.pgen2.parse import ParseError from libmodernize.fixes import lib2to3_fix_names, opt_in_fix_names, six_fix_names HAS_REQUIRED_LIBS = True except ImportError: HAS_REQUIRED_LIBS = False warnings.warn( 'The modernize pylint plugin will not be available. Either ' '\'lib2to3\', unlikely, or \'libmodernize\' was not importable.', RuntimeWarning ) # Import PyLint libs from pylint.interfaces import IRawChecker from saltpylint.checkers import BaseChecker if HAS_REQUIRED_LIBS: FIXES = lib2to3_fix_names FIXES.update(opt_in_fix_names) FIXES.update(six_fix_names) SALT_FILES = set(refactor.get_fixers_from_package('saltpylint.py3modernize.fixes')) ALL_FIXES = SALT_FILES.union(set(FIXES)) # Let's patch lib2to3.fixer_util.touch_import so we can make it import # six from salt.ext # Keep a ref of the original function FIXER_UTIL_TOUCH_IMPORT = fixer_util.touch_import # Define our override def salt_lib2to3_touch_import(package, name, node): if package is None and name == 'six': # from salt.ext import six package = 'salt.ext' elif package is None and 'six' in name: # import six name = 'salt.ext.{0}'.format(name) elif package and 'six' in package: # from six import package = 'salt.ext.{0}'.format(package) FIXER_UTIL_TOUCH_IMPORT(package, name, node) else: ALL_FIXES = () def diff_texts(old, new, diff_context_lines=3): diffs = [] if not isinstance(old, list): old = old.splitlines() if not isinstance(new, list): new = new.splitlines() for group in difflib.SequenceMatcher(None, old, new).get_grouped_opcodes(diff_context_lines): start_line = None diff = [] for tag, i1, i2, j1, j2 in group: if start_line is None: start_line = i1 + diff_context_lines + 1 if tag == 'equal': for line in old[i1:i2]: diff.append(' ' + line) continue if tag in ('replace', 'delete'): for line in old[i1:i2]: diff.append('-' + line) if tag in ('replace', 'insert'): for line in new[j1:j2]: diff.append('+' + line) diffs.append((start_line, '\n'.join(diff))) return diffs class PyLintRefactoringTool(refactor.MultiprocessRefactoringTool): diff = () def print_output(self, old, new, filename, equal): self.diff = () if equal else diff_texts(old, new) class Py3Modernize(BaseChecker): ''' Check for PEP263 compliant file encoding in file. ''' __implements__ = IRawChecker name = 'modernize' msgs = {'W1698': ('Unable to run modernize. Parse Error: %s', 'modernize-parse-error', ('Incompatible Python 3 code found')), 'W1699': ('Incompatible Python 3 code found. Proposed fix:\n%s', 'incompatible-py3-code', ('Incompatible Python 3 code found')), } priority = -1 options = (('modernize-doctests-only', {'default': 0, 'type': 'yn', 'metavar': '', 'help': 'Fix up doctests only'} ), ('modernize-fix', {'default': (), 'type': 'csv', 'metavar': '', 'help': 'Each FIX specifies a transformation; "default" includes ' 'default fixes.'} ), ('modernize-nofix', {'default': '', 'type': 'multiple_choice', 'metavar': '', 'choices': sorted(ALL_FIXES), 'help': 'Comma separated list of fixer names not to fix.'} ), ('modernize-print-function', {'default': 1, 'type': 'yn', 'metavar': '', 'help': 'Modify the grammar so that print() is a function.'} ), ('modernize-six-unicode', {'default': 0, 'type': 'yn', 'metavar': '', 'help': 'Wrap unicode literals in six.u().'} ), ('modernize-future-unicode', {'default': 0, 'type': 'yn', 'metavar': '', 'help': 'Use \'from __future__ import unicode_literals\' (only ' 'useful for Python 2.6+).'} ), ('modernize-no-six', {'default': 0, 'type': 'yn', 'metavar': '', 'help': 'Exclude fixes that depend on the six package.'} ) ) def process_module(self, node): ''' process a module ''' # Patch lib2to3.fixer_util.touch_import! fixer_util.touch_import = salt_lib2to3_touch_import flags = {} if self.config.modernize_print_function: flags['print_function'] = True salt_avail_fixes = set( refactor.get_fixers_from_package( 'saltpylint.py3modernize.fixes' ) ) avail_fixes = set(refactor.get_fixers_from_package('libmodernize.fixes')) avail_fixes.update(lib2to3_fix_names) avail_fixes.update(salt_avail_fixes) default_fixes = avail_fixes.difference(opt_in_fix_names) unwanted_fixes = set(self.config.modernize_nofix) # Explicitly disable libmodernize.fixes.fix_dict_six since we have our own implementation # which only fixes `dict.iter()` calls unwanted_fixes.add('libmodernize.fixes.fix_dict_six') if self.config.modernize_six_unicode: unwanted_fixes.add('libmodernize.fixes.fix_unicode_future') elif self.config.modernize_future_unicode: unwanted_fixes.add('libmodernize.fixes.fix_unicode') else: unwanted_fixes.add('libmodernize.fixes.fix_unicode_future') unwanted_fixes.add('libmodernize.fixes.fix_unicode') if self.config.modernize_no_six: unwanted_fixes.update(six_fix_names) unwanted_fixes.update(salt_avail_fixes) else: # We explicitly will remove fix_imports_six from libmodernize and will add # our own fix_imports_six unwanted_fixes.add('libmodernize.fixes.fix_imports_six') # Remove a bunch of libmodernize.fixes since we need to properly skip them # and we provide the proper skip rule unwanted_fixes.add('libmodernize.fixes.fix_input_six') unwanted_fixes.add('libmodernize.fixes.fix_filter') unwanted_fixes.add('libmodernize.fixes.fix_map') unwanted_fixes.add('libmodernize.fixes.fix_xrange_six') unwanted_fixes.add('libmodernize.fixes.fix_zip') explicit = set() if self.config.modernize_fix: default_present = False for fix in self.config.modernize_fix: if fix == 'default': default_present = True else: explicit.add(fix) requested = default_fixes.union(explicit) if default_present else explicit else: requested = default_fixes requested = default_fixes fixer_names = requested.difference(unwanted_fixes) rft = PyLintRefactoringTool(sorted(fixer_names), flags, sorted(explicit)) try: rft.refactor_file(node.file, write=False, doctests_only=self.config.modernize_doctests_only) except ParseError as exc: # Unable to refactor, let's not make PyLint crash try: lineno = exc.context[1][0] line_contents = node.file_stream.readlines()[lineno-1].rstrip() self.add_message('W1698', line=lineno, args=line_contents) except Exception: # pylint: disable=broad-except self.add_message('W1698', line=1, args=exc) return except AssertionError as exc: self.add_message('W1698', line=1, args=exc) return except (IOError, OSError) as exc: logging.getLogger(__name__).warn('Error while processing {0}: {1}'.format(node.file, exc)) return for lineno, diff in rft.diff: # Since PyLint's python3 checker uses 16, we'll also use that range self.add_message('W1699', line=lineno, args=diff) # Restore lib2to3.fixer_util.touch_import! fixer_util.touch_import = FIXER_UTIL_TOUCH_IMPORT def register(linter): ''' required method to auto register this checker ''' if HAS_REQUIRED_LIBS: linter.register_checker(Py3Modernize(linter)) salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/000077500000000000000000000000001347555016700222265ustar00rootroot00000000000000salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/__init__.py000066400000000000000000000000301347555016700243300ustar00rootroot00000000000000# -*- coding: utf-8 -*- salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_dict_salt_six.py000066400000000000000000000006251347555016700263020ustar00rootroot00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from libmodernize.fixes import fix_dict_six class FixDictSaltSix(fix_dict_six.FixDictSix): def transform(self, node, results): method = results['method'][0] method_name = method.value if method_name not in ('keys', 'items', 'values'): return self.transform_iter(method_name, node, results['head']) salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_filter_salt_six.py000066400000000000000000000003051347555016700266370ustar00rootroot00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from libmodernize.fixes import fix_filter class FixFilterSaltSix(fix_filter.FixFilter): skip_on = 'salt.ext.six.moves.filter' salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_imports_salt_six.py000066400000000000000000000007431347555016700270550ustar00rootroot00000000000000# -*- coding: utf-8 -*- # Import python libs from __future__ import absolute_import from lib2to3.fixes import fix_imports as lib2to3_fix_imports # Import 3rd-party libs import six from libmodernize.fixes import fix_imports_six as libmodernize_fix_imports MAPPING = {} for key, value in six.iteritems(libmodernize_fix_imports.FixImportsSix.mapping): MAPPING[key] = 'salt.ext.{0}'.format(value) class FixImportsSaltSix(lib2to3_fix_imports.FixImports): mapping = MAPPING salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_input_salt_six.py000066400000000000000000000012611347555016700265130ustar00rootroot00000000000000# -*- coding: utf-8 -*- # This is a derived work of libmodernize.fixes.fix_input_six which # in turn is deriverd work of Lib/lib2to3/fixes/fix_input.py and # Lib/lib2to3/fixes/fix_raw_input.py. Those files are under the # copyright of the Python Software Foundation and licensed under the # Python Software Foundation License 2. # # Copyright notice: # # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # 2011, 2012, 2013, 2014 Python Software Foundation. All rights reserved. from __future__ import absolute_import from libmodernize.fixes import fix_input_six class FixInputSaltSix(fix_input_six.FixInputSix): skip_on = 'salt.ext.six.moves.input' salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_map_salt_six.py000066400000000000000000000002661347555016700261350ustar00rootroot00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from libmodernize.fixes import fix_map class FixMapSaltSix(fix_map.FixMap): skip_on = 'salt.ext.six.moves.map' salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_xrange_salt_six.py000066400000000000000000000003171347555016700266410ustar00rootroot00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from libmodernize.fixes import fix_xrange_six class FixXrangeSaltSix(fix_xrange_six.FixXrangeSix): skip_on = 'salt.ext.six.moves.range' salt-pylint-2019.6.7/saltpylint/py3modernize/fixes/fix_zip_salt_six.py000066400000000000000000000002661347555016700261620ustar00rootroot00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import from libmodernize.fixes import fix_zip class FixZipSaltSix(fix_zip.FixZip): skip_on = 'salt.ext.six.moves.zip' salt-pylint-2019.6.7/saltpylint/smartup.py000066400000000000000000000023521347555016700205270ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2013-2018 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. =========================== Pylint Smartup Transformers =========================== This plugin will register some transform functions which will allow PyLint to better understand some classed used in Salt which trigger, `no-member` and `maybe-no-member` A bridge between the `pep8`_ library and PyLint ''' # Import Python libs from __future__ import absolute_import # Import PyLint libs from astroid import nodes, MANAGER def rootlogger_transform(obj): if obj.name != 'RootLogger': return def _inject_method(cls, msg, *args, **kwargs): pass if not hasattr(obj, 'trace'): setattr(obj, 'trace', _inject_method) if not hasattr(obj, 'garbage'): setattr(obj, 'garbage', _inject_method) def register(linter): ''' Register the transformation functions. ''' try: MANAGER.register_transform(nodes.Class, rootlogger_transform) except AttributeError: MANAGER.register_transform(nodes.ClassDef, rootlogger_transform) salt-pylint-2019.6.7/saltpylint/strings.py000066400000000000000000000235771347555016700205410ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` ========================================== PyLint Extended String Formatting Checkers ========================================== Proper string formatting PyLint checker ''' # Import Python libs from __future__ import absolute_import import re import sys import tokenize # Import PyLint libs try: # >= pylint 1.0 import astroid except ImportError: # pylint < 1.0 from logilab import astng as astroid # pylint: disable=no-name-in-module from saltpylint.checkers import BaseChecker, utils from pylint.checkers import BaseTokenChecker try: # >= pylint 1.0 from pylint.interfaces import IAstroidChecker except ImportError: # < pylint 1.0 from pylint.interfaces import IASTNGChecker as IAstroidChecker # pylint: disable=no-name-in-module from pylint.interfaces import ITokenChecker, IRawChecker from astroid.exceptions import InferenceError try: from astroid.exceptions import NameInferenceError except ImportError: class NameInferenceError(Exception): pass # Import 3rd-party libs import six STRING_FORMAT_MSGS = { 'W1320': ('String format call with un-indexed curly braces: %r', 'un-indexed-curly-braces-warning', 'Under python 2.6 the curly braces on a \'string.format()\' ' 'call MUST be indexed.'), 'E1320': ('String format call with un-indexed curly braces: %r', 'un-indexed-curly-braces-error', 'Under python 2.6 the curly braces on a \'string.format()\' ' 'call MUST be indexed.'), 'W1321': ('String substitution used instead of string formattting on: %r', 'string-substitution-usage-warning', 'String substitution used instead of string formattting'), 'E1321': ('String substitution used instead of string formattting on: %r', 'string-substitution-usage-error', 'String substitution used instead of string formattting'), 'E1322': ('Repr flag (!r) used in string: %r', 'repr-flag-used-in-string', 'Repr flag (!r) used in string'), 'E1323': ('String formatting used in logging: %r', 'str-format-in-logging', 'String formatting used in logging'), } BAD_FORMATTING_SLOT = re.compile(r'(\{![\w]{1}\}|\{\})') class StringCurlyBracesFormatIndexChecker(BaseChecker): __implements__ = IAstroidChecker name = 'string' msgs = STRING_FORMAT_MSGS priority = -1 options = (('un-indexed-curly-braces-always-error', {'default': 1, 'type': 'yn', 'metavar': '', 'help': 'Force un-indexed curly braces on a ' '\'string.format()\' call to always be an error.'} ), ('enforce-string-formatting-over-substitution', {'default': 1, 'type': 'yn', 'metavar': '', 'help': 'Enforce string formatting over string substitution'} ), ('string-substitutions-usage-is-an-error', {'default': 1, 'type': 'yn', 'metavar': '', 'help': 'Force string substitution usage on strings ' 'to always be an error.'} ), ) @utils.check_messages(*(STRING_FORMAT_MSGS.keys())) def visit_binop(self, node): if not self.config.enforce_string_formatting_over_substitution: return if node.op != '%': return if not (isinstance(node.left, astroid.Const) and isinstance(node.left.value, six.string_types)): return try: required_keys, required_num_args = utils.parse_format_string(node.left.value)[:2] except (utils.UnsupportedFormatCharacter, utils.IncompleteFormatString): # This is handled elsewere return if required_keys or required_num_args: if self.config.string_substitutions_usage_is_an_error: msgid = 'E1321' else: msgid = 'W1321' self.add_message( msgid, node=node.left, args=node.left.value ) if '!r}' in node.left.value: self.add_message( 'E1322', node=node.left, args=node.left.value ) @utils.check_messages(*(STRING_FORMAT_MSGS.keys())) def visit_call(self, node): func = utils.safe_infer(node.func) if isinstance(func, astroid.BoundMethod) and func.name == 'format': # If there's a .format() call, run the code below if isinstance(node.func.expr, (astroid.Name, astroid.Const)): # This is for: # foo = 'Foo {} bar' # print(foo.format(blah) for inferred in node.func.expr.infer(): if not hasattr(inferred, 'value'): # If there's no value attribute, it's not worth # checking. continue if not isinstance(inferred.value, six.string_types): # If it's not a string, continue continue if '!r}' in inferred.value: self.add_message( 'E1322', node=inferred, args=inferred.value ) if BAD_FORMATTING_SLOT.findall(inferred.value): if self.config.un_indexed_curly_braces_always_error or \ sys.version_info[:2] < (2, 7): self.add_message( 'E1320', node=inferred, args=inferred.value ) elif six.PY2: self.add_message( 'W1320', node=inferred, args=inferred.value ) try: # Walk back up until no parents are found and look for a # logging.RootLogger instance in the parent types ptr = node while True: parent = ptr.parent for inferred in parent.func.expr.infer(): try: instance_type = inferred.pytype().split('.')[0] except TypeError: continue if instance_type == 'logging': self.add_message( 'E1323', node=node, args=node.as_string(), ) break ptr = parent except (AttributeError, InferenceError, NameInferenceError): pass elif not hasattr(node.func.expr, 'value'): # If it does not have an value attribute, it's not worth # checking return elif isinstance(node.func.expr.value, astroid.Name): # No need to check these either return elif BAD_FORMATTING_SLOT.findall(node.func.expr.value): if self.config.un_indexed_curly_braces_always_error or \ sys.version_info[:2] < (2, 7): msgid = 'E1320' else: msgid = 'W1320' self.add_message( 'E1320', node=node, args=node.func.expr.value ) STRING_LITERALS_MSGS = { 'E1400': ('Null byte used in unicode string literal (should be wrapped in str())', 'null-byte-unicode-literal', 'Null byte used in unicode string literal'), } class StringLiteralChecker(BaseTokenChecker): ''' Check string literals ''' __implements__ = (ITokenChecker, IRawChecker) name = 'string_literal' msgs = STRING_LITERALS_MSGS def process_module(self, module): self._unicode_literals = 'unicode_literals' in module.future_imports def process_tokens(self, tokens): for (tok_type, token, (start_row, _), _, _) in tokens: if tok_type == tokenize.STRING: # 'token' is the whole un-parsed token; we can look at the start # of it to see whether it's a raw or unicode string etc. self.process_string_token(token, start_row) def process_string_token(self, token, start_row): if not six.PY2: return for i, c in enumerate(token): if c in '\'\"': quote_char = c break # pylint: disable=undefined-loop-variable prefix = token[:i].lower() # markers like u, b, r. after_prefix = token[i:] if after_prefix[:3] == after_prefix[-3:] == 3 * quote_char: string_body = after_prefix[3:-3] else: string_body = after_prefix[1:-1] # Chop off quotes # No special checks on raw strings at the moment. if 'r' not in prefix: self.process_non_raw_string_token(prefix, string_body, start_row) def process_non_raw_string_token(self, prefix, string_body, start_row): ''' check for bad escapes in a non-raw string. prefix: lowercase string of eg 'ur' string prefix markers. string_body: the un-parsed body of the string, not including the quote marks. start_row: integer line number in the source. ''' if 'u' in prefix: if string_body.find('\\0') != -1: self.add_message('null-byte-unicode-literal', line=start_row) def register(linter): '''required method to auto register this checker ''' linter.register_checker(StringCurlyBracesFormatIndexChecker(linter)) linter.register_checker(StringLiteralChecker(linter)) salt-pylint-2019.6.7/saltpylint/thirdparty.py000066400000000000000000000163231347555016700212310ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2017 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. saltpylint.thirdparty ~~~~~~~~~~~~~~~~~~~~~ Checks all imports against a list of known and allowed 3rd-party modules and raises a lint error if an import not in that known 3rd-party modules list is not gated. ''' # Import python libs from __future__ import absolute_import import os # Import pylint libs import astroid import astroid.exceptions from astroid.modutils import is_relative, is_standard_module from pylint.interfaces import IAstroidChecker from saltpylint.checkers import BaseChecker, utils MSGS = { 'W8410': ('3rd-party module import is not gated in a try/except: %r', '3rd-party-module-not-gated', '3rd-party module imported without being gated in a try/except.'), 'C8410': ('3rd-party local module import is not gated in a try/except: %r. ' 'Consider importing at the module global scope level and gate it ' 'in a try/except.', '3rd-party-local-module-not-gated', '3rd-party module locally imported without being gated. Consider importing ' 'at the module global scope level and gate it in a try/except'), } def get_import_package(modname): ''' Return the import package. Given modname is 'salt.utils', returns 'salt' ''' return modname.split('.')[0] class ThirdPartyImportsChecker(BaseChecker): __implements__ = IAstroidChecker name = '3rd-party-imports' msgs = MSGS priority = -2 options = ( ('allowed-3rd-party-modules', { 'default': (), 'type': 'csv', 'metavar': '<3rd-party-modules>', 'help': 'Known 3rd-party modules which don\' require being gated, separated by a comma'}), ) known_py2_modules = ('__builtin__', 'exceptions') known_py3_modules = ('ipaddress', 'builtins') known_common_std_modules = ('__future__',) known_std_modules = known_py2_modules + known_py3_modules + known_common_std_modules def __init__(self, linter=None): BaseChecker.__init__(self, linter) self._inside_try_except = False self._inside_funcdef = False self._inside_if = False self.cwd = self.allowed_3rd_party_modules = None def open(self): super(ThirdPartyImportsChecker, self).open() self.cwd = os.getcwd() self.allowed_3rd_party_modules = set(self.config.allowed_3rd_party_modules) # pylint: disable=no-member # pylint: disable=unused-argument @utils.check_messages('3rd-party-imports') def visit_if(self, node): self._inside_if = True @utils.check_messages('3rd-party-imports') def leave_if(self, node): self._inside_if = True @utils.check_messages('3rd-party-imports') def visit_tryexcept(self, node): self._inside_try_except = True @utils.check_messages('3rd-party-imports') def leave_tryexcept(self, node): self._inside_try_except = False @utils.check_messages('3rd-party-imports') def visit_functiondef(self, node): self._inside_funcdef = True @utils.check_messages('3rd-party-imports') def leave_functiondef(self, node): self._inside_funcdef = False # pylint: enable=unused-argument @utils.check_messages('3rd-party-imports') def visit_import(self, node): names = [name for name, _ in node.names] for name in names: self._check_third_party_import(node, name) @utils.check_messages('3rd-party-imports') def visit_importfrom(self, node): self._check_third_party_import(node, node.modname) def _check_third_party_import(self, node, modname): if modname in self.known_std_modules: # Don't even care about these return module_file = node.root().file if is_relative(modname, module_file): # Is the import relative to the curent module being checked return base_modname = modname.split('.', 1)[0] import_modname = modname while True: try: imported_module = node.do_import_module(import_modname) if not imported_module: break if imported_module.file.startswith(self.cwd): # This is an import to package under the project being tested return # If we reached this far, we were able to import the module but it's # not considered a module from within the project being checked break except Exception: # pylint: disable=broad-except # This is, for example, from salt.ext.six.moves import Y # Because `moves` is a dynamic/runtime module import_modname = import_modname.rsplit('.', 1)[0] if import_modname == base_modname or not import_modname: break try: if not is_standard_module(modname): if self._inside_try_except is False: if get_import_package(modname) in self.allowed_3rd_party_modules: return if self._inside_if or self._inside_funcdef: message_id = '3rd-party-local-module-not-gated' else: message_id = '3rd-party-module-not-gated' self.add_message(message_id, node=node, args=modname) except astroid.exceptions.AstroidBuildingException: # Failed to import if self._inside_try_except is False: if get_import_package(modname) in self.allowed_3rd_party_modules: return if self._inside_if or self._inside_funcdef: message_id = '3rd-party-local-module-not-gated' else: message_id = '3rd-party-module-not-gated' self.add_message(message_id, node=node, args=modname) except astroid.exceptions.InferenceError: # Failed to import if self._inside_try_except is False: if get_import_package(modname) in self.allowed_3rd_party_modules: return if self._inside_if or self._inside_funcdef: message_id = '3rd-party-local-module-not-gated' else: message_id = '3rd-party-module-not-gated' self.add_message(message_id, node=node, args=modname) except ImportError: # Definitly not a standard library import if self._inside_try_except is False: if get_import_package(modname) in self.allowed_3rd_party_modules: return if self._inside_if or self._inside_funcdef: message_id = '3rd-party-local-module-not-gated' else: message_id = '3rd-party-module-not-gated' self.add_message(message_id, node=node, args=modname) def register(linter): '''required method to auto register this checker ''' linter.register_checker(ThirdPartyImportsChecker(linter)) salt-pylint-2019.6.7/saltpylint/version.py000066400000000000000000000006541347555016700205240ustar00rootroot00000000000000# -*- coding: utf-8 -*- ''' saltpylint.version ~~~~~~~~~~~~~~~~~~~ :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)` :copyright: © 2013-2018 by the SaltStack Team, see AUTHORS for more details. :license: Apache 2.0, see LICENSE for more details. ''' # Import Python libs from __future__ import absolute_import __version_info__ = (2019, 1, 11) __version__ = '{0}.{1}.{2}'.format(*__version_info__) salt-pylint-2019.6.7/saltpylint/virt.py000066400000000000000000000045561347555016700200300ustar00rootroot00000000000000from __future__ import absolute_import import astroid from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker class VirtChecker(BaseChecker): ''' checks for compliance inside __virtual__ ''' __implements__ = IAstroidChecker name = 'virt-checker' VIRT_LOG = 'log-in-virtual' msgs = { 'E1401': ('Log statement detected inside __virtual__ function. Remove it.', VIRT_LOG, 'Loader processes __virtual__ so logging not in scope'), } options = () priority = -1 def visit_functiondef(self, node): ''' Verifies no logger statements inside __virtual__ ''' if (not isinstance(node, astroid.FunctionDef) or node.is_method() or node.type != 'function' or not node.body ): # only process functions return try: if not node.name == '__virtual__': # only need to process the __virtual__ function return except AttributeError: return # walk contents of __virtual__ function for child in node.get_children(): for functions in child.get_children(): if isinstance(functions, astroid.Call): if isinstance(functions.func, astroid.Attribute): try: # Inspect each statement for an instance of 'logging' for inferred in functions.func.expr.infer(): try: instance_type = inferred.pytype().split('.')[0] except TypeError: continue if instance_type == 'logging': self.add_message( self.VIRT_LOG, node=functions ) # Found logger, don't need to keep processing this line break except AttributeError: # Not a log function return def register(linter): ''' required method to auto register this checker ''' linter.register_checker(VirtChecker(linter)) salt-pylint-2019.6.7/setup.cfg000066400000000000000000000001701347555016700160740ustar00rootroot00000000000000[upload] sign = true identity = 84A298FF [aliases] release = clean sdist bdist_wheel upload [bdist_wheel] universal=1 salt-pylint-2019.6.7/setup.py000077500000000000000000000065101347555016700157740ustar00rootroot00000000000000#!/usr/bin/env python # -*- coding: utf-8 -*- ''' The setup script for SaltPyLint ''' # pylint: disable=file-perms,wrong-import-position from __future__ import absolute_import, with_statement import io import os import sys SETUP_KWARGS = {} USE_SETUPTOOLS = False # Change to salt source's directory prior to running any command try: SETUP_DIRNAME = os.path.dirname(__file__) except NameError: # We're most likely being frozen and __file__ triggered this NameError # Let's work around that SETUP_DIRNAME = os.path.dirname(sys.argv[0]) if SETUP_DIRNAME != '': os.chdir(SETUP_DIRNAME) SALT_PYLINT_REQS = os.path.join(os.path.abspath(SETUP_DIRNAME), 'requirements.txt') def _parse_requirements_file(requirements_file): ''' Parse requirements.txt and return list suitable for passing to ``install_requires`` parameter in ``setup()``. ''' parsed_requirements = [] with open(requirements_file) as rfh: for line in rfh.readlines(): line = line.strip() if not line or line.startswith(('#', '-r')): continue parsed_requirements.append(line) return parsed_requirements def _release_version(): ''' Returns release version ''' with io.open(os.path.join(SETUP_DIRNAME, 'saltpylint', 'version.py'), encoding='utf-8') as fh_: exec_locals = {} exec_globals = {} contents = fh_.read() if not isinstance(contents, str): contents = contents.encode('utf-8') exec(contents, exec_globals, exec_locals) # pylint: disable=exec-used return exec_locals['__version__'] # Use setuptools only if the user opts-in by setting the USE_SETUPTOOLS env var. # Or if setuptools was previously imported (which is the case when using 'pip'). # This ensures consistent behavior, but allows for advanced usage with # virtualenv, buildout, and others. if 'USE_SETUPTOOLS' in os.environ or 'setuptools' in sys.modules: try: from setuptools import setup USE_SETUPTOOLS = True # This allows correct installation of dependencies with ``pip install``. SETUP_KWARGS['install_requires'] = _parse_requirements_file(SALT_PYLINT_REQS) except ImportError: USE_SETUPTOOLS = False if USE_SETUPTOOLS is False: from distutils.core import setup # pylint: disable=import-error,no-name-in-module NAME = 'SaltPyLint' VERSION = _release_version() DESCRIPTION = ( 'Required PyLint plugins needed in the several SaltStack projects.' ) setup( name=NAME, version=VERSION, description=DESCRIPTION, author='Pedro Algarvio', author_email='pedro@algarvio.me', url='https://github.com/saltstack/salt-pylint', classifiers=[ 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.4', 'Development Status :: 5 - Production/Stable', 'Environment :: Console', 'Intended Audience :: Developers', 'Intended Audience :: Information Technology', 'Intended Audience :: System Administrators', 'License :: OSI Approved :: Apache Software License', 'Operating System :: POSIX :: Linux', ], packages=[ 'saltpylint', 'saltpylint.ext', 'saltpylint/py3modernize', 'saltpylint/py3modernize/fixes', ], **SETUP_KWARGS )