Don't make depot_tools pylintrc override local pylint configs.

In the current pylint handling, the only way to use an alternative
config is to explicitly pass it with the "--rcfile" flag, which is
confusingly inconsistent with normal pylint config file resolution.
This change allows local pylintrc files or PYLINTRC env var to override
the default depot_tools config, which is often desirable to enforce
different style guidelines for different projects.

For reference, the config file search order[1] is:
1) File specified by the "--rcfile" flag
2) pylintrc in the CWD
3) .pylintrc in the CWD
4) first pylintrc found up the python module hierarchy, if CWD is in
   a module hierarchy
5) $PYLINTRC env var
6) $HOME/.pylintrc
7) $HOME/.config/pylintrc
8) /etc/pylintrc (or equivalent on other platforms)

- 1-5 will take precedence over the new depot_tools default.
- If there is no match for 1-5 and there is no PYLINTRC env var,
  depot_tools will set PYLINTRC to its default and that will be used.
- If there is no match for 1-5 and the PYLINTRC env var is empty, then
  it will fall through to 6-8.

[1] Newer pylints have additional options, but these are the options,
and the order, common to all versions of depot_tools pylint.

Change-Id: Ib725b15bb639dc9c7cb9009fd3b504124e0c1f2a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2708749
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
Commit-Queue: Michael Moss <mmoss@chromium.org>
diff --git a/pylint_main.py b/pylint_main.py
index f2d868d..d03021d 100755
--- a/pylint_main.py
+++ b/pylint_main.py
@@ -13,9 +13,6 @@
 import os
 import sys
 
-from pylint import lint
-
-
 HERE = os.path.dirname(os.path.abspath(__file__))
 PYLINT = os.path.join(HERE, 'pylint_main.py')
 RC_FILE = os.path.join(HERE, 'pylintrc')
@@ -31,13 +28,27 @@
     argv = [x for x in argv if x != ARGS_ON_STDIN]
     argv.extend(x.strip() for x in sys.stdin)
 
-  # We prepend the command-line with the depot_tools rcfile. If another rcfile
-  # is to be used, passing --rcfile a second time on the command-line will work
-  # fine.
-  if os.path.isfile(RC_FILE):
-    # The file can be removed to test 'normal' pylint behavior.
-    argv.insert(0, '--rcfile=%s' % RC_FILE)
+  # Set default config options with the PYLINTRC environment variable. This will
+  # allow overriding with "more local" config file options, such as a local
+  # "pylintrc" file, the "--rcfile" command-line flag, or an existing PYLINTRC.
+  #
+  # Note that this is not quite the same thing as replacing pylint's built-in
+  # defaults, since, based on config file precedence, it will not be overridden
+  # by "more global" config file options, such as ~/.pylintrc,
+  # ~/.config/pylintrc, or /etc/pylintrc. This is generally the desired
+  # behavior, since we want to enforce these defaults in most cases, but allow
+  # them to be overridden for specific code or repos.
+  #
+  # If someone really doesn't ever want the depot_tools pylintrc, they can set
+  # their own PYLINTRC, or set an empty PYLINTRC to use pylint's normal config
+  # file resolution, which would include the "more global" options that are
+  # normally overridden by the depot_tools config.
+  if os.path.isfile(RC_FILE) and 'PYLINTRC' not in os.environ:
+    os.environ['PYLINTRC'] = RC_FILE
 
+  # This import has to happen after PYLINTRC is set because the module tries to
+  # resolve the config file location on load.
+  from pylint import lint  # pylint: disable=bad-option-value,import-outside-toplevel
   lint.Run(argv)