<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> import dns . zone <EOL> except ImportError : <EOL> import sys <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def diff_zones ( zone1 , zone2 , ignore_ttl = False , ignore_soa = False ) : <EOL> """<STR_LIT>""" <EOL> changes = [ ] <EOL> for name in zone1 : <EOL> name = str ( name ) <EOL> n1 = zone1 . get_node ( name ) <EOL> n2 = zone2 . get_node ( name ) <EOL> if not n2 : <EOL> changes . append ( ( str ( name ) , n1 , n2 ) ) <EOL> elif _nodes_differ ( n1 , n2 , ignore_ttl , ignore_soa ) : <EOL> changes . append ( ( str ( name ) , n1 , n2 ) ) <EOL> for name in zone2 : <EOL> n1 = zone1 . get_node ( name ) <EOL> if not n1 : <EOL> n2 = zone2 . get_node ( name ) <EOL> changes . append ( ( str ( name ) , n1 , n2 ) ) <EOL> return changes <EOL> def _nodes_differ ( n1 , n2 , ignore_ttl , ignore_soa ) : <EOL> if ignore_soa or not ignore_ttl : <EOL> for r in n1 . rdatasets : <EOL> if ignore_soa and r . rdtype == dns . rdatatype . SOA : <EOL> continue <EOL> if r not in n2 . rdatasets : <EOL> return True <EOL> if not ignore_ttl : <EOL> return r . ttl != n2 . find_rdataset ( r . rdclass , r . rdtype ) . ttl <EOL> for r in n2 . rdatasets : <EOL> if ignore_soa and r . rdtype == dns . rdatatype . SOA : <EOL> continue <EOL> if r not in n1 . rdatasets : <EOL> return True <EOL> else : <EOL> return n1 != n2 <EOL> def format_changes_plain ( oldf , newf , changes , ignore_ttl = False ) : <EOL> """<STR_LIT>""" <EOL> ret = "<STR_LIT>" % ( oldf , newf ) <EOL> for name , old , new in changes : <EOL> ret += "<STR_LIT>" % name <EOL> if not old : <EOL> for r in new . rdatasets : <EOL> ret += "<STR_LIT>" % str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> elif not new : <EOL> for r in old . rdatasets : <EOL> ret += "<STR_LIT>" % str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> else : <EOL> for r in old . rdatasets : <EOL> if r not in new . rdatasets or ( r . ttl != new . find_rdataset ( r . rdclass , r . rdtype ) . ttl and not ignore_ttl ) : <EOL> ret += "<STR_LIT>" % str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> for r in new . rdatasets : <EOL> if r not in old . rdatasets or ( r . ttl != old . find_rdataset ( r . rdclass , r . rdtype ) . ttl and not ignore_ttl ) : <EOL> ret += "<STR_LIT>" % str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> return ret <EOL> def format_changes_html ( oldf , newf , changes , ignore_ttl = False ) : <EOL> """<STR_LIT>""" <EOL> ret = '''<STR_LIT>''' % ( oldf , newf ) <EOL> for name , old , new in changes : <EOL> ret += '<STR_LIT>' % name <EOL> if not old : <EOL> for r in new . rdatasets : <EOL> ret += '<STR_LIT>' % str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> elif not new : <EOL> for r in old . rdatasets : <EOL> ret += '<STR_LIT>' % str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> else : <EOL> ret += '<STR_LIT>' <EOL> for r in old . rdatasets : <EOL> if r not in new . rdatasets or ( r . ttl != new . find_rdataset ( r . rdclass , r . rdtype ) . ttl and not ignore_ttl ) : <EOL> ret += str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> ret += '<STR_LIT>' <EOL> ret += '<STR_LIT>' <EOL> for r in new . rdatasets : <EOL> if r not in old . rdatasets or ( r . ttl != old . find_rdataset ( r . rdclass , r . rdtype ) . ttl and not ignore_ttl ) : <EOL> ret += str ( r ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> ret += '<STR_LIT>' <EOL> ret += '<STR_LIT>' <EOL> return ret + '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import optparse <EOL> import subprocess <EOL> import sys <EOL> import traceback <EOL> usage = """<STR_LIT>""" <EOL> p = optparse . OptionParser ( usage = usage ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT:html>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> opts , args = p . parse_args ( ) <EOL> opts . use_vc = opts . use_git or opts . use_bzr or opts . use_rcs <EOL> def _open ( what , err ) : <EOL> if isinstance ( what , basestring ) : <EOL> try : <EOL> return open ( what , '<STR_LIT:rb>' ) <EOL> except : <EOL> sys . stderr . write ( err + "<STR_LIT:\n>" ) <EOL> if opts . tracebacks : <EOL> traceback . print_exc ( ) <EOL> else : <EOL> try : <EOL> proc = subprocess . Popen ( what , stdout = subprocess . PIPE ) <EOL> proc . wait ( ) <EOL> if proc . returncode == <NUM_LIT:0> : <EOL> return proc . stdout <EOL> sys . stderr . write ( err + "<STR_LIT:\n>" ) <EOL> except : <EOL> sys . stderr . write ( err + "<STR_LIT:\n>" ) <EOL> if opts . tracebacks : <EOL> traceback . print_exc ( ) <EOL> if not opts . use_vc and len ( args ) != <NUM_LIT:2> : <EOL> p . print_help ( ) <EOL> sys . exit ( <NUM_LIT:64> ) <EOL> if opts . use_vc and len ( args ) not in ( <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> p . print_help ( ) <EOL> sys . exit ( <NUM_LIT:64> ) <EOL> if not opts . use_vc : <EOL> oldn , newn = args <EOL> else : <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> filename , oldr , newr = args <EOL> oldn = "<STR_LIT>" % ( oldr , filename ) <EOL> newn = "<STR_LIT>" % ( newr , filename ) <EOL> else : <EOL> filename , oldr = args <EOL> newr = None <EOL> oldn = "<STR_LIT>" % ( oldr , filename ) <EOL> newn = filename <EOL> old , new = None , None <EOL> oldz , newz = None , None <EOL> if opts . use_bzr : <EOL> old = _open ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + oldr , filename ] , <EOL> "<STR_LIT>" % ( oldr , filename ) ) <EOL> if newr != None : <EOL> new = _open ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + newr , filename ] , <EOL> "<STR_LIT>" % ( newr , filename ) ) <EOL> elif opts . use_git : <EOL> old = _open ( [ "<STR_LIT>" , "<STR_LIT>" , oldn ] , <EOL> "<STR_LIT>" % ( oldr , filename ) ) <EOL> if newr != None : <EOL> new = _open ( [ "<STR_LIT>" , "<STR_LIT>" , newn ] , <EOL> "<STR_LIT>" % ( newr , filename ) ) <EOL> elif opts . use_rcs : <EOL> old = _open ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + oldr , filename ] , <EOL> "<STR_LIT>" % ( oldr , filename ) ) <EOL> if newr != None : <EOL> new = _open ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" + newr , filename ] , <EOL> "<STR_LIT>" % ( newr , filename ) ) <EOL> if not opts . use_vc : <EOL> old = _open ( oldn , "<STR_LIT>" % oldn ) <EOL> if not opts . use_vc or newr == None : <EOL> new = _open ( newn , "<STR_LIT>" % newn ) <EOL> if not old or not new : <EOL> sys . exit ( <NUM_LIT> ) <EOL> try : <EOL> oldz = dns . zone . from_file ( old , origin = '<STR_LIT:.>' , check_origin = False ) <EOL> except dns . exception . DNSException : <EOL> sys . stderr . write ( "<STR_LIT>" , old ) <EOL> if opts . tracebacks : <EOL> traceback . print_exc ( ) <EOL> try : <EOL> newz = dns . zone . from_file ( new , origin = '<STR_LIT:.>' , check_origin = False ) <EOL> except dns . exception . DNSException : <EOL> sys . stderr . write ( "<STR_LIT>" % new ) <EOL> if opts . tracebacks : <EOL> traceback . print_exc ( ) <EOL> if not oldz or not newz : <EOL> sys . exit ( <NUM_LIT> ) <EOL> changes = diff_zones ( oldz , newz , opts . ignore_ttl , opts . ignore_soa ) <EOL> changes . sort ( ) <EOL> if not changes : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if opts . html : <EOL> print format_changes_html ( oldn , newn , changes , opts . ignore_ttl ) <EOL> else : <EOL> print format_changes_plain ( oldn , newn , changes , opts . ignore_ttl ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
