Example #1
0
    def create_item_slug(self, sa_session, item) -> bool:
        """Create/set item slug.

        Slug is unique among user's importable items for item's class.

        :param sa_session: Database session context.
        :param item: The item to create/update its slug.
        :type item: [type]
        :return: Returns true if item's slug was set/changed; false otherwise.
        :rtype: bool
        """
        cur_slug = item.slug

        # Setup slug base.
        if cur_slug is None or cur_slug == '':
            # Item can have either a name or a title.
            item_name = ''
            if hasattr(item, 'name'):
                item_name = item.name
            elif hasattr(item, 'title'):
                item_name = item.title
            slug_base = ready_name_for_url(item_name.lower())
        else:
            slug_base = cur_slug

        # Using slug base, find a slug that is not taken. If slug is taken,
        # add integer to end.
        new_slug = slug_base
        count = 1
        # Ensure unique across model class and user and don't include this item
        # in the check in case it has previously been assigned a valid slug.
        while sa_session.query(item.__class__).filter(
                item.__class__.user == item.user, item.__class__.slug
                == new_slug, item.__class__.id != item.id).count() != 0:
            # Slug taken; choose a new slug based on count. This approach can
            # handle numerous items with the same name gracefully.
            new_slug = f'{slug_base}-{count}'
            count += 1

        # Set slug and return.
        item.slug = new_slug
        return item.slug == cur_slug
Example #2
0
    def execute(self, tool, trans, incoming=None, set_output_hid=False, overwrite=True, history=None, **kwargs):
        trans.check_user_activation()
        #
        # Get history to export.
        #
        incoming = incoming or {}
        history = None
        for name, value in incoming.items():
            if isinstance(value, trans.app.model.History):
                history_param_name = name
                history = value
                del incoming[history_param_name]
                break

        if not history:
            raise Exception('There is no history to export.')

        #
        # Create the job and output dataset objects
        #
        job = trans.app.model.Job()
        job.galaxy_version = trans.app.config.version_major
        session = trans.get_galaxy_session()
        job.session_id = session and session.id
        history_id = history.id
        job.history_id = history_id
        job.tool_id = tool.id
        if trans.user:
            # If this is an actual user, run the job as that individual.  Otherwise we're running as guest.
            job.user_id = trans.user.id
        job.state = job.states.WAITING  # we need to set job state to something other than NEW, or else when tracking jobs in db it will be picked up before we have added input / output parameters
        trans.sa_session.add(job)

        compressed = incoming['compress']
        exporting_to_uri = "directory_uri" in incoming
        if not exporting_to_uri:
            # see comment below about how this should be transitioned to occuring in a
            # job handler or detached MQ-driven thread
            jeha = trans.app.model.JobExportHistoryArchive.create_for_history(
                history, job, trans.sa_session, trans.app.object_store, compressed
            )
            store_directory = jeha.temp_directory
        else:
            # creating a job directory in the web thread is bad (it is slow, bypasses
            # dynamic objectstore assignment, etc..) but it is arguably less bad than
            # creating a dataset (like above for dataset export case).
            # ensure job.id is available
            trans.sa_session.flush()
            job_directory = create_working_directory_for_job(trans.app.object_store, job)
            store_directory = os.path.join(job_directory, "working", "_object_export")
            os.makedirs(store_directory)

        #
        # Setup job and job wrapper.
        #
        cmd_line = f"--galaxy-version '{job.galaxy_version}'"
        if compressed:
            cmd_line += " -G"
        cmd_line = f"{cmd_line} {store_directory}"

        #
        # Add parameters to job_parameter table.
        #

        # Set additional parameters.
        incoming['__HISTORY_TO_EXPORT__'] = history.id
        incoming['__EXPORT_HISTORY_COMMAND_INPUTS_OPTIONS__'] = cmd_line
        if exporting_to_uri:
            directory_uri = incoming["directory_uri"]
            file_name = incoming.get("file_name")
            if file_name is None:
                hname = ready_name_for_url(history.name)
                human_timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
                if compressed:
                    extension = ".tar.gz"
                else:
                    extension = ".tar"
                file_name = f"Galaxy-History-{hname}-{human_timestamp}.{extension}"

            file_name = os.path.basename(os.path.abspath(file_name))
            sep = "" if directory_uri.endswith("/") else "/"
            incoming['__EXPORT_TO_URI__'] = f"{directory_uri}{sep}{file_name}"

        for name, value in tool.params_to_strings(incoming, trans.app).items():
            job.add_parameter(name, value)
        trans.sa_session.flush()

        job_wrapper = JobExportHistoryArchiveWrapper(trans.app, job.id)
        job_wrapper.setup_job(
            history,
            store_directory,
            include_hidden=incoming['include_hidden'],
            include_deleted=incoming['include_deleted'],
            compressed=compressed)

        return job, {}