Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:10:43

0001 #!/usr/bin/env python3
0002 
0003 import os
0004 import sys
0005 import argparse
0006 from subprocess import check_output
0007 
0008 
0009 def main():
0010     p = argparse.ArgumentParser()
0011     p.add_argument("input")
0012     p.add_argument("--exclude", nargs="+")
0013     p.add_argument("--fix", action="store_true")
0014     p.add_argument("--reject-multiple-newlines", action="store_true")
0015     p.add_argument("--github", action="store_true")
0016     args = p.parse_args()
0017 
0018     files = (
0019         str(
0020             check_output(
0021                 [
0022                     "find",
0023                     args.input,
0024                     "-iname",
0025                     "*.cpp",
0026                     "-or",
0027                     "-iname",
0028                     "*.hpp",
0029                     "-or",
0030                     "-iname",
0031                     "*.ipp",
0032                 ]
0033                 + sum((["-not", "-path", exclude] for exclude in args.exclude), [])
0034             ),
0035             "utf-8",
0036         )
0037         .strip()
0038         .split("\n")
0039     )
0040 
0041     failed = []
0042 
0043     for file in files:
0044         file = os.path.normpath(file)
0045 
0046         with open(file) as f:
0047             lines = f.readlines()
0048 
0049         if not lines[-1].endswith("\n"):
0050             print(f"Missing newline at end of file: {file}")
0051             if args.fix:
0052                 with open(file, "a") as f:
0053                     f.write("\n")
0054             else:
0055                 failed.append(file)
0056             if args.github:
0057                 print(
0058                     f"::error file={file},line={len(lines)},title=End of file check::missing newline"
0059                 )
0060         elif args.reject_multiple_newlines and lines[-1] == "\n":
0061             print(f"Multiple newlines at end of file: {file}")
0062             if args.fix:
0063                 while lines[-1] == "\n":
0064                     lines.pop(-1)
0065                 with open(file, "w") as f:
0066                     f.write("".join(lines))
0067             else:
0068                 failed.append(file)
0069             if args.github:
0070                 print(
0071                     f"::error file={{{file}}},line={{{len(lines)}}},title=End of file check::multiple newlines"
0072                 )
0073 
0074     if failed:
0075         print(f"failed for files: {' '.join(failed)}")
0076         return 1
0077 
0078     print("success")
0079     return 0
0080 
0081 
0082 if "__main__" == __name__:
0083     sys.exit(main())