add_headers.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import argparse
4 import os
5 import re
6 from datetime import datetime
7 
8 from jinja2 import Template
9 
10 # Get the Git root directory
11 GITROOT = os.popen("git rev-parse --show-toplevel").read().strip()
12 
13 # Get the current date
14 current_date = datetime.now().strftime("%Y-%m-%d")
15 
16 # Get the latest commit hash
17 commit_hash = os.popen("git rev-parse HEAD").read().strip()[:7]
18 
19 # Define the header template using Jinja2
20 header_template = Template(
21  """{{cchar}} BEGIN_HEADER
22 {{cchar}} -----------------------------------------------------------------------------
23 {{cchar}} Gauss-Jacobi Quadrature Implementation
24 {{cchar}} Authors: {{author}}
25 {{cchar}} Source: {{library_name}} Library
26 {{cchar}} License: MIT
27 {{cchar}} GitHub Repository: {{repository_url}}
28 {{cchar}} Date: {{date}}
29 {{cchar}} Commit: {{commit}}
30 {{cchar}} -----------------------------------------------------------------------------
31 {{cchar}} This code is part of the {{library_name}} library, providing an efficient
32 {{cchar}} implementation for Gauss-Jacobi quadrature nodes and weights computation.
33 {{cchar}} -----------------------------------------------------------------------------
34 {{cchar}} To cite this software:
35 {{cchar}} Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
36 {{cchar}} Zenodo: https://doi.org/10.5281/ZENODO.8285112
37 {{cchar}} ---------------------------------------------------------------------
38 {{cchar}} END_HEADER
39 
40 """
41 )
42 
43 
45  directories,
46  file_types,
47  cchar="!",
48  author="Rohit Goswami <rgoswami[at]ieee.org>",
49  repository_url="https://github.com/HaoZeke/GaussJacobiQuad",
50  library_name="GaussJacobiQuad",
51  date=current_date,
52  commit=commit_hash,
53 ):
54  # Fill in the template variables
55  header_text = header_template.render(
56  author=author,
57  repository_url=repository_url,
58  library_name=library_name,
59  date=current_date,
60  commit=commit_hash,
61  cchar=cchar,
62  )
63  for directory in directories:
64  # Iterate over all files in the specified directory
65  for root, dirs, files in os.walk(directory):
66  for filename in files:
67  if any(filename.endswith(suffix) for suffix in file_types):
68  filepath = os.path.join(root, filename)
69 
70  with open(filepath, "r") as file:
71  content = file.read()
72 
73  # Check for the existing header and replace or append
74  if "BEGIN_HEADER" in content and "END_HEADER" in content:
75  content = re.sub(
76  rf"{cchar} BEGIN_HEADER.*?{cchar} END_HEADER\n",
77  header_text,
78  content,
79  flags=re.DOTALL,
80  )
81  else:
82  content = header_text + content
83 
84  with open(filepath, "w") as file:
85  file.write(content)
86 
87  print("Headers added or updated in all files in" f" {directory} using {cchar}.")
88 
89 
90 if __name__ == "__main__":
91  parser = argparse.ArgumentParser(description="Add headers to source files.")
92  parser.add_argument(
93  "--dirs", type=str, required=True, nargs="+", help="Directory(s) to process"
94  )
95  parser.add_argument(
96  "--ftypes",
97  type=str,
98  required=True,
99  help="Comma-separated list of file types to process",
100  )
101  parser.add_argument("--cchar", type=str, required=True, help="Comment character")
102 
103  args = parser.parse_args()
104  file_types = args.ftypes.split(",")
105  add_headers(args.dirs, file_types, args.cchar)
def add_headers(directories, file_types, cchar="!", author="Rohit Goswami <rgoswami[at]ieee.org>", repository_url="https://github.com/HaoZeke/GaussJacobiQuad", library_name="GaussJacobiQuad", date=current_date, commit=commit_hash)
Definition: add_headers.py:53