k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/hack/pylint_bin.py (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2017 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  import os
    18  import sys
    19  
    20  import pylint
    21  
    22  if __name__ == '__main__':
    23      # Otherwise bazel's symlinks confuse pylint/astroid
    24      EXTRAS = set()
    25      for path in sys.path:
    26          if not os.path.isdir(path):
    27              continue
    28          for something in os.listdir(path):
    29              # bazel stopped symlinking dirs, so this now depends on files like
    30              # bazel-out/k8-fastbuild/bin/hack/verify-pylint.runfiles/pypi__sh_1_12_14/ pylint: disable=line-too-long
    31              # containing a sh.py symlink to external/pypi__sh_1_12_14/sh.py
    32              # imports that do not have files in the root dir will not have a
    33              # different real name.
    34              full = os.path.join(path, something)
    35              real = os.path.realpath(full)
    36              # If we use pip_import() then there will be
    37              # a WHEEL file symlink we can use to find the real path.
    38              # TODO(fejta): https://github.com/kubernetes/test-infra/issues/13162
    39              # (ruamel has C code, which hasn't yet worked with pip_import())
    40              if real == full: # bazel stopped symlinking dirs
    41                  wheel = os.path.join(full, 'WHEEL')
    42                  if not os.path.isfile(wheel):
    43                      continue
    44                  real = os.path.dirname(os.path.realpath(wheel))
    45              if real == full:
    46                  continue
    47              EXTRAS.add(os.path.dirname(real))
    48              break
    49      # also do one level up so foo.bar imports work :shrug:
    50      EXTRAS = set(os.path.dirname(e) for e in EXTRAS).union(EXTRAS)
    51      # append these to the path
    52      sys.path.extend(EXTRAS)
    53  
    54      # Otherwise this is the entirety of bin/pylint
    55      pylint.run_pylint()