Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
check_end_of_file.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file check_end_of_file.py
1 #!/usr/bin/env python3
2 
3 import os
4 import sys
5 import argparse
6 from subprocess import check_output
7 
8 
9 def main():
10  p = argparse.ArgumentParser()
11  p.add_argument("input")
12  p.add_argument("--exclude", nargs="+")
13  p.add_argument("--fix", action="store_true")
14  p.add_argument("--reject-multiple-newlines", action="store_true")
15  p.add_argument("--github", action="store_true")
16  args = p.parse_args()
17 
18  files = (
19  str(
20  check_output(
21  [
22  "find",
23  args.input,
24  "-iname",
25  "*.cpp",
26  "-or",
27  "-iname",
28  "*.hpp",
29  "-or",
30  "-iname",
31  "*.ipp",
32  ]
33  + sum((["-not", "-path", exclude] for exclude in args.exclude), [])
34  ),
35  "utf-8",
36  )
37  .strip()
38  .split("\n")
39  )
40 
41  failed = []
42 
43  for file in files:
44  file = os.path.normpath(file)
45 
46  with open(file) as f:
47  lines = f.readlines()
48 
49  if not lines[-1].endswith("\n"):
50  print(f"Missing newline at end of file: {file}")
51  if args.fix:
52  with open(file, "a") as f:
53  f.write("\n")
54  else:
55  failed.append(file)
56  if args.github:
57  print(
58  f"::error file={file},line={len(lines)},title=End of file check::missing newline"
59  )
60  elif args.reject_multiple_newlines and lines[-1] == "\n":
61  print(f"Multiple newlines at end of file: {file}")
62  if args.fix:
63  while lines[-1] == "\n":
64  lines.pop(-1)
65  with open(file, "w") as f:
66  f.write("".join(lines))
67  else:
68  failed.append(file)
69  if args.github:
70  print(
71  f"::error file={{{file}}},line={{{len(lines)}}},title=End of file check::multiple newlines"
72  )
73 
74  if failed:
75  print(f"failed for files: {' '.join(failed)}")
76  return 1
77 
78  print("success")
79  return 0
80 
81 
82 if "__main__" == __name__:
83  sys.exit(main())