forked from Lonami/grammers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·48 lines (39 loc) · 1.24 KB
/
pre-commit
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
38
39
40
41
42
43
44
45
46
47
48
#!/bin/env python
import os
import subprocess
import sys
LICENSE = '''\
// Copyright 2020 - developers of the `grammers` project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
'''
def warn(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def error(*args, **kwargs):
warn(*args, **kwargs)
exit(1)
def check_fmt():
return subprocess.run(('cargo', 'fmt', '--', '--check')).returncode == 0
def check_license():
ok = True
for root, _dirs, files in os.walk('./lib/'):
root = root.replace(os.path.sep, '/')
for file in files:
if file.lower().endswith('.rs') and not root.endswith('/examples'):
file = os.path.join(root, file)
with open(file, 'r', encoding='utf-8') as fd:
if fd.read(len(LICENSE)) != LICENSE:
ok = False
warn(file, 'missing license')
return ok
def main():
if not check_fmt():
error("please run 'cargo fmt' before commit")
if not check_license():
error("please make sure all files have the license header")
if __name__ == '__main__':
main()