forked from electronic-structure/SIRIUS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_format.py
37 lines (31 loc) · 1.21 KB
/
check_format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys
import subprocess
import os
from subprocess import Popen, PIPE
import difflib
env_copy = os.environ.copy()
all_files = [name.strip() for name in sys.stdin.readlines()]
files = []
for f in all_files:
extension = os.path.splitext(f)[1][1:]
if extension in ['h', 'hpp', 'cpp']:
files.append(f)
print("files to check", files)
enable_shell = False
status = 0
for name in files:
with open(name, 'r') as f:
original = f.read()
p1 = subprocess.Popen(["sed", r"s/#pragma omp/\/\/#pragma omp/g", name], shell=enable_shell, stdout=subprocess.PIPE)
p2 = subprocess.Popen(["clang-format", "-style=file"], shell=enable_shell, stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(["sed", r"s/\/\/ *#pragma omp/#pragma omp/g"], shell=enable_shell, stdin=p2.stdout, stdout=subprocess.PIPE)
formatted = p3.communicate()[0].decode('utf-8')
p3.wait()
if (original == formatted):
print("%s: OK"%name)
else:
print("%s: non-zero diff found"%name)
for line in difflib.unified_diff(original.splitlines(), formatted.splitlines(), fromfile='original', tofile='formatted'):
print(line)
status += 1
sys.exit(status)