Esempio n. 1
0
def mw_handle_target(target_text, target_format):
    """ Handles reading in a file specified in cli command.

    Args:
        target_text (str): String that specifies where 6
        target_format (str): Valid formats include `.json` and `.yml` or `.yaml`
    Returns:
        The content of the file that you specified
    Raises:
        CommandLineError: Issue with file format or appropriate file reading package not installed.
    """
    if not target_text:
        return {}
    target = {}
    if target_format == 'json':
        load_func = json.loads
    elif target_format in ('yaml', 'yml'):
        try:
            import yaml
            load_func = yaml.load
        except ImportError:
            raise UsageError('No YAML package found. To process yaml files, run: pip install PyYAML')
    elif target_format == 'python':
        load_func = ast.literal_eval
    else:
        raise UsageError('expected target-format to be one of python, json, or yaml')

    try:
        target = load_func(target_text)
    except Exception as e:
        raise UsageError('could not load target data, got: %s: %s'
                         % (e.__class__.__name__, e))

    return target
Esempio n. 2
0
def _get_creds(kf,
               user=None,
               interactive=True,
               check_env=True,
               passphrase_file=None,
               user_env_var='PPROTECT_USER',
               pass_env_var='PPROTECT_PASSPHRASE'):
    if not interactive and not check_env:
        raise UsageError('expected at least one of check_env'
                         ' and interactive to be True', 2)
    user_source = 'argument'
    passphrase, passphrase_source = None, None
    if passphrase_file:
        passphrase_file = os.path.abspath(passphrase_file)
        try:
            passphrase = open(passphrase_file, 'rb').read().decode('utf8')
        except IOError as ioe:
            if getattr(ioe, 'strerror', None):
                msg = '%s while reading passphrase from file at "%s"' % (ioe.strerror, passphrase_file)
            else:
                msg = 'Failed to read passphrase from file at "%s"' % passphrase_file
            raise UsageError(msg=msg)
        else:
            passphrase_source = "passphrase file: %s" % passphrase_file
    if user is None and user_env_var:
        user = os.getenv(user_env_var)
        user_source = 'env var: %s' % user_env_var
    if passphrase is None and pass_env_var:
        passphrase = os.getenv(pass_env_var)
        passphrase_source = 'env var: %s' % pass_env_var

    if interactive:
        msg = ''
        if user is None:
            msg = 'Verify credentials for %s' % kf.path
        elif passphrase is None:
            msg = 'Verify passphrase for %s (Using user %s from %s)' % (kf.path, user, user_source)
        if msg:
            echo.err(msg)

        if user is None:
            user = prompt('User email: ')
            user_source = 'stdin'
        if passphrase is None:
            passphrase = prompt.secret('Passphrase: ', confirm=False)
            passphrase_source = 'stdin'

    creds = Creds(_get_text(user or ''), _get_text(passphrase or ''),
                  name_source=user_source, passphrase_source=passphrase_source)
    _check_creds(kf, creds)

    return creds
Esempio n. 3
0
def mw_get_target(next_, posargs_, target_file, target_format, spec_file,
                  spec_format):
    spec_text, target_text = None, None
    if len(posargs_) == 2:
        spec_text, target_text = posargs_
    elif len(posargs_) == 1:
        spec_text, target_text = posargs_[0], None

    if spec_text and spec_file:
        raise UsageError('expected spec file or spec argument, not both')
    elif spec_file:
        try:
            with open(spec_file, 'r') as f:
                spec_text = f.read()
        except IOError as ose:
            raise UsageError('could not read spec file %r, got: %s' %
                             (spec_file, ose))

    if not spec_text:
        spec = Path()
    elif spec_format == 'python':
        if spec_text[0] not in ('"', "'", "[", "{", "("):
            # intention: handle trivial path access, assume string
            spec_text = repr(spec_text)
        spec = ast.literal_eval(spec_text)
    elif spec_format == 'json':
        spec = json.loads(spec_text)
    elif spec_format == 'python-full':
        spec = _eval_python_full_spec(spec_text)
    else:
        raise UsageError(
            'expected spec-format to be one of json, python, or python-full')

    if target_text and target_file:
        raise UsageError('expected target file or target argument, not both')
    elif target_text == '-' or target_file == '-':
        target_text = sys.stdin.read()
    elif target_file:
        try:
            target_text = open(target_file, 'r').read()
        except IOError as ose:
            raise UsageError('could not read target file %r, got: %s' %
                             (target_file, ose))
    elif not target_text and not isatty(sys.stdin):
        target_text = sys.stdin.read()

    target = mw_handle_target(target_text, target_format)

    return next_(spec=spec, target=target)
Esempio n. 4
0
def mw_write_kf(next_, kf, confirm):
    if not os.access(kf.path, os.W_OK):
        raise UsageError('expected %r to be a writable file. Check the'
                         ' permissions and try again.' % kf.path)

    modified_kf = next_(wkf=kf)

    if not modified_kf:
        return modified_kf

    if confirm:
        diff_lines = list(difflib.unified_diff(kf.get_contents().splitlines(),
                                               modified_kf.get_contents().splitlines(),
                                               kf.path + '.old', kf.path + '.new'))
        diff_lines = _get_colorized_lines(diff_lines)
        echo('Changes to be written:\n')
        echo('\n'.join(diff_lines) + '\n')
        do_write = prompt('Write changes? [y/N] ')
        if not do_write.lower().startswith('y'):
            echo('Aborting...')
            sys.exit(0)

    modified_kf.write()

    return
Esempio n. 5
0
def _create_protected(path):
    if os.path.exists(path):
        raise UsageError('Protected file already exists: %s' % path, 2)
    open(path, 'wb').close()
    kf = KeyFile.create(path=path)
    kf.write()
    return kf
Esempio n. 6
0
def mw_exit_handler(next_):
    status = 55  # should always be set to something else
    try:
        try:
            status = next_() or 0
        except PPError as ppe:
            raise UsageError(ppe.args[0])
    except KeyboardInterrupt:
        echo('')
        status = 130
    except EOFError:
        echo('')
        status = 1

    sys.exit(status)

    return
Esempio n. 7
0
def _check_creds(kf, creds):
    if kf.check_creds(creds):
        return True

    msg = 'Invalid user email'
    if creds.name_source:
        msg += ' (from %s)' % creds.name_source
    msg += ' or passphrase'
    if creds.passphrase_source:
        msg += ' (from %s)' % creds.passphrase_source
    msg += '. Check credentials and try again.'
    empty_fields = []
    if creds.name == '':
        empty_fields.append('user ID')
    if creds.passphrase == '':
        empty_fields.append('passphrase')
    if empty_fields:
        msg += ' (Warning: Empty ' + ' and '.join(empty_fields) + '.)'

    raise UsageError(msg, 1)
Esempio n. 8
0
def update_all(campaign_ids=None, jsub=False, force=False, args_=None):
    "Update all campaigns configured"
    if jsub and not args_:
        raise RuntimeError('jsub requires parsed arguments (args_)')

    campaign_ids = set(campaign_ids or [])
    if campaign_ids:
        known_campaigns = set(get_all_campaign_dirs(abspath=False))
        unknown_campaigns = campaign_ids - known_campaigns
        if unknown_campaigns:
            raise UsageError('got unknown campaign names: %s\nexpected one of: %s'
                             % (', '.join(sorted(unknown_campaigns)),
                                ', '.join(sorted(known_campaigns))))

    for campaign_dir in get_all_campaign_dirs():
        cur_campaign_id = os.path.split(campaign_dir)[1]
        if campaign_ids and cur_campaign_id not in campaign_ids:
            continue
        if jsub:
            _run_jsub_update(args_, force, cur_campaign_id)
            continue

        cur_pt = load_and_update_campaign(campaign_dir, force=force)
    return
Esempio n. 9
0
def _ensure_protected(path):
    if not os.path.exists(path):
        raise UsageError('Protected file not found: %s' % path, 2)
    kf = KeyFile.from_file(path)
    return kf
Esempio n. 10
0
def create_round(user_dao, campaign_id, advance=False, debug=False):
    'interactively create a round in the specified campaign'
    # TODO: this looks messy below, campaign was semi-undefined and
    # the comment about rdb_session rollback may not be true.
    coord_dao = CoordinatorDAO.from_campaign(user_dao, campaign_id)
    rnd_name = raw_input('?? Round name: ')
    if not rnd_name:
        print('-- round name required, aborting')
        sys.exit(0)
    juror_names_str = raw_input('?? Juror names (comma separated): ')
    juror_names = [j.strip() for j in juror_names_str.split(',')]
    vote_method = raw_input('?? Vote method (rating, yesno, or ranking): ')
    if vote_method not in ['rating', 'yesno', 'ranking']:
        print('-- vote method must be rating, yesno, or ranking, aborting')
        sys.exit(0)
    if vote_method != 'ranking':
        quorum = raw_input('?? Voting quorum: ')
    else:
        quorum = len(juror_names)
    deadline_date_str = raw_input('?? Deadline date: ')
    deadline_date = datetime.datetime.strptime(deadline_date_str, '%Y-%m-%d')
    description = raw_input('?? Description: ')
    directions = raw_input('?? Directions: ')
    if not advance:
        category_name = raw_input('?? Category: ')
    rnd = coord_dao.create_round(name=rnd_name,
                                 quorum=quorum,
                                 vote_method=vote_method,
                                 deadline_date=deadline_date,
                                 jurors=juror_names,
                                 directions=directions,
                                 description=description)

    pprint(rnd.to_info_dict())
    print('++ round %s (%r) created in campaign %s' %
          (rnd.id, rnd.name, campaign_id))

    campaign = user_dao.get_campaign(campaign_id)

    if not advance:
        entries = coord_dao.add_entries_from_cat(rnd.id, category_name)
        source = category_name
        # GIST_URL = 'https://gist.githubusercontent.com/slaporte/7433943491098d770a8e9c41252e5424/raw/ca394147a841ea5f238502ffd07cbba54b9b1a6a/wlm2015_fr_500.csv'
        # entries = maint_dao.add_entries_from_csv_gist(rnd, GIST_URL)
        # source = GIST_URL
        print('++ prepared %s entries from %r' % (len(entries), source))
        coord_dao.add_round_entries(rnd.id, entries)
    else:
        final_rnds = [r for r in campaign.rounds if r.status == 'finalized']
        last_successful_rnd = final_rnds[-1]
        advancing_group = coord_dao.get_rating_advancing_group(
            last_successful_rnd)
        entries = advancing_group
        if vote_method == 'ranking' and len(entries) > RANKING_MAX:
            print('-- %s is too many entries for ranking round, aborting' %
                  len(entries))
            raise UsageError('too many entries for ranking round')

        source = 'round(#%s)' % last_successful_rnd.id
        coord_dao.add_round_entries(rnd.id, advancing_group, source)
    print('++ added entries from %s to round %s (%r)' %
          (source, rnd.id, rnd.name))

    return rnd