Exemple #1
0
def register_dataset(transaction, dataset_type, sample, properties, ws_folder,
                     file_names):
    """ creates a new dataset of a given type.
    - the result files are copied from the session workspace
      to a temp dir close to the DSS: prepareFilesForRegistration()
    - from there, the files are moved to the DSS: transaction.moveFile()
    - finally, the remaining files are deleted from the session workspace
    """

    print("creating dataset of type: " + dataset_type)
    dataset = transaction.createNewDataSet(dataset_type)
    dataset.setSample(sample)

    # setting any given properties
    for key in properties.keySet():
        propertyValue = unicode(properties[key])
        print("setting propertyValue: " + key + " = " + propertyValue)
        if propertyValue == "":
            propertyValue = None
        dataset.setPropertyValue(key, propertyValue)

    print("dataset created with permId: " + dataset.getDataSetCode())
    print("workspace folder is: " + ws_folder)

    # create temporary folder in incoming-dir ( openbis/servers/datastore_server/data/incoming )
    threadProperties = getThreadProperties(transaction)
    #incoming_dir =  os.path.join( threadProperties[u'incoming-dir'], str(time.time()) )
    incoming_dir = os.path.join(threadProperties[u'incoming-dir'],
                                dataset_type)
    print("incoming folder is: " + incoming_dir)

    dss_service = ServiceProvider.getDssServiceRpcGeneric().getService()

    # copy all files from session workspace to (temporary) incoming directory.
    for file_name in file_names:
        ws_file_path = os.path.join(ws_folder, file_name)
        print("copying file from session workspace: " + ws_file_path)
        incoming_file_path = os.path.join(incoming_dir, file_name)
        print("to incoming: " + incoming_file_path)

        # ensure that all necessary folders exist
        try:
            os.makedirs(os.path.dirname(incoming_file_path))
            print("subdir created: " + os.path.dirname(incoming_file_path))
        except:
            pass

        # create input and output stream
        inputStream = dss_service.getFileFromSessionWorkspace(
            userSessionToken, ws_file_path)
        outputStream = FileOutputStream(File(incoming_file_path))
        IOUtils.copyLarge(inputStream, outputStream)
        IOUtils.closeQuietly(inputStream)
        IOUtils.closeQuietly(outputStream)

    print("transaction.move from incoming folder: " + incoming_dir)
    transaction.moveFile(File(incoming_dir).getAbsolutePath(), dataset)
    #    temp_dir = prepareFilesForRegistration(transaction, file_names)

    # ...and delete all files from the session workspace
    # TODO: delete it later
    #dss_service = ServiceProvider.getDssServiceRpcGeneric().getService()
    #for file_name in file_names:
    #    file_path = os.path.join(temp_dir, file_name)
    #    dss_service.deleteSessionWorkspaceFile(userSessionToken, file_name)

    return dataset.getDataSetCode()