Fortunately, Oracle Developer Suite has a tool, frmf2xml which can be used to convert binary FMB file to XML, so, gluing it all together (cvs, frmf2xml and ordinary diff utility) with PowerShell, I have written a script, Compare-CvsForms, which is much like cvs diff command, but works with theese binary forms. Actually, I've also written vdiff.py script, based on difflib, which is much more convenient for comparing such a messy data (XML, huh).
So, here is the Compare-CvsForms.ps1 script (I've put it to my PowerShell profile directory):
Param ([string]$formsFile)
if (cvs status $formsFile |
select-string 'Status: (Locally Modified|Needs Checkout|Needs Patch)')
{
# store working copy in another place
$local:temp_fmb = [System.IO.Path]::ChangeExtension(
[System.IO.Path]::GetTempFileName(), "fmb")
$local:temp_xml = [System.IO.Path]::ChangeExtension(
[System.IO.Path]::GetTempFileName(), "xml")
Move-Item -force $formsFile $local:temp_fmb
# produce xml of a working copy
frmf2xml $local:temp_fmb | out-null
if ($LastExitCode -ne 0) {
Echo "ERROR: Cannot convert working copy of form to XML!"
Move-Item -Force $local:temp_fmb $formsFile
return
}
# get repositary copy
cvs update 2>&1 | out-null
#produce xml of a repositary copy
frmf2xml $formsFile | out-null
if ($LastExitCode -ne 0) {
Echo "ERROR: Cannot convert CVS copy of form to XML!"
Move-Item -Force $local:temp_fmb $formsFile
Remove-Item -Force $xml_from
return
}
# compare xmls
$xml_from=$formsFile.substring(0, $formsFile.length-4) + "_fmb.xml"
$xml_to=$local:temp_fmb.substring(0, $local:temp_fmb.length-4) + "_fmb.xml"
vdiff.py -context $xml_from $xml_to
if ($LastExitCode -eq 0) {
$formsFile.toUpper() + ": Working copy does not differ from repositary."
}
Move-Item -Force $local:temp_fmb $formsFile
Remove-Item -Force $xml_to,$xml_from
} else {
$formsFile.toUpper() + ": Working copy is up to date."
}
And vdiff.py (pretty straightforward) script (surely, you should have Python installed, if not - you could just as easily replace this script with ordinary diff utility):
#!/usr/bin/python
import os
import sys
import difflib
import tempfile
program_name = os.path.splitext(os.path.basename(os.path.sys.argv[0]))[0].upper()
browser = "c:\\Program Files\\Internet Explorer\\iexplore.exe"
if len(sys.argv) < 3:
print "\nUSAGE:\n\n %s FROM-FILE TO-FILE\n" % program_name
sys.exit(2)
if sys.argv[1] == "-context":
context = True
from_fn = sys.argv[2]
to_fn = sys.argv[3]
else:
context = False
from_fn = sys.argv[1]
to_fn = sys.argv[2]
try:
from_lines = open(from_fn).readlines()
except IOError:
print "%s: Error while reading file -- %2" % (program_name, from_fn)
sys.exit(1)
try:
to_lines = open(to_fn).readlines()
except IOError:
print "%s: Error while reading file -- %2" % (program_name, to_fn)
sys.exit(1)
differs = False
for x in difflib.context_diff(from_lines, to_lines):
differs = True
break
if differs:
diff = difflib.HtmlDiff(wrapcolumn=70).make_file(from_lines, to_lines, from_fn, to_fn, context)
diff = diff.replace("<head>",
'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
tmp = tempfile.mkstemp(".html")
os.write(tmp[0], diff)
os.close(tmp[0])
os.spawnl(os.P_WAIT, browser, ' file://' + tmp[1] + '')
sys.exit(1)
else:
sys.exit(0)
Scripting with PowerShell actually is a great deal of fun :)
No comments:
Post a Comment