Merge "Replace open() with io.open() and force 'utf-8'"

This commit is contained in:
Jenkins 2015-08-10 18:14:11 +00:00 committed by Gerrit Code Review
commit 8c46ff8b10
6 changed files with 29 additions and 26 deletions

View File

@ -16,6 +16,7 @@
# Manage jobs in Jenkins server
import errno
import io
import os
import operator
import hashlib
@ -49,7 +50,7 @@ class CacheStorage(object):
if flush or not os.path.isfile(self.cachefilename):
self.data = {}
else:
with open(self.cachefilename, 'r') as yfile:
with io.open(self.cachefilename, 'r', encoding='utf-8') as yfile:
self.data = yaml.load(yfile)
logger.debug("Using cache: '{0}'".format(self.cachefilename))
@ -83,7 +84,8 @@ class CacheStorage(object):
# due to an exception occurring in the __init__
if getattr(self, 'data', None) is not None:
try:
with open(self.cachefilename, 'w') as yfile:
with io.open(self.cachefilename, 'w',
encoding='utf-8') as yfile:
self._yaml.dump(self.data, yfile)
except Exception as e:
self._logger.error("Failed to write to cache file '%s' on "
@ -317,9 +319,8 @@ class Builder(object):
output_fn = os.path.join(output, job.name)
logger.debug("Writing XML to '{0}'".format(output_fn))
f = open(output_fn, 'w')
f.write(job.output())
f.close()
with io.open(output_fn, 'w', encoding='utf-8') as f:
f.write(job.output().decode('utf-8'))
continue
md5 = job.md5()
if (self.jenkins.is_job(job.name)

View File

@ -14,6 +14,7 @@
# under the License.
import argparse
import io
from six.moves import configparser, StringIO
import fnmatch
import logging
@ -189,7 +190,7 @@ def setup_config_settings(options):
config.readfp(StringIO(DEFAULT_CONF))
if os.path.isfile(conf):
logger.debug("Reading config from {0}".format(conf))
conffp = open(conf, 'r')
conffp = io.open(conf, 'r', encoding='utf-8')
config.readfp(conffp)
elif options.command == 'test':
logger.debug("Not requiring config for test output generation")
@ -231,7 +232,8 @@ def execute(options, config):
plugins_info = None
if getattr(options, 'plugins_info_path', None) is not None:
with open(options.plugins_info_path, 'r') as yaml_file:
with io.open(options.plugins_info_path, 'r',
encoding='utf-8') as yaml_file:
plugins_info = yaml.load(yaml_file)
if not isinstance(plugins_info, list):
raise JenkinsJobsException("{0} must contain a Yaml list!"

View File

@ -90,12 +90,12 @@ Example:
"""
import codecs
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
import functools
import io
import logging
import re
import os
@ -183,18 +183,18 @@ class LocalLoader(OrderedConstructor, LocalAnchorLoader):
# use the load function provided in this module
import local_yaml
data = local_yaml.load(open(fn))
data = local_yaml.load(io.open(fn, 'r', encoding='utf-8'))
# Loading by providing the alternate class to the default yaml load
from local_yaml import LocalLoader
data = yaml.load(open(fn), LocalLoader)
data = yaml.load(io.open(fn, 'r', encoding='utf-8'), LocalLoader)
# Loading with a search path
from local_yaml import LocalLoader
import functools
data = yaml.load(open(fn), functools.partial(LocalLoader,
search_path=['path']))
data = yaml.load(io.open(fn, 'r', encoding='utf-8'),
functools.partial(LocalLoader, search_path=['path']))
"""
@ -243,7 +243,7 @@ class LocalLoader(OrderedConstructor, LocalAnchorLoader):
def _include_tag(self, loader, node):
filename = self._find_file(loader.construct_yaml_str(node))
with open(filename, 'r') as f:
with io.open(filename, 'r', encoding='utf-8') as f:
data = yaml.load(f, functools.partial(LocalLoader,
search_path=self.search_path
))
@ -252,7 +252,7 @@ class LocalLoader(OrderedConstructor, LocalAnchorLoader):
def _include_raw_tag(self, loader, node):
filename = self._find_file(loader.construct_yaml_str(node))
try:
with codecs.open(filename, 'r', 'utf-8') as f:
with io.open(filename, 'r', encoding='utf-8') as f:
data = f.read()
except:
logger.error("Failed to include file using search path: '{0}'"

View File

@ -17,6 +17,7 @@
import copy
import fnmatch
import io
import itertools
import logging
import pkg_resources
@ -96,7 +97,7 @@ class YamlParser(object):
self.data[cls] = group
def parse(self, fn):
with open(fn) as fp:
with io.open(fn, 'r', encoding='utf-8') as fp:
self.parse_fp(fp)
def _handle_dups(self, message):

View File

@ -17,7 +17,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import codecs
import io
import logging
import os
import re
@ -110,12 +110,12 @@ class BaseTestCase(object):
return u""
# Read XML content, assuming it is unicode encoded
xml_content = u"%s" % codecs.open(self.out_filename,
'r', 'utf-8').read()
xml_content = u"%s" % io.open(self.out_filename,
'r', encoding='utf-8').read()
return xml_content
def _read_yaml_content(self, filename):
with open(filename, 'r') as yaml_file:
with io.open(filename, 'r', encoding='utf-8') as yaml_file:
yaml_content = yaml.load(yaml_file)
return yaml_content
@ -125,7 +125,7 @@ class BaseTestCase(object):
if self.conf_filename is not None:
config = configparser.ConfigParser()
config.readfp(open(self.conf_filename))
config.readfp(io.open(self.conf_filename, 'r', encoding='utf-8'))
else:
config = {}
@ -182,7 +182,7 @@ class SingleJobTestCase(BaseTestCase):
if self.conf_filename:
config = configparser.ConfigParser()
config.readfp(open(self.conf_filename))
config.readfp(io.open(self.conf_filename, 'r', encoding='utf-8'))
else:
config = None
parser = YamlParser(config)

View File

@ -1,6 +1,5 @@
import os
import io
import codecs
import yaml
import jenkins
@ -169,9 +168,8 @@ class TestTests(CmdTestsBase):
with mock.patch('sys.stdout', console_out):
cmd.main(['test', os.path.join(self.fixtures_path,
'cmd-001.yaml')])
xml_content = codecs.open(os.path.join(self.fixtures_path,
'cmd-001.xml'),
'r', 'utf-8').read()
xml_content = io.open(os.path.join(self.fixtures_path, 'cmd-001.xml'),
'r', encoding='utf-8').read()
self.assertEqual(console_out.getvalue().decode('utf-8'), xml_content)
def test_config_with_test(self):
@ -208,7 +206,8 @@ class TestTests(CmdTestsBase):
with mock.patch('sys.stdout'):
cmd.execute(args, self.config) # probably better to fail here
with open(plugins_info_stub_yaml_file, 'r') as yaml_file:
with io.open(plugins_info_stub_yaml_file,
'r', encoding='utf-8') as yaml_file:
plugins_info_list = yaml.load(yaml_file)
registry_mock.assert_called_with(self.config, plugins_info_list)