예제 #1
0
def main():

    name = print_header()
    form = cgi.FieldStorage()
    WebUpload_ryw.setup_logging()


    #
    # get objstr.
    #
    success,objID,version = ryw.get_obj_str2(form)
    if not success:
        ryw.give_bad_news('CloneVersion: failed to get objstr.',
                          logging.critical)
        ryw_upload.quick_exit(1)

    message = 'CloneVersion: ' + objID + '#' + str(version)
    logging.info(message)
    ryw.db_print2("<BR>" + message + "<BR>", 23);


    #
    # open search file.
    #
    success,searchFile = ryw.open_search_file(
        'CloneVerson',
        os.path.join(RepositoryRoot, 'WWW', 'logs'),
        'upload.log',
        os.path.join(RepositoryRoot, 'SearchFile'),
        False)
    if not success:
        if searchFile:
            searchFile.done()
        ryw.give_bad_news('CloneVersion: ' +
                          'open search file failed. ',
                          logging.critical)
        ryw_upload.quick_exit(1)
    else:
        ryw.db_print2("search file opened." + "<BR>", 23);


    #
    # get meta and paths.
    #
    success,paths,meta = DisplaySelection.get_all_paths(
        objID, version, skipLock=False, searchFile=searchFile,
        allowNullSearchFile=False)
    if success:
        ryw.db_print_info_browser('CloneVersion: paths: ' + repr(paths), 24)
        ryw.db_print_info_browser('CloneVersion: meta: ' + repr(meta), 29)
    else:
        ryw_upload.quick_exit(1)
        
    if (searchFile):
        searchFile.done()


    #
    # we do want to clone the data if it were a list.
    #
    isList = ryw_meta.isList(meta)
    if isList:
        dataPath = paths[0]
        selName = DisplaySelection.get_sel_name(dataPath)
        if not selName:
            ryw.give_bad_news(
                'CloneVersion: isList but failed to get selection name.',
                logging.error)
            ryw_upload.quick_exit(1)
        selPath = os.path.join(dataPath, selName)
    else:
        selPath,selName = None,None
            

    #
    # change meta.
    #
    meta = change_meta(meta, name)
                                   

    #
    # deal with auxi dir.
    #
    originalAuxiDir = paths[2]
    newAuxiDir = None
    tmpdir = None
    if os.path.exists(originalAuxiDir):
        tmpdir = WebUpload_ryw.attempt_make_tmpdir()
        if not tmpdir:
            ryw_upload.quick_exit(1)
        newAuxiDir = os.path.join(tmpdir, '_AUXI')

        message = 'CloneVersion: shutil.copytree(): ' + \
                  originalAuxiDir + '  ->  ' + newAuxiDir

        try:
            shutil.copytree(originalAuxiDir, newAuxiDir)
        except:
            ryw.give_bad_news('failed: ' + message, logging.critical)
            ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)

        ryw.db_print_info_browser(message, 29)            
                              

    #
    # Now try to put a new object in the repository.
    # note that the version number will be incremented.
    #
    # "selPath" used to be just None.
    # when I added cloning list, I'm just using this to pass in the
    # path name of the selection file.
    #
    if not WebUpload_ryw.try_upload_object(meta, selPath, newAuxiDir,
                                           cloneVersion=True):
        ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)


    searchFile = WebUpload_ryw.show_one_server_object(meta)
    searchFile.done()
        
    ryw_upload.cleanup_and_exit(tmpdir, None, None, 0,
                                successMessage = 'clone version completed.')
예제 #2
0
def add_to_object_store(values, data, objectstoreroot, objectid, version,
                        auxDir, cloneVersion=False):
    """put (DATA, META, DONE) in the object store."""

    ##################################################
    # check free disk space.
    ##################################################
    if not cloneVersion and \
       not ryw_uploadObj.check_free_space(values, objectstoreroot):
        return (False, None, None, None, None, None)
    
    ##################################################
    # acquire prefix.
    ##################################################
    try:
        prefix = objectstore.nameversiontoprefix(objectstoreroot, objectid,
                                                 version)
    except:
        ryw.give_bad_news(
            'fatal_error: add_to_object_store: objectstore.nameversiontoprefix() failed.',
            logging.critical)
        return (False, None, None, None, None, None)
    
    datapath = prefix + '_DATA'
    metapath = prefix + '_META'
    donepath = prefix + '_DONE'
    auxpath  = prefix + '_AUXI'

    ##################################################
    # create parent directory.
    ##################################################
    parent = None
    try:
        parent = su.createparentdirpath(prefix)
        logging.debug('add_to_object_store: created parent dir: ' + parent)
    except:
        ryw.give_bad_news(
            'fatal_error: add_to_object_store: su.createparentdirpath() failed.',
            logging.critical)
        return (False, parent, None, None, None, None)
    
    if os.path.exists(donepath):
        ryw.give_news(
            'add_to_object_store: destination object already exists: ' +
            donepath, logging.warning)
        return (True, parent, datapath, metapath, auxpath, donepath)

    datapath = os.path.abspath(os.path.normpath(datapath))
    if (not cloneVersion):
        data = os.path.abspath(os.path.normpath(data))
        logging.debug('add_to_object_store: ' + datapath + ' ' +
                      metapath + ' ' + donepath + '.  source: ' + data)
    auxpath = os.path.abspath(os.path.normpath(auxpath))


    ##################################################
    # move data into the object store.
    ##################################################
    if cloneVersion:
        try:
            os.mkdir(datapath)
            ryw.db_print_info_browser('add_to_object_store: mkdir: ' +
                                      datapath, 29)
        except:
            ryw.give_bad_news(
                'fatal_error: add_to_object_store: cloneVersion mkdir: ' +
                datapath, logging.critical)
            return (False, parent, datapath, None, None, None)

        #
        # now deal with cloning a list, if necessary.
        #
        isList = ryw_meta.isList(values)
        if isList:
            baseName = os.path.basename(data)
            dirName = os.path.dirname(data)
            ryw.db_print2('UploadObject.add_to_object_store: basename: ' +
                          baseName, 44)
            ryw.db_print2('UploadObject.add_to_object_store: dirname: ' +
                          dirName, 44)
            destPath = os.path.join(datapath, baseName)
            srcChapterPath = os.path.join(dirName,
                                          ChapterList.CHAPTER_LIST_NAME)
            destChapterPath = os.path.join(datapath,
                                           ChapterList.CHAPTER_LIST_NAME)
            try:
                shutil.copyfile(data, destPath)
                ryw.db_print_info_browser(
                    'add_to_object_store: ' +
                    data + ' --> ' + destPath, 36)
                if os.path.exists(srcChapterPath):
                    shutil.copyfile(srcChapterPath, destChapterPath)
                    ryw.db_print_info_browser(
                        'UploadObject.add_to_object_store: ' +
                        srcChapterPath + ' --> ' + destChapterPath, 44)
                else:
                    ryw.db_print_info_browser(
                        'UploadObject.add_to_object_store: no chapter file.',
                        44)
            except:
                ryw.give_bad_news(
                    'add_to_object_store: failed to copy selection file.',
                    logging.critical)
                return (False, parent, datapath, None, None, None)
        
    elif os.path.isdir(data):
        try:
            mymove(data, datapath)
            logging.debug('add_to_object_store: moved dir: ' + data + ' ' +
                          datapath)
        except:
            ryw.give_bad_news(
                'fatal_error: add_to_object_store: move dir failed: ' +
                data + ' ' + datapath,
                logging.critical)
            return (False, parent, datapath, None, None, None)
        
    else:
        try:
            os.mkdir(datapath)
            mymove(data, os.path.join(datapath, os.path.basename(data)))
            logging.debug('add_to_object_store: moved file: ' + data + ' ' +
                          os.path.join(datapath, os.path.basename(data)))
        except:
            ryw.give_bad_news(
                'fatal_error: add_to_object_store: move file failed: ' +
                data + ' ' + datapath,
                logging.critical)
            return (False, parent, datapath, None, None, None)

    ##################################################
    # put metadata in the object store.
    ##################################################
    try:
        su.pickdump(values, metapath)
        logging.debug('add_to_object_store: written metapath: ' + metapath)
    except:
        ryw.give_bad_news(
            'fatal_error: add_to_object_store: su.pickdump() failed.',
            logging.critical)
        return (False, parent, datapath, metapath, None, None)

    ##################################################
    # put aux files in the object store.
    ##################################################
    try:
        if auxDir and os.path.exists(auxDir) and os.path.isdir(auxDir):
            mymove(auxDir, auxpath)
            logging.debug('add_to_object_store: moved auxpath: ' + auxpath)
    except:
        ryw.give_bad_news(
            'add_to_object_store: moving aux directory failed: ' + auxDir +
            ' -> ' + auxpath, logging.critical)
        return (False, parent, datapath, metapath, auxpath, None)


    ##################################################
    # put DONE flag in the object store.
    ##################################################
    try:
        f = open(donepath, 'w')
        f.close()
        logging.debug('add_to_object_store: written donepath: ' + donepath)
    except:
        ryw.give_bad_news(
            'fatal_error: add_to_object_store: write DONE file failed.',
            logging.critical)
        return (False, parent, datapath, metapath, auxpath, donepath)

    # TODO: In case of ANY failure, upload request copied to a REJECTED folder which can be looked upon later.

    return (True, parent, datapath, metapath, auxpath, donepath)