+################################################################
+# different systems of a .ly file.
+def read_pipe (c):
+ print 'pipe' , c
+ return os.popen (c).read ()
+
+def system (c):
+ print 'system' , c
+ s = os.system (c)
+ if s :
+ raise Exception ("failed")
+ return
+
+def compare_png_images (old, new, dir):
+ def png_dims (f):
+ m = re.search ('([0-9]+) x ([0-9]+)', read_pipe ('file %s' % f))
+
+ return tuple (map (int, m.groups ()))
+
+ dest = os.path.join (dir, new.replace ('.png', '.compare.jpeg'))
+ try:
+ dims1 = png_dims (old)
+ dims2 = png_dims (new)
+ except AttributeError:
+ ## hmmm. what to do?
+ system ('touch %(dest)s' % locals ())
+ return
+
+ dims = (min (dims1[0], dims2[0]),
+ min (dims1[1], dims2[1]))
+
+ system ('convert -depth 8 -crop %dx%d+0+0 %s crop1.png' % (dims + (old,)))
+ system ('convert -depth 8 -crop %dx%d+0+0 %s crop2.png' % (dims + (new,)))
+
+ system ('compare -depth 8 crop1.png crop2.png diff.png')
+
+ system ("convert -depth 8 diff.png -blur 0x3 -negate -channel alpha,blue -type TrueColorMatte -fx 'intensity' matte.png")
+
+ system ("composite -quality 65 matte.png %(new)s %(dest)s" % locals ())
+
+class FileLink:
+ def text_record_string (self):
+ return '%-30f %-20s\n' % (self.distance (),
+ self.name ())
+ def distance (self):
+ return 0.0
+
+ def name (self):
+ return ''
+
+ def link_files_for_html (self, old_dir, new_dir, dest_dir):
+ pass
+
+ def write_html_system_details (self, dir1, dir2, dest_dir):
+ pass
+
+ def html_record_string (self, old_dir, new_dir):
+ return ''
+
+class MidiFileLink (FileLink):
+ def get_midi (self, f):
+ s = open (f).read ()
+ s = re.sub ('LilyPond [0-9.]+', '', s)
+ return s
+
+ def __init__ (self, f1, f2):
+ self.files = (f1, f2)
+
+ s1 = self.get_midi (self.files[0])
+ s2 = self.get_midi (self.files[1])
+
+ self.same = (s1 == s2)
+
+ def name (self):
+ name = os.path.split (self.files[0])[1]
+ name = re.sub ('.midi', '', name)
+ return name
+
+ def distance (self):
+ ## todo: could use import MIDI to pinpoint
+ ## what & where changed.
+ if self.same:
+ return 0
+ else:
+ return 100;
+ def html_record_string (self, d1, d2):
+ return '''<tr>
+<td>
+%f
+</td>
+<td><tt>%s</tt></td>
+<td><tt>%s</tt></td>
+</tr>''' % ((self.distance(),) + self.files)
+
+class SignatureFileLink (FileLink):
+ def __init__ (self):
+ self.original_name = ''
+ self.base_names = ('','')
+ self.system_links = {}
+ self._distance = None
+ def name (self):
+ return self.original_name
+
+ def add_system_link (self, link, number):
+ self.system_links[number] = link
+
+ def calc_distance (self):
+ d = 0.0
+
+ orphan_distance = 0.0
+ for l in self.system_links.values ():
+ d = max (d, l.geometric_distance ())
+ orphan_distance += l.orphan_count ()
+
+ return d + orphan_distance
+
+ def distance (self):
+ if type (self._distance) != type (0.0):
+ return self.calc_distance ()
+
+ return self._distance
+
+ def source_file (self):
+ for ext in ('.ly', '.ly.txt'):
+ if os.path.exists (self.base_names[1] + ext):
+ return self.base_names[1] + ext
+ return ''
+
+ def add_file_compare (self, f1, f2):
+ system_index = []
+
+ def note_system_index (m):
+ system_index.append (int (m.group (1)))
+ return ''
+
+ base1 = re.sub ("-([0-9]+).signature", note_system_index, f1)
+ base2 = re.sub ("-([0-9]+).signature", note_system_index, f2)
+
+ self.base_names = (os.path.normpath (base1),
+ os.path.normpath (base2))
+
+ def note_original (match):
+ self.original_name = match.group (1)
+ return ''
+
+ if not self.original_name:
+ self.original_name = os.path.split (base1)[1]
+
+ ## ugh: drop the .ly.txt
+ for ext in ('.ly', '.ly.txt'):
+ try:
+ re.sub (r'\\sourcefilename "([^"]+)"',
+ note_original, open (base1 + ext).read ())
+ except IOError:
+ pass
+
+ s1 = read_signature_file (f1)
+ s2 = read_signature_file (f2)
+
+ link = SystemLink (s1, s2)
+
+ self.add_system_link (link, system_index[0])
+
+
+ def create_images (self, old_dir, new_dir, dest_dir):
+
+ files_created = [[], []]
+ for oldnew in (0, 1):
+ pat = self.base_names[oldnew] + '.eps'
+
+ for f in glob.glob (pat):
+ infile = f
+ outfile = (dest_dir + '/' + f).replace ('.eps', '.png')
+
+ mkdir (os.path.split (outfile)[0])
+ cmd = ('gs -sDEVICE=png16m -dGraphicsAlphaBits=4 -dTextAlphaBits=4 '
+ ' -r101 '
+ ' -sOutputFile=%(outfile)s -dNOSAFER -dEPSCrop -q -dNOPAUSE '
+ ' %(infile)s -c quit ' % locals ())
+
+ files_created[oldnew].append (outfile)
+ system (cmd)
+
+ return files_created
+
+ def link_files_for_html (self, old_dir, new_dir, dest_dir):
+ to_compare = [[], []]
+
+ exts = ['.ly']
+ if options.create_images:
+ to_compare = self.create_images (old_dir, new_dir, dest_dir)
+ else:
+ exts += ['.png', '-page*png']
+
+ for ext in exts:
+ for oldnew in (0,1):
+ for f in glob.glob (self.base_names[oldnew] + ext):
+ dst = dest_dir + '/' + f
+ link_file (f, dst)
+
+ if f.endswith ('.png'):
+ to_compare[oldnew].append (f)
+
+ if options.compare_images:
+ for (old, new) in zip (to_compare[0], to_compare[1]):
+ compare_png_images (old, new, dest_dir)
+
+
+ def html_record_string (self, old_dir, new_dir):
+ def img_cell (ly, img, name):
+ if not name:
+ name = 'source'
+ else:
+ name = '<tt>%s</tt>' % name
+
+ return '''
+<td align="center">
+<a href="%(img)s">
+<img src="%(img)s" style="border-style: none; max-width: 500px;">
+</a><br>
+<font size="-2">(<a href="%(ly)s">%(name)s</a>)
+</font>
+</td>
+''' % locals ()
+ def multi_img_cell (ly, imgs, name):
+ if not name:
+ name = 'source'
+ else:
+ name = '<tt>%s</tt>' % name
+
+ imgs_str = '\n'.join (['''<a href="%s">
+<img src="%s" style="border-style: none; max-width: 500px;">
+</a><br>''' % (img, img)
+ for img in imgs])
+
+
+ return '''
+<td align="center">
+%(imgs_str)s
+<font size="-2">(<a href="%(ly)s">%(name)s</a>)
+</font>
+</td>
+''' % locals ()
+
+
+
+ def cell (base, name):
+ pat = base + '-page*.png'
+ pages = glob.glob (pat)
+
+ if pages:
+ return multi_img_cell (base + '.ly', sorted (pages), name)
+ else:
+ return img_cell (base + '.ly', base + '.png', name)
+
+
+ html_2 = self.base_names[1] + '.html'
+ name = self.original_name
+
+ cell_1 = cell (self.base_names[0], name)
+ cell_2 = cell (self.base_names[1], name)
+ if options.compare_images:
+ cell_2 = cell_2.replace ('.png', '.compare.jpeg')
+
+ html_entry = '''
+<tr>
+<td>
+%f<br>
+(<a href="%s">details</a>)
+</td>
+
+%s
+%s
+</tr>
+''' % (self.distance (), html_2, cell_1, cell_2)
+
+ return html_entry
+
+
+ def html_system_details_string (self):
+ systems = self.system_links.items ()
+ systems.sort ()
+
+ html = ""
+ for (c, link) in systems:
+ e = '<td>%d</td>' % c
+ for d in link.distance ():
+ e += '<td>%f</td>' % d
+
+ e = '<tr>%s</tr>' % e
+
+ html += e
+
+ e = '<td>%d</td>' % c
+ for s in (link.output_expression_details_string (),
+ link.orphan_details_string (),
+ link.geo_details_string ()):
+ e += "<td>%s</td>" % s
+
+
+ e = '<tr>%s</tr>' % e
+ html += e
+
+ original = self.original_name
+ html = '''<html>
+<head>
+<title>comparison details for %(original)s</title>
+</head>
+<body>
+<table border=1>
+<tr>
+<th>system</th>
+<th>output</th>
+<th>orphan</th>
+<th>geo</th>
+</tr>
+
+%(html)s
+</table>
+
+</body>
+</html>
+''' % 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)
+ details.write (self.html_system_details_string ())
+
+################################################################
+# Files/directories
+
+import glob
+import re
+
+
+