def _login():
    """Logs in with the provided credentials. Prompts the user for a password if no password is found in the config."""
    global _password, _token

    if not _password:
        _password = _ask_password()

    try:
        io.debug('Logging in as user {}'.format(_username))
        response = requests.post(api.login(),
                                 headers={'Content-Type': 'application/json'},
                                 data=json.dumps({
                                     "username": _username,
                                     "password": _password
                                 }))
        response.raise_for_status()
        _token = response.json()['token']
    except HTTPError as e:
        if e.response.status_code == 401:
            raise McmdError('Invalid login credentials')
        else:
            raise McmdError(str(e))
    finally:
        if not _as_user:
            config.set_token(_token)
Esempio n. 2
0
def _prepare_files_for_upload(paths, names, valid_content_types):
    """
    _prepare_files_for_upload takes the paths to the files to upload, the names of them and a list of valid content
    types to translate to content types and generates a dictionary which can be uploaded in a post request
    :param paths: a list of paths to the files to upload
    :param names: the names of files to upload
    :param valid_content_types: set of the possible valid content types
    :return: a dictionary with as key the name of the file and as value a tuple with: filename, file to upload, and
    content type

    :exception McmdError when the file on the given path does not exist and when the content type of the file is
    invalid.
    """
    files = {}
    for name, path in zip(names, paths):
        file_name = file_utils.get_file_name_from_path(path)
        content_type = mimetypes.guess_type(path)[0]
        if not os_path.exists(path):
            raise McmdError('File [{}] does not exist on path [{}]'.format(
                file_name, path.strip(file_name)))
        elif content_type in valid_content_types:
            try:
                files[name] = (file_name, open(path, 'rb'), content_type)
            except FileNotFoundError:
                raise McmdError('File [{}] does not exist on path [{}]'.format(
                    file_name, path.strip(file_name)))
        else:
            raise McmdError('File [{}] does not have valid content type [{}], '
                            'content type should be in {}'.format(
                                file_name, content_type, valid_content_types))
    return files
Esempio n. 3
0
def _include_group_role(subject: Role, target_role: Role):
    if not target_role.group:
        raise McmdError('Role {} is not a group role'.format(target_role.name))
    if subject.name == target_role.name:
        raise McmdError("A role can't include itself")

    io.start('Including role {} in role {}'.format(highlight(target_role.name),
                                                   highlight(subject.name)))
    include = {'role': target_role.name}
    put(api.role(target_role.group.name, subject.name),
        data=json.dumps(include))
def principal_exists(principal_name, principal_type):
    if principal_type == PrincipalType.USER:
        return user_exists(principal_name)
    elif principal_type == PrincipalType.ROLE:
        return role_exists(principal_name)
    else:
        raise McmdError('Unknown principal type: %s' % principal_type)
def _do_script_function(line: str):
    line_parts = line.strip('$').split()
    function = line_parts[0]
    if function == 'wait':
        _wait(' '.join(line_parts[1:]).strip())
    else:
        raise McmdError("Unknown function '{}' ".format(function))
def _read_script(script):
    try:
        with open(script) as file:
            lines = [line.rstrip('\n') for line in file]
    except OSError as e:
        raise McmdError('Error reading script: {}'.format(str(e)))
    return lines
def _block_nested_scripts(command: str):
    if command == 'run':
        raise McmdError(
            'Nesting scripts is not supported',
            info='If you think it should be, feel free to submit a '
            'feature request at '
            'https://github.com/molgenis/molgenis-tools'
            '-commander/issues!')
 def test_mcmd_script_error_wrapped():
     try:
         error = McmdError("test message", info="don't use it like that")
         raise ScriptError.from_error(error, line=3)
     except ScriptError as e:
         assert e.message == "test message"
         assert e.info is "don't use it like that"
         assert e.line == 3
Esempio n. 9
0
def raise_if_non_interactive(message: str):
    if config.is_loaded() and config.get('settings', 'non_interactive'):
        raise McmdError(
            'User input required but running in non-interactive mode. Message: {}'
            .format(message),
            info=
            "Please specify your command more precisely or switch to interactive mode with 'mcmd "
            "config set interactive'")
    def handle_request(*args, **kwargs):
        auth.check_token()

        response = str()
        try:
            response = func(*args, **kwargs)
            response.raise_for_status()
            return response
        except requests.HTTPError as e:
            if _is_json(response):
                _handle_json_error(response.json())
            else:
                raise McmdError(str(e))
        except requests.exceptions.ConnectionError:
            raise MolgenisOfflineError()
        except requests.RequestException as e:
            raise McmdError(str(e))
def _remove_script(script_name):
    path = context().get_scripts_folder().joinpath(script_name)
    _check_script_exists(path)
    try:
        io.start('Removing script %s' % highlight(script_name))
        path.unlink()
    except OSError as e:
        raise McmdError('Error removing script: %s' % str(e))
Esempio n. 12
0
def _get_script(args):
    if args.from_path:
        script = Path(args.script)
    else:
        script = context().get_scripts_folder().joinpath(args.script)
    if not script.exists():
        raise McmdError("The script {} doesn't exist".format(script))
    return script
def test_ping_offline(get_version_mock, capsys):
    get_version_mock.side_effect = McmdError('')
    run_commander('ping')

    captured = capsys.readouterr().out
    assert 'Offline' in captured
    assert 'Version' not in captured
    assert get_host()['url'] in captured
    assert get_host()['username'] in captured
def _read_script(script_name):
    path = context().get_scripts_folder().joinpath(script_name)
    _check_script_exists(path)
    try:
        with path.open() as f:
            for line in f.readlines():
                log.info(line.strip())
    except OSError as e:
        raise McmdError('Error reading script: %s' % str(e))
Esempio n. 15
0
def load():
    try:
        if context().get_storage_file().exists():
            with context().get_storage_file().open('rb') as f:
                _store.update(pickle.load(f))
        else:
            _persist()
    except (pickle.PickleError, IOError) as e:
        raise McmdError("Unable to read storage from file: {}".format(
            e.message))
Esempio n. 16
0
def _get_group_id(group_name) -> str:
    groups = get(api.rest2('sys_sec_Group'),
                 params={
                     'attrs': 'id',
                     'q': 'name=={}'.format(group_name)
                 }).json()['items']
    if len(groups) == 0:
        raise McmdError('No group found with name {}'.format(groups))
    else:
        return groups[0]['id']
 def wrapper(*args, **kwargs):
     mol_version = molgenis_version.get_version_number()
     if py_version.parse(mol_version) >= py_version.parse(since):
         message = '{} is deprecated since MOLGENIS {}. ' \
                   'You are using MOLGENIS {}.'.format(action.capitalize(),
                                                       since,
                                                       molgenis_version.get_version())
         raise McmdError(message, info=info)
     else:
         return func(*args, **kwargs)
Esempio n. 18
0
def _get_subject_type(args) -> PrincipalType:
    """
    Prior to 8.1 roles can't be included with the identities API so the subject is always a user.
    """
    if args.role:
        raise McmdError(
            "Including group roles is only possible in MOLGENIS 8.3 and up (you are using {})"
            .format(get_version()))
    else:
        return PrincipalType.USER
def get_attachments(issue_num):
    validate_issue_number(issue_num)
    try:
        issue = _molgenis_repo().get_issue(int(issue_num))
    except UnknownObjectException:
        raise McmdError("Issue #%s doesn't exist" % issue_num)

    # GitHub has no API for downloading attachments so we get them from the issue description
    urls = _parse_attachment_urls(issue.body)
    return [Attachment(url.strip('()')) for url in urls]
def write(arg_string, success):
    try:
        history = open(str(context().get_history_file()), 'a')

        indicator = _INDICATOR_SUCCESS
        if not success:
            indicator = _INDICATOR_FAILURE

        history.write('%s %s\n' % (indicator, arg_string))
    except OSError as e:
        raise McmdError("Error writing to history: %s" % str(e))
Esempio n. 21
0
def _get_role(role_name: str) -> Role:
    roles = get(api.rest2('sys_sec_Role'),
                params={
                    'attrs': 'id,name,label,group(id,name)',
                    'q': 'name=={}'.format(role_name)
                }).json()['items']

    if len(roles) == 0:
        raise McmdError('No role found with name {}'.format(role_name))
    else:
        return map_to_role(roles[0])
Esempio n. 22
0
def _get_user(user_name: str) -> User:
    users = get(api.rest2('sys_sec_User'),
                params={
                    'attrs': 'id,username',
                    'q': 'username=={}'.format(user_name)
                }).json()['items']

    if len(users) == 0:
        raise McmdError('Unknown user {}'.format(user_name))
    else:
        return map_to_user(users[0])
Esempio n. 23
0
def _get_group_roles(group: Group) -> List[Role]:
    roles = get(api.rest2('sys_sec_Role'),
                params={
                    'attrs': 'id,name,label,group(id,name)',
                    'q': 'group=={}'.format(group.id)
                }).json()['items']

    if len(roles) == 0:
        raise McmdError('No roles found for group {}'.format(group.name))

    return [map_to_role(role) for role in roles]
Esempio n. 24
0
def _select_attachment(issue_num, wanted_attachment):
    """Gets attachments from a GitHub issue. If wanted_attachment is specified it will try to select that attachment."""
    attachments = github.get_attachments(issue_num)
    if len(attachments) == 0:
        raise McmdError("Issue #%s doesn't contain any files" % issue_num)

    if wanted_attachment:
        selected = [a for a in attachments if a.name == wanted_attachment]
        if len(selected) == 0:
            raise McmdError('There are no attachments named %s.' %
                            wanted_attachment)
        if len(selected) > 1:
            raise McmdError('Multiple attachments with name %s found.' %
                            wanted_attachment)
        return selected[0]
    else:
        if len(attachments) > 1:
            return _choose_attachment(attachments)
        else:
            return attachments[0]
def _validate_required_args(lines: List[ParsedLine], args: dict):
    missing_args = list()
    for line in lines:
        statement = line.statement
        if isinstance(
                statement, Value
        ) and statement.value is None and statement.name not in args:
            missing_args.append(statement.name)

    if len(missing_args) > 0:
        raise McmdError('Missing required argument(s): {}'.format(
            ', '.join(missing_args)))
Esempio n. 26
0
def read_file(file: Path) -> str:
    """
    read_file reads the file data into a string
    :param file: file to read from
    :return: file contents
    """
    try:
        with file.open() as file_handle:
            content = file_handle.read()
    except OSError as e:
        raise McmdError('Error reading file: {}'.format(str(e)))
    return content
Esempio n. 27
0
def read_file_lines(file: Path) -> List[str]:
    """
    read_file reads the file data into a list of strings
    :param file: file to read from
    :return: list of lines
    """
    try:
        with file.open() as file_handle:
            content = file_handle.read().splitlines()
    except OSError as e:
        raise McmdError('Error reading file: {}'.format(str(e)))
    return content
def _get_version():
    try:
        response = requests.get(urljoin(config.get('host', 'selected'), 'api/v2/version'),
                                headers={'Content-Type': 'application/json'})
        response.raise_for_status()

        global _version
        global _version_number
        _version = response.json()['molgenisVersion']
        _version_number = _extract_version_number(_version)
    except HTTPError as e:
        raise McmdError(str(e))
    except requests.exceptions.ConnectionError:
        raise MolgenisOfflineError()
def _create_script(args):
    lines = history.read(args.number, args.show_fails)
    if len(lines) == 0:
        log.info('History is empty.')
        return

    options = [line[1] for line in lines]
    commands = mcmd.io.ask.checkbox('Pick the lines that will form the script:', options)
    file_name = _input_script_name()
    try:
        with open(str(context().get_scripts_folder().joinpath(file_name)), 'w') as script_file:
            for cmd in commands:
                script_file.write(cmd + '\n')
    except OSError as e:
        raise McmdError("Error writing to script: %s" % str(e))
def _run_command(command: Command, state: _ScriptExecutionState):
    cmd = command.command.render(state.values)
    try:
        sub_args = arg_parser.parse_args(shlex.split(cmd))
    except ArgumentSyntaxError as e:
        raise McmdError(message=str(e))

    _block_nested_scripts(sub_args.command)

    if state.options.dry_run:
        log.info(cmd)
    else:
        setattr(sub_args, 'arg_string', cmd)

        sub_args.func(sub_args, nested=True)