EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fix_pragma.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file fix_pragma.py
1 #!/usr/bin/env python3
2 from __future__ import print_function
3 
4 import argparse
5 import os
6 from glob import glob
7 from concurrent.futures import ProcessPoolExecutor
8 import re
9 
10 code_format = """
11 #pragma once
12 {code}
13 """.strip()
14 
15 def fix_pragma(file):
16  with open(file, "r+") as f:
17  text = f.read().strip()
18 
19  def repl(m):
20  code = m.group(2).strip()
21  return code_format.format(code=code)
22 
23  newtext, num = re.subn(r"#ifndef (.*)\n#define \1.*\n((:?.|\n)+)#endif.*", repl, text, 1)
24  if num == 1:
25  f.seek(0)
26  f.truncate()
27  f.write(newtext)
28 
29 def main():
30  p = argparse.ArgumentParser()
31  p.add_argument("input")
32  args = p.parse_args()
33 
34  headers = []
35 
36  if os.path.isfile(args.input):
37  headers = [args.input]
38  elif os.path.isdir(args.input):
39  patterns = ["**/*.hpp", "**/*.h"]
40  headers = sum([glob(os.path.join(args.input, p), recursive=True) for p in patterns], [])
41  else:
42  headers = glob(args.input, recursive=True)
43 
44 
45  # for h in headers: print(h)
46 
47  for h in headers:
48  fix_pragma(h)
49 
50 
51 
52 
53 if "__main__" == __name__:
54  main()
55