export_single.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Cannot use ! in statements with this if strip-comments is true
3 
4 import argparse
5 import os
6 import re
7 import subprocess
8 from pathlib import Path
9 
10 import add_headers
11 import fortdepend
12 
13 
15  try:
16  git_root = (
17  subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
18  .strip()
19  .decode("utf-8")
20  )
21  return Path(git_root)
22  except subprocess.CalledProcessError:
23  print("This directory is not part of a Git repository.")
24  return None
25 
26 
27 def process_files(fname, strip_comments=True):
28  with open(fname, "r") as f:
29  content = f.read()
30  # Remove everything between ! BEGIN_HEADER and ! END_HEADER
31  content = re.sub(r"! BEGIN_HEADER.*?! END_HEADER\n", "", content, flags=re.DOTALL)
32  # Optionally, remove lines starting with !
33  if strip_comments:
34  content = re.sub(r"(!.*?$)|(\s*!.*?$)", "", content, flags=re.MULTILINE)
35  return content
36 
37 
38 def export_single(modulename, strip_comments=True, outname=None):
39  if outname is None:
40  outname = f"{modulename}_single.f90"
41 
42  prj_srcs = get_git_root() / "src"
43  os.chdir(prj_srcs) # Needed for fortdepend
44 
45  gaussjacobiquad = fortdepend.FortranProject()
46  concat_files = gaussjacobiquad.get_all_used_files(modulename)
47  print(f"Concatenating in reverse order {concat_files}")
48 
49  exp_dat = []
50 
51  for fname in reversed(concat_files):
52  processed_content = process_files(prj_srcs / fname, strip_comments)
53  exp_dat.append(processed_content)
54 
55  with Path(get_git_root() / "dist" / outname).open("w") as f_out:
56  f_out.write("".join(exp_dat))
57 
58 
59 if __name__ == "__main__":
60  parser = argparse.ArgumentParser(description="Export single file algorithms.")
61  parser.add_argument(
62  "--modulename", type=str, required=True, help="Module to export"
63  )
64  parser.add_argument(
65  "--outname", type=str, required=False, help="Defaults to modulename_single.f90"
66  )
67  parser.add_argument(
68  "--strip-comments",
69  dest="strip_comments",
70  action="store_true",
71  help="Remove comments",
72  )
73  parser.add_argument(
74  "--keep-comments",
75  dest="strip_comments",
76  action="store_false",
77  help="Keep comments",
78  )
79  parser.set_defaults(strip_comments=True)
80 
81  args = parser.parse_args()
82  export_single(args.modulename, args.strip_comments, args.outname)
83  add_headers.add_headers([get_git_root() / "dist"], ["f90"])
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
def get_git_root()
def export_single(modulename, strip_comments=True, outname=None)
def process_files(fname, strip_comments=True)