Пример #1
0
def irods_transform_meta_data(ibcontext, **kwargs):
    """
    Transform meta data
    """
    cfg = ibcontext['irods'].get_config(kwargs)
    key = {
        'irods_zone': get_irods_zone(cfg),
        'irods_collection': cfg['irods_collection']
    }
    data = ibcontext['cache'].read(key)
    if data is None:
        raise ValueError(
            'collection not found in cache: {0} (zone: {1})'.format(
                key['irods_collection'], key['irods_zone']))

    data = extract_irods_collecion_data(ibcontext['cache'].read(key),
                                        cfg['irods_collection'])
    md = {
        cfg['irods_collection']:
        transform_meta_data(data['meta_data'], COLLECTION_META_DATA_MAPPING)
    }
    for obj in data['objects']:
        obj['meta_data']['name'] = os.path.basename(obj['path'])
        md[obj['path']] = transform_meta_data(obj['meta_data'],
                                              OBJECT_META_DATA_MAPPING)
    ibcontext['cache'].update(key, {'target_meta_data': md})
Пример #2
0
def irods_validate_meta_data(ibcontext, **kwargs):
    """
    Validate meta data
    """
    cfg = ibcontext['irods'].get_config(kwargs)
    key = {
        'irods_zone': get_irods_zone(cfg),
        'irods_collection': cfg['irods_collection']
    }
    data = ibcontext['cache'].read(key)
    if data is None:
        raise ValueError(
            'collection not found in cache: {0} (zone: {1})'.format(
                key['irods_collection'], key['irods_zone']))
    validate_collection_meta_data(data, cfg['irods_collection'],
                                  COLLECTION_META_DATA_VALIDATION)

    validate_objects_meta_data(data, cfg['irods_collection'],
                               OBJECT_META_DATA_VALIDATION)
Пример #3
0
def create_package(ibcontext, **kwargs):
    logger = logging.getLogger('ipublish')
    irods_cfg = ibcontext['irods'].get_config(kwargs)
    ckan_cfg = ibcontext['ckan'].get_config(kwargs)
    key = {
        'irods_zone': get_irods_zone(irods_cfg),
        'irods_collection': irods_cfg['irods_collection']
    }
    data = ibcontext['cache'].read(key)
    if data is None:
        raise KeyError('No such key {0}'.format(key))
    collection = [
        x for x in data.get('irods_data')
        if x['path'] == irods_cfg['irods_collection']
    ][0]
    target_meta_data = data['target_meta_data']
    md = target_meta_data[irods_cfg['irods_collection']].copy()
    md['owner_org'] = ckan_cfg['ckan_org']
    result = ibcontext['ckan'].action('package_create', data=md, method='post')
    id = result['result']['id']
    logger.info('created package with id {0}'.format(id))
    logger.debug(result['result'])
    result['result']['resources'] = []
    try:
        for obj in collection['objects']:
            obj_md = target_meta_data[obj['path']]
            obj_md['package_id'] = id
            obj_res = ibcontext['ckan'].action('resource_create',
                                               data=obj_md,
                                               method='post')
            result['result']['resources'].append(obj_res)
    except Exception:
        ibcontext['ckan'].action('package_delete',
                                 data={'id': id},
                                 method='post')
        raise
    ibcontext['cache'].update(key, {'ckan_package': result['result']})
    repo_url = ckan_cfg['ckan_dataset_url'] + '/' + md['name']
    repository_info = {'PUBLIC_URL': repo_url, 'PUBLIC_REPO_TYPE': 'CKAN'}
    ibcontext['cache'].update(key, {'repository_info': repository_info})
Пример #4
0
def create_draft(ibcontext, **kwargs):
    logger = logging.getLogger('ipublish')
    irods_cfg = ibcontext['irods'].get_config(kwargs)
    config = ibcontext['b2share'].get_config(kwargs)
    key = {'irods_zone': get_irods_zone(irods_cfg),
           'irods_collection': irods_cfg['irods_collection']}
    logger.debug(config)
    meta_data = ibcontext['cache'].read(key)\
                                  .get('target_meta_data')\
                                  .get(irods_cfg['irods_collection'])
    logger.debug(meta_data)
    api_url = config.get('b2share_api_url')
    access_token = config.get('b2share_access_token')
    community = config.get('b2share_community')
    url = urljoin(api_url, 'records') + '/'
    data = {'titles': [{'title': meta_data['title']}],
            'community': community,
            'open_access': True,
            'community_specific': {}}
    logger.debug('url: %s', url)
    logger.debug(data)
    req = requests.post(url,
                        data=json.dumps(data),
                        params={"access_token": access_token},
                        headers={"Content-Type": "application/json"})
    req.raise_for_status()
    if req.status_code != 201:
        raise ValueError('unexpected status code %d', req.status_code)
    b2share_id = req.json()['id']
    logger.debug('created draft %s', b2share_id)
    ibcontext['cache'].update(key,
                              {'b2share_id': b2share_id})
    repo_url = urljoin(api_url,
                       'records',
                       b2share_id,
                       'draft')
    repository_info = {'PUBLIC_URL': repo_url,
                       'PUBLIC_REPO_TYPE': 'B2SHARE'}
    ibcontext['cache'].update(key,
                              {'repository_info': repository_info})
Пример #5
0
def patch_draft(ibcontext, **kwargs):
    logger = logging.getLogger('ipublish')
    irods_cfg = ibcontext['irods'].get_config(kwargs)
    config = ibcontext['b2share'].get_config(kwargs)
    api_url = config.get('b2share_api_url')
    access_token = config.get('b2share_access_token')
    key = {'irods_zone': get_irods_zone(irods_cfg),
           'irods_collection': irods_cfg['irods_collection']}
    logger.debug(config)
    meta_data = ibcontext['cache'].read(key)\
                                  .get('target_meta_data')\
                                  .get(irods_cfg['irods_collection'])
    b2share_id = ibcontext['cache'].read(key)\
                                   .get('b2share_id')
    logger.debug('b2share id: %s', b2share_id)
    pids = []
    for item in ibcontext['cache'].read(key)\
                                  .get('target_meta_data').values():
        pids.append({'alternate_identifier': item['pid'],
                     'alternate_identifier_type': 'http://hdl.handle.net/'})
    patch = [{'op': 'add',
              'path': '/creators',
              'value': [{'creator_name': meta_data['author']}]},
             {'op': 'add',
              'path': '/alternate_identifiers',
              'value': pids}]
    logger.debug(patch)
    url = urljoin(api_url,
                  'records',
                  b2share_id,
                  'draft')
    req = requests.patch(url=url,
                         params={'access_token':
                                 access_token},
                         headers={"Content-Type":
                                  "application/json-patch+json"},
                         data=json.dumps(patch))
    req.raise_for_status()
Пример #6
0
def wait_for_confirmation(ibcontext, **kwargs):
    logger = logging.getLogger('ipublish')
    b2share_cfg = ibcontext['b2share'].get_config(kwargs)
    irods_cfg = ibcontext['irods'].get_config(kwargs)
    api_url = b2share_cfg.get('b2share_api_url')
    access_token = b2share_cfg.get('b2share_access_token')
    key = {'irods_zone': get_irods_zone(irods_cfg),
           'irods_collection': irods_cfg['irods_collection']}
    b2share_id = ibcontext['cache'].read(key)\
                                   .get('b2share_id')
    logger.debug('b2share id: %s', b2share_id)
    url = urljoin(api_url,
                  'records',
                  b2share_id,
                  'draft')
    state = 'draft'
    while state == 'draft':
        req = requests.get(url=url,
                           params={'access_token': access_token})
        req.raise_for_status()
        state = req.json().get('metadata', {}).get('publication_state', None)
        logger.debug('url: %s:%s', url, state)
        if state == 'draft':
            time.sleep(5)