Exemplo n.º 1
0
Arquivo: util.py Projeto: bbcf/biorepo
def manage_fu_from_HTS(existing_fu, meas, filename, sha1, url_path, hts_path):

    #fixing bug str "true", str "false" for meas.type
    if isinstance(meas.type, basestring):
        bool_type = str2bool(meas.type)
        meas.type = bool_type

    if existing_fu:
        print "-------- EXISTING FILE FROM HTS--------"
        meas.fus.append(existing_fu)

        DBSession.add(meas)
        DBSession.flush()
        return existing_fu
        ###########################################################################################
    else:
        #new files_up building
        fu = Files_up()
        fu.path = hts_path
        #save the filename and the extension to the database
        fu.filename = filename
        if '.' in filename:
            extension = filename.split('.')[-1]
            fu.extension = extension
        else:
            fu.extension = "not specified"
        fu.sha1 = sha1
        fu.url_path = url_path

        #add to the crossing table (measurement <-> file uploaded)
        meas.fus.append(fu)

        #adding new measurement and new files_up to the db
        DBSession.add(meas)
        DBSession.add(fu)
        DBSession.flush()
        return fu
Exemplo n.º 2
0
Arquivo: util.py Projeto: bbcf/biorepo
def manage_fu_from_HTS(existing_fu, meas, filename, sha1, url_path, hts_path):

    #fixing bug str "true", str "false" for meas.type
    if isinstance(meas.type, basestring):
        bool_type = str2bool(meas.type)
        meas.type = bool_type

    if existing_fu:
        print "-------- EXISTING FILE FROM HTS--------"
        meas.fus.append(existing_fu)

        DBSession.add(meas)
        DBSession.flush()
        return existing_fu
        ###########################################################################################
    else:
        #new files_up building
        fu = Files_up()
        fu.path = hts_path
        #save the filename and the extension to the database
        fu.filename = filename
        if '.' in filename:
            extension = filename.split('.')[-1]
            fu.extension = extension
        else:
            fu.extension = "not specified"
        fu.sha1 = sha1
        fu.url_path = url_path

        #add to the crossing table (measurement <-> file uploaded)
        meas.fus.append(fu)

        #adding new measurement and new files_up to the db
        DBSession.add(meas)
        DBSession.add(fu)
        DBSession.flush()
        return fu
Exemplo n.º 3
0
Arquivo: util.py Projeto: bbcf/biorepo
def manage_fu(existing_fu, meas, public_dirname, filename, sha1, up_data, url_path, url_up, dest_raw, dest_processed, tmp_path, lab):

    tmpdir_to_delete = os.path.abspath(os.path.join(tmp_path, os.path.pardir))
    #fixing bug str "true", str "false" for meas.type
    if isinstance(meas.type, basestring):
        bool_type = str2bool(meas.type)
        meas.type = bool_type

    if existing_fu:
        print "-------- EXISTING FILE --------"
        #create symbolic link
        source = existing_fu.path + "/" + sha1

        if meas.type:
            dest = dest_raw + sha1
            #test symlin existance
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        else:
            dest = dest_processed + sha1
            #test symlink existance
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        meas.fus.append(existing_fu)

        DBSession.add(meas)
        DBSession.flush()

        flash(symlink_e + ", measurement created")
        #remove the tmp file if it didn't come from HTSStation
        if not tmpdir_to_delete.startswith('/data'):
            shutil.rmtree(tmpdir_to_delete)
        #raise redirect("./")
        return existing_fu

        ###########################################################################################
    else:
        #new files_up building
        fu = Files_up()
        #raw test for path orientation
        if meas.type:
            fu.path = path_raw(lab)
            data_dirname = os.path.join(public_dirname, fu.path)
        else:
            fu.path = path_processed(lab)
            data_dirname = os.path.join(public_dirname, fu.path)

        #save the filename and the extension to the database
        fu.filename = filename
        if '.' in filename:
            extension = filename.split('.')[-1]
            fu.extension = extension
#            root, ext = os.path.splitext(filename)
#            DOUBLE_EXTENSIONS = ['tar.gz','tar.bz2','bedGraph.gz','bed.gz']
#            if any([filename.endswith(x) for x in DOUBLE_EXTENSIONS]):
#                root, first_ext = os.path.splitext(root)
#                ext = first_ext + ext
#                fu.extension = ext
#            else:
#                fu.extension = ext
        else:
            fu.extension = "not specified"
        data_path = os.path.join(data_dirname, str(sha1))
        fu.sha1 = sha1
        fu.url_path = url_path

        #writing the file into the server HD
        #version browser
        try:
            with open(data_path, "w") as d:
                if up_data is not None:
                    d.write(up_data.value)
                elif url_path is not None and url_up:

                    u = urllib2.urlopen(url_path)
                    while True:
                        buffer = u.read(8192)
                        if not buffer:
                            break

                        d.write(buffer)
                else:
                    print "ERROR"
        #version commandline
        except:
            shutil.move(tmp_path, data_path)

        #symlink
        source = data_path
        if meas.type:
            dest = dest_raw + sha1
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        else:
            dest = dest_processed + sha1
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        #add to the crossing table (measurement <-> file uploaded)
        meas.fus.append(fu)

        #adding new measurement and new files_up to the db
        DBSession.add(meas)
        DBSession.add(fu)

        DBSession.flush()

        if meas.type:
            flash(symlink_e + ", raw data was successfully created")
        else:
            flash(symlink_e + ", processed data was successfully created")

        #remove the tmp file if it didn't come from HTSStation
        if not tmpdir_to_delete.startswith('/data'):
            shutil.rmtree(tmpdir_to_delete)

        return fu
Exemplo n.º 4
0
Arquivo: util.py Projeto: bbcf/biorepo
def manage_fu(existing_fu, meas, public_dirname, filename, sha1, up_data,
              url_path, url_up, dest_raw, dest_processed, tmp_path, lab):

    tmpdir_to_delete = os.path.abspath(os.path.join(tmp_path, os.path.pardir))
    #fixing bug str "true", str "false" for meas.type
    if isinstance(meas.type, basestring):
        bool_type = str2bool(meas.type)
        meas.type = bool_type

    if existing_fu:
        print "-------- EXISTING FILE --------"
        #create symbolic link
        source = existing_fu.path + "/" + sha1

        if meas.type:
            dest = dest_raw + sha1
            #test symlin existance
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        else:
            dest = dest_processed + sha1
            #test symlink existance
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        meas.fus.append(existing_fu)

        DBSession.add(meas)
        DBSession.flush()

        flash(symlink_e + ", measurement created")
        #remove the tmp file if it didn't come from HTSStation
        if not tmpdir_to_delete.startswith('/data'):
            shutil.rmtree(tmpdir_to_delete)
        #raise redirect("./")
        return existing_fu

        ###########################################################################################
    else:
        #new files_up building
        fu = Files_up()
        #raw test for path orientation
        if meas.type:
            fu.path = path_raw(lab)
            data_dirname = os.path.join(public_dirname, fu.path)
        else:
            fu.path = path_processed(lab)
            data_dirname = os.path.join(public_dirname, fu.path)

        #save the filename and the extension to the database
        fu.filename = filename
        if '.' in filename:
            extension = filename.split('.')[-1]
            fu.extension = extension


#            root, ext = os.path.splitext(filename)
#            DOUBLE_EXTENSIONS = ['tar.gz','tar.bz2','bedGraph.gz','bed.gz']
#            if any([filename.endswith(x) for x in DOUBLE_EXTENSIONS]):
#                root, first_ext = os.path.splitext(root)
#                ext = first_ext + ext
#                fu.extension = ext
#            else:
#                fu.extension = ext
        else:
            fu.extension = "not specified"
        data_path = os.path.join(data_dirname, str(sha1))
        fu.sha1 = sha1
        fu.url_path = url_path

        #writing the file into the server HD
        #version browser
        try:
            with open(data_path, "w") as d:
                if up_data is not None:
                    d.write(up_data.value)
                elif url_path is not None and url_up:

                    u = urllib2.urlopen(url_path)
                    while True:
                        buffer = u.read(8192)
                        if not buffer:
                            break

                        d.write(buffer)
                else:
                    print "ERROR"
        #version commandline
        except:
            shutil.move(tmp_path, data_path)

        #symlink
        source = data_path
        if meas.type:
            dest = dest_raw + sha1
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        else:
            dest = dest_processed + sha1
            if os.path.islink(dest):
                symlink_e = "This file was already in your personal folder"
            else:
                symlink_e = "This file was added to your personal folder"
                os.symlink(source, dest)

        #add to the crossing table (measurement <-> file uploaded)
        meas.fus.append(fu)

        #adding new measurement and new files_up to the db
        DBSession.add(meas)
        DBSession.add(fu)

        DBSession.flush()

        if meas.type:
            flash(symlink_e + ", raw data was successfully created")
        else:
            flash(symlink_e + ", processed data was successfully created")

        #remove the tmp file if it didn't come from HTSStation
        if not tmpdir_to_delete.startswith('/data'):
            shutil.rmtree(tmpdir_to_delete)

        return fu