0

I'm using the ConfigParser module to write out a configuration file with server information that's being collected. The 1st section I'm writing is the local system information, but it seems to be writing the 1st section twice for some reason.

Here's the code I'm using to write the config:

    def system_info(config_file):
        cnf_file = open(config_file, 'w+')
        config.add_section('System Information')
        wmi_conn_local = wmi.WMI()
        for OpSys in wmi_conn_local.Win32_OperatingSystem():
            OpSys = OpSys.Caption
        x64 = tools.is_64_bit()
        if x64 is True:
            config.set('System Information', 'System Architecture', '64-bit')
        else:
            config.set('System Information', 'System Architecture', '32-bit')
        HostName = socket.gethostname()
        config.set('System Information', 'Hostname', HostName)
        config.set('System Information', 'OS', OpSys)
        config.write(cnf_file)
        cnf_file.close()

    def sql_info(config_file):
        cnf_file = open(config_file, 'a+')
        SQLServer_raw = raw_input("Please enter the SQL Server name: ")
        server_correct = tools.query_yes_no("Is %s correct?" % SQLServer_raw)
        if server_correct is False:
            SQLServer_raw = raw_input("Sorry about that. Please enter the correct SQLCMD Path: ")
        SQLIP = socket.gethostbyname(SQLServer_raw)
        config.add_section('SQL Server')
        config.set('SQL Server', 'Server Name', SQLServer_raw)
        config.set('SQL Server', 'Server IP', SQLIP)
        SQL_User = raw_input("Please enter the username to the connection to the SQL server: ")
        user_correct = tools.query_yes_no("Is %s correct?" % SQL_User)
        if user_correct is False:
            SQL_User = raw_input("Sorry about that. Please enter the correct SQL User: ")
        config.set('SQL Server', 'SQL User', SQL_User)
        SQL_Pass = raw_input("Please enter the password for the username you specified above: ")
        pass_correct = tools.query_yes_no("Is %s correct?" % SQL_Pass)
        if pass_correct is False:
            SQL_Pass = raw_input("Sorry about that. Please enter the correct SQL Password: ")
        config.set('SQL Server', 'SQL Password', SQL_Pass)
        config.write(cnf_file)
        cnf_file.close()

if setup_exists is False:
        os.system('cls')
        print header
        print "Setup file not found. Creating file."
        logger.info("Setup file not found. Creating file.")

        print "Gathering System Information..."
        logger.info("Gathering System Information...")
        setup_config.system_info(config_file)

        print "Gathering SQL Server Information..."
        logger.info("Gathering SQL Server Information...")
        setup_config.sql_info(config_file)

Here's the output I'm seeing in the configuration file:

[System Information]
system architecture = 64-bit
hostname = FLA-7MartinezD
os = Microsoft Windows 7 Enterprise 

[System Information]
system architecture = 64-bit
hostname = FLA-7MartinezD
os = Microsoft Windows 7 Enterprise 

[SQL Server]
server name = sql1
server ip = 198.105.244.102
sql user = sa
sql password = password

Only thing I can see is that I'm using a w+ method for writing the system Info, but an a+ for the SQL section (and the other sections that will come after). I did this thinking that I'd want the SysInfo written first, and the other sections appended, but I might be wrong.

WR7500
  • 417
  • 1
  • 3
  • 12

2 Answers2

0

The bug is caused by twice

config.write(cnf_file)
cnf_file.close()

in both functions that are called and the config object is globlal, then the second time you open the file with cnf_file = open(config_file, 'a+') you are using the adding mode, so the information of system_info are added

You must extract those and place them in the main or, more easyly, open the file the second time in write mode too:

cnf_file = open(config_file, 'w+')

Trimax
  • 2,413
  • 7
  • 35
  • 59
0

Rethink you layout.

  • Why is you config variable global?
  • Do you want it to be global?

Your problem is, that the config object is global and you write it twice to a file!

  • So lets say you call the system_info(..).
  • The config object contains now a section "System Information"
  • You write the config to a file:

    [System Information]
    system architecture = 64-bit
    hostname = FLA-7MartinezD
    os = Microsoft Windows 7 Enterprise 
    
  • and now you call sql_info()

  • The config object contains now a section "SQL Server" and "System Information"
  • You append the config to the file:

    [System Information]
    system architecture = 64-bit
    hostname = FLA-7MartinezD
    os = Microsoft Windows 7 Enterprise 
    
    [System Information]
    system architecture = 64-bit
    hostname = FLA-7MartinezD
    os = Microsoft Windows 7 Enterprise 
    
    [SQL Server]
    server name = sql1
    server ip = 198.105.244.102
    sql user = sa
    sql password = password
    

Solution with global config object

    def system_info():
        if not config.has_section('System Information'): # prevent DuplicateSectionError
            config.add_section('System Information')
        wmi_conn_local = wmi.WMI()
        for OpSys in wmi_conn_local.Win32_OperatingSystem():
            OpSys = OpSys.Caption
        x64 = tools.is_64_bit()
        if x64 is True:
            config.set('System Information', 'System Architecture', '64-bit')
        else:
            config.set('System Information', 'System Architecture', '32-bit')
        HostName = socket.gethostname()
        config.set('System Information', 'Hostname', HostName)
        config.set('System Information', 'OS', OpSys)

    def sql_info():
        SQLServer_raw = raw_input("Please enter the SQL Server name: ")
        server_correct = tools.query_yes_no("Is %s correct?" % SQLServer_raw)
        if server_correct is False:
            SQLServer_raw = raw_input("Sorry about that. Please enter the correct SQLCMD Path: ")
        SQLIP = socket.gethostbyname(SQLServer_raw)
        if not config.has_section('SQL Server'): # prevent DuplicateSectionError
            config.add_section('SQL Server')
        config.set('SQL Server', 'Server Name', SQLServer_raw)
        config.set('SQL Server', 'Server IP', SQLIP)
        SQL_User = raw_input("Please enter the username to the connection to the SQL server: ")
        user_correct = tools.query_yes_no("Is %s correct?" % SQL_User)
        if user_correct is False:
            SQL_User = raw_input("Sorry about that. Please enter the correct SQL User: ")
        config.set('SQL Server', 'SQL User', SQL_User)
        SQL_Pass = raw_input("Please enter the password for the username you specified above: ")
        pass_correct = tools.query_yes_no("Is %s correct?" % SQL_Pass)
        if pass_correct is False:
            SQL_Pass = raw_input("Sorry about that. Please enter the correct SQL Password: ")
        config.set('SQL Server', 'SQL Password', SQL_Pass)

if setup_exists is False:
        os.system('cls')
        print header
        print "Setup file not found. Creating file."
        logger.info("Setup file not found. Creating file.")

        print "Gathering System Information..."
        logger.info("Gathering System Information...")
        setup_config.system_info()

        print "Gathering SQL Server Information..."
        logger.info("Gathering SQL Server Information...")
        setup_config.sql_info()

        with open(config_file, 'w') as cnf_file:
            config.write(cnf_file)

Solution with local config object (recommended)

    def system_info(config):
        if not config.has_section('System Information'): # prevent DuplicateSectionError
            config.add_section('System Information')
        wmi_conn_local = wmi.WMI()
        for OpSys in wmi_conn_local.Win32_OperatingSystem():
            OpSys = OpSys.Caption
        x64 = tools.is_64_bit()
        if x64 is True:
            config.set('System Information', 'System Architecture', '64-bit')
        else:
            config.set('System Information', 'System Architecture', '32-bit')
        HostName = socket.gethostname()
        config.set('System Information', 'Hostname', HostName)
        config.set('System Information', 'OS', OpSys)
        return config

    def sql_info(config):
        SQLServer_raw = raw_input("Please enter the SQL Server name: ")
        server_correct = tools.query_yes_no("Is %s correct?" % SQLServer_raw)
        if server_correct is False:
            SQLServer_raw = raw_input("Sorry about that. Please enter the correct SQLCMD Path: ")
        SQLIP = socket.gethostbyname(SQLServer_raw)
        if not config.has_section('SQL Server'): # prevent DuplicateSectionError
            config.add_section('SQL Server')
        config.set('SQL Server', 'Server Name', SQLServer_raw)
        config.set('SQL Server', 'Server IP', SQLIP)
        SQL_User = raw_input("Please enter the username to the connection to the SQL server: ")
        user_correct = tools.query_yes_no("Is %s correct?" % SQL_User)
        if user_correct is False:
            SQL_User = raw_input("Sorry about that. Please enter the correct SQL User: ")
        config.set('SQL Server', 'SQL User', SQL_User)
        SQL_Pass = raw_input("Please enter the password for the username you specified above: ")
        pass_correct = tools.query_yes_no("Is %s correct?" % SQL_Pass)
        if pass_correct is False:
            SQL_Pass = raw_input("Sorry about that. Please enter the correct SQL Password: ")
        config.set('SQL Server', 'SQL Password', SQL_Pass)
        return config

if setup_exists is False:
        os.system('cls')
        print header
        print "Setup file not found. Creating file."
        logger.info("Setup file not found. Creating file.")
        config = ConfigParser.RawConfigParser()
        print "Gathering System Information..."
        logger.info("Gathering System Information...")
        config = setup_config.system_info(config)

        print "Gathering SQL Server Information..."
        logger.info("Gathering SQL Server Information...")
        config = setup_config.sql_info(config)

        with open(config_file, 'w') as cnf_file:
            config.write(cnf_file)
c0ff3m4kr
  • 128
  • 1
  • 3