Exemple #1
0
    def load_config_from_file(self, api_data):
        """
        Load data from config file in yaml format.
        :param api_data: api data set
        :return: result
        """

        file_name = '.surepatch.yaml'
        file_path = os.path.expanduser('~')
        full_path = os.path.join(file_path, file_name)

        if not os.path.isfile(full_path):
            print_line('Config file does not exist: ~/{0}.'.format(file_name))
            print_line('Create config file first with parameter --action=save_config.')
            return False

        components_helper = ComponentsHelper()
        enc = components_helper.define_file_encoding(full_path)

        if enc == 'undefined':
            print_line('Undefined file encoding. Please, use utf-8 or utf-16.')
            return False

        with open(full_path, 'r') as yaml_config_file:
            try:
                config = yaml.load(yaml_config_file)

                if 'team' not in config:
                    config['team'] = None

                api_data['team'] = config['team']

                if 'user' not in config:
                    config['user'] = None

                api_data['user'] = config['user']

                if 'password' not in config:
                    config['password'] = None

                api_data['password'] = config['password']

                if 'auth_token' not in config:
                    config['auth-token'] = None

                api_data['auth_token'] = config['auth_token']

                if 'logi' not in config:
                    config['logo'] = 'off'

                api_data['logo'] = config['logo']

                return True

            except yaml.YAMLError as yaml_exception:
                print_line('Get an exception while read config file: {0}.'.format(yaml_exception))
                return False

            finally:
                yaml_config_file.close()
Exemple #2
0
 def __init__(self):
     self.web_api = WebAPI()
     self.components_helper = ComponentsHelper()
Exemple #3
0
class SetHelper(object):
    def __init__(self):
        self.web_api = WebAPI()
        self.components_helper = ComponentsHelper()

    def create_set_validate(self, api_data):
        # type: (dict) -> bool
        if api_data['platform'] is None:
            print_line(
                'Empty Platform name. Please use --platform=platform_name parameter.'
            )
            return False

        platforms = self.get_my_platforms(api_data=api_data)

        if api_data['platform'] not in platforms:
            print_line("Platform {0} does not exists.".format(
                api_data['platform']))
            return False

        if api_data['project'] is None:
            print_line(
                'Empty Project name. Please use --project=project_name parameter.'
            )
            return False

        projects = self.get_my_projects(api_data=api_data)

        if api_data['project'] not in projects:
            print_line("Project {0} does not exists.".format(
                api_data['project']))
            return False

        set_name = api_data['set']
        current_set_name = self.components_helper.get_current_set_name(
            api_data=api_data)[0]

        if current_set_name == set_name:
            print_line(
                'Current set with name {0} already exists.'.format(set_name))
            print_line(
                'Please, use another name, or use no --set parameter to autoincrement set name.'
            )
            return False

        if set_name is None:
            if current_set_name[-1].isdigit():
                d = int(current_set_name[-1])
                d = d + 1
                current_set_name = current_set_name[:-1]
                set_name = current_set_name + str(d)

            else:
                set_name = current_set_name + '.1'

        api_data['set'] = set_name

        return True

    # Target = OS packages

    def collect_data_for_set_os_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with OS packages, collected by shell command.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_os_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_os_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with OS packages, collected from shell command
        and stored in file, defined in path.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_os_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    # Target = Python packages

    def collect_data_for_set_pip_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Python PIP packages, collected from shell command.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_pip_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_pip_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Python PIP packages, collected from shell command
        and stored in file, defined in path.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_pip_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_requirements_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Python requirements.txt file, defined in path.
        :param api_data: spi data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_requirements_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_npm_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages, collected from shell command (nmp list --json).
        Shell command runs global from root path.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_npm_local_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages, collected from shell command (npm list --json).
        Shell command runs local from path, defined by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_local_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_npm_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages, collected from shell command (npm list --json)
        and stored in file, defined in path.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_package_json_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages from package.json, defined by --file parameter.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_package_json_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_package_lock_json_auto_system_path(
            self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages from package-lock.json, defined by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_lock_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gem_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from shell command.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gem_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gem_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from shell command and
        stored in gem list file, defined in --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gem_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gemfile_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from Gemfile, defined
        by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gemfile_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gemfile_lock_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from Gemfile.lock file,
        defined by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gemfile_lock_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_any_auto_user_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with different packages, collected in file,
        defined by path with simple multiline format: name=version…
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_any_auto_user_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_any_manual_user_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with different packages, asked in interactive mode.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_any_manual_user_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_php_composer_json_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from PHP Composer.json file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_php_composer_json_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".api_data['project'])
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_php_composer_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from PHP Composer.lock file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_php_composer_lock_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_maven_pom_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from Maven pom.xml file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_maven_pom(api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True
        return False

    def collect_data_for_set_yarn_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from yarn.lock file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_yarn_lock(api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = \
            api_data['organization']['platforms'][api_data['platform_number']]['projects'][api_data['project_number']][
                'url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True
        return False

    @staticmethod
    def get_my_platforms(api_data):
        # type: (dict) -> list
        """
        Get platforms names as list.
        :param api_data: api data set
        :return: result
        """

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        platforms = []
        for platform in api_data['organization']['platforms']:
            platforms.append(platform['name'])

        return platforms

    def get_my_projects(self, api_data):
        # type: (dict) -> list
        """
        Get projects names as list.
        :param api_data: api data set
        :return: result
        """

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        platform_number = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if platform_number == -1:
            return []

        projects = []
        for project in api_data['organization']['platforms'][platform_number][
                'projects']:
            projects.append(project['name'])

        return projects
Exemple #4
0
class ShowHelper(object):

    def __init__(self):
        self.web_api = WebAPI()
        self.components_helper = ComponentsHelper()

    @staticmethod
    def action_show_platforms(api_data):
        # type: (dict) -> bool
        """
        Print existing platforms.
        :param api_data: api data set
        :return: result
        """
        platforms = []

        if 'organization' not in api_data:
            print_line('Organization info error.')
            return False

        if 'platforms' not in api_data['organization']:
            print_line('Platform info error.')
            return False

        if len(api_data['organization']['platforms']) == 0:
            print_line('You have not Platforms.')
            return False

        for platform in api_data['organization']['platforms']:
            platforms.append({'name': platform['name'], 'description': platform['description']})

        print_platforms(platforms=platforms, title="Platforms", filename=api_data['file'])

        return True

    def action_show_projects(self, api_data):
        # type: (dict) -> bool
        """
        Print existing project for defined Platform.
        :param api_data: api data set
        :return: result
        """
        projects = []

        if 'organization' not in api_data:
            print_line('Organization info error.')
            return False

        if 'platforms' not in api_data['organization']:
            print_line('Platform info error.')
            return False

        if len(api_data['organization']['platforms']) == 0:
            print_line('You have not Platforms.')
            return False

        platform_number = self.web_api.get_platform_number_by_name(api_data=api_data)

        if platform_number == -1:
            print_line("No such platform: {0}.".format(api_data['platform']))
            return False

        if len(api_data['organization']['platforms'][platform_number]['projects']) == 0:
            print_line('You have not Projects.')
            return False

        for project in api_data['organization']['platforms'][platform_number]['projects']:
            projects.append({'name': project['name'], 'description': 'default project'})

        print_projects(projects=projects, title=api_data['platform'], filename=api_data['file'])

        return True

    def action_show_set(self, api_data):
        # type: (dict) -> bool
        """
        Print current Component set for defined Platform/Project.
        :param api_data: api data set
        :return: result
        """
        my_set = self.components_helper.get_current_set_name(api_data=api_data)

        if my_set[0] is None:
            print_line('Get set name error.')
            return False

        print_line('Current component set: {0}.'.format(my_set[0]))

        components = self.components_helper.get_current_component_set(api_data=api_data)[0]['components']

        if components[0] is None:
            print_line('Get component set error.')
            return False

        print_components(components=components, title=api_data['platform'] + '/' + api_data['project'], filename=api_data['file'])

        return True

    def action_show_issues(self, api_data):
        # type: (dict) -> bool
        """
        Print current Issues tor defined Platform/Project.
        :param api_data: api data set
        :return: result
        """
        api_data['platform_number'] = self.web_api.get_platform_number_by_name(api_data=api_data)

        if api_data['platform_number'] == -1:
            print_line("No such platform: {0}.".format(api_data['platform']))
            return False

        api_data['project_number'] = self.web_api.get_project_number_by_name(api_data=api_data)

        if api_data['project_number'] == -1:
            print_line("No such project {0} in platform {1}.".format(api_data['project'], api_data['platform']))
            return False

        api_data['project_url'] = api_data['organization']['platforms'][api_data['platform_number']]['projects'][api_data['project_number']]['url']

        if not self.web_api.send_get_issues_request(api_data=api_data):
            print_line("Cant load issues for platform {0} and project {1}.".format(api_data['platform'], api_data['project']))
            return False

        issues = api_data['issues']

        printed_issues = []

        for issue in issues:
            printed_issues.append({'name': 'title', 'description': issue['title']})
            printed_issues.append({'name': 'component', 'description': issue['component']['name'] + ' - ' + issue['component']['version']})
            printed_issues.append({'name': 'description', 'description': issue['description']})
            printed_issues.append({'name': 'author', 'description': issue['author']})
            printed_issues.append({'name': 'status', 'description': issue['status']})
            printed_issues.append({'name': '\n', 'description': ''})

        print_issues(issues=printed_issues, title=api_data['platform'] + '/' + api_data['project'], filename=api_data['file'])

        return True
Exemple #5
0
    def load_config_from_file(self, api_data):
        """
        Load data from config file in yaml format.
        :param api_data: api data set
        :return: result
        """

        # Default parameters for config file

        file_name = '.surepatch.yaml'
        file_path = os.path.expanduser('~')
        full_path = os.path.join(file_path, file_name)

        # Save changable flags

        server = api_data['server']

        # If file does not exist

        if not os.path.isfile(full_path):
            print_line('Config file does not exist: ~/{0}.'.format(file_name))
            print_line(
                'Create config file first with parameter --action=save_config.'
            )
            return False

        components_helper = ComponentsHelper()

        # Define config file encoding

        if components_helper.define_file_encoding(full_path) == 'undefined':
            print_line('Undefined file encoding. Please, use utf-8 or utf-16.')
            return False

        # Open config file

        with open(full_path, 'r') as yaml_config_file:
            try:

                # Load config file contents in YAML format

                config = yaml.load(yaml_config_file)

                # Analyse config elements

                if 'team' not in config:
                    config['team'] = None

                api_data['team'] = config['team']

                if 'user' not in config:
                    config['user'] = None

                api_data['user'] = config['user']

                if 'password' not in config:
                    config['password'] = None

                api_data['password'] = config['password']

                if 'auth_token' not in config:
                    config['auth-token'] = None

                api_data['auth_token'] = config['auth_token']

                if 'logo' not in config:
                    config['logo'] = 'off'

                api_data['logo'] = config['logo']

                if 'logging' not in config:
                    config['logging'] = 'off'

                api_data['logging'] = config['logging']

                if 'server' not in config:
                    config['server'] = 'prod'

                if config['server'] != server:
                    api_data['server'] = server
                else:
                    api_data['server'] = config['server']

                return True

            except yaml.YAMLError as yaml_exception:
                print_line(
                    'Get an exception while read config file: {0}.'.format(
                        yaml_exception))
                return False

            finally:
                yaml_config_file.close()
class ProjectHelper(object):
    """
    Project Helper class.
    """
    def __init__(self):
        self.web_api = WebAPI()
        self.components_helper = ComponentsHelper()

    def create_project_validate(self, api_data):
        # type: (dict) -> bool
        """
        Run action: CREATE New Project in different cases.
        :return: result, modify api_data
        """

        # Validate Platform and Project parameters

        if 'platform' not in api_data:
            print_line('There are no --platform parameter given.')
            return False

        if 'project' not in api_data:
            print_line('There are no --platform parameter given.')
            return False

        if api_data['platform'] is None or api_data['platform'] == '':
            print_line('Empty platform name.')
            return False

        if api_data['project'] is None or api_data['project'] == '':
            print_line('Empty project name.')
            return False

        # Get Platforms

        platforms = self.get_my_platforms(api_data=api_data)

        if api_data['platform'] not in platforms:
            print_line("Platform {0} does not exists.".format(
                api_data['platform']))
            return False

        # Get Projects

        projects = self.get_my_projects(api_data=api_data)

        if api_data['project'] in projects:
            print_line("Project {0} already exists.".format(
                api_data['project']))
            return False

        # Normal output

        return True

    # Target = OS packages

    def collect_data_for_project_os_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect data from OS packages by shell command.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_os_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_os_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect data from OS packages by shell command,
        stored in file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_os_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    # Target = Python packages

    def collect_data_for_project_pip_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect Python PIP packages from shell command.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_pip_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_data_for_project_pip_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Python PIP packages from shell command,
        stored in file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_pip_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_requirements_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Python requirements.txt file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_requirements_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    # Target = NodeJS NPM packages

    def collect_data_for_project_npm_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages, collected from shell command (nmp list --json).
        Shell command runs global from root path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_npm_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from shell command (npm list --json),
        stored in file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Got components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_npm_local_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from shell command (npm list --json).
        Shell command runs local from path, defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_local_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_package_lock_json_auto_system_path(
            self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from package-lock.json, defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_lock_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_package_json_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from package.json, defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_package_json_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    # Target = Ruby packages

    def collect_data_for_project_gem_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages, collected from shell command.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gem_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_gem_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages from shell command and
        stored in gem list file, defined in --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gem_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_gemfile_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages, collected from Gemfile, defined
        by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gemfile_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_gemfile_lock_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages, collected from Gemfile.lock file,
        defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gemfile_lock_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_any_auto_user_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect different packages, collected in file,
        defined by path with simple multiline format: name=version…
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_any_auto_user_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_any_manual_user_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect different packages, asked in interactive mode.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_any_manual_user_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_php_composer_json_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect PHP packages from Composer.json file.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_php_composer_json_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_php_composer_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect PHP packages from Composer.json file.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_php_composer_lock_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_maven_pom_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect packages with Maven pom.xml file
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components
        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_maven_pom(api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_yarn_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect packages with yarn.lock file
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_yarn_lock(api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def cllect_data_for_project_node_modules_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect data for node_modules from file
        :param api_data: api data set
        :return: result
        """

        # Get components

        components = api_data['components']
        api_data['components']

        if self.components_helper.get_components_node_modules(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal utput

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def delete_project(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Delete defined Project.
        :param api_data: api data set
        :return: result
        """

        # Get Platform number

        api_data['platform_number'] = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if api_data['platform_number'] == -1:
            print_line("Platform {0} does not exist.".format(
                api_data['platform']))
            return False

        # Get Project number

        api_data['project_number'] = self.web_api.get_project_number_by_name(
            api_data=api_data)

        if api_data['project_number'] == -1:
            print_line("Project {0} does not exist.".format(
                api_data['project']))
            return False

        # Get Project ID

        api_data['project_id'] = api_data['organization']['platforms'][
            api_data['platform_number']]['projects'][
                api_data['project_number']]['id']

        # Send Delete request

        return self.web_api.send_delete_project_request(api_data=api_data)

    def archive_project(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Archive defined Project.
        :param api_data: api data set
        :return: result
        """

        # get Platform number

        api_data['platform_number'] = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if api_data['platform_number'] == -1:
            print_line("Platform {0} does not exist.".format(
                api_data['platform']))
            return False

        # get Project number

        api_data['project_number'] = self.web_api.get_project_number_by_name(
            api_data=api_data)

        if api_data['project_number'] == -1:
            print_line("Project {0} does not exist.".format(
                api_data['project']))
            return False

        # Get parameters

        organization = api_data['organization']
        platforms = organization['platforms']
        platform = platforms[api_data['platform_number']]
        projects = platform['projects']
        project = projects[api_data['project_number']]
        project_id = project['id']
        project_url = project['url']
        api_data['project_id'] = project_id
        api_data['project_url'] = project_url

        # Send Archive request

        return self.web_api.send_archive_project_request(api_data=api_data)

    def restore_project(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Restore Project from Archive.
        :param api_data:
        :return:
        """

        # Get Platform number

        if api_data['platform'] is None or api_data['platform'] == '':
            print_line('Empty platform name.')
            return False

        if api_data['project'] is None or api_data['project'] == '':
            print_line('Empty project name.')
            return False

        # Get archived projects

        if not self.web_api.send_get_archived_projects_request(
                api_data=api_data):
            print_line('There were errors in obtaining archived projects.')
            return False

        api_data['project_id'] = None
        api_data['project_url'] = None
        my_archived_project = dict()

        # Verify archived projects

        for archive_project in api_data['archive_projects']:
            if api_data['project'] == archive_project['name']:
                api_data['project_id'] = archive_project['_id']
                api_data['project_url'] = archive_project['url']
                my_archived_project = archive_project
                break

        if api_data['project_id'] is None:
            print_line("Not such project {0} in archive.".format(
                api_data['project']))
            return False

        if my_archived_project['platform_id']['name'] != api_data['platform']:
            print_line("Defined project {0} not in defined platform "
                       "{1}.".format(api_data['project'],
                                     api_data['platform']))
            return False

        # Sent Restore request

        return self.web_api.send_restore_project_request(api_data=api_data)

    @staticmethod
    def get_my_platforms(api_data):
        # type: (dict) -> list
        """
        Get platforms names as list.
        :param api_data: api data set
        :return: result
        """

        # Check organization and platforms

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        # Get Platform name

        platforms = []
        for platform in api_data['organization']['platforms']:
            platforms.append(platform['name'])

        return platforms

    def get_my_projects(self, api_data):
        # type: (dict) -> list
        """
        Get projects names as list.
        :param api_data: api data set
        :return: result
        """

        # Check organization and platforms

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        # Get Platform number

        platform_number = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if platform_number == -1:
            return []

        # Get Projects

        projects = []

        for project in api_data['organization']['platforms'][platform_number][
                'projects']:
            projects.append(project['name'])

        return projects