'Write out the forward and reverse zone files for all configured zones'

def __write_zone_files(self):

    default_template_file = '/etc/cobbler/zone.template'
    cobbler_server = self.settings.server
    serial_filename = '/var/lib/cobbler/bind_serial'
    serial = time.strftime('%Y%m%d00')
    try:
        serialfd = open(serial_filename, 'r')
        old_serial = serialfd.readline()
        if (serial[0:8] == old_serial[0:8]):
            if (int(old_serial[8:10]) < 99):
                serial = ('%s%.2i' % (serial[0:8], (int(old_serial[8:10]) + 1)))
        else:
            pass
        serialfd.close()
    except:
        pass
    serialfd = open(serial_filename, 'w')
    serialfd.write(serial)
    serialfd.close()
    forward = self.__forward_zones()
    reverse = self.__reverse_zones()
    try:
        f2 = open(default_template_file, 'r')
    except:
        raise CX((_('error    reading    template    from    file:    %s') % default_template_file))
    default_template_data = ''
    default_template_data = f2.read()
    f2.close()
    zonefileprefix = (self.settings.bind_chroot_path + self.zonefile_base)
    for (zone, hosts) in forward.iteritems():
        metadata = {'cobbler_server': cobbler_server, 'serial': serial, 'zonename': zone, 'zonetype': 'forward', 'cname_record': '', 'host_record': ''}
        if (':' in zone):
            long_zone = self.__expand_IPv6((zone + '::1'))[:19]
            tokens = list(re.sub(':', '', long_zone))
            tokens.reverse()
            zone_origin = ('.'.join(tokens) + '.ip6.arpa.')
        else:
            zone_origin = ''
        try:
            fd = open(('/etc/cobbler/zone_templates/%s' % zone))
            if zone_origin:
                template_data = ((('\\$ORIGIN    ' + zone_origin) + '\n') + fd.read())
            else:
                template_data = fd.read()
            fd.close()
        except:
            if zone_origin:
                template_data = ((('\\$ORIGIN    ' + zone_origin) + '\n') + default_template_data)
            else:
                template_data = default_template_data
        metadata['cname_record'] = self.__pretty_print_cname_records(hosts)
        metadata['host_record'] = self.__pretty_print_host_records(hosts)
        zonefilename = (zonefileprefix + zone)
        if (self.logger is not None):
            self.logger.info(('generating    (forward)    %s' % zonefilename))
        self.templar.render(template_data, metadata, zonefilename, None)
    for (zone, hosts) in reverse.iteritems():
        metadata = {'cobbler_server': cobbler_server, 'serial': serial, 'zonename': zone, 'zonetype': 'reverse', 'cname_record': '', 'host_record': ''}
        try:
            fd = open(('/etc/cobbler/zone_templates/%s' % zone))
            template_data = fd.read()
            fd.close()
        except:
            template_data = default_template_data
        metadata['cname_record'] = self.__pretty_print_cname_records(hosts)
        metadata['host_record'] = self.__pretty_print_host_records(hosts, rectype='PTR')
        zonefilename = (zonefileprefix + zone)
        if (self.logger is not None):
            self.logger.info(('generating    (reverse)    %s' % zonefilename))
        self.templar.render(template_data, metadata, zonefilename, None)
