Skip to content

Commit

Permalink
Move blacklist integrity check to helpers, employ in metasmoke.py
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Aug 30, 2018
1 parent 8becde9 commit 6cf8923
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
24 changes: 24 additions & 0 deletions helpers.py
Expand Up @@ -5,6 +5,7 @@
from termcolor import colored
import requests
import regex
from glob import glob


class Helpers:
Expand Down Expand Up @@ -133,5 +134,28 @@ def to_metasmoke_link(post_url, protocol=True):
"https:" if protocol else "", api_parameter_from_link(post_url), post_id_from_link(post_url))


def blacklist_integrity_check():
bl_files = glob('bad_*.txt') + glob('blacklisted_*.txt') + ['watched_keywords.txt']
seen = dict()
errors = []
for bl_file in bl_files:
with open(bl_file, 'r') as lines:
for lineno, line in enumerate(lines, 1):
if line.endswith('\r\n'):
errors.append('{0}:{1}:DOS line ending'.format(bl_file, lineno))
elif not line.endswith('\n'):
errors.append('{0}:{1}:No newline'.format(bl_file, lineno))
elif line == '\n':
errors.append('{0}:{1}:Empty line'.format(bl_file, lineno))
elif bl_file == 'watched_keywords.txt':
line = line.split('\t')[2]
if line in seen:
errors.append('{0}:{1}:Duplicate entry {2} (also {3})'.format(
bl_file, lineno, line.rstrip('\n'), seen[line]))
else:
seen[line] = '{0}:{1}'.format(bl_file, lineno)
return errors


class SecurityError(Exception):
pass
26 changes: 5 additions & 21 deletions metasmoke.py
Expand Up @@ -19,7 +19,7 @@
import spamhandling
import classes
import chatcommunicate
from helpers import api_parameter_from_link, log, only_blacklists_changed
from helpers import api_parameter_from_link, log, only_blacklists_changed, blacklist_integrity_check
from gitmanager import GitManager
from blacklists import load_blacklists

Expand Down Expand Up @@ -141,31 +141,15 @@ def handle_websocket_data(data):
if only_blacklists_changed(GitManager.get_remote_diff()):
commit_md = "[`{0}`](https://github.com/Charcoal-SE/SmokeDetector/commit/{0})" \
.format(sha[:7])
i = [] # Currently no issues with backlists
for bl_file in glob('bad_*.txt') + glob('blacklisted_*.txt'): # Check blacklists for issues
with open(bl_file, 'r') as lines:
seen = dict()
for lineno, line in enumerate(lines, 1):
if line.endswith('\r\n'):
i.append("DOS line ending at `{0}:{1}` in {2}".format(bl_file, lineno,
commit_md))
if not line.endswith('\n'):
i.append("No newline at end of `{0}` in {1}".format(bl_file, commit_md))
if line == '\n':
i.append("Blank line at `{0}:{1}` in {2}".format(bl_file, lineno,
commit_md))
if line in seen:
i.append("Duplicate entry of {0} at lines {1} and {2} of {3} in {4}"
.format(line.rstrip('\n'), seen[line], lineno, bl_file, commit_md))
seen[line] = lineno
if i == []: # No issues
integrity = blacklist_integrity_check()
if len(integrity) == 0: # No issues
GitManager.pull_remote()
load_blacklists()
chatcommunicate.tell_rooms_with("debug", "No code modified in {0}, only blacklists"
" reloaded.".format(commit_md))
else:
i.append("please fix before pulling.")
chatcommunicate.tell_rooms_with("debug", ", ".join(i))
integrity.append("please fix before pulling.")
chatcommunicate.tell_rooms_with("debug", ", ".join(integrity))
elif "commit_status" in message:
c = message["commit_status"]
sha = c["commit_sha"][:7]
Expand Down
22 changes: 2 additions & 20 deletions test/test_blacklists.py
@@ -1,29 +1,11 @@
#!/usr/bin/env python3

from glob import glob
from helpers import only_blacklists_changed
from helpers import only_blacklists_changed, blacklist_integrity_check


def test_blacklist_integrity():
bl_files = glob('bad_*.txt') + glob('blacklisted_*.txt') + \
['watched_keywords.txt']
seen = dict()
errors = []
for bl_file in bl_files:
with open(bl_file, 'r') as lines:
for lineno, line in enumerate(lines, 1):
if line.endswith('\r\n'):
errors.append('{0}:{1}:DOS line ending'.format(bl_file, lineno))
if not line.endswith('\n'):
errors.append('{0}:{1}:No newline'.format(bl_file, lineno))
if line == '\n':
errors.append('{0}:{1}:Empty line'.format(bl_file, lineno))
if bl_file == 'watched_keywords.txt':
line = line.split('\t')[2]
if line in seen:
errors.append('{0}:{1}:Duplicate entry {2} (also {3})'.format(
bl_file, lineno, line.rstrip('\n'), seen[line]))
seen[line] = '{0}:{1}'.format(bl_file, lineno)
errors = blacklist_integrity_check()

if len(errors) == 1:
raise ValueError(errors[0])
Expand Down

0 comments on commit 6cf8923

Please sign in to comment.