Skip to content

Commit

Permalink
Merge pull request #158 from jrha/panlint
Browse files Browse the repository at this point in the history
Panlint: Mark components referred to in prefix statements as used
  • Loading branch information
stdweird authored Jul 3, 2017
2 parents 5a9700b + 7158258 commit b9334c9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
24 changes: 13 additions & 11 deletions panc/src/main/scripts/panlint/panlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

# Find usage and inclusion of components
RE_COMPONENT_INCLUDE = re.compile(r'^\s*[^#]?\s*include.*components/(?P<name>\w+)/config', re.M)
RE_COMPONENT_USE = re.compile(r'^\s*[^#]?\s*/software/components/(?P<name>\w+)/')
RE_COMPONENT_USE = re.compile(r'/software/components/(?P<name>\w+)/')
LINE_LENGTH_LIMIT = 120

# Simple regular-expression based checks that will be performed against all non-ignored lines
Expand Down Expand Up @@ -92,17 +92,17 @@ def whitespace_around_operators(self, line, string_ranges):
# simple statement text in square brackets; if any
# the "simple" pattern: letters, digtis, +/- operator, minus sign
# also whitespace, those are invalid
sqb_simple = '\s\w\d+-'
sqb_simple = r'\s\w\d+-'

sqb_before = re.search(r'[[]([' + sqb_simple + ']*)$', chars_before)
sqb_after = re.search(r'^([' + sqb_simple + ']*)[]]', chars_after)

valid = True
if op == '-' and \
re.search('\W\s*$', chars_before) and \
re.search('^\s*\d+\s*\W', chars_after):
re.search(r'\W\s*$', chars_before) and \
re.search(r'^\s*\d+\s*\W', chars_after):
# -\d not preceded or followed by eg variable name
if re.search('^\s', chars_after):
if re.search(r'^\s', chars_after):
valid = False
message = 'Unwanted space after minus sign (not operator)'
end += 2
Expand Down Expand Up @@ -180,7 +180,7 @@ def merge_diagnoses(args):
return ''

args = [a.rstrip() for a in args]
result = [' '] * max(map(len, args))
result = [' '] * max([len(a) for a in args])

for text in args:
for i, c in enumerate(text):
Expand Down Expand Up @@ -288,10 +288,12 @@ def check_line_component_use(line, components_included):
messages = []
problem_count = 0

for m in RE_COMPONENT_USE.finditer(line):
if m.group('name') not in components_included:
message = 'Component %s in use, but component config has not been included' % m.group('name')
diagnoses.append(diagnose(*m.span('name')))
for match in RE_COMPONENT_USE.finditer(line):
if match.group('name') not in components_included:
start, end = match.span('name')
debug_range(start, end, 'ComponentUse', True)
message = 'Component %s in use, but component config has not been included' % match.group('name')
diagnoses.append(diagnose(*match.span('name')))
messages.append(message)
problem_count += 1

Expand Down Expand Up @@ -484,7 +486,7 @@ def main():
print '%d problems found in total' % problems_found

if problems_found:
return(1)
return 1


if __name__ == '__main__':
Expand Down
44 changes: 41 additions & 3 deletions panc/src/main/scripts/panlint/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_profilepath_trailing_slash(self):
([], set(), 0, False)
)

good_line_2 = "prefix '/software/components/metaconfig/services/{/etc/sysconfig/fetch-crl}';"
good_line_2 = "prefix '/system/network/interfaces/eth0';"
self.assertEqual(
panlint.lint_line(good_line_2, 151, [], False),
([], set(), 0, False)
Expand All @@ -163,8 +163,8 @@ def test_profilepath_trailing_slash(self):
(bad_diag_2, set(bad_msg_2), 1, False)
)

bad_line_3 = "prefix '/software/components/filecopy/services/{/etc/strange/service.conf}/';"
bad_diag_3 = [' ^']
bad_line_3 = "prefix '/system/aii/osinstall/ks/';"
bad_diag_3 = [' ^']
bad_msg_3 = ['Unnecessary trailing slash at end of profile path']
self.assertEqual(
panlint.lint_line(bad_line_3, 182, [], False),
Expand Down Expand Up @@ -226,6 +226,44 @@ def test_find_annotation_blocks(self):
self.assertItemsEqual(panlint.find_annotation_blocks(test_text), [2, 7])
self.assertEqual(panlint.find_annotation_blocks('template garbage;\n\n# Nothing to see here.\n\n'), [])

def test_component_use(self):
# Test a line containing a standard path assignment
line_standard = "'/software/components/chkconfig/service/rdma' = dict("
diag_standard = " ^^^^^^^^^"

# Test a line setting a path prefix
line_prefix = "prefix '/software/components/metaconfig/services/{/etc/sysconfig/fetch-crl}';"
diag_prefix = " ^^^^^^^^^^"

# Test both lines with components listed as included
self.assertEqual(
panlint.lint_line(line_standard, 100, ['chkconfig'], False),
([], set(), 0, False)
)
self.assertEqual(
panlint.lint_line(line_prefix, 200, ['metaconfig'], False),
([], set(), 0, False)
)

# Test both lines without components listed as included
self.assertEqual(
panlint.lint_line(line_standard, 100, [], False),
([diag_standard], set(['Component chkconfig in use, but component config has not been included']), 1, False)
)
self.assertEqual(
panlint.lint_line(line_prefix, 200, [], False),
([diag_prefix], set(['Component metaconfig in use, but component config has not been included']), 1, False)
)

# Test both lines without components listed as included but commented out
self.assertEqual(
panlint.lint_line('# ' + line_standard, 100, [], False),
([], set(), 0, False)
)
self.assertEqual(
panlint.lint_line(' #' + line_prefix, 200, [], False),
([], set(), 0, False)
)

if __name__ == '__main__':
unittest.main()

0 comments on commit b9334c9

Please sign in to comment.