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)
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 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
def run(cls, comm_type, comm, **kwargs): if comm_type in map(lambda x: 'log_{0}'.format(x), settings.LOG_LEVELS_MAP): logger.log( logging._levelNames[settings.LOG_LEVELS_MAP[comm_type[-1]]], comm) if comm_type[-1] in 'ce': raise exceptions.RunException(comm) else: logger.warning( 'Unknown logging command {0} with message {1}'.format( comm_type, comm))
def eval_exec_section(section, kwargs, runner=None): skip_else = False retval = [False, ''] if isinstance(section, six.string_types): return evaluate_expression(section, kwargs) for i, command_dict in enumerate(section): if getattr(runner, 'stop_flag', False): break for comm_type, comm in command_dict.items(): if 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 = get_section_from_condition( (comm_type, comm), possible_else, kwargs) # run with original kwargs, so that they might be changed for code after this if to_run: retval = run_section(to_run, kwargs, runner=runner) elif comm_type == 'else': if not skip_else: msg = 'Yaml error: encountered "else" with no associated "if", skipping.' raise exceptions.YamlSyntaxError(msg) skip_else = False elif comm_type.startswith('for '): # syntax: "for $i in $x: <section> or "for $i in cl_command: <section>" control_vars, eval_expression = get_for_control_var_and_eval_expr( comm_type, kwargs) for i in eval_expression: if len(control_vars) == 2: kwargs[control_vars[0]] = i[0] kwargs[control_vars[1]] = i[1] else: kwargs[control_vars[0]] = i retval = run_section(comm, kwargs, runner=runner) elif comm_type.startswith('$'): # commands that can have exec flag appended follow if comm_type.endswith( '~'): # on exec flag, eval comm as exec section comm_ret = eval_exec_section(comm, kwargs, runner) else: # with no exec flag, eval comm as input section comm_ret = eval_literal_section(comm, kwargs, runner) retval = assign_variable(comm_type, *comm_ret, kwargs=kwargs) else: retval = Command(comm_type, comm, kwargs=kwargs).run() if not isinstance(retval, (list, tuple)) or len(retval) != 2: raise exceptions.RunException('Bad return value of last command ({ct}: {c}): {r}'.\ format(ct=comm_type, c=comm, r=retval)) assign_last_result(kwargs, *retval) return retval
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
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))
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
def _install_dependencies(self, *dep_list, **kwargs): to_install = [] for dep in dep_list: if dep.startswith('@'): if not YUMHelper.is_group_installed(dep): to_install.append(dep) else: if not RPMHelper.is_rpm_installed(dep): to_install.append(dep) if to_install: # only invoke YUM if we actually have something to install if not YUMHelper.install(*to_install): raise exceptions.RunException('Failed to install: {0}'.format( ' '.join(to_install))) for pkg in to_install: RPMHelper.was_rpm_installed(pkg)