[apparmor] [PATCH 1/2] Introduce UI_Changes functions.

Goldwyn Rodrigues rgoldwyn at suse.de
Mon Oct 23 10:38:33 UTC 2017


From: Goldwyn Rodrigues <rgoldwyn at suse.com>

This is a preparation patch to use for JSON mode of conveying
diff filename. In this patch we move diff generation functions to UI.
In the process, I have cleaned up the code to reduce code and enable reuse.

Remove unused function get_profile_diff().

Signed-off-by: Goldwyn Rodrigues <rgoldwyn at suse.com>
---
 utils/apparmor/aa.py    | 58 +++----------------------------------------------
 utils/apparmor/tools.py |  2 +-
 utils/apparmor/ui.py    | 38 ++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 56 deletions(-)

diff --git a/utils/apparmor/aa.py b/utils/apparmor/aa.py
index 47fd3aa1..42ef41ac 100644
--- a/utils/apparmor/aa.py
+++ b/utils/apparmor/aa.py
@@ -822,7 +822,7 @@ def console_select_and_upload_profiles(title, message, profiles_up):
     while 'CMD_UPLOAD_CHANGES' not in ans and 'CMD_ASK_NEVER' not in ans and 'CMD_ASK_LATER' not in ans:
         ans, arg = q.promptUser()
         if ans == 'CMD_VIEW_CHANGES':
-            display_changes(profs[arg][2], profs[arg][1])
+            aaui.UI_Changes(profs[arg][2], profs[arg][1])
     if ans == 'CMD_NEVER_ASK':
         set_profiles_local_only([i[0] for i in profs])
     elif ans == 'CMD_UPLOAD_CHANGES':
@@ -1845,14 +1845,14 @@ def save_profiles():
                     # see https://bugs.launchpad.net/ubuntu/+source/apparmor/+bug/1528139
                     newprofile = "###\n###\n### Internal error while generating diff, please use '%s' instead\n###\n###\n" % _('View Changes b/w (C)lean profiles')
 
-                display_changes_with_comments(oldprofile, newprofile)
+                aaui.UI_Changes(oldprofile, newprofile, comments=True)
 
             elif ans == 'CMD_VIEW_CHANGES_CLEAN':
                 which = list(changed.keys())[arg]
                 oldprofile = serialize_profile(original_aa[which], which, '')
                 newprofile = serialize_profile(aa[which], which, '')
 
-                display_changes(oldprofile, newprofile)
+                aaui.UI_Changes(oldprofile, newprofile)
 
         for profile_name in sorted(changed.keys()):
             write_profile_ui_feedback(profile_name)
@@ -1861,58 +1861,6 @@ def save_profiles():
 def get_pager():
     return 'less'
 
-def generate_diff(oldprofile, newprofile):
-    oldtemp = tempfile.NamedTemporaryFile('w')
-
-    oldtemp.write(oldprofile)
-    oldtemp.flush()
-
-    newtemp = tempfile.NamedTemporaryFile('w')
-    newtemp.write(newprofile)
-    newtemp.flush()
-
-    difftemp = tempfile.NamedTemporaryFile('w', delete=False)
-
-    subprocess.call('diff -u -p %s %s > %s' % (oldtemp.name, newtemp.name, difftemp.name), shell=True)
-
-    oldtemp.close()
-    newtemp.close()
-    return difftemp
-
-def get_profile_diff(oldprofile, newprofile):
-    difftemp = generate_diff(oldprofile, newprofile)
-    diff = []
-    with open_file_read(difftemp.name) as f_in:
-        for line in f_in:
-            if not (line.startswith('---') and line .startswith('+++') and line.startswith('@@')):
-                    diff.append(line)
-
-    difftemp.delete = True
-    difftemp.close()
-    return ''.join(diff)
-
-def display_changes(oldprofile, newprofile):
-    difftemp = generate_diff(oldprofile, newprofile)
-    subprocess.call('less %s' % difftemp.name, shell=True)
-    difftemp.delete = True
-    difftemp.close()
-
-def display_changes_with_comments(oldprofile, newprofile):
-    """Compare the new profile with the existing profile inclusive of all the comments"""
-    if not os.path.exists(oldprofile):
-        raise AppArmorException(_("Can't find existing profile %s to compare changes.") % oldprofile)
-    newtemp = tempfile.NamedTemporaryFile('w')
-    newtemp.write(newprofile)
-    newtemp.flush()
-
-    difftemp = tempfile.NamedTemporaryFile('w')
-
-    subprocess.call('diff -u -p %s %s > %s' % (oldprofile, newtemp.name, difftemp.name), shell=True)
-
-    newtemp.close()
-    subprocess.call('less %s' % difftemp.name, shell=True)
-    difftemp.close()
-
 def set_process(pid, profile):
     # If process not running don't do anything
     if not os.path.exists('/proc/%s/attr/current' % pid):
diff --git a/utils/apparmor/tools.py b/utils/apparmor/tools.py
index a05c54ac..7e3a691b 100644
--- a/utils/apparmor/tools.py
+++ b/utils/apparmor/tools.py
@@ -225,7 +225,7 @@ class aa_tools:
                     elif ans == 'CMD_VIEW_CHANGES':
                         #oldprofile = apparmor.serialize_profile(apparmor.original_aa[program], program, '')
                         newprofile = apparmor.serialize_profile(apparmor.aa[program], program, '')
-                        apparmor.display_changes_with_comments(filename, newprofile)
+			aaui.UI_Changes(filename, newprofile, comments=True)
             else:
                 apparmor.write_profile_ui_feedback(program)
                 self.reload_profile(filename)
diff --git a/utils/apparmor/ui.py b/utils/apparmor/ui.py
index 0010f468..be07b28a 100644
--- a/utils/apparmor/ui.py
+++ b/utils/apparmor/ui.py
@@ -18,6 +18,9 @@ import json
 import sys
 import re
 import readline
+import os
+import tempfile
+import subprocess
 
 from apparmor.common import readkey, AppArmorException, DebugLogger
 
@@ -217,6 +220,41 @@ def UI_BusyStart(message):
 def UI_BusyStop():
     debug_logger.debug('UI_BusyStop: %s' % UI_mode)
 
+def diff(oldprofile, newprofile):
+    difftemp = tempfile.NamedTemporaryFile('w')
+    subprocess.call('diff -u -p %s %s > %s' % (oldprofile, newprofile, difftemp.name), shell=True)
+    return difftemp
+
+def write_profile_to_tempfile(profile):
+    temp = tempfile.NamedTemporaryFile('w')
+    temp.write(profile)
+    temp.flush()
+    return temp
+
+def generate_diff(oldprofile, newprofile):
+    oldtemp = write_profile_to_tempfile(oldprofile)
+    newtemp = write_profile_to_tempfile(newprofile)
+    difftemp = diff(oldtemp.name, newtemp.name)
+    oldtemp.close()
+    newtemp.close()
+    return difftemp
+
+def generate_diff_with_comments(oldprofile, newprofile):
+    if not os.path.exists(oldprofile):
+        raise AppArmorException(_("Can't find existing profile %s to compare changes.") % oldprofile)
+    newtemp = write_profile_to_tempfile(newprofile)
+    difftemp = diff(oldprofile, newtemp.name)
+    newtemp.close()
+    return difftemp
+
+def UI_Changes(oldprofile, newprofile, comments=False):
+    if comments == False:
+      difftemp = generate_diff(oldprofile, newprofile)
+    else:
+      difftemp = generate_diff_with_comments(oldprofile, newprofile)
+    subprocess.call('less %s' % difftemp.name, shell=True)
+    difftemp.close()
+
 CMDS = {'CMD_ALLOW': _('(A)llow'),
         'CMD_OTHER': _('(M)ore'),
         'CMD_AUDIT_NEW': _('Audi(t)'),
-- 
2.14.2




More information about the AppArmor mailing list