Repository navigation
Expand file tree
/
Copy pathblock_files.py
More file actions
168 lines (146 loc) · 4.87 KB
/
Copy pathblock_files.py
File metadata and controls
168 lines (146 loc) · 4.87 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
"""Block environment, database dump, log and cache files in commits.
Deprecated: the block-*-files hooks will be removed in 3.0.0, replaced by the forbid-*-files
hooks, which need no code (see .pre-commit-hooks.yaml).
"""
import argparse
import os
from ctrlweb_pre_commit_hooks._common import configure_stdout, split
RULES = ('extensions', 'names', 'prefixes')
# What each --type blocks. Rules are matched against lowercase file names.
BLOCKED = {
'env': {
'extensions': (),
'names': (),
'prefixes': ('.env', 'env.'),
},
'dump': {
'extensions': (
'.sql',
'.dump',
'.sqlite',
'.sqlite3',
'.db',
'.sql.gz',
'.sql.zip',
'.dump.gz',
'.dump.zip',
'.sql.tar.gz',
'.dump.tar.gz',
),
'names': (),
'prefixes': (),
},
'log': {
'extensions': ('.log', '.err'),
'names': ('error_log',),
'prefixes': (),
},
'cache': {
'extensions': ('.cache',),
'names': (),
'prefixes': (),
},
}
# What each --type never blocks
ALLOWED = {
'env': {
'extensions': (),
# Templates meant to be committed, and TypeScript's env declaration file
'names': ('.env.example', '.env.sample', '.env.dist', '.env.template', 'env.d.ts'),
'prefixes': (),
},
}
DESCRIPTIONS = {
'env': 'environment variable',
'dump': 'database dump',
'log': 'log',
'cache': 'cache',
}
def parse_args(argv=None):
parser = argparse.ArgumentParser(description='Block environment, database, dump, log and cache files in commits.')
parser.add_argument(
'--type',
required=True,
choices=list(BLOCKED),
help='Which type of files to check ( env|dump|log|cache )',
)
# The staged files passed by pre-commit
parser.add_argument('files', nargs='*', help='List of staged files to check')
parser.add_argument(
'--extra-extensions',
default='',
help='Pipe-separated list of additional file extensions to block (e.g., .bak|.tar|.gz)',
)
parser.add_argument(
'--exclude-extensions',
default='',
help='Pipe-separated list of file extensions that are never blocked (e.g., .log|.txt)',
)
parser.add_argument(
'--extra-names',
default='',
help='Pipe-separated list of additional file names to block (e.g., file1.bak|file2.tar|file3.gz)',
)
parser.add_argument(
'--exclude-names',
default='',
help='Pipe-separated list of file names that are never blocked (e.g., filename.txt)',
)
parser.add_argument(
'--extra-prefixes',
default='',
help='Pipe-separated list of additional file name prefixes to block (e.g., env|env.)',
)
parser.add_argument(
'--exclude-prefixes',
default='',
help='Pipe-separated list of file name prefixes that are never blocked (e.g., test_|dev_)',
)
return parser.parse_args(argv)
def lowercase_items(value):
# File names are matched case-insensitively, so user input is normalized too
return tuple(item.lower() for item in split(value))
def get_rules(args):
"""The (blocked, allowed) rules for --type, including the --extra-* and --exclude-* values."""
no_rules = dict.fromkeys(RULES, ())
blocked = {rule: BLOCKED[args.type][rule] + lowercase_items(getattr(args, f'extra_{rule}')) for rule in RULES}
allowed = {
rule: ALLOWED.get(args.type, no_rules)[rule] + lowercase_items(getattr(args, f'exclude_{rule}'))
for rule in RULES
}
return blocked, allowed
def matches(filename, rules):
return (
filename.endswith(rules['extensions'])
or filename in rules['names']
or filename.startswith(rules['prefixes'])
)
def main(argv=None):
configure_stdout()
args = parse_args(argv)
blocked, allowed = get_rules(args)
blocked_files = []
for path in args.files:
# Match the file name, not the path, so files in subdirectories are caught too
filename = os.path.basename(path).lower()
# Excludes always win over blocks
if matches(filename, blocked) and not matches(filename, allowed):
blocked_files.append(path)
if not blocked_files:
return 0
description = DESCRIPTIONS[args.type]
if len(blocked_files) == 1:
article = 'an' if description[0] in 'aeiou' else 'a'
print(f"Found {article} {description} file in commit : ")
else:
print(f"Found {description} files in commit : ")
for path in blocked_files:
print(f" - '{path}'")
print(
f"\nNote: block-{args.type}-files is deprecated and will be removed in 3.0.0. "
f"Use forbid-{args.type}-files instead."
)
return 1
if __name__ == '__main__':
raise SystemExit(main())