sigs.k8s.io/cluster-api@v1.7.1/hack/boilerplate/boilerplate.py (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2015 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  from __future__ import print_function
    18  
    19  import argparse
    20  import datetime
    21  import difflib
    22  import glob
    23  import os
    24  import re
    25  import sys
    26  
    27  parser = argparse.ArgumentParser()
    28  parser.add_argument(
    29      "filenames",
    30      help="list of files to check, all files if unspecified",
    31      nargs='*')
    32  
    33  rootdir = os.path.dirname(__file__) + "/../../"
    34  rootdir = os.path.abspath(rootdir)
    35  parser.add_argument(
    36      "--rootdir", default=rootdir, help="root directory to examine")
    37  
    38  default_boilerplate_dir = os.path.join(rootdir, "hack/boilerplate")
    39  parser.add_argument(
    40      "--boilerplate-dir", default=default_boilerplate_dir)
    41  
    42  parser.add_argument(
    43      "-v", "--verbose",
    44      help="give verbose output regarding why a file does not pass",
    45      action="store_true")
    46  
    47  args = parser.parse_args()
    48  
    49  verbose_out = sys.stderr if args.verbose else open("/dev/null", "w")
    50  
    51  def get_refs():
    52      refs = {}
    53  
    54      for path in glob.glob(os.path.join(args.boilerplate_dir, "boilerplate.*.txt")):
    55          extension = os.path.basename(path).split(".")[1]
    56  
    57          ref_file = open(path, 'r')
    58          ref = ref_file.read().splitlines()
    59          ref_file.close()
    60          refs[extension] = ref
    61  
    62      return refs
    63  
    64  def is_generated_file(filename, data, regexs):
    65      for d in skipped_ungenerated_files:
    66          if d in filename:
    67              return False
    68  
    69      p = regexs["generated"]
    70      return p.search(data)
    71  
    72  def file_passes(filename, refs, regexs):
    73      try:
    74          f = open(filename, 'r')
    75      except Exception as exc:
    76          print("Unable to open %s: %s" % (filename, exc), file=verbose_out)
    77          return False
    78  
    79      data = f.read()
    80      f.close()
    81  
    82      # determine if the file is automatically generated
    83      generated = is_generated_file(filename, data, regexs)
    84  
    85      basename = os.path.basename(filename)
    86      extension = file_extension(filename)
    87      if generated:
    88          if extension == "go":
    89              extension = "generatego"
    90          elif extension == "bzl":
    91              extension = "generatebzl"
    92  
    93      if extension != "":
    94          ref = refs[extension]
    95      else:
    96          ref = refs[basename]
    97  
    98      # remove extra content from the top of files
    99      if extension == "go" or extension == "generatego":
   100          p = regexs["go_build_constraints"]
   101          (data, found) = p.subn("", data, 1)
   102      elif extension in ["sh", "py"]:
   103          p = regexs["shebang"]
   104          (data, found) = p.subn("", data, 1)
   105  
   106      data = data.splitlines()
   107  
   108      # if our test file is smaller than the reference it surely fails!
   109      if len(ref) > len(data):
   110          print('File %s smaller than reference (%d < %d)' %
   111                (filename, len(data), len(ref)),
   112                file=verbose_out)
   113          return False
   114  
   115      # trim our file to the same number of lines as the reference file
   116      data = data[:len(ref)]
   117  
   118      p = regexs["year"]
   119      for d in data:
   120          if p.search(d):
   121              if generated:
   122                  print('File %s has the YEAR field, but it should not be in generated file' %
   123                        filename, file=verbose_out)
   124              else:
   125                  print('File %s has the YEAR field, but missing the year of date' %
   126                        filename, file=verbose_out)
   127              return False
   128  
   129      if not generated:
   130          # Replace all occurrences of the regex "2014|2015|2016|2017|2018" with "YEAR"
   131          p = regexs["date"]
   132          for i, d in enumerate(data):
   133              (data[i], found) = p.subn('YEAR', d)
   134              if found != 0:
   135                  break
   136  
   137      # if we don't match the reference at this point, fail
   138      if ref != data:
   139          print("Header in %s does not match reference, diff:" %
   140                filename, file=verbose_out)
   141          if args.verbose:
   142              print(file=verbose_out)
   143              for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''):
   144                  print(line, file=verbose_out)
   145              print(file=verbose_out)
   146  
   147          return False
   148  
   149      return True
   150  
   151  def file_extension(filename):
   152      return os.path.splitext(filename)[1].split(".")[-1].lower()
   153  
   154  skipped_dirs = ['_output', '.git', "hack/boilerplate/test"]
   155  
   156  # list all the files contain 'DO NOT EDIT', but are not generated
   157  skipped_ungenerated_files = [
   158      'hack/lib/swagger.sh',
   159      'hack/boilerplate/boilerplate.py',
   160      # The generator injects `DO NOT EDIT` and thus needs to get excluded to not
   161      # get detected as false positive.
   162      'hack/tools/prowjob-gen/generator.go',
   163      ]
   164  
   165  def normalize_files(files):
   166      newfiles = []
   167      for pathname in files:
   168          if any(x in pathname for x in skipped_dirs):
   169              continue
   170          newfiles.append(pathname)
   171      for i, pathname in enumerate(newfiles):
   172          if not os.path.isabs(pathname):
   173              newfiles[i] = os.path.join(args.rootdir, pathname)
   174      return newfiles
   175  
   176  def get_files(extensions):
   177      files = []
   178      if len(args.filenames) > 0:
   179          files = args.filenames
   180      else:
   181          for root, dirs, walkfiles in os.walk(args.rootdir):
   182              # don't visit certain dirs. This is just a performance improvement
   183              # as we would prune these later in normalize_files(). But doing it
   184              # cuts down the amount of filesystem walking we do and cuts down
   185              # the size of the file list
   186              for d in skipped_dirs:
   187                  if d in dirs:
   188                      dirs.remove(d)
   189  
   190              for name in walkfiles:
   191                  pathname = os.path.join(root, name)
   192                  files.append(pathname)
   193  
   194      files = normalize_files(files)
   195      outfiles = []
   196      for pathname in files:
   197          basename = os.path.basename(pathname)
   198          extension = file_extension(pathname)
   199          if extension in extensions or basename in extensions:
   200              outfiles.append(pathname)
   201      return outfiles
   202  
   203  def get_dates():
   204      years = datetime.datetime.now().year
   205      return '(%s)' % '|'.join((str(year) for year in range(2014, years+1)))
   206  
   207  def get_regexs():
   208      regexs = {}
   209      # Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
   210      regexs["year"] = re.compile('YEAR')
   211      # get_dates return 2014, 2015, 2016, 2017, or 2018 until the current year as a regex like: "(2014|2015|2016|2017|2018)";
   212      # company holder names can be anything
   213      regexs["date"] = re.compile(get_dates())
   214      # strip the following build constraints/tags:
   215      # //go:build
   216      # // +build \n\n
   217      regexs["go_build_constraints"] = re.compile(
   218          r"^(//(go:build| \+build).*\n)+\n", re.MULTILINE)
   219      # strip #!.* from scripts
   220      regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
   221      # Search for generated files
   222      regexs["generated"] = re.compile('DO NOT EDIT')
   223      return regexs
   224  
   225  def main():
   226      regexs = get_regexs()
   227      refs = get_refs()
   228      filenames = get_files(refs.keys())
   229  
   230      for filename in filenames:
   231          if not file_passes(filename, refs, regexs):
   232              print(filename, file=sys.stdout)
   233  
   234      return 0
   235  
   236  if __name__ == "__main__":
   237      sys.exit(main())