Convert cli arguments to unicode objects

This eliminates problem when non-ascii chars in plugin path led to
encoding related errors in the build operation

Change-Id: I382491426f3e4050021e4b837f87cd749e0d7363
Closes-Bug: #1455196
This commit is contained in:
Artem Roma 2015-09-25 16:08:10 +03:00
parent d0a71fd9da
commit 11d86d05eb
4 changed files with 61 additions and 21 deletions

View File

@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import unicode_literals
import abc
import logging
import os
@ -50,9 +52,13 @@ class BaseBuildPlugin(BaseAction):
def __init__(self, plugin_path):
self.plugin_path = plugin_path
self.pre_build_hook_path = join_path(plugin_path, 'pre_build_hook')
self.meta = utils.parse_yaml(join_path(plugin_path, 'metadata.yaml'))
self.build_dir = join_path(plugin_path, '.build')
self.pre_build_hook_path = join_path(self.plugin_path,
'pre_build_hook')
self.meta = utils.parse_yaml(
join_path(self.plugin_path, 'metadata.yaml')
)
self.build_dir = join_path(self.plugin_path, '.build')
self.build_src_dir = join_path(self.build_dir, 'src')
self.checksums_path = join_path(self.build_src_dir, 'checksums.sha1')
self.name = self.meta['name']
@ -158,8 +164,10 @@ class BuildPluginV2(BaseBuildPlugin):
self.plugin_version, self.full_version = utils.version_split_name_rpm(
self.meta['version'])
self.rpm_path = os.path.abspath(
join_path(self.plugin_path, '.build', 'rpm'))
self.rpm_src_path = join_path(self.rpm_path, 'SOURCES')
self.full_name = '{0}-{1}'.format(
self.meta['name'], self.plugin_version)
@ -168,12 +176,15 @@ class BuildPluginV2(BaseBuildPlugin):
self.tar_path = join_path(self.rpm_src_path, tar_name)
fpb_dir = join_path(os.path.dirname(__file__), '..')
self.spec_src = os.path.abspath(join_path(
fpb_dir, self.rpm_spec_src_path))
self.release_tmpl_src = os.path.abspath(join_path(
fpb_dir, self.release_tmpl_src_path))
self.spec_dst = join_path(self.rpm_path, 'plugin_rpm.spec')
self.rpm_packages_mask = join_path(
self.rpm_path, 'RPMS', 'noarch', '*.rpm')

View File

@ -52,6 +52,12 @@ def handle_exception(exc):
sys.exit(-1)
def decode_string(string):
"""Custom type for add_argument method
"""
return unicode(string, 'utf-8')
def parse_args():
"""Parse arguments and return them
"""
@ -66,13 +72,13 @@ def parse_args():
group.add_argument(
'--create', help='create a plugin skeleton',
type=str, metavar='plugin_name')
type=decode_string, metavar='plugin_name')
group.add_argument(
'--build', help='build a plugin',
type=str, metavar='path_to_directory')
type=decode_string, metavar='path_to_directory')
group.add_argument(
'--check', help='check that plugin is valid',
type=str, metavar='path_to_directory')
type=decode_string, metavar='path_to_directory')
parser.add_argument(
'--debug', help='enable debug mode',
@ -80,7 +86,7 @@ def parse_args():
parser.add_argument(
'--package-version', help='which package version to use',
type=str)
type=decode_string)
result = parser.parse_args()
package_version_check(result, parser)

View File

@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import unicode_literals
import mock
import os
@ -42,11 +44,13 @@ class BaseBuild(BaseTestCase):
def setUp(self):
self.plugins_name = 'fuel_plugin'
self.plugin_path = '/tmp/{0}'.format(self.plugins_name)
self.builder = self.create_builder(self.plugin_path)
def create_builder(self, plugin_path):
with mock.patch(
'fuel_plugin_builder.actions.build.utils.parse_yaml',
return_value=self.meta):
self.builder = self.builder_class(self.plugin_path)
return self.builder_class(plugin_path)
def test_run(self):
mocked_methods = [
@ -199,19 +203,24 @@ class TestBaseBuildV2(BaseBuild):
'homepage': 'url'
}
def path_from_plugin(self, path):
return join_path(self.plugin_path, path)
def path_from_plugin(self, plugin_path, path):
return join_path(plugin_path, path)
@mock.patch('fuel_plugin_builder.actions.build.utils')
def test_make_package(self, utils_mock):
def check_make_package(self, builder, plugin_path, utils_mock):
plugin_path = plugin_path
utils_mock.get_current_year.return_value = '2014'
self.builder.make_package()
rpm_src_path = self.path_from_plugin('.build/rpm/SOURCES')
builder.make_package()
rpm_src_path = self.path_from_plugin(plugin_path,
'.build/rpm/SOURCES')
utils_mock.create_dir.assert_called_once_with(rpm_src_path)
fp_dst = self.path_from_plugin('.build/rpm/SOURCES/plugin_name-1.2.fp')
fp_dst = self.path_from_plugin(
plugin_path, '.build/rpm/SOURCES/plugin_name-1.2.fp')
utils_mock.make_tar_gz.assert_called_once_with(
self.path_from_plugin('.build/src'),
self.path_from_plugin(plugin_path, '.build/src'),
fp_dst,
'plugin_name-1.2')
@ -220,7 +229,7 @@ class TestBaseBuildV2(BaseBuild):
self.builder.rpm_spec_src_path))
utils_mock.render_to_file.assert_called_once_with(
spec_src,
join_path(self.plugin_path, '.build/rpm/plugin_rpm.spec'),
join_path(plugin_path, '.build/rpm/plugin_rpm.spec'),
{'vendor': 'author1, author2',
'description': 'Description',
'license': 'Apache and BSD',
@ -233,12 +242,25 @@ class TestBaseBuildV2(BaseBuild):
utils_mock.exec_cmd.assert_called_once_with(
'rpmbuild -vv --nodeps --define "_topdir {0}" -bb '
'{1}'.format(
self.path_from_plugin('.build/rpm'),
self.path_from_plugin('.build/rpm/plugin_rpm.spec')))
self.path_from_plugin(plugin_path, '.build/rpm'),
self.path_from_plugin(plugin_path,
'.build/rpm/plugin_rpm.spec')))
utils_mock.copy_files_in_dir.assert_called_once_with(
self.path_from_plugin('.build/rpm/RPMS/noarch/*.rpm'),
self.plugin_path)
self.path_from_plugin(plugin_path,
'.build/rpm/RPMS/noarch/*.rpm'),
plugin_path
)
def test_make_package(self):
self.check_make_package(self.builder, self.plugin_path)
def test_make_package_with_non_ascii_chars_in_path(self):
plugin_path = '/tmp/тест/' + self.plugins_name
builder = self.create_builder(plugin_path)
self.check_make_package(builder, plugin_path)
@mock.patch('fuel_plugin_builder.actions.build.utils.which',
return_value=False)

View File

@ -169,8 +169,9 @@ def render_to_file(src, dst, params):
:param src: path to template
:param dst: path where rendered template will be saved
"""
logger.debug('Render template from {0} to {1} with params: {2}'.format(
logger.debug(u'Render template from {0} to {1} with params: {2}'.format(
src, dst, params))
with open(src, 'r') as f:
template_file = f.read()