-
Notifications
You must be signed in to change notification settings - Fork 0
/
enforce_https.py
67 lines (50 loc) · 1.45 KB
/
enforce_https.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
from __future__ import annotations
import argparse
import re
import sys
from argparse import ArgumentParser
from collections.abc import Sequence
from pathlib import Path
ALLOW_HTTP_LATER_RE = re.compile(
r'allow-http-in-([0-9]+)-lines'
)
ALLOW_HTTP_RE = re.compile(
r'allow-http'
)
def main() -> int:
args = parse_args()
retval = 0
for file in args.files:
content = file.read_text()
new_content = ''
laters = []
for line in content.splitlines(keepends=True):
allow_later = ALLOW_HTTP_LATER_RE.search(line)
if allow_later:
laters.append(int(allow_later.group(1)))
if ALLOW_HTTP_RE.search(line):
new_content += line
continue
laters = [later - 1 for later in laters]
if 0 in laters:
laters.remove(0)
new_content += line
continue
new_line = line.replace('http://', 'https://') # allow-http
new_content += new_line
if new_content != content:
file.write_text(new_content)
retval = 1
return retval
class Args(argparse.Namespace):
files: Sequence[Path]
def parse_args() -> Args:
parser = ArgumentParser('enforce-https')
parser.add_argument(
'files',
nargs='+',
type=Path,
)
return parser.parse_args(namespace=Args())
if __name__ == '__main__':
sys.exit(main())