590d18
From 7919e3c6b245adb0f6d6743edaf03da704259b5d Mon Sep 17 00:00:00 2001
590d18
From: Martin Basti <mbasti@redhat.com>
590d18
Date: Fri, 10 Jul 2015 14:17:02 +0200
590d18
Subject: [PATCH] copy-schema-to-ca: allow to overwrite schema files
590d18
590d18
If content of source and target file differs, the script will ask user
590d18
for permission to overwrite target file.
590d18
590d18
https://fedorahosted.org/freeipa/ticket/5034
590d18
590d18
Reviewed-By: David Kupka <dkupka@redhat.com>
590d18
---
590d18
 install/share/copy-schema-to-ca.py | 29 ++++++++++++++++++++++++++---
590d18
 1 file changed, 26 insertions(+), 3 deletions(-)
590d18
590d18
diff --git a/install/share/copy-schema-to-ca.py b/install/share/copy-schema-to-ca.py
590d18
index 1614e11636c2f52e231ea2ff40d882209194c60a..ff6c3568586f9f4b3fac7f848869e74d0db0df34 100755
590d18
--- a/install/share/copy-schema-to-ca.py
590d18
+++ b/install/share/copy-schema-to-ca.py
590d18
@@ -15,6 +15,8 @@ import sys
590d18
 import pwd
590d18
 import shutil
590d18
 
590d18
+from hashlib import sha1
590d18
+
590d18
 from ipapython import ipautil, dogtag
590d18
 from ipapython.ipa_log_manager import root_logger, standard_logging_setup
590d18
 from ipaserver.install.dsinstance import DS_USER, schema_dirname
590d18
@@ -42,6 +44,11 @@ SCHEMA_FILENAMES = (
590d18
 )
590d18
 
590d18
 
590d18
+def _sha1_file(filename):
590d18
+    with open(filename, 'rb') as f:
590d18
+        return sha1(f.read()).hexdigest()
590d18
+
590d18
+
590d18
 def add_ca_schema():
590d18
     """Copy IPA schema files into the CA DS instance
590d18
     """
590d18
@@ -54,9 +61,25 @@ def add_ca_schema():
590d18
             root_logger.debug('File does not exist: %s', source_fname)
590d18
             continue
590d18
         if os.path.exists(target_fname):
590d18
-            root_logger.info(
590d18
-                'Target exists, not overwriting: %s', target_fname)
590d18
-            continue
590d18
+            target_sha1 = _sha1_file(target_fname)
590d18
+            source_sha1 = _sha1_file(source_fname)
590d18
+            if target_sha1 != source_sha1:
590d18
+                target_size = os.stat(target_fname).st_size
590d18
+                source_size = os.stat(source_fname).st_size
590d18
+                root_logger.info('Target file %s exists but the content is '
590d18
+                                 'different', target_fname)
590d18
+                root_logger.info('\tTarget file: sha1: %s, size: %s B',
590d18
+                                 target_sha1, target_size)
590d18
+                root_logger.info('\tSource file: sha1: %s, size: %s B',
590d18
+                                 source_sha1, source_size)
590d18
+                if not ipautil.user_input("Do you want replace %s file?" %
590d18
+                                          target_fname, True):
590d18
+                    continue
590d18
+
590d18
+            else:
590d18
+                root_logger.info(
590d18
+                    'Target exists, not overwriting: %s', target_fname)
590d18
+                continue
590d18
         try:
590d18
             shutil.copyfile(source_fname, target_fname)
590d18
         except IOError, e:
590d18
-- 
590d18
2.1.0
590d18