[prev in list] [next in list] [prev in thread] [next in thread] 

List:       omd-commits
Subject:    [omd-commits] OMD Git: omd: omd: several fixes in omd diff
From:       git () mathias-kettner ! de (git version control)
Date:       2011-10-30 8:45:51
Message-ID: 20111030084551.2CFD5C029D0 () mail ! mathias-kettner ! de
[Download RAW message or body]

Module:   omd
Branch:   master
Commit:   94ba0309c7299c9c04f245fe91d63e2c660b669f
URL:      http://omdistro.org/projects/omd/repository/revisions/94ba0309c7299c9c04f245fe91d63e2c660b669f


Author:   Mathias Kettner <mk at mathias-kettner.de>
Date:     Sun Oct 30 09:45:46 2011 +0100
Commiter: Mathias Kettner <mk at mathias-kettner.de>
Date:     Sun Oct 30 09:45:46 2011 +0100

omd: several fixes in omd diff

---

 Changelog        |    3 +
 packages/omd/omd |  156 ++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 107 insertions(+), 52 deletions(-)

diff --git a/Changelog b/Changelog
index 37fb55b..169ea52 100644
--- a/Changelog
+++ b/Changelog
@@ -33,6 +33,9 @@ This file documents the revision history for the Open Monitoring \
Distribution  the site as root via 'omd start SITE'
           - FIX: omd update handled most files twice (without need). This is
             fixed now.
+          - FIX: omd diff now correctly handles relative paths. 
+          - FIX: omd diff now correctly instantiates templates before showing
+            diff in verboes mode
 
 0.50     Sep 20 2011
           - Check_MK: fix problem with playing sounds
diff --git a/packages/omd/omd b/packages/omd/omd
index 452040a..179116a 100644
--- a/packages/omd/omd
+++ b/packages/omd/omd
@@ -2256,75 +2256,127 @@ def main_mv_or_cp(what, args, options={}):
     create_tmpfs(g_sitename)
 
 
+def main_diff(args, options={}):
+    from_version  = site_version(g_sitename)
+    from_skelroot = "/omd/versions/%s/skel" % from_version
 
-def diff_list(from_skelroot, g_sitedir, from_version, relbase, bare):
-    """ Outputs only a list of changed/deleted files compared to the current \
                version."""
-    def print_diff(relpath, source_path, target_path, source_version, source_perms):
-        source_file = source_path + '/' + relpath
-        target_file = target_path + '/' + relpath
+    # If arguments are added and those arguments are directories,
+    # then we just output the general state of the file. If only
+    # one file is specified, we directly show the unified diff.
+    # This behaviour can also be forced by the OMD option -v.
 
-        source_perm = get_skel_permissions(source_version, source_perms, relpath)
-        target_perm = get_file_permissions(target_file)
+    if len(args) == 0:
+        args = ["."]
+    elif len(args) == 1 and os.path.isfile(args[0]):
+        global opt_verbose
+        opt_verbose = True
 
-        source_type = filetype(source_file)
-        target_type = filetype(target_file)
+    global bare
+    bare = "bare" in options # sorry for the global variable here
 
-        changed_type, changed_content, changed = file_status(source_file, \
target_file) +    for arg in args:
+        diff_list(from_skelroot, g_sitedir, from_version, arg)
 
-        if not changed:
-            return
 
-        fn = tty_bold + tty_bgblue + tty_white + relpath + tty_normal
-        fn = tty_bold + relpath + tty_normal
 
-        def print_status(color, f, status, long_out):
-            if bare:
-                sys.stdout.write("%s %s\n" % (status, f))
-            else:
-                sys.stdout.write(color + " %s %s\n" % (long_out, f))
-
-            # Show a little more details in verbose mode
-            if opt_verbose:
-                arrow = tty_magenta + '->' + tty_normal
-                if 'c' in status:
-                    os.system("pwd ; diff -u %s %s" % (source_file, target_file))
-                elif status == 'p':
-                    sys.stdout.write("    %s %s %s\n" % (source_perm, arrow, \
                target_perm))
-                elif 't' in status:
-                    sys.stdout.write("    %s %s %s\n" % (source_type, arrow, \
                target_type))
-
-        if not target_type:
-            print_status(good, fn, 'd', 'Deleted')
-            return
+def diff_list(from_skelroot, g_sitedir, from_version, orig_path):
+    # Compare a list of files/directories with the original state
+    # and output differences. If opt_verbose then we output the complete
+    # diff, otherwise just the state. Only files present in skel/ are
+    # handled at all.
 
-        elif changed_type and changed_content:
-            print_status(good, fn, 'tc', 'Changed type and content')
+    read_skel_permissions()
+    old_perms = load_skel_permissions(from_version)
 
-        elif changed_type and not changed_content:
-            print_status(good, fn, 't', 'Changed type')
+    # Prepare paths:
+    # orig_path: this was specified by the user
+    # rel_path:  path relative to the site's dir
+    # abs_path:  absolute path
 
-        elif changed_content and not changed_type:
-            print_status(good, fn, 'c', 'Changed content')
+    # Get absolute path to site dir. This can be (/opt/omd/sites/XXX)
+    # due to the symbolic link /omd
+    old_dir = os.getcwd()
+    os.chdir(g_sitedir)
+    abs_sitedir = os.getcwd()
+    os.chdir(old_dir)
+
+    # Create absolute paths first
+    abs_path = orig_path
+    if not abs_path.startswith("/"):
+        if abs_path == ".":
+            abs_path = ""
+        elif abs_path.startswith("./"):
+            abs_path = path[2:]
+        abs_path = os.getcwd() + "/" + abs_path
+    abs_path = abs_path.rstrip("/")
+
+    # Make sure that path does not lie outside the OMD site
+    if abs_path.startswith(g_sitedir):
+        rel_path = abs_path[len(g_sitedir) + 1:]
+    elif abs_path.startswith(abs_sitedir):
+        rel_path = abs_path[len(abs_sitedir) + 1:]
+    else:
+        bail_out("Omd diff only works for files in the site's directory.")
 
-        if source_perm != target_perm:
-            print_status(warn, fn, 'p', 'Changed permissions')
+    if not os.path.isdir(abs_path):
+        print_diff(rel_path, from_skelroot, g_sitedir, from_version, old_perms)
+    else:
+        walk_skel(from_skelroot, print_diff, (from_skelroot, g_sitedir, 
+                  from_version, old_perms), relbase = rel_path)
 
-    read_skel_permissions()
-    old_perms = load_skel_permissions(from_version)
 
-    if not os.path.isdir(relbase):
-        print_diff(relbase, from_skelroot, g_sitedir, from_version, old_perms)
-    else:
-        walk_skel(from_skelroot, print_diff, (from_skelroot, g_sitedir, \
from_version, old_perms), relbase = relbase) +def print_diff(rel_path, source_path, \
target_path, source_version, source_perms): +    source_file = source_path + '/' + \
rel_path +    target_file = target_path + '/' + rel_path
 
+    source_perm = get_skel_permissions(source_version, source_perms, rel_path)
+    target_perm = get_file_permissions(target_file)
 
-def main_diff(args, options={}):
-    from_version  = site_version(g_sitename)
-    from_skelroot = "/omd/versions/%s/skel" % from_version
+    source_type = filetype(source_file)
+    target_type = filetype(target_file)
+
+    changed_type, changed_content, changed = file_status(source_file, target_file)
+
+    if not changed:
+        return
+
+    fn = tty_bold + tty_bgblue + tty_white + rel_path + tty_normal
+    fn = tty_bold + rel_path + tty_normal
+
+    def print_status(color, f, status, long_out):
+        if bare:
+            sys.stdout.write("%s %s\n" % (status, f))
+        elif not opt_verbose:
+            sys.stdout.write(color + " %s %s\n" % (long_out, f))
+        else:
+            arrow = tty_magenta + '->' + tty_normal
+            if 'c' in status:
+                source_content = instantiate_skel(source_file)
+                if 0 == os.system("which colordiff > /dev/null 2>&1"):
+                    diff = "colordiff"
+                else:
+                    diff = "diff"
+                os.popen("bash -c \"%s -u <(cat) '%s'\"" % (diff, target_file), \
"w").write(source_content) +            elif status == 'p':
+                sys.stdout.write("    %s %s %s\n" % (source_perm, arrow, \
target_perm)) +            elif 't' in status:
+                sys.stdout.write("    %s %s %s\n" % (source_type, arrow, \
target_type)) +
+    if not target_type:
+        print_status(good, fn, 'd', 'Deleted')
+        return
+
+    elif changed_type and changed_content:
+        print_status(good, fn, 'tc', 'Changed type and content')
+
+    elif changed_type and not changed_content:
+        print_status(good, fn, 't', 'Changed type')
 
-    relbase = len(args) > 0 and args[0].rstrip('/') or '.'
-    diff_list(from_skelroot, g_sitedir, from_version, relbase, "bare" in options)
+    elif changed_content and not changed_type:
+        print_status(good, fn, 'c', 'Changed content')
 
+    if source_perm != target_perm:
+        print_status(warn, fn, 'p', 'Changed permissions')
 
 def walk_skel(root, handler, args, exclude_if_in = None, relbase = '.'):
     # Walks all files in the skeleton dir to execute a function for each file


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic