# HG changeset patch
# User Ivo Hinkelmann <ihi@openoffice.org>
# Date 1297347065 -3600
# Node ID b026dd9164d06314db95832bc24ea761063f48fb
# Parent  9a8591d22a1599ecc18da4eb0e471c05991f8d01
sdf2: save moved strings into zipfile

diff --git a/l10ntools/scripts/tool/l10nupdate.py b/l10ntools/scripts/tool/l10nupdate.py
--- a/l10ntools/scripts/tool/l10nupdate.py
+++ b/l10ntools/scripts/tool/l10nupdate.py
@@ -1,4 +1,4 @@
-import tempfile, sys, subprocess, sdf_raw, sdf2, os, identfile, heapq, glob, sdfutils
+import tempfile, sys, subprocess, sdf_raw, sdf2, os, identfile, heapq, glob, sdfutils, cStringIO, tempfile, shutil, zipfile
 
 # run localize
 # add all new strings to sourcedb
@@ -72,6 +72,7 @@
                 obsolete_cnt += 1 
                 cleanup_list.append(line)
         
+        moved_strings = list()
         for key,line in new_dict.iteritems():
             candidate = self.detect_moved(obsolete_dict, line)
             if not candidate == None:
@@ -89,6 +90,7 @@
                     del ident.ident[line.get_identifier()]
                 if str(candidate) in obsolete_dict:
                     del obsolete_dict[str(candidate)]
+                moved_strings.append(line)
 
         if cmd == "cleanup":
             print "Removeing obsolete strings from sdf2 ident file"
@@ -102,7 +104,37 @@
                 
         print "| new      : %(new_cnt)7d | moved   : %(moved_cnt)7d | changed: %(changed_cnt)7d |" % { "new_cnt": new_cnt, "moved_cnt": moved_cnt , "changed_cnt": changed_cnt }
         print "| unchanged: %(unchanged_cnt)7d | obsolete: %(obsolete_cnt)7d | total  : %(total_cnt)7d |" % { "unchanged_cnt": unchanged_cnt, "obsolete_cnt": obsolete_cnt , "total_cnt": new_cnt+changed_cnt+unchanged_cnt }
- 
+        
+        self.save_moved_strings(moved_strings)
+
+    def save_moved_strings(self, moved_strings):
+        if( len(moved_strings) == 0 ):
+            return
+        print "Searching for moved translated strings and write them to zipfile moved_strings.zip"
+        tempdir = tempfile.mkdtemp()
+        for data_file in glob.glob(sdfutils.SdfUtils.get_data_location()+"*/localize.sdf2"):
+            lang = data_file.split("/")[-2]
+            lines = sdfutils.SdfUtils.read_file(data_file)
+            sdf_dict = dict()
+            file = cStringIO.StringIO() 
+            for line in lines:
+                sd = sdf2.Sdf2DataEntity()
+                sd.set_properties(line)
+                sdf_dict[sd.keyid] = sd
+            for moved_string in moved_strings:
+                if moved_string.keyid in sdf_dict:
+                    file.write(str(sdf2.convert_sdf2dataentity_to_sdf(moved_string, sdf_dict[moved_string.keyid]))+"\n")
+            sdfutils.SdfUtils.write_file(tempdir+"/"+lang+"_moved_strings.sdf", file.getvalue())
+        try:
+            myzip = zipfile.ZipFile(tempdir+"/moved_strings.zip", "w")
+            for file in glob.glob(tempdir+"/*_moved_strings.sdf"):
+                myzip.write(file,file.split("/")[-1])
+            myzip.close()
+        except IOError:
+            print "Can not save moved strings to zipfile +"+tempdir+"/moved_strings.zip"
+        shutil.move(tempdir+"/moved_strings.zip", "moved_strings.zip")
+        shutil.rmtree(tempdir)
+
     def detect_moved(self, ident, line):
         # filename, gid,lid,sid,hash
         pq = []
diff --git a/l10ntools/scripts/tool/sdfutils.py b/l10ntools/scripts/tool/sdfutils.py
--- a/l10ntools/scripts/tool/sdfutils.py
+++ b/l10ntools/scripts/tool/sdfutils.py
@@ -4,13 +4,31 @@
 class SdfUtils:
 
     @staticmethod
+    def append_file(file, data):
+        try:
+            fh = open(file, "a")
+            if isinstance(data, str):
+                fh.write(data)
+            elif isinstance(data, dict):
+                for line in data.values():
+                    fh.write(str(line) + "\n")
+            elif isinstance(data, list):
+                for line in data:
+                    fh.write(str(line) + "\n")
+            fh.close()
+        except IOError:
+            print "Can not append to file "+file
+            sys.exit(13)
+            
+    
+    @staticmethod
     def write_file(file, data):
         try:
             fd, tmpfile = tempfile.mkstemp()
             fh = os.fdopen(fd, "w")
     
             if isinstance(data, str):
-                fh.write(str)
+                fh.write(data)
             elif isinstance(data, dict):
                 for line in sorted(data.values(), key = lambda a: str(a)):
                     fh.write(str(line) + "\n")
