(root)/
gcc-13.2.0/
gcc/
m2/
tools-src/
tidydates.py
       1  #!/usr/bin/env python3
       2  
       3  # utility to tidy dates and detect lack of copyright.
       4  
       5  # Copyright (C) 2016-2023 Free Software Foundation, Inc.
       6  #
       7  # This file is part of GNU Modula-2.
       8  #
       9  # GNU Modula-2 is free software; you can redistribute it and/or modify
      10  # it under the terms of the GNU General Public License as published by
      11  # the Free Software Foundation; either version 3, or (at your option)
      12  # any later version.
      13  #
      14  # GNU Modula-2 is distributed in the hope that it will be useful,
      15  # but WITHOUT ANY WARRANTY; without even the implied warranty of
      16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17  # GNU General Public License for more details.
      18  #
      19  # You should have received a copy of the GNU General Public License
      20  # along with GNU Modula-2; see the file COPYING.  If not, write to the
      21  # Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
      22  # 02110-1301, USA.
      23  
      24  import os
      25  import pathlib
      26  import shutil
      27  import sys
      28  
      29  max_line_length = 60
      30  
      31  COPYRIGHT = 'Copyright (C)'
      32  
      33  
      34  def visit_dir(directory, ext, func):
      35      # visit_dir - call func for each file below, dir, matching extension, ext.
      36      list_of_files = os.listdir(directory)
      37      list_of_files.sort()
      38      for filename in list_of_files:
      39          path = pathlib.PurePath(filename)
      40          full = os.path.join(directory, filename)
      41          if path.is_file(full):
      42              if path.suffix == ext:
      43                  func(full)
      44          elif path.is_dir(full):
      45              visit_dir(full, ext, func)
      46  
      47  
      48  def is_year(year):
      49      # is_year - returns True if, year, is legal.
      50      if len(year) == 5:
      51          year = year[:-1]
      52      for c in year:
      53          if not c.isdigit():
      54              return False
      55      return True
      56  
      57  
      58  def handle_copyright(outfile, lines, n, leader1, leader2):
      59      # handle_copyright look for Copyright in the comment.
      60      global max_line_length
      61      i = lines[n]
      62      c = i.find(COPYRIGHT)+len(COPYRIGHT)
      63      outfile.write(i[:c])
      64      d = i[c:].split()
      65      start = c
      66      seen_date = True
      67      years = []
      68      while seen_date:
      69          if d == []:
      70              n += 1
      71              i = lines[n]
      72              d = i[2:].split()
      73          else:
      74              e = d[0]
      75              punctuation = ''
      76              if len(d) == 1:
      77                  d = []
      78              else:
      79                  d = d[1:]
      80              if c > max_line_length:
      81                  outfile.write('\n')
      82                  outfile.write(leader1)
      83                  outfile.write(leader2)
      84                  outfile.write(' '*(start-2))
      85                  c = start
      86              if is_year(e):
      87                  if (e[-1] == '.') or (e[-1] == ','):
      88                      punctuation = e[-1]
      89                      e = e[:-1]
      90                  else:
      91                      punctuation = ''
      92              else:
      93                  seen_date = False
      94              if seen_date:
      95                  if not (e in years):
      96                      c += len(e) + len(punctuation)
      97                      outfile.write(' ')
      98                      outfile.write(e)
      99                      outfile.write(punctuation)
     100                      years += [e]
     101              else:
     102                  if start < c:
     103                      outfile.write('\n')
     104                      outfile.write(leader1)
     105                      outfile.write(leader2)
     106                      outfile.write(' '*(start-2))
     107  
     108                  outfile.write(' ')
     109                  outfile.write(e)
     110                  outfile.write(punctuation)
     111                  for w in d:
     112                      outfile.write(' ')
     113                      outfile.write(w)
     114      outfile.write('\n')
     115      return outfile, n+1
     116  
     117  
     118  def handle_header(filename, leader1, leader2):
     119      # handle_header reads in the header of a file and inserts
     120      # a line break around the Copyright dates.
     121      print('------------------------------')
     122      lines = open(filename).readlines()
     123      if len(lines) > 20:
     124          with open('tmptidy', 'w') as outfile:
     125              n = 0
     126              for i in lines:
     127                  if i.find('Copyright (C)') >= 0:
     128                      outfile, n = handle_copyright(outfile, lines,
     129                                                    n, leader1, leader2)
     130                      outfile.writelines(lines[n:])
     131                      outfile.close()
     132                      print('-> mv tmptidy', filename)
     133                      shutil.move('tmptidy', filename)
     134                      return
     135                  else:
     136                      outfile.write(lines[n])
     137                      n += 1
     138          sys.stdout.write('%s:1:1 needs a Copyright notice..\n' % filename)
     139  
     140  
     141  def bash_tidy(filename):
     142      # bash_tidy - tidy up dates using '#' comment
     143      handle_header(filename, '#', ' ')
     144  
     145  
     146  def c_tidy(filename):
     147      # c_tidy - tidy up dates using '/* */' comments
     148      handle_header(filename, ' ', '*')
     149  
     150  
     151  def m2_tidy(filename):
     152      # m2_tidy - tidy up dates using '(* *)' comments
     153      handle_header(filename, ' ', ' ')
     154  
     155  
     156  def main():
     157      # main - for each file extension call the appropriate tidy routine.
     158      visit_dir('.', '.in', bash_tidy)
     159      visit_dir('.', '.py', bash_tidy)
     160      visit_dir('.', '.c', c_tidy)
     161      visit_dir('.', '.h', c_tidy)
     162      visit_dir('.', '.def', m2_tidy)
     163      visit_dir('.', '.mod', m2_tidy)
     164  
     165  
     166  main()