Use string startswith() and endswith() methods instead of slicing to check for prefixes and suffixes.

This commit is contained in:
Mark Grassi 2022-08-10 22:45:03 -04:00
parent 854602c0d9
commit 091c6ad59d
6 changed files with 14 additions and 14 deletions

View file

@ -2032,7 +2032,7 @@ def parse_profile_data(data, file, do_include, in_preamble):
aaui.UI_Important(_('Ignoring no longer supported change hat declaration "^%(hat)s," found in file: %(file)s line: %(line)s')
% {'hat': matches[0], 'file': file, 'line': lineno + 1})
elif line[0] == '#':
elif line.startswith('#'):
# Handle initial comments
if not profile:
if line.startswith('# Last Modified:'):

View file

@ -89,8 +89,8 @@ class AARE:
def glob_path(self):
"""Glob the given file or directory path"""
if self.regex[-1] == '/':
if self.regex[-4:] == '/**/' or self.regex[-3:] == '/*/':
if self.regex.endswith('/'):
if self.regex.endswith(('/**/', '/*/')):
# /foo/**/ and /foo/*/ => /**/
newpath = re.sub('/[^/]+/\*{1,2}/$', '/**/', self.regex) # re.sub('/[^/]+/\*{1,2}$/', '/\*\*/', self.regex)
elif re.search('/[^/]+\*\*[^/]*/$', self.regex):
@ -102,7 +102,7 @@ class AARE:
else:
newpath = re.sub('/[^/]+/$', '/*/', self.regex)
else:
if self.regex[-3:] == '/**' or self.regex[-2:] == '/*':
if self.regex.endswith(('/**', '/*')):
# /foo/** and /foo/* => /**
newpath = re.sub('/[^/]+/\*{1,2}$', '/**', self.regex)
elif re.search('/[^/]*\*\*[^/]+$', self.regex):

View file

@ -182,7 +182,7 @@ def is_skippable_file(path):
basename = os.path.basename(path)
if not basename or basename[0] == '.' or basename == 'README':
if not basename or basename.startswith('.') or basename == 'README':
return True
skippable_suffix = (
@ -259,9 +259,9 @@ def convert_regexp(regexp):
# ?< is the negative lookback operator
new_reg = new_reg.replace('*', '(((?<=/)[^/\000]+)|((?<!/)[^/\000]*))')
new_reg = new_reg.replace(multi_glob, '(((?<=/)[^\000]+)|((?<!/)[^\000]*))')
if regexp[0] != '^':
if not regexp.startswith('^'):
new_reg = '^' + new_reg
if regexp[-1] != '$':
if not regexp.endswith('$'):
new_reg = new_reg + '$'
return new_reg

View file

@ -204,7 +204,7 @@ def re_match_include_parse(line, rule_name):
path = matches.group('quotedpath')
# LP: 1738880 - parser doesn't handle relative paths everywhere, and
# neither do we (see aa.py)
if rule_name == 'include' and path and path[0] != '/':
if rule_name == 'include' and path and not path.startswith('/'):
raise AppArmorException(_('Syntax error: %s must use quoted path or <...>') % rule_name)
if not path:
@ -236,14 +236,14 @@ def strip_parenthesis(data):
The parenthesis must be the first and last char, otherwise they won't be removed.
Even if no parenthesis get removed, the result will be strip()ped.
"""
if data[0] + data[-1] == '()':
if data.startswith('(') and data.endswith(')'):
return data[1:-1].strip()
else:
return data.strip()
def strip_quotes(data):
if len(data) > 1 and data[0] + data[-1] == '""':
if len(data) > 1 and data.startswith('"') and data.endswith('"'):
return data[1:-1]
else:
return data

View file

@ -548,7 +548,7 @@ def split_perms(perm_string, deny):
if perm_string[0] in file_permissions:
perms.add(perm_string[0])
perm_string = perm_string[1:]
elif perm_string[0] == 'x':
elif perm_string.startswith('x'):
if not deny:
raise AppArmorException(_("'x' must be preceded by an exec qualifier (i, P, C or U)"))
exec_mode = 'x'

View file

@ -97,7 +97,7 @@ class Severity:
"""Returns the rank for the given path"""
if '@' in path: # path contains variable
return self.handle_variable_rank(path, mode)
elif path[0] == '/': # file resource
elif path.startswith('/'): # file resource
return self.handle_file(path, mode)
else:
raise AppArmorException("Unexpected path input: %s" % path)
@ -175,9 +175,9 @@ class Severity:
leading = True
if resource.find(variable + "/") != -1 and resource.find(variable + "//") == -1:
trailing = True
if replacement[0] == '/' and replacement[:2] != '//' and leading: # finds if the replacement has leading / or not
if replacement.startswith('/') and not replacement.startswith('//') and leading: # finds if the replacement has leading / or not
replacement = replacement[1:]
if replacement[-1] == '/' and replacement[-2:] != '//' and trailing:
if replacement.endswith('/') and not replacement.endswith('//') and trailing:
replacement = replacement[:-1]
return resource.replace(variable, replacement)