github.com/google/cadvisor@v0.49.1/build/boilerplate/boilerplate.py (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2016 Google Inc. All Rights Reserved.
     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 glob
    21  import json
    22  import mmap
    23  import os
    24  import re
    25  import sys
    26  
    27  parser = argparse.ArgumentParser()
    28  parser.add_argument("filenames", help="list of files to check, all files if unspecified", nargs='*')
    29  args = parser.parse_args()
    30  
    31  rootdir = os.path.dirname(__file__) + "/../../"
    32  rootdir = os.path.abspath(rootdir)
    33  
    34  def get_refs():
    35      refs = {}
    36      for path in glob.glob(os.path.join(rootdir, "build/boilerplate/boilerplate.*.txt")):
    37          extension = os.path.basename(path).split(".")[1]
    38  
    39          ref_file = open(path, 'r')
    40          ref = ref_file.read().splitlines()
    41          ref_file.close()
    42          refs[extension] = ref
    43  
    44      return refs
    45  
    46  def file_passes(filename, refs, regexs):
    47      try:
    48          f = open(filename, 'r')
    49      except:
    50          return False
    51  
    52      data = f.read()
    53      f.close()
    54  
    55      extension = file_extension(filename)
    56      ref = refs[extension]
    57  
    58      # remove build tags from the top of Go files
    59      if extension == "go":
    60          p = regexs["go_build_constraints"]
    61          (data, found) = p.subn("", data, 1)
    62  
    63      # remove shebang from the top of shell files
    64      if extension == "sh":
    65          p = regexs["shebang"]
    66          (data, found) = p.subn("", data, 1)
    67  
    68      data = data.splitlines()
    69  
    70      # if our test file is smaller than the reference it surely fails!
    71      if len(ref) > len(data):
    72          return False
    73  
    74      # trim our file to the same number of lines as the reference file
    75      data = data[:len(ref)]
    76  
    77      p = regexs["year"]
    78      for d in data:
    79          if p.search(d):
    80              return False
    81  
    82      # Replace all occurrences of the regex "2016|2015|2014" with "YEAR"
    83      p = regexs["date"]
    84      for i, d in enumerate(data):
    85          (data[i], found) = p.subn('YEAR', d)
    86          if found != 0:
    87              break
    88  
    89      # if we don't match the reference at this point, fail
    90      if ref != data:
    91          return False
    92  
    93      return True
    94  
    95  def file_extension(filename):
    96      return os.path.splitext(filename)[1].split(".")[-1].lower()
    97  
    98  skipped_dirs = ['Godeps', 'vendor', 'third_party', '_gopath', '_output', '.git']
    99  def normalize_files(files):
   100      newfiles = []
   101      for pathname in files:
   102          if any(x in pathname for x in skipped_dirs):
   103              continue
   104          newfiles.append(pathname)
   105      for i, pathname in enumerate(newfiles):
   106          if not os.path.isabs(pathname):
   107              newfiles[i] = os.path.join(rootdir, pathname)
   108      return newfiles
   109  
   110  def get_files(extensions):
   111      files = []
   112      if len(args.filenames) > 0:
   113          files = args.filenames
   114      else:
   115          for root, dirs, walkfiles in os.walk(rootdir):
   116              # don't visit certain dirs. This is just a performance improvement
   117              # as we would prune these later in normalize_files(). But doing it
   118              # cuts down the amount of filesystem walking we do and cuts down
   119              # the size of the file list
   120              for d in skipped_dirs:
   121                  if d in dirs:
   122                      dirs.remove(d)
   123  
   124              for name in walkfiles:
   125                  pathname = os.path.join(root, name)
   126                  files.append(pathname)
   127  
   128      files = normalize_files(files)
   129      outfiles = []
   130      for pathname in files:
   131          extension = file_extension(pathname)
   132          if extension in extensions:
   133              outfiles.append(pathname)
   134      return outfiles
   135  
   136  def get_regexs():
   137      regexs = {}
   138      # Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
   139      regexs["year"] = re.compile( 'YEAR' )
   140      # dates can be something in the 21st century
   141      regexs["date"] = re.compile( '20[0-9][0-9]' )
   142      # strip // +build \n\n build and //go:build constraints
   143      regexs["go_build_constraints"] = re.compile(r"^(//\s*(\+build|go:build).*\n)+\n", re.MULTILINE)
   144      # strip #!.* from shell scripts
   145      regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
   146      return regexs
   147  
   148  def main():
   149      regexs = get_regexs()
   150      refs = get_refs()
   151      filenames = get_files(refs.keys())
   152  
   153      for filename in filenames:
   154          if not file_passes(filename, refs, regexs):
   155              print(filename, file=sys.stdout)
   156  
   157  if __name__ == "__main__":
   158    sys.exit(main())