cmd_changelog: Don't update changelog if user doesn't save it in editor. Fixes #20
authorEd Bartosh <eduard.bartosh@intel.com>
Fri, 1 Jun 2012 09:58:21 +0000 (12:58 +0300)
committerEd Bartosh <eduard.bartosh@intel.com>
Mon, 4 Jun 2012 10:11:19 +0000 (13:11 +0300)
Change-Id: I9e4a63981f8c984203d60f1dfe1f3206f127e3ef

gitbuildsys/cmd_changelog.py
gitbuildsys/utils.py

index f31a19e..3e39869 100644 (file)
@@ -23,8 +23,10 @@ import os
 import datetime
 import glob
 import subprocess
+import shutil
 
 import msger
+import utils
 
 from conf import configmgr
 
@@ -110,7 +112,6 @@ def do(opts, _args):
         spec_file_list = glob.glob("%s/packaging/*.spec" % project_root_dir)
         if spec_file_list:
             fn_changes = os.path.splitext(spec_file_list[0])[0] + ".changes"
-            open(fn_changes, 'w').close() # touch
         else:
             msger.error("Found no changes nor spec files under packaging dir")
 
@@ -136,7 +137,16 @@ def do(opts, _args):
         msger.error("Nothing found between %s and HEAD" % commitid_since)
 
     new_entries = make_log_entries(commits, repo)
-    add_entries(fn_changes, new_entries)
 
-    subprocess.call("%s %s" % (EDITOR, fn_changes), shell=True)
-    msger.info("Change log file updated.")
+    # create temporary copy and update it with new entries
+    temp = utils.TempCopy(fn_changes)
+    add_entries(temp.name, new_entries)
+    temp.update_stat()
+
+    subprocess.call("%s %s" % (EDITOR, temp.name), shell=True)
+
+    if temp.is_changed():
+       msger.info("Change log has been updated.")
+    else:
+        msger.info("Change log has not been updated")
+
index 490ba0d..7ba1969 100644 (file)
@@ -21,6 +21,8 @@ import os
 import glob
 import platform
 import re
+import tempfile
+import shutil
 
 import msger
 import runner
@@ -309,3 +311,34 @@ def guess_spec(workdir, default_spec):
         else:
             specfile = specs[0]
     return specfile
+
+class TempCopy(object):
+    """Copy original file to temporary file in the same directory as
+       original. Creates empty termporary file if original doesn't exist.
+       Deletes termporary file when object is destroyed.
+    """
+
+    def __init__(self, orig_fpath):
+        self.orig_fpath = orig_fpath
+
+        # create temp file
+        tmpffd, self.name = tempfile.mkstemp(dir=os.path.dirname(orig_fpath))
+        os.close(tmpffd)
+
+        # copy original file to temp
+        if os.path.exists(orig_fpath):
+            shutil.copy2(orig_fpath, self.name)
+
+        self.stat = os.stat(self.name)
+
+    def update_stat(self):
+        """Updates stat info."""
+        self.stat = os.stat(self.name)
+
+    def is_changed(self):
+        """Check if temporary file has been changed."""
+        return os.stat(self.name) != self.stat
+
+    def __del__(self):
+        if os.path.exists(self.name):
+            os.unlink(self.name)