Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/CI/fix_pragma.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #!/usr/bin/env python3
0002 
0003 import argparse
0004 import os
0005 from glob import glob
0006 import re
0007 
0008 code_format = """
0009 #pragma once
0010 {code}
0011 """.strip()
0012 
0013 
0014 def fix_pragma(file):
0015     with open(file, "r+") as f:
0016         text = f.read().strip()
0017 
0018         def repl(m):
0019             code = m.group(2).strip()
0020             return code_format.format(code=code)
0021 
0022         newtext, num = re.subn(
0023             r"#ifndef (.*)\n#define \1.*\n((:?.|\n)+)#endif.*", repl, text, 1
0024         )
0025         if num == 1:
0026             f.seek(0)
0027             f.truncate()
0028             f.write(newtext)
0029 
0030 
0031 def main():
0032     p = argparse.ArgumentParser()
0033     p.add_argument("input")
0034     args = p.parse_args()
0035 
0036     headers = []
0037 
0038     if os.path.isfile(args.input):
0039         headers = [args.input]
0040     elif os.path.isdir(args.input):
0041         patterns = ["**/*.hpp", "**/*.ipp"]
0042         headers = sum(
0043             [glob(os.path.join(args.input, p), recursive=True) for p in patterns], []
0044         )
0045     else:
0046         headers = glob(args.input, recursive=True)
0047 
0048     for h in headers:
0049         fix_pragma(h)
0050 
0051 
0052 if "__main__" == __name__:
0053     main()