Exemple #1
0
    def _install_dependencies(self):
        """Install missing dependencies"""
        for dep_t, dep_l in self.dependencies.items():
            if not dep_l:
                continue
            pkg_mgr = self.get_package_manager(dep_t)
            pkg_mgr.works()
            to_resolve = []
            for dep in dep_l:
                if not pkg_mgr.is_pkg_installed(dep):
                    to_resolve.append(dep)
            if not to_resolve:
                # nothing to install, let's move on
                continue
            to_install = pkg_mgr.resolve(*to_resolve)
            confirm = self._ask_to_confirm(pkg_mgr, *to_install)
            if not confirm:
                msg = 'List of packages denied by user, exiting.'
                logger.error(msg)
                raise exceptions.DependencyException(msg)

            type(self).install_lock = True
            logger.info('Installing dependencies, sit back and relax ...')
            installed = pkg_mgr.install(*to_install)
            type(self).install_lock = False

            if not installed:
                msg = 'Failed to install dependencies, exiting.'
                logger.error(msg)
                raise exceptions.DependencyException(msg)
            else:
                logger.info('Successfully installed dependencies!')
Exemple #2
0
    def run(self, **parsed_args):
        """Runs all errors, dependencies and run methods of all *Assistant objects in self.path.
        Raises:
            devassistant.exceptions.ExecutionException with a cause if something goes wrong
        """
        error = None
        # run 'pre_run', 'logging', 'dependencies' and 'run'
        try: # serve as a central place for error logging
            self._logging(parsed_args)
            if not 'deps_only' in parsed_args:
                self._run_path_run('pre', parsed_args)
            self._run_path_dependencies(parsed_args)
            if not 'deps_only' in parsed_args:
                self._run_path_run('', parsed_args)
        except exceptions.ExecutionException as e:
            if not getattr(e, 'already_logged', False):
                # this is here primarily because of log_ command, that logs the message itself
                logger.error(utils.u(e))
            error = e

        # in any case, run post_run
        try: # serve as a central place for error logging
            self._run_path_run('post', parsed_args)
        except exceptions.ExecutionException as e:
            if not getattr(e, 'already_logged', False):
                # this is here primarily because of log_ command, that logs the message itself
                logger.error(utils.u(e))
            error = e

        if error: raise error
Exemple #3
0
 def run(self):
     if os.path.isfile(self.kwargs['package']):
         old_level = logger.getEffectiveLevel()
         logger.setLevel(logging.ERROR)
         try:
             d = dapi.Dap(self.kwargs['package'])
             if not dapi.DapChecker.check(d):
                 raise exceptions.ExecutionException(
                     'This DAP is not valid, info can\'t be displayed.')
         finally:
             logger.setLevel(old_level)
         logger.infolines(
             dapicli.format_local_dap(d,
                                      full=self.kwargs.get('full', False)))
     elif self.kwargs.get('installed'):
         try:
             logger.infolines(
                 dapicli.format_installed_dap(self.kwargs['package'],
                                              full=self.kwargs.get(
                                                  'full', False)))
         except exceptions.DapiError as e:
             logger.error(utils.exc_as_decoded_string(e))
             raise exceptions.ExecutionException(
                 utils.exc_as_decoded_string(e))
     else:
         try:
             logger.infolines(
                 dapicli.format_dap_from_dapi(self.kwargs['package'],
                                              full=self.kwargs.get(
                                                  'full', False)))
         except exceptions.DapiError as e:
             logger.error(utils.exc_as_decoded_string(e))
             raise exceptions.ExecutionException(
                 utils.exc_as_decoded_string(e))
Exemple #4
0
    def resolve(cls, *args):
        # TODO: we may need to rewrite this for e.g. suse, which
        # is rpm based, but doesn't use yum; same for install()/is_available()/can_operate()
        logger.info('Resolving RPM dependencies ...')
        import yum
        y = yum.YumBase()
        y.setCacheDir(tempfile.mkdtemp())
        for pkg in args:
            if pkg.startswith('@'):
                y.selectGroup(pkg[1:])
            else:
                try:
                    y.install(y.returnPackageByDep(pkg))
                except yum.Errors.YumBaseError:
                    msg = 'Package not found: {pkg}'.format(pkg=pkg)
                    logger.error(msg)
                    raise exceptions.DependencyException(msg)
        y.resolveDeps()
        logger.debug('Installing/Updating:')
        to_install = []
        for pkg in y.tsInfo.getMembers():
            to_install.append(pkg.po.ui_envra)
            logger.debug(pkg.po.ui_envra)

        return to_install
Exemple #5
0
 def _get_github_user(cls, login, token, **kwargs):
     if not cls._user:
         try:
             # try logging with token
             gh = cls._gh_module.Github(login_or_token=token)
             cls._user = gh.get_user()
             # try if the authentication was successful
             cls._user.login
         except cls._gh_module.GithubException:
             # if the token was set, it was wrong, so make sure it's reset
             cls._token = None
             # login with username/password
             password = DialogHelper.ask_for_password(
                 prompt='Github Password for {username}:'.format(
                     username=login))
             gh = cls._gh_module.Github(login_or_token=login,
                                        password=password)
             cls._user = gh.get_user()
             try:
                 cls._user.login
                 cls._github_create_auth(
                     **kwargs)  # create auth for future use
             except cls._gh_module.GithubException as e:
                 msg = 'Wrong username or password\nGitHub exception: {0}'.format(
                     e)
                 logger.error(msg)
                 # reset cls._user to None, so that we don't use it if calling this multiple times
                 cls._user = None
                 raise exceptions.RunException(msg)
     return cls._user
    def resolve(cls, *args):
        # TODO: we may need to rewrite this for e.g. suse, which
        # is rpm based, but doesn't use yum; same for install()/is_available()/can_operate()
        logger.info('Resolving RPM dependencies ...')
        import yum
        y = yum.YumBase()
        y.setCacheDir(tempfile.mkdtemp())
        for pkg in args:
            if pkg.startswith('@'):
                y.selectGroup(pkg[1:])
            else:
                try:
                    y.install(y.returnPackageByDep(pkg))
                except yum.Errors.YumBaseError:
                    msg = 'Package not found: {pkg}'.format(pkg=pkg)
                    logger.error(msg)
                    raise exceptions.DependencyException(msg)
        y.resolveDeps()
        logger.debug('Installing/Updating:')
        to_install = []
        for pkg in y.tsInfo.getMembers():
            to_install.append(pkg.po.ui_envra)
            logger.debug(pkg.po.ui_envra)

        return to_install
Exemple #7
0
    def _github_create_repo(cls, **kwargs):
        """Create repo on GitHub.

        If repository already exists then RunException will be raised.

        Raises:
            devassistant.exceptions.RunException on error
        """
        username = cls._github_username(**kwargs)
        reponame = cls._github_reponame(**kwargs)
        password = getpass.getpass(prompt='GitHub password:'******'Repository already exists on GitHub'
                logger.error(msg)
                raise exceptions.RunException(msg)
            else:
                new_repo = user.create_repo(reponame)
                logger.info('Your new repository: {0}'.format(new_repo.html_url))
        except github.GithubException as e:
            msg = 'GitHub error: {0}'.format(e)
            logger.log(msg)
            raise exceptions.RunException(msg)
Exemple #8
0
 def run(self):
     pkgs = exs = []
     try:
         pkgs = self.kwargs['package']
     except KeyError:
         pkgs = dapicli.get_installed_daps()
         if pkgs:
             logger.info('Updating all DAP packages ...')
         else:
             logger.info(
                 'No installed DAP packages found, nothing to update.')
     for pkg in pkgs:
         logger.info('Updating DAP {pkg} ...'.format(pkg=pkg))
         try:
             updated = dapicli.install_dap(
                 pkg,
                 update=True,
                 update_allpaths=self.kwargs['allpaths'],
                 force=self.kwargs['force'])
             if updated:
                 logger.info(
                     'DAP {pkg} successfully updated.'.format(pkg=pkg))
             else:
                 logger.info(
                     'DAP {pkg} is already up to date.'.format(pkg=pkg))
         except exceptions.DapiError as e:
             exs.append(utils.exc_as_decoded_string(e))
             logger.error(utils.exc_as_decoded_string(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #9
0
    def run(self, **parsed_args):
        """Runs all errors, dependencies and run methods of all *Assistant objects in self.path.
        Raises:
            devassistant.exceptions.ExecutionException with a cause if something goes wrong
        """
        error = None
        # run 'pre_run', 'logging', 'dependencies' and 'run'
        try: # serve as a central place for error logging
            self._logging(parsed_args)
            if not 'deps_only' in parsed_args:
                self._run_path_run('pre', parsed_args)
            self._run_path_dependencies(parsed_args)
            if not 'deps_only' in parsed_args:
                self._run_path_run('', parsed_args)
        except exceptions.ExecutionException as e:
            if not getattr(e, 'already_logged', False):
                # this is here primarily because of log_ command, that logs the message itself
                logger.error(six.text_type(e))
                if isinstance(e, exceptions.YamlError): # if there's a yaml error, just shut down
                    raise e
            error = e

        # in any case, run post_run
        try: # serve as a central place for error logging
            self._run_path_run('post', parsed_args)
        except exceptions.ExecutionException as e:
            if not getattr(e, 'already_logged', False):
                # this is here primarily because of log_ command, that logs the message itself
                logger.error(six.text_type(e))
            error = e

        if error: raise error
Exemple #10
0
    def _github_create_repo(cls, **kwargs):
        """Create repo on GitHub.

        If repository already exists then RunException will be raised.

        Raises:
            devassistant.exceptions.RunException on error
        """
        username = cls._github_username(**kwargs)
        reponame = cls._github_reponame(**kwargs)
        password = getpass.getpass(prompt='GitHub password:'******'Repository already exists on GitHub'
                logger.error(msg)
                raise exceptions.RunException(msg)
            else:
                new_repo = user.create_repo(reponame)
                logger.info('Your new repository: {0}'.format(
                    new_repo.html_url))
        except github.GithubException as e:
            msg = 'GitHub error: {0}'.format(e)
            logger.log(msg)
            raise exceptions.RunException(msg)
Exemple #11
0
def dependencies_section(section, kwargs, runner=None):
    # "deps" is the same structure as gets returned by "dependencies" method
    skip_else = False
    deps = []

    for i, dep in enumerate(section):
        if getattr(runner, 'stop_flag', False):
            break
        for dep_type, dep_list in dep.items():
            # rpm dependencies (can't handle anything else yet)
            # we don't allow general commands, only "call"/"use" command here
            if dep_type in ['call', 'use']:
                deps.extend(Command(dep_type, dep_list, kwargs).run())
            elif dep_type in package_managers.managers.keys(): # handle known types of deps the same, just by appending to "deps" list
                fmtd = list(map(lambda dep: format_str(dep, kwargs), dep_list))
                deps.append({dep_type: fmtd})
            elif dep_type.startswith('if'):
                possible_else = None
                if len(section) > i + 1: # do we have "else" clause?
                    possible_else = list(section[i + 1].items())[0]
                _, skip_else, to_run = get_section_from_condition((dep_type, dep_list), possible_else, kwargs)
                if to_run:
                    deps.extend(dependencies_section(to_run, kwargs, runner=runner))
            elif dep_type == 'else':
                # else on its own means error, otherwise execute it
                if not skip_else:
                    msg = 'Yaml error: encountered "else" with no associated "if", skipping.'
                    logger.error(msg)
                    raise exceptions.YamlSyntaxError(msg)
                skip_else = False
            else:
                logger.warning('Unknown dependency type {0}, skipping.'.format(dep_type))

    return deps
Exemple #12
0
 def _get_github_user(cls, login, token, **kwargs):
     if not cls._user:
         try:
             # try logging with token
             gh = cls._gh_module.Github(login_or_token=token)
             cls._user = gh.get_user()
             # try if the authentication was successful
             cls._user.login
         except cls._gh_module.GithubException:
             # if the token was set, it was wrong, so make sure it's reset
             cls._token = None
             # login with username/password
             password = DialogHelper.ask_for_password(
                     prompt='Github Password for {username}:'.format(username=login))
             gh = cls._gh_module.Github(login_or_token=login, password=password)
             cls._user = gh.get_user()
             try:
                 cls._user.login
                 cls._github_create_auth(**kwargs) # create auth for future use
             except cls._gh_module.GithubException as e:
                 msg = 'Wrong username or password\nGitHub exception: {0}'.format(e)
                 logger.error(msg)
                 # reset cls._user to None, so that we don't use it if calling this multiple times
                 cls._user = None
                 raise exceptions.RunException(msg)
     return cls._user
Exemple #13
0
 def run(self):
     pkgs = exs = []
     try:
         pkgs = self.kwargs['package']
     except KeyError:
         pkgs = dapicli.get_installed_daps()
         if pkgs:
             logger.info('Updating all DAP packages ...')
         else:
             logger.info('No installed DAP packages found, nothing to update.')
     for pkg in pkgs:
         logger.info('Updating DAP {pkg} ...'.format(pkg=pkg))
         try:
             updated = dapicli.install_dap(pkg,
                                           update=True,
                                           update_allpaths=self.kwargs['allpaths'],
                                           force=self.kwargs['force'])
             if updated:
                 logger.info('DAP {pkg} successfully updated.'.format(pkg=pkg))
             else:
                 logger.info('DAP {pkg} is already up to date.'.format(pkg=pkg))
         except exceptions.DapiError as e:
             exs.append(utils.exc_as_decoded_string(e))
             logger.error(utils.exc_as_decoded_string(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #14
0
 def run(self):
     if os.path.isfile(self.kwargs['package']):
         old_level = logger.getEffectiveLevel()
         logger.setLevel(logging.ERROR)
         try:
             d = dapi.Dap(self.kwargs['package'])
             if not dapi.DapChecker.check(d):
                 raise exceptions.ExecutionException(
                     'This DAP is not valid, info can\'t be displayed.')
         finally:
             logger.setLevel(old_level)
         dapicli.print_local_dap(d, full=self.kwargs.get('full', False))
     elif self.kwargs.get('installed'):
         try:
             dapicli.print_installed_dap(self.kwargs['package'],
                                         full=self.kwargs.get('full', False))
         except exceptions.DapiError as e:
             logger.error(utils.exc_as_decoded_string(e))
             raise exceptions.ExecutionException(utils.exc_as_decoded_string(e))
     else:
         try:
             dapicli.print_dap_from_dapi(self.kwargs['package'],
                                         full=self.kwargs.get('full', False))
         except exceptions.DapiError as e:
             logger.error(utils.exc_as_decoded_string(e))
             raise exceptions.ExecutionException(utils.exc_as_decoded_string(e))
    def _install_dependencies(self):
        """Install missing dependencies"""
        for dep_t, dep_l in self.dependencies.items():
            if not dep_l:
                continue
            pkg_mgr = self.get_package_manager(dep_t)
            pkg_mgr.works()
            to_resolve = []
            for dep in dep_l:
                if not pkg_mgr.is_pkg_installed(dep):
                    to_resolve.append(dep)
            if not to_resolve:
                # nothing to install, let's move on
                continue
            to_install = pkg_mgr.resolve(*to_resolve)
            confirm = self._ask_to_confirm(pkg_mgr, *to_install)
            if not confirm:
                msg = 'List of packages denied by user, exiting.'
                logger.error(msg)
                raise exceptions.DependencyException(msg)

            type(self).install_lock = True
            logger.info('Installing dependencies, sit back and relax ...')
            installed = pkg_mgr.install(*to_install)
            type(self).install_lock = False

            if not installed:
                msg = 'Failed to install dependencies, exiting.'
                logger.error(msg)
                raise exceptions.DependencyException(msg)
            else:
                logger.info('Successfully installed dependencies!')
Exemple #16
0
 def run(cls, **kwargs):
     try:
         dapicli.print_search(' '.join(kwargs['query']), kwargs['page'])
     except Exception as e:
         raise e
         logger.error(str(e))
         raise exceptions.ExecutionException(str(e))
 def works(cls):
     try:
         import yum
         return True
     except ImportError:
         msg = 'Package manager for "{0}" not operational: {1}'.format(dep_t, e)
         logger.error(msg)
         raise exceptions.PackageManagerNotOperational(msg)
 def works(cls):
     try:
         ClHelper.run_command('npm')
         return True
     except exceptions.ClException as e:
         msg = 'Package manager for "{0}" not operational: {1}'.format(dep_t, e)
         logger.error(msg)
         raise exceptions.PackageManagerNotOperational(msg)
Exemple #19
0
 def works(cls):
     try:
         ClHelper.run_command('npm')
         return True
     except exceptions.ClException as e:
         msg = 'Package manager for "{0}" not operational: {1}'.format(
             dep_t, e)
         logger.error(msg)
         raise exceptions.PackageManagerNotOperational(msg)
Exemple #20
0
 def works(cls):
     try:
         import yum
         return True
     except ImportError:
         msg = 'Package manager for "{0}" not operational: {1}'.format(
             dep_t, e)
         logger.error(msg)
         raise exceptions.PackageManagerNotOperational(msg)
Exemple #21
0
 def run(self):
     if [self.kwargs[k] for k in ['installed', 'remote', 'available']].count(True) > 1:
         logger.error('Only one of --installed, --remote or --available '
                      'can be used simultaneously')
         return
     if self.kwargs['remote'] or self.kwargs['available']:
         dapicli.print_daps(simple=self.kwargs['simple'],
                            skip_installed=self.kwargs['available'])
     else:
         dapicli.print_installed_dap_list(simple=self.kwargs['simple'])
 def get_package_manager(self, dep_t):
     """Choose proper package manager and return it."""
     # one package manager can possibly handle multiple dep types,
     # so we can't just do manager.shortcut == dep_t
     for manager in managers.values():
         if manager.match(dep_t):
             return manager
     err = "Package manager for dependency type {0} was not found".format(dep_t)
     logger.error(err)
     raise exceptions.PackageManagerUnknown(err)
Exemple #23
0
 def get_package_manager(self, dep_t):
     """Choose proper package manager and return it."""
     # one package manager can possibly handle multiple dep types,
     # so we can't just do manager.shortcut == dep_t
     for manager in managers.values():
         if manager.match(dep_t):
             return manager
     err = "Package manager for dependency type {0} was not found".format(
         dep_t)
     logger.error(err)
     raise exceptions.PackageManagerUnknown(err)
Exemple #24
0
 def run(cls, **kwargs):
     exs = []
     for pkg in kwargs['package']:
         logger.info('Uninstalling {pkg}...'.format(pkg=pkg))
         try:
             done = dapicli.uninstall_dap(pkg, confirm=kwargs['force'])
             if done:
                 logger.info('{pkg} successfully uninstalled'.format(pkg=pkg))
         except Exception as e:
             exs.append(str(e))
             logger.error(str(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #25
0
    def run(cls, comm_type, comm, **kwargs):
        fg = False
        i = False
        if 'f' in comm_type:
            fg = True
        if 'i' in comm_type:
            i = True
        try:
            result = ClHelper.run_command(comm, fg, i)
        except plumbum.ProcessExecutionError as e:
            logger.error(e)
            raise exceptions.RunException(e)

        return result.strip() if hasattr(result, 'strip') else result
Exemple #26
0
    def _dot_devassistant_read(cls, comm, **kwargs):
        """Don't use this directly from assistants (yet), raises uncaught exception
        if anything goes wrong."""
        dot_devassistant = os.path.join(os.path.abspath(os.path.expanduser(comm)), '.devassistant')
        try:
            with open(dot_devassistant, 'r') as stream:
                result = yaml.load(stream)
        except IOError as e:
            msg = 'Couldn\'t find properly formatted .devassistant file: {0}'.format(e)
            logger.error(msg)
            raise exceptions.RunException(msg)

        result['name'] = os.path.basename(os.path.abspath(os.path.expanduser(comm)))
        return result
Exemple #27
0
 def run(self):
     if [self.kwargs[k]
             for k in ['installed', 'remote', 'available']].count(True) > 1:
         logger.error('Only one of --installed, --remote or --available '
                      'can be used simultaneously')
         return
     if self.kwargs['remote'] or self.kwargs['available']:
         logger.infolines(
             dapicli.format_daps(simple=self.kwargs['simple'],
                                 skip_installed=self.kwargs['available']))
     else:
         logger.infolines(
             dapicli.format_installed_dap_list(
                 simple=self.kwargs['simple']))
Exemple #28
0
    def run(cls, comm_type, comm, **kwargs):
        fg = False
        i = False
        if 'f' in comm_type:
            fg = True
        if 'i' in comm_type:
            i = True
        try:
            result = ClHelper.run_command(comm, fg, i)
        except plumbum.ProcessExecutionError as e:
            logger.error(e)
            raise exceptions.RunException(e)

        return result.strip() if hasattr(result, 'strip') else result
Exemple #29
0
    def run(self):
        newargs = {}
        newargs['q'] = ' '.join(self.kwargs['query'])
        newargs['noassistants'] = self.kwargs['noassistants']
        newargs['unstable'] = self.kwargs['unstable']
        newargs['notactive'] = self.kwargs['deactivated']
        newargs['minimal_rank'] = self.kwargs['minrank']
        newargs['minimal_rank_count'] = self.kwargs['mincount']
        if not self.kwargs['allplatforms']:
            newargs['platform'] = utils.get_distro_name()

        try:
            logger.infolines(dapicli.format_search(**newargs))
        except exceptions.DapiError as e:
            logger.error(utils.exc_as_decoded_string(e))
            raise exceptions.ExecutionException(utils.exc_as_decoded_string(e))
Exemple #30
0
    def run(self):
        newargs = {}
        newargs['q'] = ' '.join(self.kwargs['query'])
        newargs['noassistants'] = self.kwargs['noassistants']
        newargs['unstable'] = self.kwargs['unstable']
        newargs['notactive'] = self.kwargs['deactivated']
        newargs['minimal_rank'] = self.kwargs['minrank']
        newargs['minimal_rank_count'] = self.kwargs['mincount']
        if not self.kwargs['allplatforms']:
            newargs['platform'] = utils.get_distro_name()

        try:
            dapicli.print_search(**newargs)
        except exceptions.DapiError as e:
            logger.error(utils.exc_as_decoded_string(e))
            raise exceptions.ExecutionException(utils.exc_as_decoded_string(e))
Exemple #31
0
 def run(cls, **kwargs):
     exs = []
     for pkg in kwargs['package']:
         logger.info('Installing DAP {pkg} ...'.format(pkg=pkg))
         if os.path.isfile(pkg):
             method = dapicli.install_dap_from_path
         else:
             method = dapicli.install_dap
         try:
             pkgs = method(pkg, force=kwargs['force'])
             logger.info('Successfully installed DAPs {pkgs}'.format(pkgs=' '.join(pkgs)))
         except Exception as e:
             exs.append(str(e))
             logger.error(str(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #32
0
 def run(cls, **kwargs):
     error = False
     old_level = logger.getEffectiveLevel()
     for pkg in kwargs['package']:
         try:
             if kwargs['nowarnings']:
                 logger.setLevel(logging.ERROR)
             d = dapi.Dap(pkg)
             if not d.check(network=kwargs['network']):
                 error = True
         except (exceptions.DapFileError, exceptions.DapMetaError) as e:
             logger.error(str(e))
             error = True
     logger.setLevel(old_level)
     if error:
         raise exceptions.ExecutionException('One or more packages are not sane')
Exemple #33
0
    def _github_create_repo(cls, **kwargs):
        """Create repo on GitHub.

        If repository already exists then RunException will be raised.

        Raises:
            devassistant.exceptions.RunException on error
        """
        reponame = cls._github_reponame(**kwargs)

        if reponame in map(lambda x: x.name, cls._user.get_repos()):
            msg = 'Repository already exists on GitHub'
            logger.error(msg)
            raise exceptions.RunException(msg)
        else:
            new_repo = cls._user.create_repo(reponame)
            logger.info('Your new repository: {0}'.format(new_repo.html_url))
Exemple #34
0
 def run(cls, **kwargs):
     import os
     exs = []
     for pkg in kwargs['package']:
         logger.info('Installing {pkg}...'.format(pkg=pkg))
         if os.path.isfile(pkg):
             method = dapicli.install_dap_from_path
         else:
             method = dapicli.install_dap
         try:
             method(pkg)
             logger.info('{pkg} successfully installed'.format(pkg=pkg))
         except Exception as e:
             exs.append(str(e))
             logger.error(str(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #35
0
    def _dot_devassistant_read(cls, comm, **kwargs):
        """Don't use this directly from assistants (yet), raises uncaught exception
        if anything goes wrong."""
        dot_devassistant = os.path.join(
            os.path.abspath(os.path.expanduser(comm)), '.devassistant')
        try:
            with open(dot_devassistant, 'r') as stream:
                result = yaml.load(stream)
        except IOError as e:
            msg = 'Couldn\'t find properly formatted .devassistant file: {0}'.format(
                e)
            logger.error(msg)
            raise exceptions.RunException(msg)

        result['name'] = os.path.basename(
            os.path.abspath(os.path.expanduser(comm)))
        return result
Exemple #36
0
 def run(self):
     error = False
     old_level = logger.getEffectiveLevel()
     for pkg in self.kwargs['package']:
         try:
             if self.kwargs['nowarnings']:
                 logger.setLevel(logging.ERROR)
             d = dapi.Dap(pkg)
             if not dapi.DapChecker.check(d, network=self.kwargs['network'],
                                          yamls=not self.kwargs['noyamlcheck']):
                 error = True
         except (exceptions.DapFileError, exceptions.DapMetaError) as e:
             logger.error(utils.exc_as_decoded_string(e))
             error = True
     logger.setLevel(old_level)
     if error:
         raise exceptions.ExecutionException('One or more packages are not sane')
Exemple #37
0
    def _install_dependencies(self, ui, debug):
        """Install missing dependencies"""
        for dep_t, dep_l in self.dependencies:
            if not dep_l:
                continue
            pkg_mgr = self.get_package_manager(dep_t)
            pkg_mgr.works()
            to_resolve = []
            for dep in dep_l:
                if not pkg_mgr.is_pkg_installed(dep):
                    to_resolve.append(dep)
            if not to_resolve:
                # nothing to install, let's move on
                continue
            to_install = pkg_mgr.resolve(*to_resolve)
            confirm = self._ask_to_confirm(ui, pkg_mgr, *to_install)
            if not confirm:
                msg = 'List of packages denied by user, exiting.'
                raise exceptions.DependencyException(msg)

            type(self).install_lock = True
            # TODO: we should do this more systematically (send signal to cl/gui?)
            logger.info('Installing dependencies, sit back and relax ...',
                        extra={'event_type': 'dep_installation_start'})
            if ui == 'cli' and not debug:  # TODO: maybe let every manager to decide when to start
                event = threading.Event()
                t = EndlessProgressThread(event)
                t.start()
            installed = pkg_mgr.install(*to_install)
            if ui == 'cli' and not debug:
                event.set()
                t.join()
                if installed:
                    logger.info(' Done.')
                else:
                    logger.error(' Failed.')
            type(self).install_lock = False

            log_extra = {'event_type': 'dep_installation_end'}
            if not installed:
                msg = 'Failed to install dependencies, exiting.'
                logger.error(msg, extra=log_extra)
                raise exceptions.DependencyException(msg)
            else:
                logger.info('Successfully installed dependencies!',
                            extra=log_extra)
Exemple #38
0
 def run(cls, **kwargs):
     pkgs = exs = []
     try:
         pkgs = kwargs['package']
     except KeyError:
         logger.info('Updating all packages')
         pkgs = dapicli.get_installed_daps()
     for pkg in pkgs:
         logger.info('Updating {pkg}...'.format(pkg=pkg))
         try:
             dapicli.install_dap(pkg, update=True)
             logger.info('{pkg} successfully updated'.format(pkg=pkg))
         except Exception as e:
             exs.append(str(e))
             logger.error(str(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #39
0
 def run(self):
     exs = []
     for pkg in self.kwargs['package']:
         logger.info('Installing DAP {pkg} ...'.format(pkg=pkg))
         if os.path.isfile(pkg):
             method = dapicli.install_dap_from_path
         else:
             method = dapicli.install_dap
         try:
             pkgs = method(pkg, force=self.kwargs['force'],
                           nodeps=self.kwargs['nodeps'], reinstall=self.kwargs['reinstall'])
             logger.info('Successfully installed DAPs {pkgs}'.format(pkgs=' '.join(pkgs)))
         except exceptions.DapiError as e:
             exs.append(utils.exc_as_decoded_string(e))
             logger.error(utils.exc_as_decoded_string(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
    def _install_dependencies(self, ui, debug):
        """Install missing dependencies"""
        for dep_t, dep_l in self.dependencies:
            if not dep_l:
                continue
            pkg_mgr = self.get_package_manager(dep_t)
            pkg_mgr.works()
            to_resolve = []
            for dep in dep_l:
                if not pkg_mgr.is_pkg_installed(dep):
                    to_resolve.append(dep)
            if not to_resolve:
                # nothing to install, let's move on
                continue
            to_install = pkg_mgr.resolve(*to_resolve)
            confirm = self._ask_to_confirm(ui, pkg_mgr, *to_install)
            if not confirm:
                msg = 'List of packages denied by user, exiting.'
                raise exceptions.DependencyException(msg)

            type(self).install_lock = True
            # TODO: we should do this more systematically (send signal to cl/gui?)
            logger.info('Installing dependencies, sit back and relax ...',
                        extra={'event_type': 'dep_installation_start'})
            if ui == 'cli' and not debug:  # TODO: maybe let every manager to decide when to start
                event = threading.Event()
                t = EndlessProgressThread(event)
                t.start()
            installed = pkg_mgr.install(*to_install)
            if ui == 'cli' and not debug:
                event.set()
                t.join()
                if installed:
                    logger.info(' Done.')
                else:
                    logger.error(' Failed.')
            type(self).install_lock = False

            log_extra = {'event_type': 'dep_installation_end'}
            if not installed:
                msg = 'Failed to install dependencies, exiting.'
                logger.error(msg, extra=log_extra)
                raise exceptions.DependencyException(msg)
            else:
                logger.info('Successfully installed dependencies!', extra=log_extra)
Exemple #41
0
 def run(cls, **kwargs):
     exs = []
     uninstalled = []
     for pkg in kwargs['package']:
         if pkg in uninstalled:
             logger.info('DAP {pkg} already uninstalled'.format(pkg=pkg))
             continue
         logger.info('Uninstalling DAP {pkg} ...'.format(pkg=pkg))
         try:
             done = dapicli.uninstall_dap(pkg, confirm=kwargs['force'])
             if done:
                 logger.info('DAPs {pkgs} successfully uninstalled'.format(pkgs=' '.join(done)))
                 uninstalled += done
         except Exception as e:
             exs.append(str(e))
             logger.error(str(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #42
0
    def run(cls, comm_type, comm, **kwargs):
        log_level = logging.DEBUG
        log_error = True
        if 'i' in comm_type:
            log_level = logging.INFO
        if 'n' in comm_type:
            log_error = False
        scls = []
        if '__scls__' in kwargs:
            scls = reduce(lambda x, y: x + y, kwargs['__scls__'], scls)
        try:
            result = ClHelper.run_command(comm, log_level, scls=scls)
        except exceptions.ClException as e:
            if log_error:
                logger.error(e)
            raise e

        return result.strip() if hasattr(result, 'strip') else result
Exemple #43
0
 def run(self):
     exs = []
     uninstalled = []
     for pkg in self.kwargs['package']:
         if pkg in uninstalled:
             logger.info('DAP {pkg} already uninstalled'.format(pkg=pkg))
             continue
         logger.info('Uninstalling DAP {pkg} ...'.format(pkg=pkg))
         try:
             done = dapicli.uninstall_dap(pkg, confirm=self.kwargs['force'],
                                          allpaths=self.kwargs['allpaths'])
             if done:
                 logger.info('DAPs {pkgs} successfully uninstalled'.format(pkgs=' '.join(done)))
                 uninstalled += done
         except exceptions.DapiError as e:
             exs.append(utils.exc_as_decoded_string(e))
             logger.error(utils.exc_as_decoded_string(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #44
0
 def run(self):
     error = False
     old_level = logger.getEffectiveLevel()
     for pkg in self.kwargs['package']:
         try:
             if self.kwargs['nowarnings']:
                 logger.setLevel(logging.ERROR)
             d = dapi.Dap(pkg)
             if not dapi.DapChecker.check(
                     d,
                     network=self.kwargs['network'],
                     yamls=not self.kwargs['noyamlcheck']):
                 error = True
         except (exceptions.DapFileError, exceptions.DapMetaError) as e:
             logger.error(utils.exc_as_decoded_string(e))
             error = True
     logger.setLevel(old_level)
     if error:
         raise exceptions.ExecutionException(
             'One or more packages are not sane')
Exemple #45
0
 def run(self):
     exs = []
     for pkg in self.kwargs['package']:
         logger.info('Installing DAP {pkg} ...'.format(pkg=pkg))
         if os.path.isfile(pkg):
             method = dapicli.install_dap_from_path
         else:
             method = dapicli.install_dap
         try:
             pkgs = method(pkg,
                           force=self.kwargs['force'],
                           nodeps=self.kwargs['nodeps'],
                           reinstall=self.kwargs['reinstall'],
                           __ui__=self.kwargs['__ui__'])
             logger.info('Successfully installed DAPs {pkgs}'.format(
                 pkgs=' '.join(pkgs)))
         except exceptions.DapiError as e:
             exs.append(utils.exc_as_decoded_string(e))
             logger.error(utils.exc_as_decoded_string(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #46
0
    def run(cls, comm_type, comm, **kwargs):
        log_level = logging.DEBUG
        log_error = True
        if 'i' in comm_type:
            log_level = logging.INFO
        if 'n' in comm_type:
            log_error = False
        scls = []
        if '__scls__' in kwargs:
            scls = reduce(lambda x, y: x + y, kwargs['__scls__'], scls)
        try:
            result = ClHelper.run_command(comm, log_level, scls=scls)
        except exceptions.ClException as e:
            if log_error:
                try:
                    logger.error(unicode(e))
                except:
                    logger.error(e)
            raise e

        return result.strip() if hasattr(result, 'strip') else result
Exemple #47
0
 def run(self):
     exs = []
     uninstalled = []
     for pkg in self.kwargs['package']:
         if pkg in uninstalled:
             logger.info('DAP {pkg} already uninstalled'.format(pkg=pkg))
             continue
         logger.info('Uninstalling DAP {pkg} ...'.format(pkg=pkg))
         try:
             done = dapicli.uninstall_dap(pkg,
                                          confirm=self.kwargs['force'],
                                          allpaths=self.kwargs['allpaths'],
                                          __ui__=self.kwargs['__ui__'])
             if done:
                 logger.info('DAPs {pkgs} successfully uninstalled'.format(
                     pkgs=' '.join(done)))
                 uninstalled += done
         except exceptions.DapiError as e:
             exs.append(utils.exc_as_decoded_string(e))
             logger.error(utils.exc_as_decoded_string(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #48
0
 def run(self):
     dap = self.kwargs['dap']
     doc = self.kwargs.get('doc', None)
     docdir = utils.find_file_in_load_dirs(os.path.join('doc', dap))
     all_docs = []
     if docdir is not None:
         all_docs = self._get_doc_files(docdir)
     if not all_docs:
         logger.info('DAP {0} has no documentation.'.format(dap))
     elif doc is not None:
         doc_fullpath = os.path.join(docdir, doc)
         if doc_fullpath in all_docs:
             self._show_doc(doc_fullpath)
         else:
             msg = 'DAP {0} has no document "{1}".'.format(dap, doc)
             logger.error(msg)
             raise exceptions.ExecutionException(msg)
     else:
         logger.info('DAP {0} has these docs:'.format(dap))
         for d in all_docs:
             logger.info(d[len(docdir):].strip(os.path.sep))
         logger.info('Use "da doc {0} <DOC>" to see a specific document'.format(dap))
Exemple #49
0
def dependencies_section(section, kwargs, runner=None):
    # "deps" is the same structure as gets returned by "dependencies" method
    skip_else = False
    deps = []

    for i, dep in enumerate(section):
        if getattr(runner, 'stop_flag', False):
            break
        for dep_type, dep_list in dep.items():
            # rpm dependencies (can't handle anything else yet)
            # we don't allow general commands, only "call"/"use" command here
            if dep_type in ['call', 'use']:
                deps.extend(Command(dep_type, dep_list, kwargs).run())
            # handle known types of deps the same, just by appending to "deps" list
            elif dep_type in package_managers.managers.keys():
                fmtd = list(map(lambda dep: format_str(dep, kwargs), dep_list))
                deps.append({dep_type: fmtd})
            elif dep_type.startswith('if'):
                possible_else = None
                if len(section) > i + 1:  # do we have "else" clause?
                    possible_else = list(section[i + 1].items())[0]
                _, skip_else, to_run = get_section_from_condition(
                    (dep_type, dep_list), possible_else, kwargs)
                if to_run:
                    deps.extend(
                        dependencies_section(to_run, kwargs, runner=runner))
            elif dep_type == 'else':
                # else on its own means error, otherwise execute it
                if not skip_else:
                    msg = 'Yaml error: encountered "else" with no associated "if", skipping.'
                    logger.error(msg)
                    raise exceptions.YamlSyntaxError(msg)
                skip_else = False
            else:
                logger.warning(
                    'Unknown dependency type {0}, skipping.'.format(dep_type))

    return deps
Exemple #50
0
 def run(cls, **kwargs):
     pkgs = exs = []
     try:
         pkgs = kwargs['package']
     except KeyError:
         pkgs = dapicli.get_installed_daps()
         if pkgs:
             logger.info('Updating all DAP packages ...')
         else:
             logger.info('No installed DAP packages found, nothing to update.')
     for pkg in pkgs:
         logger.info('Updating DAP {pkg} ...'.format(pkg=pkg))
         try:
             updated = dapicli.install_dap(pkg, update=True, force=kwargs['force'])
             if updated:
                 logger.info('DAP {pkg} successfully updated.'.format(pkg=pkg))
             else:
                 logger.info('DAP {pkg} is already up to date.'.format(pkg=pkg))
         except Exception as e:
             exs.append(str(e))
             logger.error(str(e))
     if exs:
         raise exceptions.ExecutionException('; '.join(exs))
Exemple #51
0
 def run(self):
     dap = self.kwargs['dap']
     doc = self.kwargs.get('doc', None)
     docdir = utils.find_file_in_load_dirs(os.path.join('doc', dap))
     all_docs = []
     if docdir is not None:
         all_docs = self._get_doc_files(docdir)
     if not all_docs:
         logger.info('DAP {0} has no documentation.'.format(dap))
     elif doc is not None:
         doc_fullpath = os.path.join(docdir, doc)
         if doc_fullpath in all_docs:
             self._show_doc(doc_fullpath)
         else:
             msg = 'DAP {0} has no document "{1}".'.format(dap, doc)
             logger.error(msg)
             raise exceptions.ExecutionException(msg)
     else:
         logger.info('DAP {0} has these docs:'.format(dap))
         for d in all_docs:
             logger.info(d[len(docdir):].strip(os.path.sep))
         logger.info(
             'Use "da doc {0} <DOC>" to see a specific document'.format(
                 dap))
Exemple #52
0
    def _github_create_repo(cls, **kwargs):
        """Create repo on GitHub.

        If repository already exists then RunException will be raised.

        Raises:
            devassistant.exceptions.RunException on error
        """
        reponame = cls._github_reponame(**kwargs)

        if reponame in map(lambda x: x.name, cls._user.get_repos()):
            msg = 'Repository already exists on GitHub'
            logger.error(msg)
            raise exceptions.RunException(msg)
        else:
            try:
                new_repo = cls._user.create_repo(reponame)
            except cls._gh_module.GithubException:
                msg = 'Failed to create GitHub repo. This sometime happens when you delete '
                msg += 'a repo and then you want to create the same one immediately. Wait '
                msg += 'for few minutes and then try again.'
                logger.error(msg)
                raise exceptions.RunException(msg)
            logger.info('Your new repository: {0}'.format(new_repo.html_url))
Exemple #53
0
    def _run_one_section(self, section, kwargs):
        skip_else = False

        for i, command_dict in enumerate(section):
            if self.stop_flag:
                break
            for comm_type, comm in command_dict.items():
                if comm_type.startswith('call'):
                    # calling workflow:
                    # 1) get proper run section (either from self or from snippet)
                    # 2) if running snippet, add its files to kwargs['__files__']
                    # 3) actually run
                    # 4) if running snippet, pop its files from kwargs['__files__']
                    sect = self._get_section_from_call(comm, 'run')

                    if sect is None:
                        logger.warning(
                            'Couldn\'t find section to run: {0}.'.format(comm))
                        continue

                    if self._is_snippet_call(comm, **kwargs):
                        # we're calling a snippet => add files and template_dir to kwargs
                        snippet = yaml_snippet_loader.YamlSnippetLoader.get_snippet_by_name(
                            comm.split('.')[0])

                        if '__files__' not in kwargs:
                            kwargs['__files__'] = []
                            kwargs['__template_dir__'] = []
                        kwargs['__files__'].append(snippet.get_files_section())
                        kwargs['__template_dir__'].append(
                            snippet.get_template_dir())

                    self._run_one_section(sect, copy.deepcopy(kwargs))

                    if self._is_snippet_call(comm, **kwargs):
                        kwargs['__files__'].pop()
                        kwargs['__template_dir__'].pop()
                elif comm_type.startswith('$'):
                    # intentionally pass kwargs as dict, not as keywords
                    try:
                        self._assign_variable(comm_type, comm, kwargs)
                    except exceptions.YamlSyntaxError as e:
                        logger.error(e)
                        raise e
                elif comm_type.startswith('if'):
                    possible_else = None
                    if len(section) > i + 1:  # do we have "else" clause?
                        possible_else = list(section[i + 1].items())[0]
                    _, skip_else, to_run = self._get_section_from_condition(
                        (comm_type, comm), possible_else, **kwargs)
                    if to_run:
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(to_run, kwargs)
                elif comm_type == 'else':
                    if not skip_else:
                        logger.warning(
                            'Yaml error: encountered "else" with no associated "if", skipping.'
                        )
                    skip_else = False
                elif comm_type.startswith('for'):
                    # syntax: "for $i in $x: <section> or "for $i in cl_command: <section>"
                    try:
                        control_var, expression = self._parse_for(comm_type)
                    except exceptions.YamlSyntaxError as e:
                        logger.error(e)
                        raise e
                    try:
                        eval_expression = evaluate_expression()
                    except exceptions.YamlSyntaxError as e:
                        logger.log(e)
                        raise e

                    for i in eval_expression:
                        kwargs[control_var] = i
                        self._run_one_section(comm, kwargs)
                elif comm_type.startswith('scl'):
                    if '__scls__' not in kwargs:
                        kwargs['__scls__'] = []
                    # list of lists of scl names
                    kwargs['__scls__'].append(comm_type.split()[1:])
                    self._run_one_section(comm, kwargs)
                    kwargs['__scls__'].pop()
                else:
                    files = kwargs['__files__'][-1] if kwargs.get(
                        '__files__', None) else self._files
                    template_dir = kwargs['__template_dir__'][-1] if kwargs.get(
                        '__template_dir__', None) else self.template_dir
                    run_command(
                        comm_type,
                        CommandFormatter.format(comm_type, comm, template_dir,
                                                files, **kwargs), **kwargs)
Exemple #54
0
    def _log_if_not_logged(self, err):
        if not getattr(err, 'already_logged', False):
            # this is here primarily because of log_ command, that logs the message itself
            logger.error(utils.exc_as_decoded_string(err))

        return err