]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/output-distance.py
(FileLink): new class. collect
[lilypond.git] / buildscripts / output-distance.py
index bebafe755935243f014c1812ef4f301023a1c33d..f32c1052c5edb9c524ad5431c050d1c9b2e68516 100644 (file)
@@ -15,10 +15,9 @@ X_AXIS = 0
 Y_AXIS = 1
 INFTY = 1e6
 
-OUTPUT_EXPRESSION_PENALTY = 1
-ORPHAN_GROB_PENALTY = 1
+OUTPUT_EXPRESSION_PENALTY = 100
+ORPHAN_GROB_PENALTY = 1000
 THRESHOLD = 1.0
-inspect_max_count = 0
 
 def max_distance (x1, x2):
     dist = 0.0
@@ -170,14 +169,14 @@ class SystemLink:
         return d
     
     def orphan_distance (self):
-        d = 0
+        d = 0.0
         for (g1,g2) in self.back_link_dict.items ():
             if g2 == None:
                 d += ORPHAN_GROB_PENALTY
         return d
     
     def output_exp_distance (self):
-        d = 0
+        d = 0.0
         for (g1,g2) in self.back_link_dict.items ():
             if g2:
                 d += g1.expression_distance (g2)
@@ -188,15 +187,6 @@ class SystemLink:
         return (self.output_exp_distance (),
                 self.orphan_distance (),
                 self.geometric_distance ())
-    
-def read_signature_file (name):
-    print 'reading', name
-    exp_str = ("[%s]" % open (name).read ())
-    entries = safeeval.safe_eval (exp_str)
-
-    grob_sigs = [GrobSignature (e) for e in entries]
-    sig = SystemSignature (grob_sigs)
-    return sig
 
 
 class FileLink:
@@ -240,6 +230,7 @@ class FileLink:
         
         base1 = re.sub ("-([0-9]+).signature", note_system_index, f1)
         base2 = re.sub ("-([0-9]+).signature", note_system_index, f2)
+#        name = os.path.split (base1)[1]
 
         self.base_names = (os.path.normpath (base1),
                            os.path.normpath (base2))
@@ -249,9 +240,8 @@ class FileLink:
             return ''
         
         if not self.original_name:
-            self.original_name = os.path.split (base1)[1]
 
-            ## ugh: drop the .ly.txt
+            ## ugh: can't we drop the .ly.txt?
             for ext in ('.ly', '.ly.txt'):
                 try:
                     re.sub (r'\\sourcefilename "([^"]+)"',
@@ -266,13 +256,20 @@ class FileLink:
 
         self.add_system_link (link, system_index[0])
 
-    def link_files_for_html (self, old_dir, new_dir, dest_dir):
-        for ext in ('.png', '.ly'):
-            for oldnew in (0,1):
-                link_file (self.base_names[oldnew] + ext, 
-                           dest_dir + '/' + self.base_names[oldnew] + ext)
+    def link_files_for_html (self, old_dir, new_dir):
 
-    def html_record_string (self,  old_dir, new_dir):
+        ## todo should create new_dir/old_suffix/HIER/ARCHY/old-file
+        
+        old_suffix = os.path.split (old_dir)[1]
+        old_dest_dir = os.path.join (new_dir, old_suffix)
+        name = os.path.split (self.base_names[0])[1]
+        os.link (self.base_names[0] + '.png',
+                 old_dest_dir + '/' + name + '.png')
+        if self.source_file ():
+            os.link (self.source_file (),
+                     old_dest_dir + "/" + name + '.ly')
+        
+    def html_record_string (self, old_dir, new_dir):
         def img_cell (ly, img, name):
             if not name:
                 name = 'source'
@@ -290,13 +287,20 @@ class FileLink:
 ''' % locals ()
         
 
-        img_1  = self.base_names[0] + '.png'
-        ly_1  = self.base_names[0] + '.ly'
-        img_2  = self.base_names[1] + '.png'
-        ly_2  = self.base_names[1] + '.ly'
-        html_2  = self.base_names[1] + '.html'
+        old_suffix = os.path.split (old_dir)[1]
+        old_name = os.path.split (self.base_names[0])[1]
+
+        img_1 = os.path.join (old_suffix, old_name + '.png')
+        ly_1 = os.path.join (old_suffix, old_name + '.ly')
         name = self.original_name
+
+        base_2 = self.base_names[1].replace (new_dir, '')
+        base_2 = re.sub ("^/*", '', base_2)
+        img_2 = base_2 + '.png'
         
+
+        ly_2 = img_2.replace ('.png','.ly')
+
         html_entry = '''
 <tr>
 <td>
@@ -307,8 +311,7 @@ class FileLink:
 %s
 %s
 </tr>
-''' % (self.distance (), html_2,
-       img_cell (ly_1, img_1, name), img_cell (ly_2, img_2, name))
+''' % (self.distance (), base_2 + '.html', img_cell (ly_1, img_1, name), img_cell (ly_2, img_2, name))
 
 
         return html_entry
@@ -349,10 +352,8 @@ class FileLink:
 ''' % locals ()
         return html
 
-    def write_html_system_details (self, dir1, dir2, dest_dir):
-        dest_file =  os.path.join (dest_dir, self.base_names[1] + '.html')
-
-        details = open_write_file (dest_file)
+    def write_html_system_details (self, dir2):
+        details = open (os.path.join (dir2, os.path.split (self.base_names[1])[1]) + '.html', 'w')
         details.write (self.html_system_details_string ())
 
 ################################################################
@@ -361,6 +362,14 @@ class FileLink:
 import glob
 import re
 
+def read_signature_file (name):
+    print 'reading', name
+    exp_str = ("[%s]" % open (name).read ())
+    entries = safeeval.safe_eval (exp_str)
+
+    grob_sigs = [GrobSignature (e) for e in entries]
+    sig = SystemSignature (grob_sigs)
+    return sig
 
 
 def compare_signature_files (f1, f2):
@@ -412,18 +421,13 @@ class ComparisonData:
                 self.compare_trees (d1, d2)
     
     def compare_directories (self, dir1, dir2):
-
+        
         (paired, m1, m2) = paired_files (dir1, dir2, '*.signature')
 
         self.missing += [(dir1, m) for m in m1] 
         self.added += [(dir2, m) for m in m2] 
 
         for p in paired:
-            if (inspect_max_count
-                and len (self.file_links) > inspect_max_count):
-                
-                continue
-            
             f2 = dir2 +  '/' + p
             f1 = dir1 +  '/' + p
             self.compare_files (f1, f2)
@@ -447,7 +451,9 @@ class ComparisonData:
         if filename == '':
             out = sys.stdout
         else:
-            out = open_write_file (filename)
+            out = open (filename, 'w')
+            
+
 
         ## todo: support more scores.
         results = [(link.distance(), link)
@@ -463,12 +469,12 @@ class ComparisonData:
         out.write ('\n\n')
         out.write ('%d below threshold\n' % len ([1 for s,l  in results
                                                     if THRESHOLD >=  s > 0.0]))
-        out.write ('%d unchanged\n' % len ([1 for (s,l) in results if s == 0.0]))
+        out.write ('%d unchanged' % len ([1 for (s,l) in results if s == 0.0]))
         
-    def create_text_result_page (self, dir1, dir2, dest_dir):
-        self.write_text_result_page (dest_dir + '/index.txt')
+    def create_text_result_page (self, dir1, dir2):
+        self.write_text_result_page (dir2 + '/' + os.path.split (dir1)[1] + '.txt')
         
-    def create_html_result_page (self, dir1, dir2, dest_dir):
+    def create_html_result_page (self, dir1, dir2):
         dir1 = dir1.replace ('//', '/')
         dir2 = dir2.replace ('//', '/')
         
@@ -479,13 +485,17 @@ class ComparisonData:
 
         html = ''
         old_prefix = os.path.split (dir1)[1]
+        dest_dir = os.path.join (dir2, old_prefix)
+        os.mkdir (dest_dir)
+
         for (score, link) in results:
             if score <= THRESHOLD:
                 continue
 
-            link.write_html_system_details (dir1, dir2, dest_dir)
-            link.link_files_for_html (dir1, dir2, dest_dir) 
-            html += link.html_record_string (dir1, dir2)
+            link.write_html_system_details (dir2)
+
+            link.link_files_for_html (dir1, dir2) 
+            html += link.html_record_string (dir1, dir2) 
 
 
         html = '''<html>
@@ -510,42 +520,23 @@ class ComparisonData:
                  % len ([1 for (s,l) in results if s == 0.0]))
 
 
-        dest_file = dest_dir + '/index.html'
-        open_write_file (dest_file).write (html)
+        open (os.path.join (dir2, old_prefix) + '.html', 'w').write (html)
         
     def print_results (self):
         self.write_text_result_page ('')
         
+        
 
-def compare_trees (dir1, dir2, dest_dir):
+def compare_trees (dir1, dir2):
     data = ComparisonData ()
     data.compare_trees (dir1, dir2)
     data.print_results ()
-
-    if os.path.isdir (dest_dir):
-        system ('rm -rf %s '% dest_dir)
-
-    data.create_html_result_page (dir1, dir2, dest_dir)
-    data.create_text_result_page (dir1, dir2, dest_dir)
+    data.create_html_result_page (dir1, dir2)
+#    data.create_text_result_page (dir1, dir2)
     
 ################################################################
 # TESTING
 
-def mkdir (x):
-    if not os.path.isdir (x):
-        print 'mkdir', x
-        os.makedirs (x)
-
-def link_file (x, y):
-    mkdir (os.path.split (y)[0])
-    os.link (x, y)
-    
-def open_write_file (x):
-    d = os.path.split (x)[0]
-    mkdir (d)
-    return open (x, 'w')
-
-
 def system (x):
     
     print 'invoking', x
@@ -566,25 +557,17 @@ def test_compare_trees ():
     system ('cp 20expr{-*.signature,.ly,.png} dir1')
     system ('cp 19{-*.signature,.ly,.png} dir2/')
     system ('cp 19{-*.signature,.ly,.png} dir1/')
-    system ('cp 19-1.signature 19-sub-1.signature')
-    system ('cp 19.ly 19-sub.ly')
-    system ('cp 19.png 19-sub.png')
-    
-    system ('mkdir -p dir1/subdir/ dir2/subdir/')
-    system ('cp 19-sub{-*.signature,.ly,.png} dir1/subdir/')
-    system ('cp 19-sub{-*.signature,.ly,.png} dir2/subdir/')
     system ('cp 20grob{-*.signature,.ly,.png} dir2/')
     system ('cp 20grob{-*.signature,.ly,.png} dir1/')
 
     ## introduce differences
     system ('cp 19-1.signature dir2/20-1.signature')
-    system ('cp 20-1.signature dir2/subdir/19-sub-1.signature')
 
     ## radical diffs.
     system ('cp 19-1.signature dir2/20grob-1.signature')
     system ('cp 19-1.signature dir2/20grob-2.signature')
 
-    compare_trees ('dir1', 'dir2', 'compare-dir1dir2')
+    compare_trees ('dir1', 'dir2')
 
 
 def test_basic_compare ():
@@ -646,11 +629,18 @@ def test_basic_compare ():
         print '%-20s' % k, v
 
     assert combinations['20-20'] == (0.0,0.0,0.0)
-    assert combinations['20-20expr'][0] > 0.0
+    assert combinations['20-20expr'][0] > 50.0
     assert combinations['20-19'][2] < 10.0
     assert combinations['20-19'][2] > 0.0
 
 
+def test_sigs (a,b):
+    sa = read_signature_file (a)
+    sb = read_signature_file (b)
+    link = SystemLink (sa, sb)
+    print link.distance()
+
+
 def run_tests ():
     do_clean = 0
     dir = 'output-distance-test'
@@ -677,14 +667,7 @@ def main ():
                   dest="run_test",
                   action="store_true",
                   help='run test method')
-    p.add_option ('--max-count',
-                  dest="max_count",
-                  metavar="COUNT",
-                  type="int",
-                  default=0, 
-                  action="store",
-                  help='only analyze COUNT signature pairs')
+
     (o,a) = p.parse_args ()
 
     if o.run_test:
@@ -695,10 +678,7 @@ def main ():
         p.print_usage()
         sys.exit (2)
 
-    global inspect_max_count
-    inspect_max_count = o.max_count
-
-    compare_trees (a[0], a[1], a[1] + '/' +  a[0])
+    compare_trees (a[0], a[1])
 
 if __name__ == '__main__':
     main()