def _set_rel_paths(self): """Convert all file/directory locations to paths relative to the current working directory. paml requires that all paths specified in the control file be relative to the directory from which it is called rather than absolute paths. """ if self.working_dir is not None: self._rel_working_dir = _relpath(self.working_dir) if self.alignment is not None: self._rel_alignment = _relpath(self.alignment, self.working_dir) if self.out_file is not None: self._rel_out_file = _relpath(self.out_file, self.working_dir)
def execute(self): productionLocation = self.productionDetails[2] fileLocation = self.fileDetails[3] fileLocation = os.path.join(productionLocation, fileLocation) fileLocationDir = os.path.dirname(fileLocation) absRefLoc = os.path.normcase(posixpath.normpath(os.path.join(self.productionDetails[2], self.currentFileLocation))) absNewLoc = absRefLoc[0:len(absRefLoc)-len(self.currentFilename)]+self.newFilename newpath = "//"+_relpath(absNewLoc, fileLocationDir) handle = blendfile.openBlendFile(fileLocation, 'r+b') for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): relPath = libraryblock.Get("name").split("\0")[0].replace("\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath==absRefLoc: libraryblock.Set("name", newpath) for imageblock in handle.FindBlendFileBlocksWithCode("IM"): relPath = imageblock.Get("name").split("\0")[0].replace("\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath==absRefLoc: imageblock.Set("name", newpath) handle.close()
def execute(self): productionLocation = self.productionDetails[2] fileLocation = self.fileDetails[3] fileLocation = os.path.join(productionLocation, fileLocation) fileLocationDir = os.path.dirname(fileLocation) absRefLoc = os.path.normcase( posixpath.normpath( os.path.join(self.productionDetails[2], self.currentFileLocation))) absNewLoc = absRefLoc[0:len(absRefLoc) - len(self.currentFilename)] + self.newFilename newpath = "//" + _relpath(absNewLoc, fileLocationDir) handle = blendfile.openBlendFile(fileLocation, 'r+b') for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): relPath = libraryblock.Get("name").split("\0")[0].replace( "\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath == absRefLoc: libraryblock.Set("name", newpath) for imageblock in handle.FindBlendFileBlocksWithCode("IM"): relPath = imageblock.Get("name").split("\0")[0].replace("\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath == absRefLoc: imageblock.Set("name", newpath) handle.close()
def _make_relative(file_paths, base=None): if not base: base = os.getcwd() if base[-1] != os.path.sep: base += os.path.sep return [_relpath(path, base) if path.startswith(base) else path for path in file_paths]
def copy_to_build_dir(filepath, root=root): filepath_abs = os.path.abspath(filepath) if os.path.isabs(filepath): filepath = filepath_abs if filepath_abs.startswith(root): mod_dir = join_path(build_dir, os.path.dirname(_relpath(filepath, root))) copy_once_if_newer(filepath_abs, mod_dir)
def execute(self): productionLocation = self.productionDetails[2] fileLocation = self.fileDetails[3] fileLocation = os.path.join(productionLocation, fileLocation) fileLocationDir = os.path.dirname(fileLocation) absNewLoc = os.path.normcase( posixpath.normpath( os.path.join( self.productionDetails[2], self.newFileDetails[indexer.INDEX_FILE_LOCATION]))) newpath = "//" + _relpath(absNewLoc, fileLocationDir) handle = blendfile.openBlendFile(fileLocation, 'r+b') if self.libraryDetails[indexer.INDEX_ELEMENT_TYPE] == 'LI': for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): path = libraryblock.Get("filename") if path == None: path = libraryblock.Get( "filepath") #blender 2.5 renamed field path = path.split("\0")[0].replace("\\", "/") if path == self.libraryDetails[ indexer.INDEX_ELEMENT_LI_FILENAME]: libraryblock.Set("name", newpath) elif self.libraryDetails[indexer.INDEX_ELEMENT_TYPE] == 'IM': for libraryblock in handle.FindBlendFileBlocksWithCode("IM"): path = libraryblock.Get("name").split("\0")[0] if path == self.libraryDetails[ indexer.INDEX_ELEMENT_LI_FILENAME]: libraryblock.Set("name", newpath) handle.close()
def execute(self): productionLocation = self.productionDetails[indexer.INDEX_PRODUCTION_LOCATION] fileLocation = self.fileDetails[indexer.INDEX_FILE_LOCATION] fileLocation = os.path.join(productionLocation, fileLocation) subDirs = fileLocation.replace(self.sourceDirectory, self.targetDirectory, 1) fileLocationDir = os.path.dirname(subDirs) absRefLoc = os.path.normcase(posixpath.normpath(os.path.join(self.productionDetails[2], subDirs))) absNewLoc = os.path.normcase(posixpath.normpath(os.path.join(self.productionDetails[2], self.referenceFileDetails[indexer.INDEX_FILE_LOCATION]))) newpath = "//"+_relpath(absNewLoc, fileLocationDir) handle = blendfile.openBlendFile(fileLocation, 'r+b') for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): relPath = libraryblock.Get("name").split("\0")[0].replace("\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath==absNewLoc: libraryblock.Set("name", newpath) for imageblock in handle.FindBlendFileBlocksWithCode("IM"): relPath = imageblock.Get("name").split("\0")[0].replace("\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath==absNewLoc: imageblock.Set("name", newpath) handle.close()
def determineProductionLocation(productionDir, productionFile, blenderPath): productionFileDir = os.path.dirname(productionFile) if blenderPath.startswith("//"): relpath = os.path.normpath(blenderPath[2:].split("\0")[0]).replace("\\", "/") abspath = os.path.join(productionFileDir, relpath) return _relpath(abspath, productionDir) return blenderPath
def copy_to_build_dir(filepath, root=root): filepath = os.path.abspath(filepath) if os.path.realpath(filepath).startswith(root): mod_dir = os.path.join( build_dir, os.path.dirname(_relpath(filepath))) if not os.path.isdir(mod_dir): os.makedirs(mod_dir) shutil.copy(filepath, mod_dir)
def copy_to_build_dir(filepath, root=root): filepath_abs = os.path.abspath(filepath) if os.path.isabs(filepath): filepath = filepath_abs if filepath_abs.startswith(root): mod_dir = join_path( build_dir, os.path.dirname(_relpath(filepath, root))) copy_once_if_newer(filepath_abs, mod_dir)
def execute(self): productionLocation = self.productionDetails[2] fileLocation = self.fileDetails[3] currentFileLocation = os.path.normcase( os.path.normpath(os.path.join(productionLocation, fileLocation))) newFileLocation = os.path.normcase( os.path.normpath( os.path.join( productionLocation, os.path.join(self.newLocation, self.currentFilename)))) dirLocation = os.path.normcase( os.path.normpath(os.path.dirname(newFileLocation))) #create target directory if not existing if not os.path.exists(dirLocation): os.makedirs(dirLocation) if svn.isKnownSVNFile(currentFileLocation): svn.svnMove(currentFileLocation, newFileLocation) else: shutil.move(currentFileLocation, newFileLocation) #update ID and IM tags of blend files. if self.currentFilename.endswith(".blend"): handle = blendfile.openBlendFile(newFileLocation, 'r+b') for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): relPath = libraryblock.Get("name").split("\0")[0] absPath = blendfile.blendPath2AbsolutePath( currentFileLocation, relPath) normPath = os.path.normcase(os.path.normpath(absPath)) newRelPath = _relpath(normPath, dirLocation) libraryblock.Set("name", "//" + newRelPath) for libraryblock in handle.FindBlendFileBlocksWithCode("IM"): relPath = libraryblock.Get("name").split("\0")[0] if len(relPath) > 0: absPath = blendfile.blendPath2AbsolutePath( currentFileLocation, relPath) normPath = os.path.normcase(os.path.normpath(absPath)) newRelPath = _relpath(normPath, dirLocation) libraryblock.Set("name", "//" + newRelPath) handle.close() pass
def replace_url(mo): """ make sure urls are relative to css path """ css_url = mo.group(0)[4:].strip(")").replace("'", "").replace('"','') css_path = os.path.join(os.path.dirname(src_fpath), css_url) rel_path = _relpath(css_path, fname_dir) return "url(%s)" % rel_path
def copy_to_build_dir(filepath, root=os.getcwd()): filepath_abs = os.path.abspath(filepath) if os.path.isabs(filepath): filepath = filepath_abs if filepath_abs.startswith(root): # distutil extension depends are relative to cwd mod_dir = join_path(build_dir, os.path.dirname(_relpath(filepath, root))) copy_once_if_newer(filepath_abs, mod_dir)
def execute(self): productionLocation = self.productionDetails[2] fileLocation = self.fileDetails[3] currentFileLocation = os.path.normcase(os.path.normpath(os.path.join(productionLocation, fileLocation))) newFileLocation = os.path.normcase(os.path.normpath(os.path.join(productionLocation, os.path.join(self.newLocation, self.currentFilename)))) dirLocation = os.path.normcase(os.path.normpath(os.path.dirname(newFileLocation))) #create target directory if not existing if not os.path.exists(dirLocation): os.makedirs(dirLocation) if svn.isKnownSVNFile(currentFileLocation): svn.svnMove(currentFileLocation, newFileLocation) else: shutil.move(currentFileLocation, newFileLocation) #update ID and IM tags of blend files. if self.currentFilename.endswith(".blend"): handle = blendfile.openBlendFile(newFileLocation, 'r+b') for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): relPath = libraryblock.Get("name").split("\0")[0] absPath = blendfile.blendPath2AbsolutePath(currentFileLocation, relPath) normPath = os.path.normcase(os.path.normpath(absPath)) newRelPath = _relpath(normPath, dirLocation) libraryblock.Set("name", "//"+newRelPath) for libraryblock in handle.FindBlendFileBlocksWithCode("IM"): relPath = libraryblock.Get("name").split("\0")[0] if len(relPath)>0: absPath = blendfile.blendPath2AbsolutePath(currentFileLocation, relPath) normPath = os.path.normcase(os.path.normpath(absPath)) newRelPath = _relpath(normPath, dirLocation) libraryblock.Set("name", "//"+newRelPath) handle.close() pass
def execute(self): productionLocation = self.productionDetails[2] fileLocation = self.fileDetails[3] fileLocation = os.path.join(productionLocation, fileLocation) fileLocationDir = os.path.dirname(fileLocation) absNewLoc = os.path.normcase(posixpath.normpath(os.path.join(self.productionDetails[2], self.newFileDetails[indexer.INDEX_FILE_LOCATION]))) newpath = "//"+_relpath(absNewLoc, fileLocationDir) handle = blendfile.openBlendFile(fileLocation, 'r+b') if self.libraryDetails[indexer.INDEX_ELEMENT_TYPE] == 'LI': for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): path = libraryblock.Get("filename") if path == None: path = libraryblock.Get("filepath") #blender 2.5 renamed field path = path.split("\0")[0].replace("\\", "/") if path == self.libraryDetails[indexer.INDEX_ELEMENT_LI_FILENAME]: libraryblock.Set("name", newpath) elif self.libraryDetails[indexer.INDEX_ELEMENT_TYPE] == 'IM': for libraryblock in handle.FindBlendFileBlocksWithCode("IM"): path = libraryblock.Get("name").split("\0")[0] if path == self.libraryDetails[indexer.INDEX_ELEMENT_LI_FILENAME]: libraryblock.Set("name", newpath) handle.close()
def execute(self): productionLocation = self.productionDetails[ indexer.INDEX_PRODUCTION_LOCATION] fileLocation = self.fileDetails[indexer.INDEX_FILE_LOCATION] fileLocation = os.path.join(productionLocation, fileLocation) subDirs = fileLocation.replace(self.sourceDirectory, self.targetDirectory, 1) fileLocationDir = os.path.dirname(subDirs) absRefLoc = os.path.normcase( posixpath.normpath(os.path.join(self.productionDetails[2], subDirs))) absNewLoc = os.path.normcase( posixpath.normpath( os.path.join( self.productionDetails[2], self.referenceFileDetails[indexer.INDEX_FILE_LOCATION]))) newpath = "//" + _relpath(absNewLoc, fileLocationDir) handle = blendfile.openBlendFile(fileLocation, 'r+b') for libraryblock in handle.FindBlendFileBlocksWithCode("LI"): relPath = libraryblock.Get("name").split("\0")[0].replace( "\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath == absNewLoc: libraryblock.Set("name", newpath) for imageblock in handle.FindBlendFileBlocksWithCode("IM"): relPath = imageblock.Get("name").split("\0")[0].replace("\\", "/") absPath = blendfile.blendPath2AbsolutePath(fileLocation, relPath) normPath = os.path.normpath(absPath) if normPath == absNewLoc: imageblock.Set("name", newpath) handle.close()
def relpath(*args): return _relpath(*args).replace(os.sep, '/')
def indexExistingFile(connection, productionId, productionDir, file): """index existing file.""" relpath = _relpath(file, productionDir) dbFile = connection.execute("select id from file where location=?", [relpath]).fetchone() connection.execute("delete from element where file_id=?", dbFile) indexNewFile(connection, productionId, productionDir, file, dbFile[0])
def indexOldFile(connection, productionId, productionDir, file): """remove a file from production scope""" relpath = _relpath(file, productionDir) dbFile = connection.execute("select id from file where location=?", [relpath]).fetchone() connection.execute("delete from element where file_id=?", dbFile) connection.execute("delete from file where id=?", dbFile)
def indexNewFile(connection, productionId, productionDir, file, useFileId=None): """index a new file""" if useFileId == None: newId = connection.execute("select max(id)+1 from file").fetchone()[0] if newId == None: newId = 0 connection.execute( "insert into file values (?,?,?,?,?,?)", [ newId, productionId, os.path.basename(file), _relpath(file, productionDir), int(os.path.getmtime(file)), int(os.path.getsize(file)), ], ) else: newId = useFileId connection.execute( "update file set lastupdate=?, length=? where id=?", [int(os.path.getmtime(file)), int(os.path.getsize(file)), newId], ) if file.endswith(".blend"): log.info("indexing file " + file) bf = blendfile.openBlendFile(file) # index the file firstElementId = connection.execute("select max(id)+1 from element").fetchone()[0] offsetElementId = 0 if firstElementId == None: firstElementId = 0 bfVersion = bf.Header.Version bfPointerSize = bf.Header.PointerSize bfLittleEndianness = bf.Header.LittleEndianness bfCompressed = bf.compressed bfCurrentScenePointer = None bfElementId = firstElementId offsetElementId = offsetElementId + 1 bfCurrentSceneId = None for block in bf.FindBlendFileBlocksWithCode("GLOB"): bfCurrentScenePointer = block.Get("curscene") # flags=block.Get("fileflags") # bfCompressed = (flags & G_FILE_COMPRESS) == G_FILE_COMPRESS # print(flags, bfCompressed) for block in bf.FindBlendFileBlocksWithCode("SC"): scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 if block.OldAddress == bfCurrentScenePointer: bfCurrentSceneId = scId scName = block.Get("id.name") scWidth = block.Get("r.xsch") scHeight = block.Get("r.ysch") scSize = block.Get("r.size") scXparts = block.Get("r.xparts") scYparts = block.Get("r.yparts") scStartFrame = block.Get("r.sfra") scEndFrame = block.Get("r.efra") scStep = block.Get("frame_step") scImageType = formatImageType(block.Get("r.imtype")) connection.execute( "insert into element (id, file_id, blendfile_id, name, type, sc_width, sc_height, sc_size, sc_xparts, sc_yparts, sc_startframe, sc_endframe, sc_framestep, sc_outputtype) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [ scId, newId, bfElementId, scName, scName[0:2], scWidth, scHeight, scSize, scXparts, scYparts, scStartFrame, scEndFrame, scStep, scImageType, ], ) for block in bf.FindBlendFileBlocksWithCode("MA"): scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 scName = block.Get("id.name") connection.execute( "insert into element (id, file_id, blendfile_id, name, type) values (?,?,?,?,?)", [scId, newId, bfElementId, scName, scName[0:2]], ) for block in bf.FindBlendFileBlocksWithCode("OB"): # sdna = block.SDNAIndex dnaIndex = block.SDNAIndex dnaStruct = block.File.Catalog.Structs[dnaIndex] if dnaStruct.Type[0] == "Object": scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 scName = block.Get("id.name") connection.execute( "insert into element (id, file_id, blendfile_id, name, type) values (?,?,?,?,?)", [scId, newId, bfElementId, scName, scName[0:2]], ) for type in [ "AC", "AR", "BR", "CA", "CU", "GD", "GR", "IP", "LA", "LT", "MB", "ME", "NT", "SO", "TE", "TX", "VF", "WO", "KE", ]: for block in bf.FindBlendFileBlocksWithCode(type): scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 scName = block.Get("id.name") connection.execute( "insert into element (id, file_id, blendfile_id, name, type) values (?,?,?,?,?)", [scId, newId, bfElementId, scName, scName[0:2]], ) for block in bf.FindBlendFileBlocksWithCode("IM"): scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 scName = block.Get("id.name") liName = block.Get("name") liName = liName.split("\0")[0] liFilename = liName liName = determineProductionLocation(productionDir, file, liName) if liName != None and liName != "" and (not liName.endswith("Untitled")) and liName != "Render Result": connection.execute( "insert into element (id, file_id, blendfile_id, name, type, li_name, li_filename) values (?,?,?,?,?,?,?)", [scId, newId, bfElementId, scName, scName[0:2], liName, liFilename], ) libref = dict() for block in bf.FindBlendFileBlocksWithCode("LI"): scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 liOldAddress = block.OldAddress libref[liOldAddress] = scId scName = block.Get("id.name") liName = block.Get("name") liName = determineProductionLocation(productionDir, file, liName) liFilename = block.Get("filename") # absolute path when saved? if liFilename == None: # in blender 2.5 filename has been renamed to filepath liFilename = block.Get("filepath") # absolute path when saved? connection.execute( "insert into element (id, file_id, blendfile_id, name, type, li_name, li_filename) values (?,?,?,?,?,?,?)", [scId, newId, bfElementId, scName, scName[0:2], liName, liFilename], ) for block in bf.FindBlendFileBlocksWithCode("ID"): scId = firstElementId + offsetElementId offsetElementId = offsetElementId + 1 scName = block.Get("name") liOldAddress = block.Get("lib") liId = libref[liOldAddress] connection.execute( "insert into element (id, file_id, blendfile_id, name, type, library_id) values (?,?,?,?,?,?)", [scId, newId, bfElementId, scName, "ID", liId], ) connection.execute( "insert into element (id, file_id, type, bf_current_scene_id, bf_version, bf_pointersize, bf_littleendian, bf_compressed) values (?,?,?,?,?,?,?,?)", [bfElementId, newId, "BF", bfCurrentSceneId, bfVersion, bfPointerSize, bfLittleEndianness, bfCompressed], ) bf.close()
def callWithLogDir(fn, root, delete): root_expanded = os.path.expanduser(root) if not os.path.isdir( root_expanded) and tunerconfig.config_defaults.output_dir == root: os.mkdir(root_expanded) d = tempfile.mkdtemp(prefix='pbtunerun_' + config.name + '_', dir=root_expanded) if not delete: print d global cur cur = StorageDirsTemplate(d) try: return fn() finally: if delete: shutil.rmtree(d) else: print d candidate = lambda cid: cur.candidate(cid) mutatorlog = lambda m: cur.mutatorlog(m) inputpfx = lambda size, number: cur.inputpfx(size, number) clearInputs = lambda: cur.clearInputs() openCsvStats = lambda name, header: cur.openCsvStats(name, header) saveFile = lambda path: cur.saveFile(path) relpath = lambda d: _relpath(d, cur.root)
warnings.warn("Failed to record git status: "+e) cur = None def callWithLogDir(fn, root, delete): root_expanded = os.path.expanduser(root) if not os.path.isdir(root_expanded) and tunerconfig.config_defaults.output_dir == root: os.mkdir(root_expanded) d = tempfile.mkdtemp(prefix='pbtunerun_'+config.name+'_', dir=root_expanded) if not delete: print d global cur cur = StorageDirsTemplate(d) try: return fn() finally: if delete: shutil.rmtree(d) else: print d candidate = lambda cid: cur.candidate(cid) mutatorlog = lambda m: cur.mutatorlog(m) inputpfx = lambda size, number: cur.inputpfx(size, number) clearInputs = lambda : cur.clearInputs() openCsvStats = lambda name, header: cur.openCsvStats(name, header) saveFile = lambda path: cur.saveFile(path) relpath = lambda d: _relpath(d, cur.root)