Exemple #1
0
    def _run_path_dependencies(self):
        """Runs *Assistant.dependencies methods.
        Raises:
            devassistant.exceptions.DependencyException with a cause if something goes wrong
        """
        deps = []

        for a in self.path:
            if 'dependencies' in vars(a.__class__) or isinstance(a, yaml_assistant.YamlAssistant):
                deps.extend(a.dependencies(**self.parsed_args))

        run_command('dependencies_from_struct', deps)
Exemple #2
0
    def _run_one_section(self, section, kwargs):
        execute_else = False

        for i, command_dict in enumerate(section):
            for comm_type, comm in command_dict.items():
                if comm_type.startswith('run'):
                    s = self._get_section_to_run(section=comm, kwargs_override=False, **kwargs)
                    # use copy of kwargs, so that local kwargs don't get modified
                    self._run_one_section(s, copy.deepcopy(kwargs))
                elif comm_type == 'snippet':
                    snippet, section_name = self._get_snippet_and_section_name(comm, **kwargs)
                    section = snippet.get_run_section(section_name) if snippet else None
                    if section:
                        # push and pop snippet's files into kwargs
                        if '__files__' in kwargs:
                            kwargs['__files__'].append(snippet.get_files_section())
                        else:
                            kwargs['__files__'] = [snippet.get_files_section()]
                        # use copy of kwargs, so that local kwargs don't get modified
                        self._run_one_section(section, copy.deepcopy(kwargs))
                        kwargs['__files__'].pop()
                    else:
                        logger.warning('Couldn\'t find run section "{0}", in snippet {1} skipping.'.format(section_name,
                                                                                                           comm.split('(')[0]))
                elif comm_type.startswith('$'):
                    # intentionally pass kwargs as dict, not as keywords
                    self._assign_variable(comm_type, comm, kwargs)
                elif comm_type.startswith('if'):
                    if self._evaluate(comm_type[2:].strip(), **kwargs):
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                    elif len(section) > i + 1:
                        next_section_dict = section[i + 1]
                        next_section_comm_type, next_section_comm = list(next_section_dict.items())[0]
                        if next_section_comm_type == 'else':
                            execute_else = True
                elif comm_type == 'else':
                    # else on its own means error, otherwise execute it
                    if not list(section[i - 1].items())[0][0].startswith('if'):
                        logger.warning('Yaml error: encountered "else" with no associated "if", skipping.')
                    elif execute_else:
                        execute_else = False
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                else:
                    files = kwargs['__files__'][-1] if kwargs.get('__files__', None) else self._files
                    run_command(comm_type, CommandFormatter.format(comm, self.template_dir, files, **kwargs), **kwargs)
Exemple #3
0
 def proper_kwargs(self, **kwargs):
     """Returns kwargs possibly updated with values from .devassistant
     file, when appropriate."""
     if self.role == 'modifier':
         try:
             kwargs.update(run_command('dda_r', '.', **kwargs))
         except BaseException as e:
             raise exceptions.RunException('Couldn\'t find properly formatted .devassistant in current dir: {0}'.format(e))
     return kwargs
Exemple #4
0
 def proper_kwargs(self, **kwargs):
     """Returns kwargs possibly updated with values from .devassistant
     file, when appropriate."""
     if self.role == 'modifier':
         try:
             kwargs.update(run_command('dda_r', '.', **kwargs))
         except BaseException as e:
             raise exceptions.RunException(
                 'Couldn\'t find properly formatted .devassistant in current dir: {0}'
                 .format(e))
     return kwargs
Exemple #5
0
    def _evaluate(self, expression, **kwargs):
        result = True
        invert_result = False
        expr = expression.strip()
        if expr.startswith('not '):
            invert_result = True
            expr = expr[4:]

        if expr.startswith('$'):
            var_name = self._get_var_name(expr)
            result = kwargs.get(var_name, False)
        elif expr.startswith('defined '):
            result = self._get_var_name(expr[8:]) in kwargs
        else:
            try:
                result = run_command('cl', CommandFormatter.format(expr, self.template_dir, self._files, **kwargs), **kwargs)
            except exceptions.RunException:
                result = False

        return result if not invert_result else not result
Exemple #6
0
    def _evaluate(self, expression, **kwargs):
        result = True
        invert_result = False
        expr = expression.strip()
        if expr.startswith('not '):
            invert_result = True
            expr = expr[4:]

        if expr.startswith('$'):
            var_name = self._get_var_name(expr)
            result = kwargs.get(var_name, False)
        elif expr.startswith('defined '):
            result = self._get_var_name(expr[8:]) in kwargs
        else:
            try:
                result = run_command(
                    'cl',
                    CommandFormatter.format(expr, self.template_dir,
                                            self._files, **kwargs), **kwargs)
            except exceptions.RunException:
                result = False

        return result if not invert_result else not result
Exemple #7
0
    def _run_one_section(self, section, kwargs):
        execute_else = False

        for i, command_dict in enumerate(section):
            for comm_type, comm in command_dict.items():
                if comm_type.startswith('run'):
                    s = self._get_section_to_run(section=comm,
                                                 kwargs_override=False,
                                                 **kwargs)
                    # use copy of kwargs, so that local kwargs don't get modified
                    self._run_one_section(s, copy.deepcopy(kwargs))
                elif comm_type == 'snippet':
                    snippet, section_name = self._get_snippet_and_section_name(
                        comm, **kwargs)
                    section = snippet.get_run_section(
                        section_name) if snippet else None
                    if section:
                        # push and pop snippet's files into kwargs
                        if '__files__' in kwargs:
                            kwargs['__files__'].append(
                                snippet.get_files_section())
                        else:
                            kwargs['__files__'] = [snippet.get_files_section()]
                        # use copy of kwargs, so that local kwargs don't get modified
                        self._run_one_section(section, copy.deepcopy(kwargs))
                        kwargs['__files__'].pop()
                    else:
                        logger.warning(
                            'Couldn\'t find run section "{0}", in snippet {1} skipping.'
                            .format(section_name,
                                    comm.split('(')[0]))
                elif comm_type.startswith('$'):
                    # intentionally pass kwargs as dict, not as keywords
                    self._assign_variable(comm_type, comm, kwargs)
                elif comm_type.startswith('if'):
                    if self._evaluate(comm_type[2:].strip(), **kwargs):
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                    elif len(section) > i + 1:
                        next_section_dict = section[i + 1]
                        next_section_comm_type, next_section_comm = list(
                            next_section_dict.items())[0]
                        if next_section_comm_type == 'else':
                            execute_else = True
                elif comm_type == 'else':
                    # else on its own means error, otherwise execute it
                    if not list(section[i - 1].items())[0][0].startswith('if'):
                        logger.warning(
                            'Yaml error: encountered "else" with no associated "if", skipping.'
                        )
                    elif execute_else:
                        execute_else = False
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                else:
                    files = kwargs['__files__'][-1] if kwargs.get(
                        '__files__', None) else self._files
                    run_command(
                        comm_type,
                        CommandFormatter.format(comm, self.template_dir, files,
                                                **kwargs), **kwargs)