def patch_meta_list_with_chapters(self, metaList=None):
        """10/24/08: called by ShowQueue.go_through_list() when
        it's initiated by DisplaySelection.py.
        metaList is sent in from ShowQueue.go_through_list().
        it's otherwise None."""

        if not metaList:
            metaList = self.metaList

        if not metaList:
            ryw.give_bad_news('patch_meta_list_with_chapters: ' +
                              'unexpected empty metaList.', logging.critical)
            return

        for meta in metaList:
            objstr = meta['id'] + '#' + str(meta['version'])
            ryw.db_print2('patch_meta_list_with_chapters: objstr: ' + objstr,
                          41)

            if not self.chapterDict.has_key(objstr):
                continue

            chapter = self.chapterDict[objstr][2]
            if chapter:
                meta['chapter_number'] = chapter
                ryw.db_print2('patch_meta_list_with_chapters: ' +
                              'chapter patched: ' + chapter, 41)
def get_file_paths2(objID, version, skipLk=False, searchFile=None,
                  allowNullSearchFile = False):
    """10/21/08: rewritten to return chapterListFile as well."""
    
    success,dataPath = get_path(objID, version, skipLock = skipLk,
                                searchFile = searchFile,
                                allowNullSearchFile = allowNullSearchFile)
    if not success:
        return None

    name = get_sel_name(dataPath)
    if not name:
        ryw.give_bad_news(
            'DisplaySelection: failed to get selection file name:<br>'+
            dataPath, logging.error)
        return None

    rfpath = os.path.join(dataPath, name)

    chapterListName = get_chapterlist_name(dataPath)
    chapterListFullName = os.path.join(dataPath,
                                       ChapterList.CHAPTER_LIST_NAME)
    ryw.db_print2('get_file_paths2: full name is: ' +
                  chapterListFullName, 39)
    
    return (rfpath, chapterListName, chapterListFullName)
def launchExplorer(path):
    ryw.db_print2('launchExplorer: path is: ' + path, 59)
    try:
        return ryw_bizarro.launch_explorer(path)
    except:
        ryw.give_bad_news("Failed to launch Explorer",logging.warning)
	return False
    def initialize_with_meta_list(self, reqList, searchFile, reverseLists):
        """called by ChapterListForm() to initialize chapter list from
        the meta data.
        modeled after ShowQueue.go_through_list().
        also called by ChapterListFormHandle()."""

        success = True
        
        if not searchFile or not reverseLists:
            raise NameError('ChapterList.initialize_with_meta_list:' +
                            'bad searchFile or bad reverseLists.')

        metaList = ryw_meta.get_meta_list(reqList, searchFile)
        searchFile.done()

        metaList = ryw.sortmeta_chapter_number(metaList)

        ryw.db_print2('initialize_with_meta_list: done sorting.', 38)

        numMatches = len(metaList)

        if numMatches <= 0:
            ryw.give_news('ChapterList: the selection is empty.<br>',
                          logging.error)
            success = False
        else:
            self.chapterDict = {}
            self.itemList = []
            self.metaList = metaList

            for meta in metaList:
                objID = meta['id']
                version = meta['version']
                objstr = objID + '#' + str(version)
                self.itemList.append(objstr)

                title = ''
                if meta.has_key('title'):
                    title = meta['title']

                chapter = None
                if meta.has_key('chapter_number'):
                    chapter = meta['chapter_number']

                alias = None
                if meta.has_key('content_alias'):
                    alias = meta['content_alias']

                self.chapterDict[objstr] = [alias, title, chapter]

            ryw.db_print2('chapterDict is: ' + repr(self.chapterDict), 41)
        
        return success
def flock_lock(flockSelf):
    fl = flockSelf
    ryw.db_print2("ryw_linux:flock_lock(): file is: " + fl.file, 56)
    if fl.type["LOCK_EX"]:
        lockFlags = portalocker.LOCK_EX
    else:
        lockFlags = portalocker.LOCK_SH

    if fl.type["LOCK_NB"]:
        lockFlags = lockFlags | portalocker.LOCK_NB

    portalocker.lock(fl.hfile, lockFlags)
    def process_form(self, form):
        """called by ChapterListFormHandle.py."""

        selLen = form.getfirst("selection_length", '')
        if selLen == '':
            ryw.give_bad_news('ChapterListFormHandle: ' +
                              'failed to get selection_length', logging.error)
            return (False, None)
        
        selLen = int(selLen)
        ryw.db_print2('process_form: selection length is: ' + str(selLen), 40)

        chapterList = {}

        for i in range(0,selLen):

            objField = 'item_objstr' + str(i)
            itemStr = form.getfirst(objField, '')
            if itemStr:
                ryw.db_print2('process_form: found objstr: ' + itemStr, 40)
            else:
                continue
        
            chapterField = 'chapter_number' + str(i)
            chapterStr = form.getfirst(chapterField, '')
            chapterStr = chapterStr.strip()
            if chapterStr:
                ryw.db_print2('process_form: found chapter: ' + itemStr, 40)
            else:
                continue
            chapterList[itemStr] = chapterStr
            ryw.db_print2('process_form: ' + itemStr + ' -> ' + chapterStr, 41)

        self.formEntries = chapterList
        return (True, chapterList)
def move_stuff(src, dst):
    """used to be UploadObject.mymove()"""

    volSrc = win32file.GetVolumePathName(src)
    volDst = win32file.GetVolumePathName(dst)
    func = None
    if volSrc == volDst: 	# same physical drive
        func = shutil.move
    elif os.path.isdir(src):	# diff drive, src is dir
        func = shutil.copytree
    else:			# diff drive, src is file
        func = shutil.copy 
    func(src,dst)
    ryw.db_print2('ryw_xp:move_stuff: src is: ' + src +
                  ' --->  dst is: ' + dst, 56)
def matches_disjunction(meta, query):
    ## query is a list of literals
    ## at least one of them must match

    ryw.db_print2('matches_disjunction: query is: ' + repr(query), 53)
    
    for literal in query:
        ryw.db_print2('matches_disjunction: literal is: ' + repr(literal), 53)
	#
	# a "literal" is like:
	# ('all_keys_concatenated', ['contains', 'nocase'], 'lecture')
	#
        if matches_literal(meta, literal):
	    return True

    return False
    def read_file(self, fullFileName):
        """called by read_list_and_merge() below."""

        if not os.path.exists(fullFileName):
            self.chapterList = None
            return True

        try:
            self.chapterList = su.pickload(fullFileName)
        except:
            ryw.give_bad_news('ChapterList.read_file: pickload failed: ' +
                              fullFileName, logging.error)
            return False

        ryw.db_print2('ChapterList.read_file succes: ' + fullFileName, 41)
        ryw.db_print2('ChapterList.read_file: ' + repr(self.chapterList), 41)
        return True
def output_text(outputFile, meta, firstLine=False):
    """mirrors ryw_view.get_auto_hindi_translation_string()."""

    f = outputFile
    count = 0
    ryw.db_print_info_browser("\n", 89)

    for type in ["title", "description"]:
        if not meta.has_key(type):
            continue
        allText = meta[type]
        textList = ryw.split_content(allText)

        for text in textList:
            md5sum = ryw.md5sum(text)

            #
            # if it's already in the dictionary, don't bother.
            #
            autoDict = google_english2hindi.GOOGLE_AUTO_DICT
            if autoDict.has_key(md5sum):
                ryw.db_print_info_browser("already found in dictionary: " + md5sum, 89)
                continue

            text = text.replace('"', '\\"')
            # text = text.replace("^M", "")
            outputStr = '"' + md5sum + '"' + " => " + '"' + text + '"'

            try:
                if not firstLine:
                    f.write(",")
                f.write("\n")

                f.write(outputStr)
                count += 1
                ryw.db_print2(outputStr, 89)
                firstLine = False
            except:
                ryw.give_bad_news("output_text: failed to write line: " + outputStr, logging.error)
                return (False, count)

    return (True, count)
def process_tricky_attributes(form, meta, filename):
    """hidden and obscure attributes."""

    meta['upload_datetime'] = repr(datetime.datetime.now())
    meta['upload_datetime_real'] = repr(datetime.datetime.now())
    #meta['upload_datetime'] = datetime.datetime.now()
    logging.debug('process_tricky_attributes: ' +
                  repr(meta['upload_datetime']))

    if not process_date_time(form, meta):
        return False    
    
    objectID = form.getfirst('object_ID', '')
    if not objectID:
        try:
            objectID = objectstore.generateNewObjectID()
        except:
            ryw.give_bad_news('fatal_error: unable to obtain new object ID.',
                              logging.critical)
            return False
    meta['id'] = objectID

    path = form.getfirst('path', '')
    if not path:
        contentType = form.getfirst('content_type', '')
        if not contentType:
            ryw.give_bad_news('fatal_error: unable to determine content type.',
                              logging.critical)
            return False
        now = datetime.datetime.now()
        monthStr = now.strftime('%y%m/')

        dateTimeRand = ryw.date_time_rand()
        filename = dateTimeRand + '_' + filename
        
        path = os.path.join('/upload', monthStr, contentType, filename)
    meta['path'] = os.path.normpath(path)
    ryw.db_print2('process_tricky_attributes: path is: ' + meta['path'], 57)
    
    return True
def go_through_object_store(objectStoreRoot, reverseLists):

    ryw.give_news2('go_through_object_store: entered...<BR>', logging.info)

    searchFile = reverseLists.searchFile
    for objID,version in objectstore.objectversioniterator(objectStoreRoot):

        objstr = objID + '#' + str(version)
        ryw.db_print2('examining ' + objstr + '...<BR>', 7)

        success,meta = searchFile.get_meta(objID, version)
        if not success or not meta:
            ryw.give_bad_news('go_through_object_store: get_meta2 failed: ' +
                               objstr, logging.warning)
            continue

        isList = meta.has_key('sys_attrs') and 'isList' in meta['sys_attrs']
        if not isList:
            ryw.db_print2('go_through_object_store: not a list object: ' +
                         objstr + '<BR>', 6)
            ryw.give_news2(' . ', logging.info)
            continue
        ryw.db_print2('go_through_object_sotre: found a list.<BR>', 6)

        containees = ReverseLists.read_container_file(
            objID, version, searchFile, RepositoryRoot)
        ryw.db_print2('go_through_object_store: containees are: ' +
                      repr(containees) + '<BR>', 7)
        if not reverseLists.add(objstr, containees):
            ryw.give_bad_news('go_through_object_store: ReverseLists.add ' +
                              'failed: ' + objstr, logging.error)
        else:
            ryw.db_print2('go_through_object_store: successfully added.<BR>',
                          7)
            alias = 'unnamed'
            if meta.has_key('content_alias'):
                alias = meta['content_alias']
            ryw.give_news2(objstr + ' ' + alias + ', ', logging.info)

    ryw.give_news2('<BR>done.', logging.info)
def add_queue(meta, searchFile, repositoryRoot):
    """called by UploadQueue.py, after saving a selection.
    returns success."""
    
    objID = meta['id']
    version = meta['version']

    logging.info('add_queue: entered: ' + objID)
    ryw.db_print2('add_queue: entered: ' + objID, 12)

    success,reverseLists = open_reverse_lists(
        'add_queue:', '', '',
        os.path.join(repositoryRoot, 'ReverseLists'), True,
        searchFile = searchFile,
        repositoryRoot = repositoryRoot)
    if not (success and reverseLists):
        ryw.give_bad_news('add_queue: failed to open ReverseLists.',
                          logging.critical)
        if reverseLists:
            reverseLists.done()
        return False

    containees = read_container_file(objID, version,
                                     searchFile, repositoryRoot)
    ryw.db_print2('add_queue: found containees: ' + repr(containees), 12)
    if not containees or containees == []:
        return True
    
    success = reverseLists.add(objID + '#' + str(version), containees)
    reverseLists.done()
    ryw.db_print2('add_queue: finished.', 12)
    return success
def create_view_path(values, resources, objectid, version):
    """create the view path."""

    if not values.has_key('path'):
        ryw.give_bad_news('warning: no view path given.', logging.warning)
        return (True, None)
    path = values['path']
    ryw.db_print2('create_view_path: path is: ' + path, 57)

    if not resources.has_key('viewroot'):
        ryw.give_bad_news('fatal_error: no viewroot in resources file.',
                          logging.critical)
        return (False, None)
    viewroot = resources['viewroot']
    ryw.db_print2('create_view_path: viewroot is: ' + viewroot, 57)
    path = os.path.join(viewroot, path)
    ryw.db_print2('create_view_path: now path is: ' + path, 57)

    success = ryw_uploadObj.create_view_path(path, objectid, version)
    return (success, path)
    def compare_form_entries_against_meta(self):
        """compared the chapter numbers entered in a form
        against what's in the meta.  if it's the same as the
        default contained in meta, then we don't need to
        write that down in the local chapter list.
        called by ChapterListFormHandle.py."""

        formEntries = self.formEntries
        self.chapterList = copy.copy(formEntries)

        if self.chapterList == {}:
            return
        
        for objstr,chapterStr in formEntries.iteritems():

            #
            # if the form has nothing for this object, then skip it.
            #
            if not formEntries.has_key(objstr):
                continue
            else:
                ryw.db_print2('compare_form_entries_against_meta: ' +
                              'form entry says: ' + formEntries[objstr], 41)

            #
            # if the meta data somehow says it has nothing,
            # then there's no need to for me delete anything from the
            # form entry list.
            #
            if not self.chapterDict.has_key(objstr):
                ryw.db_print2('compare_form_entries_against_meta: ' +
                              'objstr not found in chapterDict: ' + objstr,
                              41)
                continue
            metaChapter = self.chapterDict[objstr][2]
            if metaChapter:
                ryw.db_print2('compare_form_entries_against_meta: ' +
                              'meta chapter is: ' + metaChapter, 41)
            else:
                continue
            
            #
            # both the form entry and the metadata says something.
            # so we should compare them to see if I should omit the form
            # entry in case they are the same thing.
            #
            if (formEntries[objstr] == metaChapter):
                ryw.db_print2('compare_form_entries_against_meta: ' +
                              'found to be same: ' + metaChapter, 41)
                del self.chapterList[objstr]
            else:
                ryw.db_print2('compare_form_entries_against_meta: ' +
                              'found to be different: ' + metaChapter, 41)
                continue
            
        ryw.db_print2('compare_form_entries_against_meta: ' +
                      'the list to be written is: ' +
                      repr(self.chapterList), 41)
def uploadobject(values, data, auxDir, hasVersion = False,
                 cloneVersion=False):
    """update metadata file.
    upload object into object store.
    create view path."""

    logging.debug('UploadObject.uploadobject entered.')
    ryw.db_print2('uploadobject: values are: ' + repr(values), 57)

    ##################################################
    # check attributes.
    ##################################################
    success,resources = check_attributes(values)
    if not success:
        return False

    objectid = values['id']
    #
    # not strictly necessary, but I'm doing this to hardwire all
    # places of gettting objectstoreroot.
    #
    #objectstoreroot = values['objectstore']
    objectstoreroot = ryw.hard_wired_objectstore_root()
    logging.debug('uploadobject: passed check_attributes: '+
                  objectstoreroot + ' ' + objectid)


    ##################################################
    # give metadata to the search server.
    ##################################################
    success,version = add_to_search_file(values, hasVersion, cloneVersion)
    if not success:
        remove_from_search_file(objectid, version)
        return False
    else:
        ryw.db_print_info_browser('uploadobject: ' + objectid + '#' +
                                  str(version), 29)        


    ##################################################
    # put stuff into the object store.
    ##################################################
    success,parent,datapath,metapath,auxpath,donepath = \
        add_to_object_store(values, data, objectstoreroot, objectid, version,
                            auxDir, cloneVersion)
    if not success:
        remove_from_search_file(objectid, version)
        remove_from_object_store(parent, datapath, metapath, auxpath, donepath)
        return False


    ##################################################
    # create view path.
    ##################################################
    if not cloneVersion:
        success,path = create_view_path(values, resources, objectid, version)
        #ryw.give_news('uploadobject: introducing error...', logging.debug)
        #success = False
        if not success:
            remove_from_search_file(objectid,version)
            remove_from_object_store(parent, datapath, metapath,
                                     auxpath, donepath)
            remove_view_path(path)
            return False

    # logging.debug('just for testing...')
    # remove_view_path(path)
    # return False
    
    return True
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)
def main():
    """modeled after DisplaySelection.py.
    This is for displaying a form that allows editing the chapter list."""

    name = ShowQueue.init_log()
    ryw_view.print_header_logo()
    print '<TITLE>Edit the Chapter List</TITLE>'

    success,objID,version = ryw.get_obj_str()
    if not success:
        ryw.give_bad_news('ChapterListForm: failed to get objstr.',
                          logging.critical)
        DisplaySelection.exit_now(1)

    success,searchFile,reverseLists = \
        ReverseLists.open_searchfile_reverselists(
        'DisplaySelection.main:')
    if not success:
        DisplaySelection.exit_now(1)

    print '<BR><B>Edit chapter names for this selection:</B><BR><BR>'

    success = DisplaySelection.print_title(objID, version, name, searchFile)
    if not success:
        DisplaySelection.exit_now(1)

    ryw.db_print2('<BR>ChapterListForm.py entered.<BR>', 38)
    
    nameTriple = DisplaySelection.get_file_paths2(
        objID, version, searchFile = searchFile)
    if not nameTriple:
        DisplaySelection.exit_now(1)
    rfpath,chapterListPath,chapterFullName = nameTriple
    if not rfpath:
        DisplaySelection.exit_now(1)

    reqList = ShowQueue.read_list(rfpath)
    if not reqList:
        ryw.give_news('This selection is empty.<BR>', logging.info)
        print '<BR>'
        print_links(objID, version, name)
        DisplaySelection.exit_now(0)

    ryw.db_print2('ChapterListForm.py: gotten selection list.', 38)

    success,chapterList = ChapterList.create_and_initialize(
        objID + '#' + str(version),
        reqList, searchFile, reverseLists,
        chapterFullName)
    if not success:
        ryw.give_bad_news('ChapterList.create_and_initialize failed: ' +
                          objID + '#' + str(version), logging.error)
        DisplaySelection.exit_now(1)

    page = chapterList.make_form_string()
    print page
    
    ryw_view.print_footer()

    reverseLists.done()
    searchFile.done()
def main():
    """main function processing upload."""

    # initialization.
    name = print_header()
    form = cgi.FieldStorage()
    setup_logging()

    localSuccess, localFound, localFilePath, localIsDir = ryw_upload.check_local_file(form)
    if not localSuccess:
        ryw.give_bad_news("check_local_file failed.", logging.error)
        ryw_upload.quick_exit(1)

    if not ryw_upload.check_required_fields(form, checkFile=not localFound):
        ryw_upload.quick_exit(1)

    if localFound:
        buf = None
    else:
        # just read a tiny bit to see if we have an empty upload file.
        buf = ryw_upload.attempt_read_uploaded_file(form, "local_filename")
        if not buf:
            ryw_upload.quick_exit(1)

    localExcerptResult = ryw_upload.check_local_file(form, fieldName="local_excerpt_filename")

    # check aux file uploads: thumbnails, exerpts...
    success, auxExists, aux = ryw_upload.check_aux_file_uploads(form, localExcerptStuff=localExcerptResult)
    if not success:
        ryw_upload.quick_exit(1)

    tmpdir = attempt_make_tmpdir()
    if not tmpdir:
        ryw_upload.quick_exit(1)

    success, auxDir, auxInfo = ryw_upload.read_aux_files(
        form, aux, tmpdir, auxExists, localExcerptStuff=localExcerptResult
    )
    if not success:
        ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)

    filename = ryw_upload.decide_tmp_data_file_name(form, localPath=localFilePath, isLocalDir=localIsDir)
    if not filename:
        ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)

    success, found, bytes = ryw_upload.copy_local_file_for_upload(
        form, tmpdir, filename, localFound, localFilePath, localIsDir
    )
    if not success:
        ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)
    kB = math.ceil(bytes / 1024.0)

    if not found:
        ryw.give_news2("<BR>Copying remote file...", logging.info)
        kB, bytes = ryw_upload.read_uploaded_file(form, buf, tmpdir, filename, "local_filename")
    if kB == 0:
        ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)

    meta = ryw_upload.try_process_attributes(name, form, filename, kB, bytes)
    if not meta:
        ryw_upload.cleanup_and_exit(tmpdir, None, None, 1)

    meta = ryw_upload.add_aux_attributes(meta, auxInfo)

    if not localIsDir:
        ryw_ffmpeg.try_exec(RepositoryRoot, meta, tmpdir, filename)

    success, metafile = ryw_upload.write_tmp_metafile(meta, tmpdir)
    if not success:
        ryw_upload.cleanup_and_exit(tmpdir, metafile, None, 1)

    if localIsDir:
        nameToUpload, extractDir = (os.path.join(tmpdir, filename), None)
    else:
        nameToUpload, extractDir = ryw_upload.try_unzip_file(form, tmpdir, filename, kB)
        if not nameToUpload:
            ryw_upload.cleanup_and_exit(tmpdir, metafile, extractDir, 1)

    ryw.give_news2("<BR>Storing the data in the repository...", logging.info)
    ryw.db_print2("<BR>" + "meta: " + repr(meta) + "<BR>", 57)
    ryw.db_print2("nameToUpload: " + nameToUpload + "<BR>", 22)
    ryw.db_print2("auxDir: " + repr(auxDir) + "<BR>", 22)
    if not try_upload_object(meta, nameToUpload, auxDir):
        ryw_upload.cleanup_and_exit(tmpdir, metafile, extractDir, 1)

    # ryw_view.show_server_object(meta)
    searchFile = show_one_server_object(meta)
    searchFile.done()

    # cgi.print_form(form)

    ryw_upload.cleanup_and_exit(tmpdir, metafile, extractDir, 0)
def flock_init(flockSelf, file):
    ryw.db_print2("ryw_linux.flock_init: file is: " + file, 56)
    fl = flockSelf
    fl.hfile = open(file, "a+")
def move_stuff(src, dst):
    """used to be UploadObject.mymove()"""

    shutil.move(src, dst)
    ryw.db_print2("ryw_linux:move_stuff: src is: " + src + " --->  dst is: " + dst, 56)
def main():
    """modeled after ChapterListForm.py.
    This is for displaying a form that allows editing the chapter list."""

    name = ShowQueue.init_log()
    ryw_view.print_header_logo()
    print '<TITLE>Processing the Chapter List</TITLE>'

    ryw.db_print2('<BR>ChapterListFormHandle.py: entered... <BR>', 39)

    form = cgi.FieldStorage()

    success,objID,version = ryw.get_obj_str(form = form)
    if not success:
        ryw.give_bad_news('ChapterListFormHandle: failed to get objstr.',
                          logging.critical)
        DisplaySelection.exit_now(1)

    ryw.db_print2('ChapterListFormHandle: objstr is: ' +
                  objID + '#' + str(version), 40)

    chapterList = ChapterList.ChapterList(objID + '#' + str(version))
    success,formEntries = chapterList.process_form(form)
    
    if not success:
        ryw.give_bad_news('ChapterListFormHandle: process_form() failed.',
                          logging.error)
        DisplaySelection.exit_now(1)
    
    success,searchFile,reverseLists = \
        ReverseLists.open_searchfile_reverselists(
        'DisplaySelection.main:')
    if not success:
        DisplaySelection.exit_now(1)

    success = DisplaySelection.print_title(objID, version, name, searchFile)
    if not success:
        DisplaySelection.exit_now(1)

    ryw.db_print2('<BR>ChapterListFormHandle.py entered.<BR>', 41)

    nameTriple = DisplaySelection.get_file_paths2(
        objID, version, searchFile = searchFile)
    if not nameTriple:
        DisplaySelection.exit_now(1)
    rfpath,chapterListPath,chapterListFullName = nameTriple
    if not rfpath:
        DisplaySelection.exit_now(1)

    ryw.db_print2('ChapterListFormHandle: chapter list file is: ' +
                  chapterListFullName, 41)

    reqList = ShowQueue.read_list(rfpath)
    if not reqList:
        ryw.give_news('This selection is empty.<BR>', logging.info)
        print '<BR>'
        print_links(objID, version, name)
        DisplaySelection.exit_now(0)

    ryw.db_print2('ChapterListForm.py: gotten selection list.', 41)

    success = chapterList.initialize_with_meta_list(
        reqList, searchFile, reverseLists)

    if not success:
        ryw.give_bad_news('ChapterListFormHandle: ' +
                          'initialize_with_meta_list failed.',
                          logging.error)
        DisplaySelection.exit_now(1)

    chapterList.compare_form_entries_against_meta()
    if not chapterList.write_file(chapterListFullName):
        ryw.give_bad_news('ChapterListFormHandle: write_file() failed.',
                          logging.error)
        DisplaySelection.exit_now(1)

    ryw.give_news('chapter list saved.', logging.info)
                              
    ryw_view.print_footer()

    reverseLists.done()
    searchFile.done()
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.')
def flock_unlock(flockSelf):
    fl = flockSelf
    ryw.db_print2("ryw_linux:flock_unlock(): file is: " + fl.file, 56)
    portalocker.unlock(fl.hfile)
    fl.hfile.close()
	def generate(self):
            """Main Browse View function."""
            ryw.print_header()
            self.print_initialHTML()
            form = cgi.FieldStorage()
            #	setup_logging()

            viewroot = form.getfirst('viewroot', '')
            if not viewroot:
                ryw.give_bad_news('viewroot not found',logging.error)
                sys.exit(1)

            ryw.db_print2('Browse.generate: viewroot is: ' + viewroot, 61)
                          
            if viewroot[-1] == os.sep:
	       	viewroot = viewroot[:-1]

            relpath = form.getfirst('relpath', os.sep)
            ryw.db_print2('Browse.generate: relpath1: ' + relpath, 61)
            relpath = ryw_bizarro.fix_browse_rel_path(relpath)
            ryw.db_print2('Browse.generate: relpath2: ' + relpath, 61)
            
            if relpath[0] != os.sep:
	       	relpath = os.sep + relpath
            if relpath[-1] != os.sep:
	       	relpath = relpath + os.sep

            ryw.db_print2('Browse.generate: relpath3: ' + relpath, 61)
            path = viewroot + relpath

            self.viewroot = viewroot
            self.relpath = relpath

            # path must be a directory

            if not os.path.exists(path):
	       	ryw.give_news('path no longer exists: ' + path,
	       		      logging.warning)
	       	sys.exit(1)
            ll = os.listdir(path)

            #success,searchFile = ryw.open_search_file(
            #    self.scriptName + ':', self.logDir, self.logFile,
            #    self.searchFile, False)
            #if not success:
            #    sys.exit(1)

            count=0

            title = relpath[1:-1]
            title = title.replace("\\"," &raquo; ")
            #print "<H2>The Digital StudyHall</H2>"
            ryw_view.print_logo()
            if title != "":
	       	print "<H3> content repository: %s </H3>" % (title,)
            else:
                print "<H3> content repository: </H3>"

            print "<TABLE border=0 width=100%><TR>"
            for i in ll:
                if not os.path.isfile(path+i):
                    print '<td width=20%%><A HREF="%s?viewroot=%s&relpath=%s%s"><img src="/icons/folder.gif" border=0><br>%s</A></td>' % (self.scriptName,viewroot,relpath,i,i)
                    count += 1
                    if (count == 5):
                        print "</TR><TR>"
                        count = 0
            while (count < 5):
	       	print "<td width=20%>&nbsp;</td>"
	       	count += 1
            print "</TR></TABLE>"
				      
				
            metalist = []

            for i in ll:
            
	       	if os.path.isfile(path + i):
                    # get its metadata from server and add option
                    # for 'add to downloadqueue'
                    f = open(path + i)
                    line = f.readline()
                    f.close()
                    if line[-1] == '\n':
                        line = line[:-1]
                    objname, version = line.split('#')
                    version = int(version)

                    #success,d = searchFile.get_meta(objname,
                    #				       version)
                    success,d = ryw.get_meta(self.absObjStoreRoot,
                                             objname,
                                             version)
                    if not success:
                        logging.warning(
                            self.scriptName + ': get_meta failed: ' +
                            objname + ' ' + str(version))
                        continue
			  
                    metalist.append(d)


            metalist = ryw.sortmeta(metalist)

            success,searchFile,reverseLists = \
                ReverseLists.open_searchfile_reverselists(
                    'Browse.generate:')
            if not success:
                sys.exit(1)

            displayObject = ryw_view.DisplayObject(
                self.Root, calledByVillageSide = self.calledByVillageSide,
                missingFileFunc = reqDownloadFunc,
                searchFile = searchFile,
                reverseLists = reverseLists)

            displayObject.begin_print()
            for i in metalist:
                #self.generate_row(d)
                displayObject.show_an_object_compact(i)
		      
            displayObject.end_print()
            self.print_footer()
            searchFile.done()
            reverseLists.done()
def main(logDir, logFile, searchFile, scriptName, resources = None):
    """main function processing search request."""

    # initialization.
    name = print_header()
    form = cgi.FieldStorage()
    setup_logging(logDir, logFile)

    ## cgi.print_form(form)

    ## parse the form to get: query, sorting information, start_index
    ## to know which subset of matches/results to return,
    ## search_attributes for the next_page_button
    query, sort_tuple, start_index, search_attributes, error_message = \
           parse_form(form)
    if query is None:
	print '<P> ERROR while parsing form and constructing ' + \
              'search query:', error_message
	sys.exit(1)

    ## print '<HR>Query:', query

    ## read index file to get the list of dictionaries:
    ## one dictionary for each version of each object, contains its meta-data

    #
    # used to open search file without read.
    # because Sobti is doing the read by himself below.
    # I have to change this because the SearchFile will be passed
    # onto the ReverseLists module for more later lookups.
    # this makes it necessary to do a real SearchFile open.
    #
    #success,searchFileLock = ryw.open_search_file(
    #    'Search:', logDir, logFile, searchFile, False, skipRead = True)
    success,searchFileLock = ryw.open_search_file(
        'Search:', logDir, logFile, searchFile, False, skipRead = False)
    if not success:
        ryw.give_bad_news('Search: failed to acquire search file lock: ' +
                          searchFile, logging.critical)
        ryw_upload.quick_exit(1)
    #else:
        #
        # mis-named: it's really not a searchFileLock, but searchFile itself.
        #
        #displayObject.set_search_file(searchFileLock)
        

    #
    # this is when Sobti used to do his own read, now replaced by mine.
    #
    #metas, error_message = read_index_file_to_get_metas(searchFile)
    metas = searchFileLock.convert_to_sobti_list()
    if metas is None:
	print '<P> ERROR while reading the index file to get ' + \
              'the meta-data dictionaries:', error_message
        searchFileLock.done()
        ryw_upload.quick_exit(1)

    ## build a list of metas that satisfy the given query
    matches = []
    for meta in metas:
        if not ryw_view.should_show_object(meta, resources):
            continue
	if cnf_match.matches_cnf(meta, query):
            matches.append(meta)
            ryw.db_print2('Search:main() ' + repr(meta), 53)

    num_matches = len(matches)

    ## sort the matches by the given sort tuple
    matches, error_message = sort_matches(matches, sort_tuple)
    if matches is None:
	print '<P> ERROR while sorting matches:', error_message
        searchFileLock.done()
        ryw_upload.quick_exit(1)


    #
    # save all current search results in a file for
    # possible inclusion in the current selection.
    #
    matchAllName = save_matches(matches)
    

    ## Return the start_index..(start_index + N) entries from the top
    if num_matches == 0 or start_index >= num_matches:
	num_items = 0
    else:
	start_index = max(0, start_index)
        assert start_index < num_matches

	end_index = start_index + NUM_OBJECTS_PER_PAGE - 1
	end_index = max(0, end_index)
	end_index = min(num_matches - 1, end_index)

	if not (0 <= start_index <= end_index < num_matches):
            print '<P> ASSERT ERROR: start_index, end_index, ' + \
                  'num_matches', start_index, end_index, num_matches
            searchFileLock.done()
            ryw_upload.quick_exit(1)

	num_items = end_index - start_index + 1

    ## num_items is the number of items that will actually be displayed
    ## num_items <= num_matches
    if num_items == 0:
	print '<P><H3>No objects to display</H3>'
        searchFileLock.done()
        ryw_upload.quick_exit(1)
    else:
	#print '<P><H3>%d objects satisfy the search criteria, ' + \
        #      'displaying %d of them</H3>' % (num_matches, num_items)
        print """
<BR><B><FONT SIZE=2>%d
object(s) satisfy the search criteria.</FONT></B>""" % \
              (num_matches)
        print """
<BR><B><FONT SIZE=2>displaying matches %d - %d.</FONT></B><BR>""" % \
              (start_index + 1, start_index + num_items)

        print_next_page_button1(search_attributes, end_index + 1, scriptName,
                                num_matches)

        shownMatches = []
        
        success,reverseLists = ReverseLists.open_reverse_lists(
            'Search:', '', '',
            os.path.join(RepositoryRoot, 'ReverseLists'), True,
            searchFile = searchFileLock,
            repositoryRoot = RepositoryRoot)
        if not (success and reverseLists):
            ryw.give_bad_news('Search.main: failed to open ReverseLists.',
                              logging.critical)
            if reverseLists:
                reverseLists.done()
            return False

        displayObject = ryw_view.DisplayObject(
            RepositoryRoot, calledByVillageSide = False,
            missingFileFunc=Browse.reqDownloadFunc,
            searchFile = searchFileLock,
            reverseLists = reverseLists)
        
        displayObject.begin_print()
	for i in range(start_index, end_index + 1):
            #display_object(matches[i])
            displayObject.show_an_object_compact(matches[i])
            shownMatches.append(matches[i])
        displayObject.end_print()
        reverseLists.done()

        #
        # save search results on this page in a file for
        # possible inclusion in the current selection.
        #
        matchThisPageName = save_matches(shownMatches)

        print_selection_links(matchAllName, matchThisPageName)
        
    	## Include a next-page button
	print_next_page_button2(search_attributes, end_index + 1, scriptName,
                                num_matches)

    searchFileLock.done()
    ryw_view.print_footer()