github.com/iDigitalFlame/xmt@v0.5.4/tools/header_check.py (about)

     1  #!/usr/bin/python
     2  # Copyright (C) 2020 - 2023 iDigitalFlame
     3  #
     4  # This program is free software: you can redistribute it and/or modify
     5  # it under the terms of the GNU General Public License as published by
     6  # the Free Software Foundation, either version 3 of the License, or
     7  # any later version.
     8  #
     9  # This program is distributed in the hope that it will be useful,
    10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  # GNU General Public License for more details.
    13  #
    14  # You should have received a copy of the GNU General Public License
    15  # along with this program.  If not, see <https://www.gnu.org/licenses/>.
    16  #
    17  # header_check.py <dir>
    18  # Checks to see if all *.go files have the correct GNU license header.
    19  #
    20  
    21  from glob import glob
    22  from os.path import join
    23  from sys import argv, exit
    24  
    25  
    26  def scan_dir(path):
    27      for i in glob(join(path, "**/**.go"), recursive=True):
    28          if "unit_tests" in i or "src/" in i:
    29              continue
    30          with open(i, "r") as f:
    31              b = f.read().split("\n")
    32          if len(b) < 8:
    33              raise ValueError(f"{i}: Empty file!")
    34          n = 0
    35          if b[0].startswith("//go:build"):
    36              if not b[1].startswith("// +build"):
    37                  raise ValueError(f"{i}: No old build directive after new directive!")
    38              n = 3
    39              while b[n - 1].startswith("// +build"):
    40                  n = n + 1
    41          if not b[n].startswith("// Copyright (C) 2020"):
    42              raise ValueError(f"{i}: Missing copyright!")
    43          if b[n + 1] != "//":
    44              raise ValueError(f"{i}: Missing black space after copyright!")
    45          if not b[n + 2].startswith("// This program is free software:"):
    46              raise ValueError(f"{i}: Missing freedom text!")
    47          del b
    48  
    49  
    50  def scan_dir2(path):
    51      for i in glob(join(path, "**/**.go"), recursive=True):
    52          if "unit_tests" in i:
    53              continue
    54          with open(i, "r") as f:
    55              b = f.read().split("\n")
    56          if len(b) < 8:
    57              raise ValueError(f"{i}: Empty file!")
    58          n = 0
    59          if b[0].startswith("//go:build"):
    60              if len(b[1]) != 0:
    61                  raise ValueError(f"{i}: No empty space after build directive!")
    62              n = 2
    63          if not b[n].startswith("// Copyright (C) 2020"):
    64              raise ValueError(f"{i}: Missing copyright!")
    65          if b[n + 1] != "//":
    66              raise ValueError(f"{i}: Missing black space after copyright!")
    67          if not b[n + 2].startswith("// This program is free software:"):
    68              raise ValueError(f"{i}: Missing freedom text!")
    69          del b
    70  
    71  
    72  if __name__ == "__main__":
    73      if len(argv) < 2:
    74          print(f"{argv[0]} <dir>")
    75          exit(1)
    76      scan_dir(argv[1])