def in_copy(objname, version, dstDataPath, dstAuxiPath, driveroot, mapDict): itemName = objname + '#' + version success,dirName,dataDir,auxiDir = get_map_entry(driveroot, mapDict, itemName) if not success: return False ryw.give_news3(' copying ' + dirName + ' ... ', logging.info) try: su.copytree(dataDir, dstDataPath) except: ryw.give_bad_news('in_copy: failed to copy data directory: ' + itemName + ': ' + dataDir, logging.error) return False logging.debug('in_copy: successfully copied data directory: ' + itemName + ': ' + dataDir) if os.path.exists(auxiDir): try: su.copytree(auxiDir, dstAuxiPath) except: ryw.give_bad_news('in_copy: failed to copy auxi directory: ' + itemName + ': ' + auxiDir, logging.error) return False logging.debug('in_copy: successfully copied auxi directory: ' + itemName + ': ' + auxiDir) return True
def copy_scripts_not_used(repDir): ryw.give_news2('copying scripts... ', logging.info) # place update.py src = os.path.join(RepositoryRoot, 'pythonscriptsforclient', 'update.py') dst = os.path.join(repDir, 'update.py') try: shutil.copyfile(src, dst) except: ryw.give_bad_news('copy_scripts: failed to copy update.py: ' + src + ' ' + dst, logging.critical) return False logging.debug('copy_scripts done: ' + repDir) # place pythonsripts if copy_script_dir(repDir, 'pythonscriptsforclient', 'pythonscripts'): ryw.give_news2('pythonscriptsforclient: done. ', logging.info) else: ryw.give_bad_news('copy_scripts: copy_script_dir failed.', logging.critical) return False # place Common scripts if copy_script_dir(repDir, 'CommonScripts4Clients', 'CommonScripts4Clients'): ryw.give_news2('Common: done. ', logging.info) else: ryw.give_bad_news('copy_scripts: copy_script_dir failed.', logging.critical) return False # place Nihao scripts if copy_script_dir(repDir, 'NihaoScripts4Clients', 'NihaoScripts4Clients'): ryw.give_news2('NihaoScripts4Clients: done. ', logging.info) else: ryw.give_bad_news('copy_scripts: copy_script_dir failed.', logging.critical) return False # place a whole copy of code anyhow try: su.copytree(os.path.join(RepositoryRoot, '..', 'Postmanet-2'), os.path.join(repDir, '..', 'Postmanet-2'), isInstall = True) ryw.give_news2('Postmanet-2: done. ', logging.info) except: ryw.give_bad_news('copy_scripts: failed to place Postmanet-2.', logging.error) return False logging.debug('copy_scripts: done.') ryw.give_news2('<BR>', logging.info) return True
def copy_view(repDir, viewRoot): ryw.give_news2('copying view... ', logging.info) try: dst = os.path.join(repDir, 'View') su.copytree(viewRoot, dst) except: ryw.give_bad_news('copy_view: failed: ' + viewRoot + ' ' + dst, logging.critical) return False logging.debug('copy_view: done: '+dst) ryw.give_news2('done. ', logging.info) ryw.give_news2('<BR>', logging.info) return True
def copy_scripts(repDir): ryw.give_news2('copying scripts... ', logging.info) # place a whole copy of code anyhow try: su.copytree(os.path.join(RepositoryRoot, '..', 'Postmanet-2'), os.path.join(repDir, '..', 'Postmanet-2'), isInstall = True) ryw.give_news2('Postmanet-2: done. ', logging.info) except: ryw.give_bad_news('copy_scripts: failed to place Postmanet-2.', logging.error) return False logging.debug('copy_scripts: done.') ryw.give_news2('<BR>', logging.info) return True
def out_copy(itemName, srcDataPath, srcMetaPath, srcAuxiPath, currCounter, dataRoot, auxiRoot, mapDict, onlyMeta = False): objID,version = itemName.split('#') version = int(version) success,dirName,currCounter = out_obj_dir_name( os.path.join(RepositoryRoot, 'WWW', 'ObjectStore'), objID, version, currCounter) if not success: raise 'out_copy: out_obj_dir_name failed.' dataDir = os.path.join(dataRoot, dirName) auxiDir = os.path.join(auxiRoot, dirName) ryw.give_news2(dirName + ', ', logging.info) if onlyMeta: try: os.makedirs(os.path.join(dataDir, stubDirName)) except: msg = 'out_copy: failed to make meta only stub: '+ \ dataDir ryw.give_bad_news(msg, logging.critical) raise msg else: su.copytree(srcDataPath, dataDir) logging.debug('out_copy: successfully copied: ' + srcDataPath + ' -> ' + dataDir) if os.path.exists(srcAuxiPath): su.copytree(srcAuxiPath, auxiDir) logging.debug('out_copy: successfully copied: ' + srcAuxiPath + ' -> ' + auxiDir) mapDict[itemName] = dirName logging.debug('out_copy: entered mapping: ' + itemName + ' : ' + dirName) return (currCounter, mapDict)
def copy_local_file_for_upload(form, tmpdir, uploadFileName, localFound, localPath, localDir, isCopyingExcerpt = False): if not localFound: return (True, False, 0) if localDir: dirKB = ryw_disc.getRecursiveSizeInKB(localPath) bytes = dirKB * 1024 if bytes == 0: ryw.give_bad_news( 'copy_local_file_for_upload: 0-sized local directory: '+ localPath, logging.error) return (False, False, 0) else: success,bytes = ryw.get_file_size(localPath) if not success: return (False, False, 0) #if uploadFileName != truncateLocalName: # ryw.give_bad_news( # 'copy_local_file_for_upload: repeated local file name does<BR>' + # ' not match the name of the local file to be uploaded.<BR>' + # uploadFileName + '<BR>' + localName, # logging.error) # return (False, False, 0) freeKB = ryw.free_MB(tmpdir) * 1024 kB = math.ceil(bytes / 1024.0) if pretty_much_out_of_space(kB, freeKB): ryw.give_bad_news( 'copy_local_file_for_upload: nearly out of space ' + 'while uploading queue, KB: ' + repr(kB), logging.error) return (False, False, 0) tmpFileName = os.path.join(tmpdir, uploadFileName) try: if localDir: ryw.give_news2('<BR>Copying local directory: ' + localPath, logging.info) if isCopyingExcerpt: # # don't want the "excerpts" directory to contain # just a lone directory inside in the common case. # copy the content into the "excerpts" directory directly. # "tmpdir" in this case is the "excerpts" directory. # su.copytree tolerates an existing destination directory # and doesn't wipe out what's already in it. # tmpFileName = os.path.normpath(tmpdir) su.copytree(localPath, tmpFileName) else: shutil.copytree(localPath, tmpFileName) else: ryw.give_news2('<BR>Copying local file: ' + localPath, logging.info) shutil.copyfile(localPath, tmpFileName) #shutil.copyfile( # os.path.join(RepositoryRoot, 'Tmp-in', 'foo.txt'), # tmpFileName) except: ryw.give_bad_news('copy_local_file_for_upload: ' + 'failed to copy data: ' + localPath + ' -> ' + tmpFileName, logging.critical) return (False, False, 0) logging.debug('copy_local_file_for_upload: ' + 'succeeded copying local data: ' + localPath + ' -> ' + tmpFileName, logging.info) return (True, True, bytes)
## pythonpath = _winreg.EnumValue(a, 0)[1] ## print 'pythonpath =', pythonpath ##except: ## print 'ERROR>>> Python installation not found. Quitting.' ## sys.exit(1) ### ---- # ---- get apache path ---- apachepath = get_apache_path() if apachepath: create_httpd_conf(apachepath, dst) create_registry_entry(dst) ## TODO: remove pythonpath later pythonpath = sys.executable su.createdirpath(dst) su.copytree(common, os.path.join(dst, 'Common')) su.copyscripttree(os.path.join(src, 'cgi-bin', 'Nihao'), os.path.join(dst, 'cgi-bin', 'Nihao'), [os.path.join(dst, 'Common')], "NihaoRoot = '%s'" % dst.replace('\\', '\\\\')) su.copytree(os.path.join(src, 'WWW', 'Nihao'), os.path.join(dst, 'WWW', 'Nihao'))
create_registry_entry(dst) ## TODO: remove pythonpath later pythonpath = sys.executable su.createdirpath(dst) #RYW #su.copytree(common, os.path.join(dst, 'Common'), isInstall = True) su.copyscripttree(common, os.path.join(dst, 'Common'), [], "NihaoRoot = '%s'" % dst.replace('\\', '\\\\'), isInstall = True) su.copytree(os.path.join(topdir, 'icons'), os.path.join(dst, 'WWW', 'icons'), isInstall = True) su.copyscripttree(os.path.join(src, 'cgi-bin', 'Nihao'), os.path.join(dst, 'cgi-bin', 'Nihao'), [os.path.join(dst, 'Common')], "NihaoRoot = '%s'" % dst.replace('\\', '\\\\'), isInstall = True) su.copytree(os.path.join(src, 'WWW', 'Nihao'), os.path.join(dst, 'WWW', 'Nihao'), isInstall = True) # RYW: place a copy of the whole code anyhow. su.copytree(topdir, os.path.join(dst, '..', 'Postmanet-2'), isInstall = True) #RYW shutil.copyfile(os.path.join(src, 'WWW', 'index.html'), os.path.join(dst, 'WWW', 'index.html')) shutil.copyfile(os.path.join(src, 'WWW', 'postmanetWhoAmI.txt'),
def generate_html(items,itempath, repDir, tmpImgDir): ryw.give_news2('generating static html pages... ', logging.info) htmlDir = os.path.join(repDir, "html") if not ryw.try_mkdir(htmlDir, 'addRobotWriteRequest:generate_html'): return False try: f = open(os.path.join(htmlDir,"index.html"),"w") f.write(""" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Objects on this disk</title> """) f.write(ryw_view.css_str()) f.write(""" </head> <body> """) f.write(ryw_view.logo_str_for_disc()) f.write(""" <h3>Objects on Disk</h3><p>(you are on this page either because you are browsing the disc directly or were directed to it due to an error while merging it.) <BR> """) write_page(items,itempath,f, tmpImgDir) f.write(ryw_view.end_print_str()) f.write(ryw_view.footer2_str()) f.write(""" </body> </html> """) f.close() srcIconsDir = os.path.join(RepositoryRoot, "WWW", "icons") dstIconsDir = os.path.join(htmlDir, "icons") su.copytree(srcIconsDir, dstIconsDir) parentDir, repdirname = os.path.split(repDir) f = open(os.path.join(parentDir,"index.html"), "w") f.write(""" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title> Repository folders on this disk </title> <meta http-equiv="refresh" content="1;URL=.\%s\html\index.html"> </head> <body> loading page containing list of things on this disk.... </body> </html> """ % (repdirname,)) f.close() except: ryw.give_bad_news('addRobotWriteRequest: generate_html files: failed to write file: ',logging.critical) return False ryw.give_news2('done. ', logging.info) ryw.give_news2('<BR>', logging.info) return True
def copy_file_carefully(oldFile, newFile, oldDir, newDir, tmpPrefix): """oldFile = oldSearchFile newFile = incomingSearchFile oldDir = mywww = c:\Postmanet\nihao\WWW\repository? newDir = mydisk (not used) tmpPrefix = 'SearchFile_incoming' """ # make this function generic and reusable for copying anything in a # robust manner: # (1) see if the bak version is there but not the new version, # if yes, restore it first. (existing restore is bad: need to check # whether the new version is there...) remove bak version... # this is general sanity check that can be called from anywhere. # (2) is the new stuff there? # (3) copy new stuff to tmp name. # (4) rename the existing stuff to bak name. # (5) rename the new tmpname to its real name. # (6) remove the bak name. # for testing. # check_logging(os.path.join('c:\\Postmanet\\Nihao', 'WWW', 'repository'), 'upload.log') # # (1) do something about oldFile_bak if it exists. # restore_cleanup_old_version(oldFile) # # (2) if the newFile isn't there, something is wrong. # if not os.path.exists(newFile): give_bad_news("copy_file_carefully: new file does not exist: " + newFile, logging.warning) return False # # (3) copy the newFile to a temporary name. # tmpname, objPrefix = ryw_upload.make_tmp_name(oldDir, tmpPrefix, "") try: su.copytree(newFile, tmpname) except: give_bad_news("copy_file_carefully: failed to copy newFile: " + newFile + " " + tmpname, logging.critical) cleanup_path(tmpname, "copy_file_carefully:") return False logging.debug("copy_file_carefully: successfully copied incoming file: " + newFile + " " + tmpname) # # (4) rename the existing old file to a backup name. # if not move_to_bak(oldFile): cleanup_path(tmpname, "copy_repository_view:") return False # # (5) rename the incoming file from its tmp name to its real name. # try: shutil.move(tmpname, oldFile) except: give_bad_news( "copy_file_carefully: failed to rename tmp name to real name: " + tmpname + " " + oldFile, logging.critical ) cleanup_path(oldFile, "copy_file_carefully:") restore_cleanup_old_version(oldFile) cleanup_path(tmpname, "copy_file_carefully:") return False logging.debug("copy_file_carefully: successfully renamed incoming file: " + tmpname + " " + oldFile) # # (6) remove the backup name. # restore_cleanup_old_version(oldFile) return True
## print 'ERROR>>> Python installation not found. Quitting.' ## sys.exit(1) ### ---- apachepath = get_apache_path() if apachepath: create_httpd_conf(apachepath, dst) create_registry_entry(dst) # TODO: pythonpath unnecessary, but some other script may still want to read it from Resources.txt pythonpath = sys.executable su.createdirpath(dst) su.copytree(common, os.path.join(dst, 'Common')) su.copytree(os.path.join(src, 'pythonscriptsforclient'), os.path.join(dst, 'pythonscriptsforclient')) su.copytree(os.path.join(src, 'WWW'), os.path.join(dst, 'WWW')) # special treatment for scripts in pylist = ['bin', 'cgi-bin'] for d in pylist: su.copyscripttree(os.path.join(src, d), os.path.join(dst, d), [os.path.join(dst, 'Common')], "RepositoryRoot = '%s'" % dst.replace('\\', '\\\\')) ## for d in pylist: ## if not os.path.exists(os.path.join(dst, d)): ## os.mkdir(dst + d) ## ## for py in os.listdir(src + d): ## su.copyScript(os.path.join(src + d, py), os.path.join(dst + d, py), [dst + 'Common'], "RepositoryRoot = '" + dst.replace('\\', '\\\\') + "'")