def get(self, path):
        try:
            parent = self.sess.collections.get(dirname(path))
        except CollectionDoesNotExist:
            raise DataObjectDoesNotExist()

        query = self.sess.query(DataObject)\
            .filter(DataObject.name == basename(path))\
            .filter(DataObject.collection_id == parent.id)
        results = query.all()
        if len(results) <= 0:
            raise DataObjectDoesNotExist()
        return iRODSDataObject(self, parent, results)
Beispiel #2
0
    def setFileMetadata(self, destination_dir, file_name, metadata):
        """
		This function sets the metadata for a given file

		@param	destination_dir, path to destination dir in iRODS
		@param	file_name, the name for the file in Galaxy
		@param	metadata, the metadata for the file (including provenance)
		@return True if finished successfully
		@throws DataObjectDoesNotExist if the file does not exists in iRODS

		"""
        # Step 1. Check if file exists
        path = os.path.join(destination_dir, file_name)
        obj = self.session.data_objects.get(path)
        if obj == None:
            raise DataObjectDoesNotExist("The object " + path +
                                         " was not found at iRODS server.")

        # Step 2. Clear previous metadata
        obj.metadata.remove_all()

        print("Setting metadata to file: " + str(metadata))

        # Step 3. Set metadata
        for key, value in metadata.iteritems():
            obj.metadata.add(key, value)

        return True
Beispiel #3
0
 def get(self, path):
     name = os.path.basename(path)
     coll = self.session.collections.get(os.path.dirname(path))
     for obj in coll.data_objects:
         if obj.name == name:
             return obj
     raise DataObjectDoesNotExist(path)
def download_irods(path, output_folder=None, overwrite=False):
    """
    Download file from ALTA using iRODS

    :param path: Path to file on iRODS server
    :param str output_folder: Output folder (Default: current directory)
    :param bool overwrite: Overwrite output file if it already exists
    """
    # check if iRODS tools are available
    if not have_irods:
        print("iRODS tools not available; reinstall arts_tools "
              "with pip install --upgrade arts_tools[irods]")
        return

    # get the irods environment file
    try:
        env_file = os.environ['IRODS_ENVIRONMENT_FILE']
    except KeyError:
        env_file = os.path.expanduser('~/.irods/irods_environment.json')
    if not os.path.isfile(env_file):
        print(
            "iRODS environment file not found. Set IRODS_ENVIRONMENT_FILE if the file is not in the default "
            "location. Also make sure your iRODS environment is set up properly (with iinit)"
        )
        return

    # create output folder and get full path to output file
    if output_folder is None:
        output_folder = os.getcwd()
    os.makedirs(output_folder, exist_ok=True)
    output_file = os.path.join(output_folder, os.path.basename(path))

    # check if the file already exists
    if os.path.isfile(output_file) and not overwrite:
        print(f"File already exists: {output_file}")
        return
    if overwrite:
        download_options = {FORCE_FLAG_KW: ''}
    else:
        download_options = {}

    # create SSL context
    ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH,
                                             cafile=None,
                                             capath=None,
                                             cadata=None)
    # open an iRODS session
    with iRODSSession(irods_env_file=env_file,
                      ssl_context=ssl_context) as session:
        try:
            session.data_objects.get(path, output_folder, **download_options)
        except DataObjectDoesNotExist:
            # add filename to error message
            raise DataObjectDoesNotExist(f"File not found: {path}")
Beispiel #5
0
    def get_collection_tree(self, base, path, sessions_cleanup=True):
        """
        Lists the folders and files attributes at the input 'path'

        Parameters
        ----------
        base : str
            The base path to validate ; eg. P000000001/C000000001
        path : str
            The collection's id; eg. P000000001/C000000001/SubFolder1/Experiment1/
        sessions_cleanup: bool
            If true, the session will be closed after retrieving the values.

        Returns
        -------
        dict
            The folders and files attributes at the requested path
        """
        output = []
        base_path = "/nlmumc/projects/" + base
        absolute_path = "/nlmumc/projects/" + path

        if not is_safe_path(base_path, absolute_path):
            raise CAT_NO_ACCESS_PERMISSION

        collection = self.session.collections.get(absolute_path)
        for coll in collection.subcollections:
            # query extra collection info: ctime
            query = self.session.query(iRODSCollection).filter(
                iRODSCollection.id == coll.id)
            try:
                result = query.one()
            except NoResultFound:
                raise CollectionDoesNotExist()

            name = irods_basename(result[iRODSCollection.name])
            ctime = result[iRODSCollection.create_time]
            relative_path = path + "/" + name

            folder_node = {
                "name": name,
                "path": relative_path,
                "type": "folder",
                "size": "--",
                "rescname": "--",
                "ctime": ctime.strftime("%Y-%m-%d %H:%M:%S"),
            }

            output.append(folder_node)

        for data in collection.data_objects:
            # query extra data info: ctime
            query = self.session.query(DataObject).filter(
                DataObject.id == data.id)
            try:
                result = query.first()
            except NoResultFound:
                raise DataObjectDoesNotExist()

            ctime = result[DataObject.create_time]
            relative_path = path + "/" + data.name

            data_node = {
                "name": data.name,
                "path": relative_path,
                "type": "file",
                "size": data.size,
                "rescname": data.resource_name,
                "offlineResource": data.resource_name == "arcRescSURF01",
                "ctime": ctime.strftime("%Y-%m-%d %H:%M:%S"),
            }

            output.append(data_node)

        if sessions_cleanup:
            self.session.cleanup()

        return output