Beispiel #1
0
 def __init__(self, institution='tardis'):
     self.institution = institution
     self.sites = None
     self.site_manager = SiteManager()
 def __init__(self, institution='tardis'):
     self.institution = institution
     self.sites = None
     self.site_manager = SiteManager()
Beispiel #3
0
class SyncManager(object):
    def __init__(self, institution='tardis'):
        self.institution = institution
        self.sites = None
        self.site_manager = SiteManager()

    def generate_exp_uid(self, experiment):
        uid_str = "%s.%s" % (self.institution, experiment.pk)
        return uid_str

    def _exp_from_uid(self, uid):
        try:
            [institution, pk] = uid.split('.')
            pk = int(pk)
        except ValueError:
            raise TransferService.InvalidUIDError('Invalid format')

        if institution != self.institution:
            raise TransferService.InvalidUIDError('Invalid institution')
        try:
            exp = Experiment.objects.get(pk=pk)
        except Experiment.DoesNotExist:
            raise TransferService.InvalidUIDError('Experiment does not exist')
        return exp

    # originally '_register_file_settings' in parser
    #
    # TODO change the mecat-as app etc to call this
    # rather than the parser.py files registration logic
    #
    def push_experiment_to_institutions(self, experiment, owners):
        """
        Transfers experiment metadata to experiment owner's
        MyTardis instance at the home institute.

        :keyword experiment: Experiment object to transfer.
        :keyword owners: Emails of owners of the experiment.
        """
        sites = []
        # loop over sites
        for ss in self.site_manager.sites():
            # is the email domain of the experiment's owner registered
            # by any site?
            site_owners = []
            for owner in owners:
                for domain in ss['email-endswith']:
                    if owner.endswith(domain):
                        site_owners.append(owner)
                        break

            register_settings = ss['register']

            # register meta-data and file transfer request at another
            # MyTARDIS instance
            if site_owners:
                success = self._post_experiment(experiment, site_owners,
                                                register_settings)
                sites.append((ss['register']['url'], success))
        # return a list of registered sites
        return sites

    def _post_experiment(self, experiment, site_owners, site_settings):
        uid = self.generate_exp_uid(experiment)
        url = site_settings['url']
        username = site_settings['username']
        password = site_settings['password']
        protocol = site_settings['fileProtocol']

        # Create the form with simple fields
        mpform = MultiPartForm()
        mpform.add_field('username', username)
        mpform.add_field('password', password)
        mpform.add_field('from_url', settings.MYTARDIS_SITE_URL)
        mpform.add_field('originid', uid)

        for owner in site_owners:
            mpform.add_field('experiment_owner', owner)

        # export METS file
        filename = 'mets_expid_%i_%s' % (experiment.id, protocol)
        logger.debug('=== extracting mets file for experiment %s ' % uid)
        from tardis.tardis_portal.metsexporter import MetsExporter
        exporter = MetsExporter()
        if protocol:
            # translate vbl:// into tardis:// url for datafiles
            metsfile = exporter.export(experimentId=experiment.id,
                                       replace_protocols={'vbl': protocol},
                                       filename=filename,
                                       export_images=False)
        else:
            metsfile = exporter.export(experimentId=experiment.id,
                                       filename=filename,
                                       export_images=False)
        logger.debug('=== extraction done, filename = %s' % metsfile)

        f = open(metsfile, "r")
        mpform.add_file('xmldata', 'METS.xml', fileHandle=f)
        body = str(mpform)

        logger.debug('about to send register request to site %s' % url)

        # build the request
        headers = {
            'User-agent': 'MyTardis',
            'Content-type': mpform.get_content_type(),
        }

        # This should be made into a background task.
        # Or rather- the processing on the other end should be.
        h = Http(timeout=9999, disable_ssl_certificate_validation=True)
        h.force_exception_to_status_code = True
        resp, content = h.request(url, 'POST', headers=headers, body=body)
        f.close()

        if resp.status != 200:
            logger.error('Posting experiment to %s failed:' % url)
            logger.error('%s: %s' % (resp.status, resp.reason))
            logger.debug('SERVER RESPONSE: ' + content)
            return False
        return True

    def start_file_transfer(self, uid, site_settings_url, dest_path):
        exp = self._exp_from_uid(uid)
        site_settings = self.site_manager.get_site_settings(site_settings_url)
        if site_settings is None:
            raise TransferService.SiteError(
                'Error retrieving settings for site %s' % site_settings_url)
        return self._start_file_transfer(exp, site_settings['transfer'],
                                         dest_path)

    def get_status(self, uid):
        exp = self._exp_from_uid(uid)
        return self._get_status(exp)

    def _start_file_transfer(self, experiment, settings, path):
        return True

    def _get_status(self, experiment):
        return (TransferService.TRANSFER_FAILED, time.time(), {})
class SyncManager(object):

    def __init__(self, institution='tardis'):
        self.institution = institution
        self.sites = None
        self.site_manager = SiteManager()

    def generate_exp_uid(self, experiment):
        uid_str = "%s.%s" % (self.institution, experiment.pk)
        return uid_str

    def _exp_from_uid(self, uid):
        try:
            [institution, pk] = uid.split('.')
            pk = int(pk)
        except ValueError:
            raise TransferService.InvalidUIDError('Invalid format')

        if institution != self.institution:
            raise TransferService.InvalidUIDError('Invalid institution')
        try:
            exp = Experiment.objects.get(pk=pk)
        except Experiment.DoesNotExist:
            raise TransferService.InvalidUIDError('Experiment does not exist')
        return exp

    # originally '_register_file_settings' in parser
    #
    # TODO change the mecat-as app etc to call this 
    # rather than the parser.py files registration logic
    #
    def push_experiment_to_institutions(self, experiment, owners):
   
        """
        Transfers experiment metadata to experiment owner's
        MyTardis instance at the home institute.

        :keyword experiment: Experiment object to transfer.
        :keyword owners: Emails of owners of the experiment.
        """
        sites = []
        # loop over sites
        for ss in self.site_manager.sites():
            # is the email domain of the experiment's owner registered
            # by any site?
            site_owners = []
            for owner in owners:
                for domain in ss['email-endswith']:
                    if owner.endswith(domain):
                        site_owners.append(owner)
                        break

            register_settings = ss['register']

            # register meta-data and file transfer request at another
            # MyTARDIS instance
            if site_owners:
                success = self._post_experiment(experiment, site_owners, register_settings)
                sites.append((ss['register']['url'], success))
        # return a list of registered sites
        return sites

    def _post_experiment(self, experiment, site_owners, site_settings):
        uid = self.generate_exp_uid(experiment)
        url = site_settings['url']
        username = site_settings['username']
        password = site_settings['password']
        protocol = site_settings['fileProtocol']

        # Create the form with simple fields
        mpform = MultiPartForm()
        mpform.add_field('username', username)
        mpform.add_field('password', password)
        mpform.add_field('from_url', settings.MYTARDIS_SITE_URL)
        mpform.add_field('originid', uid)

        for owner in site_owners:
            mpform.add_field('experiment_owner', owner)

        # export METS file
        filename = 'mets_expid_%i_%s' % (experiment.id, protocol)
        logger.debug('=== extracting mets file for experiment %s ' % uid)
        from tardis.tardis_portal.metsexporter import MetsExporter
        exporter = MetsExporter()
        if protocol:
            # translate vbl:// into tardis:// url for datafiles
            metsfile = exporter.export(experimentId=experiment.id,
                                       replace_protocols={'vbl': protocol},
                                       filename=filename,
                                       export_images=False)
        else:
            metsfile = exporter.export(experimentId=experiment.id,
                                       filename=filename,
                                       export_images=False)
        logger.debug('=== extraction done, filename = %s' % metsfile)

        f = open(metsfile, "r")
        mpform.add_file('xmldata', 'METS.xml', fileHandle=f)
        body = str(mpform)

        logger.debug('about to send register request to site %s' % url)

        # build the request
        headers = {
            'User-agent': 'MyTardis',
            'Content-type': mpform.get_content_type(),
        }

        # This should be made into a background task.
        # Or rather- the processing on the other end should be.
        h = Http(timeout=9999, disable_ssl_certificate_validation=True)
        h.force_exception_to_status_code = True
        resp, content = h.request(url, 'POST', headers=headers, body=body)
        f.close()

        if resp.status != 200:
            logger.error('Posting experiment to %s failed:' % url)
            logger.error('%s: %s' % (resp.status, resp.reason))
            logger.debug('SERVER RESPONSE: ' + content)
            return False
        return True

    def start_file_transfer(self, uid, site_settings_url, dest_path):
        exp = self._exp_from_uid(uid)
        site_settings = self.site_manager.get_site_settings(site_settings_url)
        if site_settings is None:
            raise TransferService.SiteError('Error retrieving settings for site %s' % site_settings_url)
        return self._start_file_transfer(exp, site_settings['transfer'], dest_path)

    def get_status(self, uid):
        exp = self._exp_from_uid(uid)
        return self._get_status(exp)


    def _start_file_transfer(self, experiment, settings, path):
        return True


    def _get_status(self, experiment):
        return (TransferService.TRANSFER_FAILED, time.time(), {})