User:Tardis/regdiff.py: Difference between revisions

Content deleted Content added
version 0.2: improved safety
m Replaced deprecated <source> tags with <syntaxhighlight> (via WP:JWB)
 
(2 intermediate revisions by one other user not shown)
Line 1:
<sourcesyntaxhighlight lang="python">
#!/usr/bin/env python
#regdiff.py
#Created November 9 2008
#Updated November 910 2008
#Version 0.23.1
 
#This program is free software; you can redistribute it and/or modify it under
Line 19:
#applying it transforms a registry represented by the first into a registry
#represented by the second. Usually you should redirect the output to a file.
 
#Set displayProgress to a positive value n to report with a # every n lines.
 
#Warnings:
#The output deletes all keys and values present in the first but not in the
#second! Therefore, this should be run only on exports of complete subtrees.
 
#The input must be sorted (values sorted within each key); I believe that
#Regedit's export does this, and will also guarantee complete subtrees.
 
#It's probably wise to double check any key removals in the output: look for
Line 42 ⟶ 47:
# - support deletion disabling
# - detect misorderings
#Version 0.3, November 9 2008:
# - make allowKeyDelete apply to the last keys to delete
# - verify continuous progress
# - support displaying progress
#Version 0.3.1, November 10 2008:
# - use case-insensitive and subkey-aware comparisons for ordering
 
#Bugs:
Line 48 ⟶ 59:
 
#I don't know whether .REG files are really UTF-16 or UCS-2.
 
#I'm not sure that the last blank line is really necessary; a trailing CRLF
#may be sufficient.
 
import sys,codecs
 
def keycompare(a,b):
"""Return an integer indicating the ordering of keys a and b."""
return cmp(a.lower().split('\\'),b.lower().split('\\'))
 
class line(object):
Line 69 ⟶ 87:
self.delete=s[1]=='-'
self.lastkey=self.name=s[1+self.delete:-3] # ends in "]\r\n"
if k is not None and self.lastkey<k.lastkey: is not None and\
keycompare(self.lastkey,k.lastkey)<0:
raise ValueError,"key %r precedes %r in input"%\
(k.lastkey,self.lastkey)
Line 81 ⟶ 100:
elif c=='"': break
else: raise IOError,"unterminated name in "+repr(s)
elif s[0]=!='@': passraise #IOError,"unrecognized key'format: "+repr(s default value)
else: raise IOError,"unrecognized format: "+repr(s)
# The name for @ is "", which properly sorts before everything.
self.name=s[1:index-1]
Line 93 ⟶ 111:
else:
self.lastkey=k.lastkey
if not k.iskey and self.name.lower()<k.name.lower():
raise ValueError,"value %r precedes %r in input"%\
(k.name,self.name)
Line 157 ⟶ 175:
allowKeyDelete=True
allowValueDelete=True
displayProgress=0
ci=codecs.lookup("utf_16")
fo=ci.streamreader(open(sys.argv[1],'rb'))
Line 170 ⟶ 189:
o.old=True
killing=False # the tree being deleted, if any
iters=0
while True:
iters+=1
if displayProgress and iters%displayProgress==0:
sys.stderr.write('#')
 
if o.old: o=line(nextLogical(fo),o)
if n.old: n=line(nextLogical(fn),n)
Line 180 ⟶ 204:
# values (since the values go with a previous key), and EOF comes after
# everything. Positive values mean that n comes first.
c=o.eof-n.eof or cmpkeycompare(o.lastkey,n.lastkey) or\
o.iskey-n.iskey or cmp(o.name.lower(),n.name.lower())
o.old=c<=0
n.old=c>=0
assert o.old or n.old,"not advancing in the file"
 
if killing and (o.eof or not isunder(o.lastkey,killing)): killing=False
Line 192 ⟶ 217:
# Delete a whole key if the new file is past all its subkeys.
# Note that n.lastkey!=o.name, because n must be a key.
if (n.eof or not isunder(n.lastkey,o.name)) and allowKeyDelete:
killing=o.name
out.write("\r\n[-"+o.name+"]\r\n")
Line 203 ⟶ 228:
 
out.write("\r\n")
if displayProgress: sys.stderr.write('\n')
</source>
</syntaxhighlight>