Analysis Software
Documentation for sPHENIX simulation 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 
16 def fix_pragma(file):
17  with open(file, "r+") as f:
18  text = f.read().strip()
19 
20  def repl(m):
21  code = m.group(2).strip()
22  return code_format.format(code=code)
23 
24  newtext, num = re.subn(
25  r"#ifndef (.*)\n#define \1.*\n((:?.|\n)+)#endif.*", repl, text, 1
26  )
27  if num == 1:
28  f.seek(0)
29  f.truncate()
30  f.write(newtext)
31 
32 
33 def main():
34  p = argparse.ArgumentParser()
35  p.add_argument("input")
36  args = p.parse_args()
37 
38  headers = []
39 
40  if os.path.isfile(args.input):
41  headers = [args.input]
42  elif os.path.isdir(args.input):
43  patterns = ["**/*.hpp", "**/*.h"]
44  headers = sum(
45  [glob(os.path.join(args.input, p), recursive=True) for p in patterns], []
46  )
47  else:
48  headers = glob(args.input, recursive=True)
49 
50  # for h in headers: print(h)
51 
52  for h in headers:
53  fix_pragma(h)
54 
55 
56 if "__main__" == __name__:
57  main()