Files
pre-commit-hooks/pre_commit_hooks/detect_private_key.py
Alexander Dupuy a6023ac0d7 Implement Markdown trailing space line break preservation
Markdown uses two or more trailing spaces on a line to indicate a forced
line break `<br/>` - these will be preserved for files with a markdown
extension (default = `.md` or `.markdown`).

Add `--markdown-linebreak-ext=X,Y` to add extensions (`*` matches any),
and `--no-markdown-linebreak-ext` to disable this feature.

If you want to set specific extension `foo` only (and not md/markdown),
use `--no-markdown-linebreak-ext --markdown-linebreak-ext=foo`

Tries to prevent --markdown-linebreak-ext from eating filenames as if they were
extensions by rejecting any with '.' or '/' (or even Windows-style '\' or ':')

Update README.md to include information on these arguments as well as
arguments added to other hooks

Add extensive tests using pytest.mark.parametrize

test that `txt` file is not considered as 'txt' extension
test that `.txt` file is not considered as 'txt' extension

The latter is the (correct) behavior of os.path.splitext(), and an example
of why it is better to use the libraries than to mangle strings yourself.
2015-05-11 08:52:32 +02:00

33 lines
857 B
Python

from __future__ import print_function
import argparse
import io
import sys
def detect_private_key(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to check')
args = parser.parse_args(argv)
private_key_files = []
for filename in args.filenames:
with io.open(filename, 'r') as f:
content = f.read()
if 'BEGIN RSA PRIVATE KEY' in content:
private_key_files.append(content)
if 'BEGIN DSA PRIVATE KEY' in content:
private_key_files.append(content)
if private_key_files:
for private_key_file in private_key_files:
print('Private key found: {0}'.format(private_key_file))
return 1
else:
return 0
if __name__ == '__main__':
sys.exit(detect_private_key())