Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
run: |
git fetch --no-tags --depth=2 origin "$GITHUB_SHA"
sh ci/comment-check.sh HEAD^1
- name: comment-check-drift
if: github.event_name == 'pull_request'
run: python3 -I tools/comment-check/drift.py
- name: Rust ${{ env.RUST_VERSION }}
run: |
rustup toolchain install "$RUST_VERSION" --profile minimal --no-self-update
Expand Down
35 changes: 21 additions & 14 deletions tools/comment-check/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def __init__(self, s):
self.s, self.lines, self.out, self.first, self.lost = s, Lines(s), [], None, False


def shell_quoted(r, i):
def shell_quoted(r, i, nested=False):
s, n, j, sure = r.s, len(r.s), i + 1, True
while j < n:
if s[j] == '\\':
Expand All @@ -233,16 +233,24 @@ def shell_quoted(r, i):
sure = sure and inner
elif s[j] == '`':
j = skip_quoted(s, j, '`', newline_ends=False)
sure = sure and not nested
else:
j += 1
r.lost = True
return n, False


def continued(s, j, start):
k = j - 1
while k > start and s[k - 1] == '\\':
k -= 1
return s[j - 1] == '\n' and (j - 1 - k) % 2 == 1


def command_start(s, i, start):
j = i
while j > start and s[j - 1] in ' \t':
j -= 1
while j > start and (s[j - 1] in ' \t' or continued(s, j, start)):
j -= 2 if s[j - 1] == '\n' else 1
if j == start or s[j - 1] in ';&|()\n':
return True
k = j
Expand Down Expand Up @@ -313,23 +321,22 @@ def shell_words(r, i, nested=False):
elif c == '$' and s.startswith("$'", i):
i, boundary = skip_quoted(s, i + 1, "'", newline_ends=False), False
elif c == '"':
i, inner = shell_quoted(r, i)
i, inner = shell_quoted(r, i, nested)
sure, boundary = sure and inner, False
quiet = quiet if inner else lines.at(i - 1)[0]
elif c == '`':
i, boundary = skip_quoted(s, i, c, newline_ends=False), False
sure = sure and not nested
elif c == '\\':
i, boundary = i + 2, False
i, boundary = i + 2, boundary and s.startswith('\\\n', i)
elif nested and boundary and SHELL_WORD.match(s, i):
word = SHELL_WORD.match(s, i).group()
if word in ('case', 'esac'):
if word in ('case', 'esac') and command_start(s, i, start):
sure = False
if command_start(s, i, start):
if word == 'case':
cases.append(depth)
elif cases:
cases.pop()
if word == 'case':
cases.append(depth)
elif cases:
cases.pop()
i, boundary = i + len(word), False
else:
i, boundary = i + 1, False
Expand Down Expand Up @@ -531,13 +538,13 @@ def scan_html(s):
else:
k += 1
i, tag = min(k + 1, n), m.group(2).lower()
if m.group(1) or tag not in ('script', 'style'):
if m.group(1) or tag not in ('script', 'style', 'title', 'textarea'):
continue
close = re.compile(r'</' + tag + r'\b', re.I).search(s, i)
close = re.compile(r'</' + tag + r'(?=[\t\n\f\r />])', re.I).search(s, i)
stop = n if not close else close.start()
if tag == 'style':
scan_css(s[:stop], i, lines, out)
elif JS_TYPES.match(attrs.get('type', '')):
elif tag == 'script' and JS_TYPES.match(attrs.get('type', '')):
scan_js(s[:stop], i, lines, out)
i = stop

Expand Down
43 changes: 43 additions & 0 deletions tools/comment-check/drift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
import pathlib
import sys
import urllib.error
import urllib.request

SOURCE_COMMIT = '2b0b0d192d9013f223279d507bc7a809540f66da'
SOURCE_REPO = 'openwizdev/OpenWiz'
SOURCE_PATH = 'tools/comment-check/check.py'
LOCAL = pathlib.Path(__file__).resolve().parent / 'check.py'


def source_bytes():
url = 'https://raw.githubusercontent.com/' + SOURCE_REPO + '/' + SOURCE_COMMIT + '/' + SOURCE_PATH
request = urllib.request.Request(url, headers={'User-Agent': 'comment-check-drift'})
try:
with urllib.request.urlopen(request, timeout=60) as response:
payload = response.read()
status = getattr(response, 'status', 200)
except urllib.error.URLError as error:
print('comment-check drift: could not read ' + SOURCE_REPO + '@' + SOURCE_COMMIT + ': ' + str(error), file=sys.stderr)
return None
if status != 200 or not payload:
print('comment-check drift: ' + SOURCE_REPO + '@' + SOURCE_COMMIT + ' returned no bytes', file=sys.stderr)
return None
return payload


def main():
source = source_bytes()
if source is None:
return 2
local = LOCAL.read_bytes()
if local != source:
print('comment-check drift: ' + SOURCE_PATH + ' differs from ' + SOURCE_REPO + '@' + SOURCE_COMMIT, file=sys.stderr)
print('source bytes ' + str(len(source)) + ', copy bytes ' + str(len(local)), file=sys.stderr)
return 1
print('comment-check drift: ' + SOURCE_PATH + ' matches ' + SOURCE_REPO + '@' + SOURCE_COMMIT)
return 0


if __name__ == '__main__':
sys.exit(main())
Loading