# HG changeset patch
# User Ivo Hinkelmann <ihi@openoffice.org>
# Date 1297183844 -3600
# Node ID 9a8591d22a1599ecc18da4eb0e471c05991f8d01
# Parent  99a5cf6d10b61678bde5117e380da725488109a2
sdf2: add support for multi repositories / some cleanup

diff --git a/l10ntools/scripts/tool/fast_merge3.py b/l10ntools/scripts/tool/fast_merge3.py
--- a/l10ntools/scripts/tool/fast_merge3.py
+++ b/l10ntools/scripts/tool/fast_merge3.py
@@ -31,23 +31,13 @@
                 os.unlink(dir_str)
             except:
                 pass
-            #try:
-            #    f = open(dir_str, "wa")
-            #except IOError:
-            #    print "ERROR: Can not write file " + dir_str
-            #    sys.exit(-1)
-        #f.write(str(line)+"\n")
-    #if f != None:
-    #    f.close() 
 
 def process_sdf2_data(sdf2_list, merge_dir, fh_dict, ident_dict):
-    #print sdf2_list
     for sdf2_file in sdf2_list:
         data = read_sdf2_data_file(sdf2_file)
         for keyid,line in data.iteritems():        
             #link ident + data
             fh = get_fh(ident_dict[keyid], merge_dir, fh_dict)
-            #fh.write(str(line)+"\n")
             fh.write(str(sdf2.convert_sdf2dataentity_to_sdf(ident_dict[keyid], line))+"\n")
         
 def write(fh_dict):
@@ -66,7 +56,6 @@
 def get_fh(line, merge_dir, fh_dict):
     dir_str = make_path(merge_dir, line)
     dir_str += "/localize.sdf"  
-#    dir_str += "/data.sdf2"  
 
     if dir_str in fh_dict:
         return fh_dict[dir_str]
diff --git a/l10ntools/scripts/tool/sdffile.py b/l10ntools/scripts/tool/identfile.py
copy from l10ntools/scripts/tool/sdffile.py
copy to l10ntools/scripts/tool/identfile.py
--- a/l10ntools/scripts/tool/sdffile.py
+++ b/l10ntools/scripts/tool/identfile.py
@@ -1,4 +1,4 @@
-import os, shutil, sys, sdf2, tempfile
+import os, shutil, sys, sdf2, tempfile, sdfutils
 
 class IdentFile:
     
@@ -8,12 +8,7 @@
         self.used_keyids = dict()
         
     def read(self, file):
-        try:
-            fh = open(file, "r")
-            tmplines = [line.strip('\n') for line in fh.readlines()]
-        except IOError:
-            print "Can not open file "+file
-            sys.exit(-1)
+        tmplines = sdfutils.SdfUtils.read_file(file)
             
         # Update ident hash    
         for line in tmplines:
@@ -24,21 +19,7 @@
             #self.lines.append(ie)
             
     def write(self, file):
-        try:
-            fd, tmpfile = tempfile.mkstemp()
-            fh = os.fdopen(fd, "w")
-            mylist = list()
-            self.rehash()
-            for line in self.ident.values():
-                mylist.append(str(line))
-            mylist.sort(key=str.lower)
-            for line in mylist:
-                fh.write(line+"\n")
-            fh.close()
-            shutil.move(tmpfile, file)
-        except IOError:
-            print "Can not write to file "+file
-            sys.exit(-1)
+        sdfutils.SdfUtils.write_file(file, self.ident.values())
 
     def make_sdf2idententity(sdfentity):
         c_ident = sdf2.Sdf2IdentEntity()
@@ -49,7 +30,6 @@
         c_ident.lid = line.lid           
         c_ident.sid = line.platform           
         c_ident.lang = line.langid          
-        #c_ident.keyid = sdf2.calc_keyid(line.project+line.source_file+line.lid+line.gid+line.platform+line.resource_type)
         c_ident.source_hash = sdf2.calc_hash(line.text+line.helptext+line.quickhelptext+line.title)
         c_ident.source_text = line.text   
         c_ident.source_helptext = line.helptext       
@@ -66,7 +46,6 @@
         c_ident.lid = line.lid           
         c_ident.sid = line.platform           
         c_ident.lang = line.langid          
-        #c_ident.keyid = sdf2.calc_keyid(line.project+line.source_file+line.lid+line.gid+line.platform+line.resource_type)
         c_ident.source_hash = sdf2.calc_hash(line.text+line.helptext+line.quickhelptext+line.title)
         c_ident.source_text = line.text   
         c_ident.source_helptext = line.helptext       
diff --git a/l10ntools/scripts/tool/l10nimport.py b/l10ntools/scripts/tool/l10nimport.py
--- a/l10ntools/scripts/tool/l10nimport.py
+++ b/l10ntools/scripts/tool/l10nimport.py
@@ -1,4 +1,4 @@
-import sdf, sdf2, sys, tempfile, os
+import sdf, sdf2, sys, tempfile, os, sdfutils
 
 def run(opts):
     x = l10nimport(opts)
@@ -16,7 +16,7 @@
             print "Usage: l10n import <file>"
             sys.exit(13)
 
-        lines = self.read_file(self.filename)
+        lines = sdfutils.SdfUtils.read_file(self.filename)
         new_dict = dict()
         for line in lines:
             sdfl = sdf.SdfEntity()
@@ -37,42 +37,17 @@
                 cur_lang = de.lang          
                 localize_dict = self.read_sdf2_data(de.lang)
                 localize_dict[de.keyid] = de
-                #print "added " + str(de)
             elif de.lang == cur_lang:        
                 localize_dict[de.keyid] = de
-                #print "added " + str(de)
-            else:
-                print "Not reachable, remove me!"
-        self.write_sdf2_data(cur_lang, localize_dict)
-
-    def write_sdf2_data(self, lang, localize_dict):
-        lines = sorted(localize_dict.values(), key=lambda a: str(a))
-        #todo: write to temp and shutil.move
-        try:
-            fh = open(os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/"+lang+"/localize.sdf2", "w")
-            for line in lines:
-                fh.write(str(line)+"\n")
-        except IOError:
-            print "Can not write to file "+ os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/"+lang+"/localize.sdf2"
-        fh.close()
+        
+        sdfutils.SdfUtils.write_file(sdfutils.SdfUtils.get_data_location()+cur_lang+"/localize.sdf2", localize_dict)
 
     def read_sdf2_data(self, lang):
         localize_dict = dict()
-        sdf_data = self.read_file(os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/"+lang+"/localize.sdf2")
-                
+        sdf_data = sdfutils.SdfUtils.read_file(sdfutils.SdfUtils.get_data_location()+lang+"/localize.sdf2")
+               
         for line in sdf_data:
             se = sdf2.Sdf2DataEntity()
             se.set_properties(line)
             localize_dict[se.keyid] = se
         return localize_dict
-
-    def read_file(self, filename):
-        try:
-            f = open(filename, "r")
-            lines = [line.strip("\n") for line in f.readlines()]
-        except IOError, e:
-            print "Can not read file "+filename
-            print e
-            sys.exit(13)
-        f.close()
-        return lines
diff --git a/l10ntools/scripts/tool/l10nstatus.py b/l10ntools/scripts/tool/l10nstatus.py
--- a/l10ntools/scripts/tool/l10nstatus.py
+++ b/l10ntools/scripts/tool/l10nstatus.py
@@ -1,4 +1,4 @@
-import os, sys, glob, sdf2
+import os, sys, glob, sdf2, sdfutils
 
 def run(opts):
     s = l10nstatus(opts)
@@ -32,18 +32,12 @@
 
     def exe(self, cmd=""):
         use_lang = ""
-        default_ident_location = os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/ident.sdf2"
+        default_ident_location = sdfutils.SdfUtils.get_ident_location()
         if len(self.opts) == 2:
-            default_location = os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/*/localize.sdf2" 
+            default_location = sdfutils.SdfUtils.get_data_location()+"*/localize.sdf2" 
         elif len(self.opts) == 3:
             use_lang = self.opts[2]
             lang_list = use_lang.split(",")    
-        try:
-            fh = open(default_ident_location, "r")
-            tmplines = [line.strip('\n') for line in fh.readlines()]
-        except IOError:
-            print "Can not open file "+file
-            sys.exit(-1)
          
         status = L10nStatus()
         self.read_ident_data(default_ident_location)
@@ -53,15 +47,10 @@
         else:
             sdf_data_list = list()
             for lang in lang_list:
-                sdf_data_list.append(os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/"+lang+"/localize.sdf2")
+                sdf_data_list.append(sdfutils.SdfUtils.get_data_location()+lang+"/localize.sdf2")
                         
         for file in sdf_data_list:
-            mylines = list()
-            try:
-                fh = open(file, "r")
-                lines = [line.strip('\n') for line in fh.readlines()]
-            except IOError:
-                print "Can not openfile "+file
+            lines = sdfutils.SdfUtils.read_file(file)
             lang = file.split("/")
             lang = lang[-2]
             
@@ -128,12 +117,7 @@
 
 
     def read_ident_data(self, default_ident_location):
-        try:
-            fh = open(default_ident_location, "r")
-            tmplines = [line.strip('\n') for line in fh.readlines()]
-        except IOError:
-            print "Can not open file "+file
-            sys.exit(-1)
+        tmplines = sdfutils.SdfUtils.read_file(default_ident_location)
             
         for line in tmplines:
             ie = sdf2.Sdf2IdentEntity()
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, sdffile, heapq, glob
+import tempfile, sys, subprocess, sdf_raw, sdf2, os, identfile, heapq, glob, sdfutils
 
 # run localize
 # add all new strings to sourcedb
@@ -12,8 +12,7 @@
     a.update_ident_file("cleanup")
 
 class l10nupdate:
-    # fix me ;)
-    default_ident_location = os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/ident.sdf2"
+    default_ident_location = sdfutils.SdfUtils.get_ident_location()
     
     def __init__(self):
         pass
@@ -38,25 +37,17 @@
 #            print stdout_value
 #            print stderr_value
         
-        lines = []
-        try:
-            f = open(tmpfile, "r")
-            lines = [line.strip('\n') for line in f.readlines()]
-        except IOError:
-            pass
-        
+        lines = sdfutils.SdfUtils.read_file(tmpfile)
         sdffile = list()
         for line in lines:
             se = sdf_raw.SdfEntity()
             se.set_properties(line)
             sdffile.append(se)
-        
-        #os.remove(tmpfile)
         return sdffile
     
     def update_ident_file(self, cmd):
         sdf_file = self.run_localize()
-        ident = sdffile.IdentFile()
+        ident = identfile.IdentFile()
         ident.read(self.default_ident_location)
         unchanged_cnt, new_cnt, changed_cnt, moved_cnt, obsolete_cnt = 0, 0, 0, 0, 0
         new_dict, obsolete_dict = dict(), dict()
@@ -108,9 +99,6 @@
             obsolete_cnt = 0
 
         ident.write(self.default_ident_location)
-        #print "Update:"
-        #print "new strings: "+str(new_cnt)+"moved strings: "+str(moved_cnt)+"changed strings: "+str(changed_cnt)+
-        #print "untouched strings: "+str(unchanged_cnt)+"\nobsolete strings: "+str(obsolete_cnt)+"\ntotal strings: "+str(new_cnt+changed_cnt+unchanged_cnt)
                 
         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 }
@@ -146,20 +134,15 @@
 
     def clean_sdf2_data(self, cleanup_list):
         print "Removeing obsolete strings from sdf2 data files"
-        default_localize_location =  os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/*/localize.sdf2"
+        default_localize_location =  sdfutils.SdfUtils.get_data_location()+"*/localize.sdf2"
         file_list = []
         for file in glob.glob(default_localize_location):
             file_list.append(file)
 
         for file in file_list:
             sdf_dict = dict()
-            try:
-                fh = open(file, "r")
-                lines = [line.strip("\n") for line in fh.readlines()]
-            except IOError:
-                print "Can not read file "+file
-                sys.exit(13)
-            fh.close()
+            
+            lines = sdfutils.SdfUtils.read_file(file)
             for line in lines:
                 sd = sdf2.Sdf2DataEntity()
                 sd.set_properties(line)
@@ -167,11 +150,4 @@
             for item in cleanup_list:
                 if item.keyid in sdf_dict:
                     del sdf_dict[item.keyid]
-            try:
-                fh = open(file, "w")
-                for val in sorted(sdf_dict.values(), key=lambda a: str(a)):
-                    fh.write(str(val)+"\n")
-            except IOError:
-                print "Can not write to file "+file
-                sys.exit(13)
-            fh.close()
+            sdfutils.SdfUtils.write_file(file, sdf_dict)
diff --git a/l10ntools/scripts/tool/l10nverify.py b/l10ntools/scripts/tool/l10nverify.py
--- a/l10ntools/scripts/tool/l10nverify.py
+++ b/l10ntools/scripts/tool/l10nverify.py
@@ -1,7 +1,7 @@
-import os, sdf2
+import os, sdf2, sdfutils
 # check that there is no dupe identifier
 
-def run():
+def run(opts):
     v = l10nverify()
     v.read()
 
@@ -11,18 +11,13 @@
         self.lines = list()
         
     def read(self):
-        default_ident_location = os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/ident.sdf2"
+        lines = sdfutils.SdfUtils.read_file(sdfutils.SdfUtils.get_ident_location())
         
-        try:
-            f = open(default_ident_location, "r")
-            lines = [line.strip('\n') for line in f.readlines()]
-        except IOError:
-            pass
         keyids = dict();
         for line in lines:
             ie = sdf2.Sdf2IdentEntity()
             ie.set_properties(line)
-            #print str(ie)
+            
             if ie.keyid in keyids:
                 print "Found dupe:\n"+str(keyids[ie.keyid])+"\n"+str(ie)
             else:
diff --git a/l10ntools/scripts/tool/sdfutils.py b/l10ntools/scripts/tool/sdfutils.py
new file mode 100644
--- /dev/null
+++ b/l10ntools/scripts/tool/sdfutils.py
@@ -0,0 +1,61 @@
+import tempfile, sdf, sdf2, sdf_raw, os, shutil, sys
+
+
+class SdfUtils:
+
+    @staticmethod
+    def write_file(file, data):
+        try:
+            fd, tmpfile = tempfile.mkstemp()
+            fh = os.fdopen(fd, "w")
+    
+            if isinstance(data, str):
+                fh.write(str)
+            elif isinstance(data, dict):
+                for line in sorted(data.values(), key = lambda a: str(a)):
+                    fh.write(str(line) + "\n")
+            elif isinstance(data, list):
+                for line in sorted(data, key = lambda a: str(a)):
+                    fh.write(str(line) + "\n")
+            fh.close()
+            shutil.move(tmpfile, file)
+        except IOError:
+            print "Can not write file "+file
+            sys.exit(13)
+
+    @staticmethod
+    def read_file(file):
+        try:
+            fh = open(file, "r")
+            lines = [line.strip("\n") for line in fh.readlines()]
+            fh.close()
+        except IOError:
+            print "Can not read file "+file
+            sys.exit(13)
+        return lines
+
+
+    @staticmethod
+    def get_ident_location():
+        cur_dir = os.getcwd()
+        if cur_dir.startswith(os.environ["SRC_ROOT"]) or cur_dir.startswith(os.environ["SOURCE_ROOT_DIR"]+"/l10n"):
+            #return os.environ["SOURCE_ROOT_DIR"]+"/l10n/l10n/source/ident.sdf2"
+            return os.environ["SRC_ROOT"]+"/l10n/source/ident.sdf2"
+        elif cur_dir.startswith(os.environ["SOURCE_ROOT_DIR"]+"/sun"):
+            return os.environ["SOURCE_ROOT_DIR"]+"/sun/l10n_so/source/ident.sdf2"
+        else:
+            print "Please call this command inside of the repository ( e.g. "+os.environ["SRC_ROOT"]+")"
+            sys.exit(13)
+
+    @staticmethod
+    def get_data_location():
+        cur_dir = os.getcwd()
+        if cur_dir.startswith(os.environ["SRC_ROOT"]) or cur_dir.startswith(os.environ["SOURCE_ROOT_DIR"]+"/l10n"):
+            #return os.environ["SOURCE_ROOT_DIR"]+"/l10n/l10n/source/ident.sdf2"
+            return os.environ["SRC_ROOT"]+"/l10n/source/"
+        elif cur_dir.startswith(os.environ["SOURCE_ROOT_DIR"]+"/sun"):
+            return os.environ["SOURCE_ROOT_DIR"]+"/sun/l10n_so/source/"
+        else:
+            print "Please call this command inside of the repository ( e.g. "+os.environ["SRC_ROOT"]+")"
+            sys.exit(13)
+
