def write_temp_bird_conf(dummy_ip_prefix,
                         config_file,
                         variable_name,
                         prefixes):
    """Write in a temporary file the list of IP-Prefixes.

    A failure to create and write the temporary file will exit main program.

    Arguments:
        dummy_ip_prefix (str): The dummy IP prefix, which must be always
        config_file (str): The file name of bird configuration
        variable_name (str): The name of the variable set in bird configuration
        prefixes (list): The list of IP-Prefixes to write

    Returns:
        The filename of the temporary file

    """
    log = logging.getLogger(PROGRAM_NAME)
    comment = ("# {i} is a dummy IP Prefix. It should NOT be used and "
               "REMOVED from the constant.".format(i=dummy_ip_prefix))

    # the temporary file must be on the same filesystem as the bird config
    # as we use os.rename to perform an atomic update on the bird config.
    # Thus, we create it in the same directory that bird config is stored.
    tm_file = os.path.join(os.path.dirname(config_file), str(time.time()))
    log.debug("going to write to %s", tm_file)

    try:
        with open(tm_file, 'w') as tmpf:
            tmpf.write("# Generated {t} by {n} (pid={p})\n"
                       .format(t=datetime.datetime.now(),
                               n=PROGRAM_NAME,
                               p=os.getpid()))
            tmpf.write("{c}\n".format(c=comment))
            tmpf.write("define {n} =\n".format(n=variable_name))
            tmpf.write("{s}[\n".format(s=4 * ' '))
            # all entries of the array need a trailing comma except the last
            # one. A single element array doesn't need a trailing comma.
            tmpf.write(',\n'.join([' '*8 + n for n in prefixes]))
            tmpf.write("\n{s}];\n".format(s=4 * ' '))
    except OSError as error:
        log.critical("failed to write temporary file %s: %s. This is a FATAL "
                     "error, this exiting main program", tm_file, error)
        sys.exit(1)
    else:
        return tm_file
def write_temp_bird_conf ( dummy_ip_prefix , config_file , variable_name , prefixes ) : log = logging . getLogger ( PROGRAM_NAME ) comment = ( "# {i} is a dummy IP Prefix. It should NOT be used and " "REMOVED from the constant." . format ( i = dummy_ip_prefix ) ) tm_file = os . path . join ( os . path . dirname ( config_file ) , str ( time . time ( ) ) ) log . debug ( "going to write to %s" , tm_file ) try : with open ( tm_file , 'w' ) as tmpf : tmpf . write ( "# Generated {t} by {n} (pid={p})\n" . format ( t = datetime . datetime . now ( ) , n = PROGRAM_NAME , p = os . getpid ( ) ) ) tmpf . write ( "{c}\n" . format ( c = comment ) ) tmpf . write ( "define {n} =\n" . format ( n = variable_name ) ) tmpf . write ( "{s}[\n" . format ( s = 4 * ' ' ) ) tmpf . write ( ',\n' . join ( [ ' ' * 8 + n for n in prefixes ] ) ) tmpf . write ( "\n{s}];\n" . format ( s = 4 * ' ' ) ) except OSError as error : log . critical ( "failed to write temporary file %s: %s. This is a FATAL " "error, this exiting main program" , tm_file , error ) sys . exit ( 1 ) else : return tm_file
Write in a temporary file the list of IP-Prefixes.

    A failure to create and write the temporary file will exit main program.

    Arguments:
        dummy_ip_prefix (str): The dummy IP prefix, which must be always
        config_file (str): The file name of bird configuration
        variable_name (str): The name of the variable set in bird configuration
        prefixes (list): The list of IP-Prefixes to write

    Returns:
        The filename of the temporary file