Files
deb-python-fuelclient/fuelclient/utils.py
Evgeniy L 75c87d131f Add 2.0.0 plugins support
* install action now can install both .fp plugins and .rpm

* remove action can remove .fp and .rpm plugins

* update and downgrade actions are available for new plugin
  formats only

* register action was added for the users who install plugin
  directly with yum/rpm

* unregister action was added for the users who remove plugin
  directly with yum/rpm

* sync action is required for those users who use yum to perform
  massive update which includes a lot of plugins

* all unit tests were rewritten, now each layer
  (cli, objects, utils) is tested separately.

Change-Id: I2bc837fdaf356a4f4c7291cd0dbfc0a92dc433d6
Implements: blueprint plugins-security-fixes-delivery
2015-03-02 17:16:40 +00:00

132 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import glob
import io
import os
import subprocess
import yaml
from distutils.version import StrictVersion
from fnmatch import fnmatch
from fuelclient.cli import error
def _wait_and_check_exit_code(cmd, child):
"""Wait for child and check it's exit code
:param cmd: command
:param child: object which returned by subprocess.Popen
:raises: ExecutedErrorNonZeroExitCode
"""
child.wait()
exit_code = child.returncode
if exit_code != 0:
raise error.ExecutedErrorNonZeroExitCode(
u'Shell command executed with "{0}" '
'exit code: {1} '.format(exit_code, cmd))
def exec_cmd(cmd, cwd=None):
"""Execute shell command logging.
:param str cmd: shell command
:param str cwd: None is default
"""
child = subprocess.Popen(
cmd, stdout=None,
stderr=subprocess.STDOUT,
shell=True,
cwd=cwd)
_wait_and_check_exit_code(cmd, child)
def exec_cmd_iterator(cmd):
"""Execute command with logging.
:param cmd: shell command
:returns: generator where yeach item
is line from stdout
"""
child = subprocess.Popen(
cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
for line in child.stdout:
yield line
_wait_and_check_exit_code(cmd, child)
def parse_yaml_file(path):
"""Parses yaml
:param str path: path to yaml file
:returns: deserialized file
"""
with io.open(path, encoding='utf-8') as f:
data = yaml.load(f)
return data
def glob_and_parse_yaml(path):
"""Parses yaml files by mask.
:param str path: mask
:returns: iterator
"""
for f in glob.iglob(path):
yield parse_yaml_file(f)
def major_plugin_version(version):
"""Retrieves major version.
"1.2.3" -> "1.2"
:param str version: version
:returns: only major version
"""
version_tuple = StrictVersion(version).version
major = '.'.join(map(str, version_tuple[:2]))
return major
def iterfiles(dir_path, file_pattern):
"""Returns generator where each item is a path to file, that satisfies
file_patterns condtion
:param dir_path: path to directory, e.g /etc/puppet/
:param file_pattern: unix filepattern to match files
"""
for root, dirs, file_names in os.walk(dir_path):
for file_name in file_names:
if fnmatch(file_name, file_pattern):
yield os.path.join(root, file_name)
def file_exists(path):
"""Checks if file exists
:param str path: path to the file
:returns: True if file is exist, Flase if is not
"""
return os.path.lexists(path)