Make a few enhancements to pslint

This commit is contained in:
Tim Rühsen 2016-02-08 14:11:52 +01:00
parent 36609787d5
commit d19c46c003
1 changed files with 21 additions and 21 deletions

View File

@ -42,23 +42,19 @@ def error(msg):
errors += 1 errors += 1
# skip_order_check = True # skip_order_check = True
def print_list(list): def print_psl(list):
print(" [" + ", ".join( str(x) for x in list) + "]") for domain in list:
print(".".join(str(label) for label in reversed(domain)))
def psl_key(s): def psl_key(s):
if s[0] == '*': if s[0] == '*':
return 0 return 0
if s[0] == '!': if s[0] == '!':
return 1 return 1
return 0 return 2
def psl_sort(group):
# we have to extend the inner lists for sorting/comparing
# needed = max(len(lables) for lables in group)
# return sorted(group, key = lambda labels: (len(labels), labels.extend([None] * (needed - len(labels)))))
return sorted(group, key = lambda labels: (len(labels), labels))
def check_order(group): def check_order(group):
"""Check the correct order of a domain group"""
global skip_order_check global skip_order_check
try: try:
@ -68,21 +64,23 @@ def check_order(group):
# check if the TLD is the identical within the group # check if the TLD is the identical within the group
if any(group[0][0] != labels[0] for labels in group): if any(group[0][0] != labels[0] for labels in group):
error('Domain group TLD is not consistent') warning('Domain group TLD is not consistent')
return
# sort by # of labels, label-by-label (labels are in reversed order)
sorted_group = sorted(group, key = lambda labels: (len(labels), psl_key(labels[-1][0]), labels))
sorted_group = psl_sort(group)
if group != sorted_group: if group != sorted_group:
warning('Incorrectly sorted group of domains') warning('Incorrectly sorted group of domains')
print_list(group) print(" " + str(group))
print_list(sorted_group) print(" " + str(sorted_group))
print_psl(sorted_group)
finally: finally:
del group[:] del group[:]
def lint_psl(infile): def lint_psl(infile):
"""Parses PSL file and extract strings and return code""" """Parses PSL file and performs syntax checking"""
global line, nline global line, nline
PSL_FLAG_EXCEPTION = (1<<0) PSL_FLAG_EXCEPTION = (1<<0)
@ -109,12 +107,12 @@ def lint_psl(infile):
# empty line (end of sorted domain group) # empty line (end of sorted domain group)
if not line: if not line:
# check_order(group) check_order(group)
continue continue
# check for section begin/end # check for section begin/end
if line[0:2] == "//": if line[0:2] == "//":
# check_order(group) check_order(group)
if section == 0: if section == 0:
if line == "// ===BEGIN ICANN DOMAINS===": if line == "// ===BEGIN ICANN DOMAINS===":
@ -146,6 +144,8 @@ def lint_psl(infile):
if section == 0: if section == 0:
error('Rule outside of section') error('Rule outside of section')
group.append(list(reversed(line.split('.'))))
# decode UTF-8 input into unicode, needed only for python 2.x # decode UTF-8 input into unicode, needed only for python 2.x
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
line = line.decode('utf-8') line = line.decode('utf-8')
@ -174,10 +174,10 @@ def lint_psl(infile):
labels = line.split('.') labels = line.split('.')
# collect reversed list of labels # collect reversed list of labels
if sys.version_info[0] < 3: # if sys.version_info[0] < 3:
group.append(list(reversed(line.encode('utf-8').split('.')))) # group.append(list(reversed(line.encode('utf-8').split('.'))))
else: # else:
group.append(list(reversed(line.split('.')))) # group.append(list(reversed(line.split('.'))))
for label in labels: for label in labels:
if not label: if not label: