Fix StringIO and ConfigParser import issue for Python2/3 compatible code.

Use io.StringIO to replace StringIO.StringIO, and use unicode as StringIO()
  can only handle unicode strings.

Story: 2003429
Task: 24618

Change-Id: Ie2ced7bba00d11b1698422cbe495abc0fbab7637
Signed-off-by: chenyan <yan.chen@intel.com>
Signed-off-by: Sun Austin <austin.sun@intel.com>
This commit is contained in:
chenyan 2018-08-30 17:28:00 +08:00 committed by Sun Austin
parent 29b0f7a9cc
commit a539b771cf
4 changed files with 25 additions and 25 deletions

View File

@ -13,7 +13,7 @@ from cgcs_patch.authapi import config
from cgcs_patch.authapi import hooks
from cgcs_patch.authapi import policy
import ConfigParser
from six.moves import configparser
auth_opts = [
cfg.StrOpt('auth_strategy',
@ -32,7 +32,7 @@ def get_pecan_config():
def setup_app(pecan_config=None, extra_hooks=None):
config_parser = ConfigParser.RawConfigParser()
config_parser = configparser.RawConfigParser()
config_parser.read('/etc/patching/patching.conf')
policy.init()

View File

@ -6,8 +6,8 @@ SPDX-License-Identifier: Apache-2.0
"""
import os
import ConfigParser
import StringIO
from six.moves import configparser
import io
import logging
import socket
import cgcs_patch.utils as utils
@ -48,7 +48,7 @@ def read_config():
global controller_port
global agent_port
config = ConfigParser.SafeConfigParser(defaults)
config = configparser.SafeConfigParser(defaults)
config.read(patching_conf)
patching_conf_mtime = os.stat(patching_conf).st_mtime
@ -62,8 +62,8 @@ def read_config():
# The platform.conf file has no section headers, which causes problems
# for ConfigParser. So we'll fake it out.
ini_str = '[platform_conf]\n' + open(tsc.PLATFORM_CONF_FILE, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
ini_str = u'[platform_conf]\n' + open(tsc.PLATFORM_CONF_FILE, 'r').read()
ini_fp = io.StringIO(ini_str)
config.readfp(ini_fp)
try:
@ -71,7 +71,7 @@ def read_config():
global nodetype
nodetype = value
except ConfigParser.Error:
except configparser.Error:
logging.exception("Failed to read nodetype from config")
return False
@ -103,12 +103,12 @@ def get_mgmt_iface():
# so return the cached value.
return mgmt_if
config = ConfigParser.SafeConfigParser()
config = configparser.SafeConfigParser()
# The platform.conf file has no section headers, which causes problems
# for ConfigParser. So we'll fake it out.
ini_str = '[platform_conf]\n' + open(tsc.PLATFORM_CONF_FILE, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
ini_str = u'[platform_conf]\n' + open(tsc.PLATFORM_CONF_FILE, 'r').read()
ini_fp = io.StringIO(ini_str)
config.readfp(ini_fp)
try:
@ -118,7 +118,7 @@ def get_mgmt_iface():
mgmt_if = value
platform_conf_mtime = os.stat(tsc.PLATFORM_CONF_FILE).st_mtime
except ConfigParser.Error:
except configparser.Error:
logging.exception("Failed to read management_interface from config")
return None
return mgmt_if

View File

@ -11,7 +11,7 @@ import socket
import json
import select
import subprocess
import ConfigParser
from six.moves import configparser
import rpm
import os
@ -592,7 +592,7 @@ class PatchController(PatchService):
pass
def write_state_file(self):
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
cfgfile = open(state_file, 'w')
@ -602,7 +602,7 @@ class PatchController(PatchService):
cfgfile.close()
def read_state_file(self):
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.read(state_file)
@ -611,7 +611,7 @@ class PatchController(PatchService):
self.patch_op_counter = counter
LOG.info("patch_op_counter is: %d" % self.patch_op_counter)
except ConfigParser.Error:
except configparser.Error:
LOG.exception("Failed to read state info")
def handle_nbr_patch_op_counter(self, host, nbr_patch_op_counter):

View File

@ -6,8 +6,8 @@ SPDX-License-Identifier: Apache-2.0
"""
import os
import ConfigParser
import StringIO
from six.moves import configparser
import io
import logging
SW_VERSION = ""
@ -54,17 +54,17 @@ def _load():
# The build.info file has no section headers, which causes problems
# for ConfigParser. So we'll fake it out.
ini_str = '[build_info]\n' + open(build_info, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
ini_str = u'[build_info]\n' + open(build_info, 'r').read()
ini_fp = io.StringIO(ini_str)
config = ConfigParser.SafeConfigParser()
config = configparser.SafeConfigParser()
config.readfp(ini_fp)
try:
value = config.get('build_info', 'SW_VERSION')
SW_VERSION = value.strip('"')
except ConfigParser.Error:
except configparser.Error:
logging.exception("Failed to read SW_VERSION from /etc/build.info")
return False
@ -72,8 +72,8 @@ def _load():
# The platform.conf file has no section headers, which causes problems
# for ConfigParser. So we'll fake it out.
ini_str = '[platform_conf]\n' + open(PLATFORM_CONF_FILE, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
ini_str = u'[platform_conf]\n' + open(PLATFORM_CONF_FILE, 'r').read()
ini_fp = io.StringIO(ini_str)
config.readfp(ini_fp)
try:
@ -147,7 +147,7 @@ def _load():
if config.has_option('platform_conf', 'security_feature'):
security_feature = config.get('platform_conf', 'security_feature')
except ConfigParser.Error:
except configparser.Error:
logging.exception("Failed to read platform.conf")
return False