Advertisement
einWikinger

TortoiseSVN Diff script for MySQL Workbench scheme files

Jun 12th, 2012
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. # extensions: mwb
  2. # TortoiseSVN Diff script for MySQL Workbench scheme files
  3. # 2012 by Oliver Iking, Z-Software GmbH, oliverikingREPLACETHISWITHANATz-software.net, http://www.z-software.net/
  4. # This work is licensed under a Creative Commons Attribution 3.0 Unported License - http://creativecommons.org/licenses/by/3.0/
  5.  
  6. # Will produce two diffable documents, which don't resemble the FULL MWB content, but the scheme relevant data.
  7. # Merging is not possible
  8.  
  9. # Open your TortoiseSVN (or TortoiseSomething) settings, go to the "Diff Viewer" tab and click on "Advanced". Add
  10. # a row with the extension ".mwb" and a command line of
  11. # "path\to\python.exe" "path\to\diff-mwb.py" %base %mine
  12. # Apply changes and now you can diff mysql workbench scheme files
  13.  
  14. import sys
  15. import zipfile
  16. import os
  17. import time
  18. import tempfile
  19. import re
  20.  
  21. if sys.version >= '3':
  22.     import bytes
  23.  
  24. def readFromZip(zf, fname):
  25.     if sys.version < '3':
  26.         return zf.read(fname)
  27.     else:
  28.         return bytes.decode( zf.read(fname) )
  29.  
  30. # mysql workbench XML will have _ptr_ attributes which are modified on each save for almost each XML node. Remove the visual litter,
  31. # make actual changes stand out.
  32. def sanitizeMwbXml( xml ):
  33.     return re.sub('_ptr_="([0-9a-fA-F]{8})"', '', xml)
  34.  
  35. try:
  36.     if len(sys.argv) < 2:
  37.         print("Not enough parameters, cannot diff documents!")
  38.         sys.exit(1)
  39.  
  40.     docOld = sys.argv[1]
  41.     docNew = sys.argv[2]
  42.  
  43.     if not os.path.exists(docOld) or not os.path.exists(docNew):
  44.         print("Documents don't exist, cannot diff!")
  45.         sys.exit(1)
  46.  
  47.     # Workbench files are actually zip archives
  48.     zipA = zipfile.ZipFile( docOld, 'r' )
  49.     zipB = zipfile.ZipFile( docNew, 'r' )
  50.  
  51.     tempSubpath = os.tempnam(None,"mwbcompare")
  52.  
  53.     docA = os.path.join( tempSubpath, "mine.document.mwb.xml" )
  54.     docB = os.path.join( tempSubpath, "theirs.document.mwb.xml" )
  55.  
  56.     os.makedirs( tempSubpath )
  57.  
  58.     if os.path.exists(docA) or os.path.exists(docB):
  59.         print("Cannot extract documents, files exist!")
  60.         sys.exit(1)
  61.    
  62.     # Read, sanitize and write actual scheme XML contents to temporary files
  63.    
  64.     docABytes = sanitizeMwbXml( readFromZip( zipA, "document.mwb.xml" ) )
  65.     docBBytes = sanitizeMwbXml( readFromZip( zipB, "document.mwb.xml" ) )
  66.  
  67.     docAFile = open(docA, "w")
  68.     docBFile = open(docB, "w")
  69.  
  70.     docAFile.write(docABytes)
  71.     docBFile.write(docBBytes)
  72.  
  73.     docAFile.close()
  74.     docBFile.close()
  75.  
  76.     os.system("TortoiseProc /command:diff /path:\"" + docA + "\" /path2:\"" + docB + "\"");
  77.  
  78.     # TortoiseProc will spawn a subprocess so we can't delete the files. They're in the tempdir, so they
  79.     # will be cleaned up eventually
  80.     #os.unlink(docA)
  81.     #os.unlink(docB)
  82.  
  83.     sys.exit(0)
  84. except Exception as e:
  85.     print str(e)
  86.     # Sleep, or the command window will close
  87.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement