Beispiel #1
0
def get_resource(xnat_project, xnat_session, xnat_experiment,
                 xnat_resource_group, xnat_resource_id, target_path):
    """Download a single resource file from xnat. Target path should be
    full path to store the file, including filename"""

    try:
        archive = xnat.get_resource(xnat_project,
                                    xnat_session,
                                    xnat_experiment,
                                    xnat_resource_group,
                                    xnat_resource_id,
                                    zipped=False)
    except Exception as e:
        logger.error(
            'Failed downloading resource archive from:{} with reason:{}'.
            format(xnat_session, e))
        return

    # check that the target path exists
    target_dir = os.path.split(target_path)[0]
    if not os.path.exists(target_dir):
        try:
            os.makedirs(target_dir)
        except OSError:
            logger.error('Failed to create directory:{}'.format(target_dir))
            return

    # copy the downloaded file to the target location
    try:
        source = archive[1]
        if not DRYRUN:
            shutil.copyfile(source, target_path)
    except:
        logger.error('Failed copying resource:{} to target:{}.'.format(
            source, target_path))

    # # extract the files from the archive, ignoring the filestructure
    # try:
    #     with zipfile.ZipFile(archive[1]) as zip_file:
    #         member = zip_file.namelist()[0]
    #         source = zip_file.open(member)
    #         if not DRYRUN:
    #             with open(target_path, 'wb') as target:
    #                 shutil.copyfileobj(source, target)
    #         target.close()
    # except:
    #     logger.error('Failed extracting resources archive:{}'
    #                  .format(xnat_session), exc_info=True)

    # finally delete the temporary archive
    try:
        os.remove(archive[1])
    except OSError:
        logger.error(
            'Failed to remove temporary archive:{} on system:{}'.format(
                archive, platform.node()))
    return (target_path)
Beispiel #2
0
def get_resource_archive_from_xnat(xnat_project, session, resourceid):
    """Downloads and extracts a resource archive from xnat
    to a local temp file
    Returns the path to the tempfile (for later cleanup)"""

    logger.debug('Downloadind resources for:{}, series:{}.'.format(
        session, resourceid))
    try:
        resource_archive = xnat.get_resource(xnat_project, session, session,
                                             resourceid)
    except Exception as e:
        return None
    return (resource_archive)
Beispiel #3
0
def download_resource(xnat, xnat_experiment, xnat_resource_id,
                      xnat_resource_uri, target_path):
    """
    Download a single resource file from XNAT. Target path should be
    full path to store the file, including filename
    """

    try:
        source = xnat.get_resource(xnat_experiment.project,
                                   xnat_experiment.subject,
                                   xnat_experiment.name,
                                   xnat_resource_id,
                                   xnat_resource_uri,
                                   zipped=False)
    except Exception as e:
        logger.error("Failed downloading resource archive from {} with "
                     "reason: {}".format(xnat_experiment.name, e))
        return

    # check that the target path exists
    target_dir = os.path.split(target_path)[0]
    if not os.path.exists(target_dir):
        try:
            os.makedirs(target_dir)
        except OSError:
            logger.error("Failed to create directory: {}".format(target_dir))
            return

    # copy the downloaded file to the target location
    try:
        if not DRYRUN:
            shutil.copyfile(source, target_path)
    except (IOError, OSError):
        logger.error("Failed copying resource {} to target {}".format(
            source, target_path))

    # finally delete the temporary archive
    try:
        os.remove(source)
    except OSError:
        logger.error(
            "Failed to remove temporary archive {} on system {}".format(
                source, platform.node()))
    return target_path
Beispiel #4
0
def get_resource(xnat_project, xnat_session, xnat_experiment,
                 xnat_resource_group, xnat_resource_id, target_path):
    """Download a single resource file from xnat. Target path should be
    full path to store the file, including filename"""

    try:
        archive = xnat.get_resource(xnat_project,
                                    xnat_session,
                                    xnat_experiment,
                                    xnat_resource_group,
                                    xnat_resource_id,
                                    zipped=False)
    except Exception as e:
        logger.error(
            'Failed downloading resource archive from:{} with reason:{}'.
            format(xnat_session, e))
        return

    # check that the target path exists
    target_dir = os.path.split(target_path)[0]
    if not os.path.exists(target_dir):
        try:
            os.makedirs(target_dir)
        except OSError:
            logger.error('Failed to create directory:{}'.format(target_dir))
            return

    # copy the downloaded file to the target location
    try:
        source = archive[1]
        if not DRYRUN:
            shutil.copyfile(source, target_path)
    except:
        logger.error('Failed copying resource:{} to target:{}.'.format(
            source, target_path))

    # finally delete the temporary archive
    try:
        os.remove(archive[1])
    except OSError:
        logger.error(
            'Failed to remove temporary archive:{} on system:{}'.format(
                archive, platform.node()))
    return (target_path)