<s> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> '''<STR_LIT>''' <EOL> import os <EOL> import json <EOL> import argparse <EOL> import six <EOL> import sys <EOL> import copy <EOL> import traceback <EOL> import logging <EOL> import time <EOL> import pstats <EOL> import cProfile <EOL> import datetime <EOL> from firewall . functions import splitArgs <EOL> from firewall . client import FirewallClient <EOL> from firewall . client import FirewallClientZoneSettings <EOL> from firewall . client import FirewallClientServiceSettings <EOL> parser = argparse . ArgumentParser ( add_help = False ) <EOL> parser . add_argument ( "<STR_LIT>" , default = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , default = False , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( "<STR_LIT>" , default = False , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( "<STR_LIT>" , default = False , action = '<STR_LIT:store_true>' ) <EOL> _cache = { } <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> TIMEOUT = <NUM_LIT:20> <EOL> DEFAULT_TARGET = '<STR_LIT>' <EOL> RUNNINGS = [ '<STR_LIT>' ] <EOL> def remove_client ( ) : <EOL> if _cache . get ( '<STR_LIT:f>' ) : <EOL> client = _cache . pop ( '<STR_LIT:f>' , None ) <EOL> if client is not None : <EOL> del client <EOL> def fw ( retries = <NUM_LIT:6> ) : <EOL> '''<STR_LIT>''' <EOL> if _cache . get ( '<STR_LIT:f>' ) is not None : <EOL> if _cache . get ( '<STR_LIT>' , <NUM_LIT:0> ) < <NUM_LIT:2> : <EOL> try : <EOL> if _cache [ '<STR_LIT:f>' ] . getZones ( ) is None : <EOL> time . sleep ( <NUM_LIT> ) <EOL> assert _cache [ '<STR_LIT:f>' ] . getZones ( ) is not None <EOL> _cache . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> _cache [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> except ( Exception , ) : <EOL> remove_client ( ) <EOL> if _cache . get ( '<STR_LIT:f>' ) is None : <EOL> state = '<STR_LIT>' <EOL> ttl = time . time ( ) + TIMEOUT <EOL> attempt = <NUM_LIT:0> <EOL> established = False <EOL> loggued = False <EOL> while time . time ( ) < ttl : <EOL> try : <EOL> attempt += <NUM_LIT:1> <EOL> remove_client ( ) <EOL> _cache [ '<STR_LIT:f>' ] = FirewallClient ( ) <EOL> state = _cache [ '<STR_LIT:f>' ] . get_property ( '<STR_LIT:state>' ) <EOL> if state in RUNNINGS : <EOL> stop = False <EOL> if attempt == <NUM_LIT:1> : <EOL> stop = True <EOL> elif not established : <EOL> established = True <EOL> else : <EOL> stop = True <EOL> if stop : <EOL> log . info ( '<STR_LIT>' ) <EOL> break <EOL> continue <EOL> else : <EOL> established = False <EOL> time . sleep ( <NUM_LIT> ) <EOL> except ( Exception , ) : <EOL> log . error ( traceback . format_exc ( ) ) <EOL> state = '<STR_LIT>' <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> if not loggued : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( state ) ) <EOL> loggued = True <EOL> if _cache . get ( '<STR_LIT:f>' ) is None : <EOL> if not retries : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> else : <EOL> return fw ( retries - <NUM_LIT:1> ) <EOL> return _cache [ '<STR_LIT:f>' ] <EOL> def mark_reload ( ) : <EOL> _cache [ '<STR_LIT>' ] = True <EOL> def lazy_reload ( ) : <EOL> if _cache . get ( '<STR_LIT>' ) : <EOL> fw ( ) . reload ( ) <EOL> _cache [ '<STR_LIT>' ] = False <EOL> def get_zones ( cache = True ) : <EOL> '''<STR_LIT>''' <EOL> key = '<STR_LIT>' <EOL> result = _cache . get ( key ) <EOL> if not result or not cache : <EOL> zones = None <EOL> ttl = time . time ( ) + TIMEOUT <EOL> while ( zones is None ) and ( time . time ( ) < ttl ) : <EOL> try : <EOL> zones = fw ( ) . getZones ( ) <EOL> break <EOL> except ( Exception ) : <EOL> log . error ( traceback . format_exc ( ) ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> if zones is None : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> result = zones <EOL> _cache [ key ] = result <EOL> return result <EOL> def get_service ( s , cache = True ) : <EOL> key = '<STR_LIT>' . format ( s ) <EOL> result = _cache . get ( key ) <EOL> if not result or not cache : <EOL> result = fw ( ) . config ( ) . getService ( s ) <EOL> _cache [ key ] = result <EOL> return result <EOL> def get_services ( cache = True ) : <EOL> key = '<STR_LIT>' <EOL> result = _cache . get ( key , { } ) <EOL> if not result or not cache : <EOL> ss = [ get_service ( a , cache = cache ) <EOL> for a in fw ( ) . config ( ) . listServices ( ) ] <EOL> result = { } <EOL> for i in ss : <EOL> result [ i . get_property ( '<STR_LIT:name>' ) ] = i <EOL> _cache [ key ] = result <EOL> return _cache [ key ] <EOL> def service_ports ( zsettings ) : <EOL> return [ tuple ( "<STR_LIT>" . format ( b ) for b in a ) <EOL> for a in zsettings . getPorts ( ) ] <EOL> def in_zones ( z ) : <EOL> zones = get_zones ( ) <EOL> ret = z in zones <EOL> if not ret : <EOL> zones = get_zones ( cache = False ) <EOL> ret = z in zones <EOL> return ret <EOL> def get_zone ( z , cache = True ) : <EOL> k = '<STR_LIT>' . format ( z ) <EOL> result = _cache . get ( k , None ) <EOL> if not result or not cache : <EOL> result = fw ( ) . config ( ) . getZoneByName ( z ) <EOL> _cache [ k ] = result <EOL> return result <EOL> def define_zone ( z , <EOL> zdata , <EOL> masquerade = None , <EOL> errors = None , <EOL> changes = None , <EOL> apply_retry = <NUM_LIT:0> , <EOL> ** kwargs ) : <EOL> if errors is None : <EOL> errors = [ ] <EOL> if changes is None : <EOL> changes = [ ] <EOL> if '<STR_LIT:target>' in zdata : <EOL> msg = '<STR_LIT>' <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> log . info ( msg . format ( z , zdata ) ) <EOL> if not in_zones ( z ) : <EOL> try : <EOL> zn = fw ( ) . config ( ) . addZone ( z , FirewallClientZoneSettings ( ) ) <EOL> except ( Exception , ) as ex : <EOL> if '<STR_LIT>' in "<STR_LIT>" . format ( ex ) : <EOL> mark_reload ( ) <EOL> lazy_reload ( ) <EOL> if not in_zones ( z ) : <EOL> zn = fw ( ) . config ( ) . addZone ( <EOL> z , FirewallClientZoneSettings ( ) ) <EOL> else : <EOL> zn = get_zone ( z ) <EOL> else : <EOL> raise ex <EOL> zn . setShort ( z ) <EOL> zn . setDescription ( z ) <EOL> msg = '<STR_LIT>' . format ( z ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> mark_reload ( ) <EOL> else : <EOL> zn = get_zone ( z ) <EOL> masquerade = bool ( masquerade ) <EOL> cmask = bool ( zn . getMasquerade ( ) ) <EOL> if cmask is not masquerade : <EOL> zn . setMasquerade ( masquerade ) <EOL> msg = '<STR_LIT>' . format ( z , masquerade ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> target = zdata . get ( '<STR_LIT:target>' , None ) <EOL> if '<STR_LIT:target>' in zdata : <EOL> ztarget = "<STR_LIT>" . format ( zn . getTarget ( ) ) <EOL> if not target : <EOL> target = DEFAULT_TARGET <EOL> if target : <EOL> target = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:default>' : '<STR_LIT:default>' <EOL> } . get ( target . lower ( ) , target . upper ( ) ) <EOL> if ztarget != target : <EOL> msg = '<STR_LIT>' . format ( z ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> zn . setTarget ( target ) <EOL> mark_reload ( ) <EOL> def define_service ( z , <EOL> zdata , <EOL> errors = None , <EOL> changes = None , <EOL> apply_retry = <NUM_LIT:0> , <EOL> ** kwargs ) : <EOL> if errors is None : <EOL> errors = [ ] <EOL> if changes is None : <EOL> changes = [ ] <EOL> msg = '<STR_LIT>' <EOL> if zdata : <EOL> if zdata . get ( '<STR_LIT:port>' ) : <EOL> msg += '<STR_LIT::>' <EOL> for port in zdata . get ( '<STR_LIT:port>' ) : <EOL> portinfo = "<STR_LIT>" . format ( port [ '<STR_LIT:port>' ] ) <EOL> port [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if '<STR_LIT>' in port : <EOL> portinfo = '<STR_LIT>' . format ( portinfo , port [ '<STR_LIT>' ] ) <EOL> if portinfo : <EOL> msg = '<STR_LIT>' . format ( msg , portinfo ) <EOL> msg = msg . format ( z , zdata ) <EOL> log . info ( msg ) <EOL> aservices = get_services ( ) <EOL> if z not in aservices : <EOL> aservices = get_services ( cache = False ) <EOL> if z not in aservices : <EOL> msg = '<STR_LIT>' . format ( z ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> zsettings = fw ( ) . config ( ) . addService ( <EOL> z , FirewallClientServiceSettings ( ) ) <EOL> zsettings . setShort ( z ) <EOL> zsettings . setDescription ( z ) <EOL> else : <EOL> zsettings = aservices [ z ] <EOL> if '<STR_LIT:port>' in zdata : <EOL> ports = [ ] <EOL> for port in zdata [ '<STR_LIT:port>' ] : <EOL> protocols = port . get ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for p in protocols : <EOL> pp = ( str ( port [ '<STR_LIT:port>' ] ) , p ) <EOL> if pp not in ports : <EOL> ports . append ( pp ) <EOL> diff = False <EOL> sports = service_ports ( zsettings ) <EOL> if len ( sports ) != len ( ports ) : <EOL> diff = True <EOL> if not diff : <EOL> for p in ports : <EOL> if p not in sports : <EOL> diff = True <EOL> if diff : <EOL> zsettings . setPorts ( ports ) <EOL> mark_reload ( ) <EOL> msg = '<STR_LIT>' . format ( z ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> def link_interfaces ( z , <EOL> zdata , <EOL> interfaces , <EOL> errors = None , <EOL> changes = None , <EOL> apply_retry = <NUM_LIT:0> , <EOL> ** kwargs ) : <EOL> if errors is None : <EOL> errors = [ ] <EOL> if changes is None : <EOL> changes = [ ] <EOL> msg = '<STR_LIT>' . format ( z ) <EOL> log . info ( msg ) <EOL> if not in_zones ( z ) : <EOL> errors . append ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : z , <EOL> '<STR_LIT>' : ValueError ( '<STR_LIT>' . format ( z ) ) } ) <EOL> for ifc in zdata . get ( '<STR_LIT>' , [ ] ) : <EOL> if z in interfaces . get ( ifc , [ ] ) : <EOL> msg = '<STR_LIT>' . format ( ifc , z ) <EOL> log . info ( msg ) <EOL> continue <EOL> try : <EOL> zn = z . lower ( ) <EOL> zone = fw ( ) . getZoneOfInterface ( ifc ) <EOL> if not isinstance ( zone , basestring ) : <EOL> zone = '<STR_LIT>' <EOL> zone = zone . lower ( ) <EOL> if zone != zn : <EOL> zone = fw ( ) . changeZoneOfInterface ( z , ifc ) <EOL> msg = '<STR_LIT>' . format ( ifc , zone ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : "<STR_LIT>" . format ( z , ifc ) , <EOL> '<STR_LIT>' : ex } ) <EOL> def get_rules ( z , cache = True ) : <EOL> k = '<STR_LIT>' . format ( z ) <EOL> rules = _cache . get ( k , [ ] ) <EOL> if not rules or not cache : <EOL> rules = fw ( ) . getRichRules ( z ) <EOL> _cache [ k ] = rules <EOL> return rules <EOL> def get_directs ( cache = True ) : <EOL> k = '<STR_LIT>' <EOL> rules = _cache . get ( k , [ ] ) <EOL> if not rules or not cache : <EOL> rules = fw ( ) . getAllRules ( ) <EOL> _cache [ k ] = rules <EOL> return rules <EOL> def configure_rules ( z , <EOL> zdata , <EOL> errors = None , <EOL> changes = None , <EOL> apply_retry = <NUM_LIT:0> , <EOL> ** kwargs ) : <EOL> if errors is None : <EOL> errors = [ ] <EOL> if changes is None : <EOL> changes = [ ] <EOL> msg = '<STR_LIT>' . format ( z ) <EOL> log . info ( msg ) <EOL> for rule in zdata . get ( '<STR_LIT>' , [ ] ) : <EOL> try : <EOL> if apply_retry > <NUM_LIT:0> : <EOL> rules = get_rules ( z , cache = False ) <EOL> else : <EOL> rules = get_rules ( z ) <EOL> rules_chunks = [ sorted ( a . split ( ) ) for a in rules ] <EOL> rule_chunk = sorted ( rule . split ( ) ) <EOL> if rule_chunk not in rules_chunks : <EOL> rules = get_rules ( z , cache = False ) <EOL> rules_chunks = [ sorted ( a . split ( ) ) for a in rules ] <EOL> if rule_chunk not in rules_chunks : <EOL> msg = '<STR_LIT>' . format ( z , rule ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> fw ( ) . addRichRule ( z , rule ) <EOL> else : <EOL> msg = '<STR_LIT>' . format ( z , rule ) <EOL> log . info ( msg ) <EOL> except ( Exception ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : "<STR_LIT>" . format ( z , rule ) , <EOL> '<STR_LIT>' : ex } ) <EOL> def get_direct_chunks ( rules ) : <EOL> rules_chunks = [ ] <EOL> for a in rules [ : ] : <EOL> chunk = [ ] <EOL> for rulepart in copy . deepcopy ( a ) : <EOL> parts = rulepart <EOL> if not isinstance ( parts , ( list , tuple , dict , set ) ) : <EOL> parts = [ a for a in "<STR_LIT>" . format ( parts ) . split ( ) ] <EOL> chunk . extend ( sorted ( [ a for a in parts ] ) ) <EOL> rules_chunks . append ( chunk ) <EOL> return rules_chunks <EOL> def configure_directs ( jconfig , <EOL> errors = None , <EOL> changes = None , <EOL> apply_retry = <NUM_LIT:0> , <EOL> ** kwargs ) : <EOL> if errors is None : <EOL> errors = [ ] <EOL> if changes is None : <EOL> changes = [ ] <EOL> msg = '<STR_LIT>' <EOL> log . info ( msg ) <EOL> for rule in jconfig . get ( '<STR_LIT>' , [ ] ) : <EOL> try : <EOL> drule = rule . split ( None , <NUM_LIT:4> ) <EOL> if len ( drule ) != <NUM_LIT:5> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' . format ( rule ) ) <EOL> rule_chunk = get_direct_chunks ( [ drule ] ) [ <NUM_LIT:0> ] <EOL> if apply_retry > <NUM_LIT:0> : <EOL> rules = get_directs ( cache = False ) <EOL> else : <EOL> rules = get_directs ( ) <EOL> rules_chunks = get_direct_chunks ( rules ) <EOL> if rule_chunk not in rules_chunks : <EOL> rules = get_directs ( ) <EOL> rules_chunks = get_direct_chunks ( rules ) <EOL> if rule_chunk not in rules_chunks : <EOL> msg = '<STR_LIT>' . format ( rule ) <EOL> log . info ( msg ) <EOL> changes . append ( msg ) <EOL> fw ( ) . addRule ( drule [ <NUM_LIT:0> ] , <EOL> drule [ <NUM_LIT:1> ] , <EOL> drule [ <NUM_LIT:2> ] , <EOL> int ( drule [ <NUM_LIT:3> ] ) , <EOL> splitArgs ( drule [ <NUM_LIT:4> ] ) ) <EOL> else : <EOL> msg = '<STR_LIT>' . format ( rule ) <EOL> log . info ( msg ) <EOL> except ( Exception ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : "<STR_LIT>" . format ( rule ) , <EOL> '<STR_LIT>' : ex } ) <EOL> def _main ( vopts , <EOL> jconfig , <EOL> errors = None , <EOL> changes = None , <EOL> apply_retry = <NUM_LIT:0> , <EOL> ** kwargs ) : <EOL> if errors is None : <EOL> errors = [ ] <EOL> if changes is None : <EOL> changes = [ ] <EOL> fw ( ) <EOL> for z , zdata in six . iteritems ( jconfig [ '<STR_LIT>' ] ) : <EOL> try : <EOL> masq = z in jconfig [ '<STR_LIT>' ] <EOL> if not masq : <EOL> masq = None <EOL> define_zone ( z , zdata , errors = errors , changes = changes , <EOL> apply_retry = apply_retry , ** kwargs ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : z , <EOL> '<STR_LIT>' : ex } ) <EOL> for z , zdata in six . iteritems ( jconfig [ '<STR_LIT>' ] ) : <EOL> try : <EOL> define_service ( z , zdata , errors = errors , changes = changes , <EOL> apply_retry = apply_retry , ** kwargs ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : z , <EOL> '<STR_LIT>' : ex } ) <EOL> log . error ( trace ) <EOL> interfaces = { } <EOL> try : <EOL> for z , izdata in six . iteritems ( jconfig . get ( '<STR_LIT>' , { } ) ) : <EOL> for ifc in fw ( ) . getInterfaces ( z ) : <EOL> zns = interfaces . setdefault ( ifc , [ ] ) <EOL> zns . append ( z ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( trace ) <EOL> for z , zdata in six . iteritems ( jconfig [ '<STR_LIT>' ] ) : <EOL> try : <EOL> link_interfaces ( z , zdata , interfaces , errors = errors , <EOL> changes = changes , apply_retry = apply_retry , <EOL> ** kwargs ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : z , <EOL> '<STR_LIT>' : ex } ) <EOL> lazy_reload ( ) <EOL> for z , zdata in six . iteritems ( jconfig [ '<STR_LIT>' ] ) : <EOL> try : <EOL> configure_rules ( z , zdata , errors = errors , changes = changes , <EOL> apply_retry = apply_retry , ** kwargs ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : z , <EOL> '<STR_LIT>' : ex } ) <EOL> try : <EOL> configure_directs ( jconfig , errors = errors , changes = changes , <EOL> apply_retry = apply_retry , ** kwargs ) <EOL> except ( Exception , ) as ex : <EOL> trace = traceback . format_exc ( ) <EOL> errors . append ( { '<STR_LIT>' : trace , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : z , <EOL> '<STR_LIT>' : ex } ) <EOL> log . info ( '<STR_LIT>' ) <EOL> errortypes = [ a [ '<STR_LIT:type>' ] for a in errors ] <EOL> if '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:1> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:2> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:3> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:4> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:5> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:6> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:7> <EOL> elif '<STR_LIT>' in errortypes : <EOL> code = <NUM_LIT:8> <EOL> elif len ( errors ) : <EOL> code = <NUM_LIT:255> <EOL> else : <EOL> code = <NUM_LIT:0> <EOL> return code <EOL> def main ( ) : <EOL> logging . basicConfig ( <EOL> level = logging . DEBUG , <EOL> format = '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' ) <EOL> opts = parser . parse_args ( ) <EOL> vopts = vars ( opts ) <EOL> config = vopts [ '<STR_LIT>' ] <EOL> if vopts . get ( '<STR_LIT>' ) : <EOL> root_logger = logging . getLogger ( ) <EOL> root_logger . disabled = True <EOL> if vopts . get ( '<STR_LIT>' ) : <EOL> pr = cProfile . Profile ( ) <EOL> pr . enable ( ) <EOL> if not os . path . exists ( config ) : <EOL> raise OSError ( '<STR_LIT>' . format ( config ) ) <EOL> os . exit ( <NUM_LIT:1> ) <EOL> with open ( config ) as f : <EOL> jconfig = json . loads ( f . read ( ) ) <EOL> if [ a for a in jconfig ] == [ '<STR_LIT>' ] : <EOL> jconfig = jconfig [ '<STR_LIT>' ] <EOL> changes = [ ] <EOL> errors = [ ] <EOL> apply_retry = <NUM_LIT:0> <EOL> retries = <NUM_LIT:3> <EOL> while retries : <EOL> retries -= <NUM_LIT:1> <EOL> old_errors = errors <EOL> errors = [ ] <EOL> code = _main ( vopts , jconfig , <EOL> changes = changes , <EOL> errors = errors , <EOL> apply_retry = apply_retry ) <EOL> for i in reversed ( old_errors ) : <EOL> if i not in errors : <EOL> errors . append ( i ) <EOL> if code : <EOL> apply_retry += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> old_errors . extend ( errors ) <EOL> displayed = [ ] <EOL> if errors : <EOL> for error in errors : <EOL> if error in displayed : <EOL> continue <EOL> displayed . append ( error ) <EOL> try : <EOL> msg = '<STR_LIT>' . format ( error [ '<STR_LIT:type>' ] ) <EOL> if error . get ( '<STR_LIT:id>' ) : <EOL> msg += '<STR_LIT>' . format ( error [ '<STR_LIT:id>' ] ) <EOL> log . error ( msg ) <EOL> try : <EOL> log . error ( '<STR_LIT>' . format ( error [ '<STR_LIT>' ] ) ) <EOL> except ( Exception , ) : <EOL> pass <EOL> if vopts [ '<STR_LIT>' ] : <EOL> try : <EOL> log . error ( '<STR_LIT>' . format ( error [ '<STR_LIT>' ] ) ) <EOL> except ( Exception , ) : <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( error [ '<STR_LIT>' ] ) <EOL> except Exception : <EOL> continue <EOL> log . error ( '<STR_LIT>' ) <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( code ) ) <EOL> if vopts . get ( '<STR_LIT>' ) : <EOL> date = datetime . datetime . now ( ) . isoformat ( ) <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> os . makedirs ( '<STR_LIT>' ) <EOL> ficp = '<STR_LIT>' . format ( date ) <EOL> fico = '<STR_LIT>' . format ( date ) <EOL> ficn = '<STR_LIT>' . format ( date ) <EOL> if not os . path . exists ( ficp ) : <EOL> pr . dump_stats ( ficp ) <EOL> with open ( ficn , '<STR_LIT:w>' ) as fic : <EOL> pstats . Stats ( pr , stream = fic ) . sort_stats ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( ficp , fico ) ) <EOL> if vopts . get ( '<STR_LIT>' , False ) : <EOL> if changes : <EOL> changes = '<STR_LIT:\n>' . join ( <EOL> [ a . replace ( '<STR_LIT:">' , '<STR_LIT>' ) . replace ( "<STR_LIT:'>" , '<STR_LIT>' ) for a in changes ] ) <EOL> ret = { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:">' ) . format ( changes ) } <EOL> else : <EOL> ret = { '<STR_LIT>' : False , '<STR_LIT>' : '<STR_LIT>' } <EOL> print ( json . dumps ( ret ) ) <EOL> return code <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
