From f5ed9cffe31e1be8e75771c536ee010786efe2cb Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Sun, 6 Oct 2024 22:03:25 +0200 Subject: [PATCH] serialize_profile(): simplify and cleanup Drop `comment.replace('\\n', '\n')` because that doesn't make sense and doesn't change anything - not even a comment that contains the literal string '\n' (backslash + letter n). Besides that, get rid of the 'string' variable and store everything in 'data'. --- utils/apparmor/aa.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/utils/apparmor/aa.py b/utils/apparmor/aa.py index f6d2587cf..76465c933 100644 --- a/utils/apparmor/aa.py +++ b/utils/apparmor/aa.py @@ -2007,7 +2007,8 @@ def write_piece(profile_data, depth, name, nhat): def serialize_profile(profile_data, name, options): - string = '' + ''' combine the preamble and profiles in a file to a string (to be written to the profile file) ''' + data = [] if not isinstance(options, dict): @@ -2016,12 +2017,11 @@ def serialize_profile(profile_data, name, options): include_metadata = options.get('METADATA', False) if include_metadata: - string = '# Last Modified: %s\n' % time.asctime() + data.extend(['# Last Modified: %s' % time.asctime()]) # if profile_data[name].get('initial_comment', False): # comment = profile_data[name]['initial_comment'] -# comment.replace('\\n', '\n') -# string += comment + '\n' +# data.append(comment) if options.get('is_attachment'): prof_filename = get_profile_filename_from_attachment(name, True) @@ -2035,23 +2035,22 @@ def serialize_profile(profile_data, name, options): if active_profiles.profiles[prof]['parent']: continue # child profile or hat, already part of its parent profile + # aa-logprof asks to save each file separately. Therefore only update the given profile, and keep the original version of other profiles in the file if prof != name: if original_aa.get(prof, {}).get(prof, {}).get('initial_comment', False): comment = original_aa[prof][prof]['initial_comment'] - comment.replace('\\n', '\n') - data.append(comment + '\n') + data.extend([comment, '']) + data.extend(write_piece(split_to_merged(original_aa), 0, prof, prof)) + else: if profile_data[name].get('initial_comment', False): comment = profile_data[name]['initial_comment'] - comment.replace('\\n', '\n') - data.append(comment + '\n') + data.extend([comment, '']) data.extend(write_piece(profile_data, 0, name, name)) - string += '\n'.join(data) - - return string + '\n' + return '\n'.join(data) + '\n' def write_profile_ui_feedback(profile, is_attachment=False, out_dir=None):