forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standards.py
executable file
·124 lines (92 loc) · 3.83 KB
/
standards.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
import os
import sys
import stat
import glob
import argparse
import datetime
parser = argparse.ArgumentParser()
parser.add_argument("--hook", action="store_true")
parser.add_argument("--unhook", action="store_true")
parser.add_argument("--check", action="store_true")
parser.add_argument("--fix", action="store_true")
parser.add_argument("--verbosity", default="minimal")
parser.add_argument("--tool-path", default="dotnet-format")
parser.add_argument("--project-path", default="testproject")
parser.add_argument("--project-glob", default="*.sln")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
exit(1)
args = parser.parse_args()
hook_path = "./.git/hooks/pre-push"
hook_exec = f"python3 {os.path.basename(sys.argv[0])} --check"
if args.hook:
print("hook: execute")
if os.path.exists(hook_path):
print(f"hook: git pre-push hook file already exists: `{hook_path}`")
print("hook: please make sure to backup and delete the existing pre-push hook file")
exit("hook: failed")
print("hook: write git pre-push hook file contents")
hook_file = open(hook_path, "w")
hook_file.write(f"#!/bin/sh\n\n{hook_exec}\n")
hook_file.close()
print("hook: make git pre-push hook file executable")
hook_stat = os.stat(hook_path)
os.chmod(hook_path, hook_stat.st_mode | stat.S_IEXEC)
print("hook: succeeded")
if args.unhook:
print("unhook: execute")
hook_path = "./.git/hooks/pre-push"
if os.path.isfile(hook_path):
print(f"unhook: found file -> `{hook_path}`")
delete = False
hook_file = open(hook_path, "r")
if hook_exec in hook_file.read():
delete = True
else:
print("unhook: existing git pre-push hook file was not created by this script")
exit("unhook: failed")
hook_file.close()
if delete:
os.remove(hook_path)
print(f"unhook: delete file -> `{hook_path}`")
print("unhook: succeeded")
if args.check or args.fix:
glob_match = os.path.join(args.project_path, args.project_glob)
glob_files = glob.glob(glob_match)
print(f"glob: found {len(glob_files)} files matching -> {glob_match}")
if len(glob_files) == 0:
print("glob: no project files found!")
print("glob: \tdid you forget to generate your solution and project files in Unity?")
print("glob: \tdid you double-check --project-path and/or --project-glob arguments?")
exit(f"glob: failed")
any_old = False
for project_file in glob_files:
file_stat = os.stat(project_file)
check_days = 7
modified_time = datetime.datetime.fromtimestamp(file_stat.st_mtime)
days_ago_time = datetime.datetime.now() - datetime.timedelta(days=check_days)
if modified_time < days_ago_time:
any_old = True
print(f"glob: last modified more than {check_days} days ago -> {project_file}")
if any_old:
print(f"glob: some project files are not modified for more than {check_days} days ago")
print("glob: please consider regenerating project files in Unity")
if args.check:
print("check: execute")
any_error = False
for project_file in glob_files:
print(f"check: project -> {project_file}")
any_error = 0 != os.system(f"{args.tool_path} {project_file} --fix-whitespace --fix-style error --check --verbosity {args.verbosity}") or any_error
if any_error:
exit("check: failed")
print("check: succeeded")
if args.fix:
print("fix: execute")
any_error = False
for project_file in glob_files:
print(f"fix: project -> {project_file}")
any_error = 0 != os.system(f"{args.tool_path} {project_file} --fix-whitespace --fix-style error --verbosity {args.verbosity}") or any_error
if any_error:
exit("fix: failed")
print("fix: succeeded")