def getTemplatePath(self, sSection, pathVar="" , **kwargs): sTemplateDir = self.getPath("template", sSection, "template_dir", default="", **kwargs) if not sTemplateDir: return "" sEntityDir = self.getPath("template", sSection, "entity_dir", **kwargs) p = self.getPath("template", sSection, pathVar=pathVar, **kwargs) p = pathRedir(p, sEntityDir, sTemplateDir) return p
def _checkTemplatePaths(self, out_invalidPaths, sSection="project"): sTemplateDir = self.getTemplateDir(sSection) if sTemplateDir: sEntityDir = self.getPath("template", sSection, "entity_dir") for _, sRcPath in self.iterRcPaths("template", sSection): if not pathStartsWith(sRcPath, sEntityDir): continue sRcPath = pathRedir(sRcPath, sEntityDir, sTemplateDir) if osp.exists(sRcPath): continue out_invalidPaths.append(sRcPath) for sChildSection in self.getVar(sSection, "child_sections", ()): self._checkTemplatePaths(out_invalidPaths, sChildSection)
def createDirsAndFiles(self, sSpace="public", **kwargs): bDryRun = kwargs.pop("dryRun", True) bCheckDb = kwargs.pop("checkDb", True) bLog = kwargs.pop("log", True) bLoadDbNodes = kwargs.pop("loadDbNodes", True) sEntityName = self.name library = self.getLibrary(sSpace) cls = self.__class__ cls.assertNameParts(cls.getNameParts(sEntityName)) sTemplatePath = self.getTemplatePath() if not sTemplatePath: raise EnvironmentError("{} has NO template configured.".format(self)) sDestPathList = [] bNewEntity = False sEntityDirPath = self.getPath(sSpace) entityDir = library._weakDir(sEntityDirPath, dbNode=False) if not osp.exists(sEntityDirPath): dbNode = entityDir.loadDbNode() if dbNode: sMsg = """{}'s directory already exists in DB: \n{}""" raise RuntimeError(sMsg.format(self, dbNode.dataRepr())) bNewEntity = True if not bDryRun: os.makedirs(sEntityDirPath) entityDir.refresh(simple=True, dbNode=False) sDestPathList.append(sEntityDirPath) if bCheckDb and bLoadDbNodes: entityDir.loadChildDbNodes(recursive=True, noVersions=True) ruledDirDct = OrderedDict() newDirList = [] sSrcPathIter = iterPaths(sTemplatePath, ignoreFiles=ignorePatterns("*.db", ".*")) for sSrcPath in sSrcPathIter: sDestPath = pathRedir(sSrcPath, sTemplatePath, sEntityDirPath).format(**vars(self)) bExists = False if bNewEntity else osp.exists(sDestPath) if bExists: continue bIsDir = (sSrcPath.endswith("/") or sDestPath.endswith("/")) if bCheckDb: if bIsDir: sEntryType = "Directory" drcEntry = library._weakDir(sDestPath, dbNode=False) else: sEntryType = "File" drcEntry = library._weakFile(sDestPath, dbNode=False) dbNode = drcEntry.loadDbNode(fromDb=False) if dbNode: print "{} already exists in DB: '{}'".format(sEntryType, drcEntry.dbPath()) continue sDestPathList.append(sDestPath) bDirCreated = True if bIsDir: sDirPath = pathNorm(sDestPath) if not bDryRun: os.makedirs(sDirPath) else: sDirPath = osp.dirname(sDestPath) if not osp.exists(sDirPath): if not bDryRun: os.makedirs(sDirPath) else: bDirCreated = False if not bDryRun: copyFile(sSrcPath, sDestPath, dry_run=bDryRun) if not bDryRun: drcDir = library.getEntry(sDirPath, dbNode=False) else: drcDir = library._weakDir(sDirPath, dbNode=False) if drcDir not in ruledDirDct: sSyncRuleList = [] if bDirCreated: sSyncRuleList = drcDir.getParam("default_sync_rules", None) newDirList.append(drcDir) else: dbNode = drcDir.loadDbNode(fromDb=False) if dbNode: drcDir.refresh(simple=True, dbNode=False) sSyncRuleList = drcDir.syncRules if sSyncRuleList: ruledDirDct[drcDir] = sSyncRuleList newDbNodeList = [] for rcDir in newDirList: initData = rcDir.getDbNodeInitData(syncRules=False) newDbNodeList.append(initData) if newDbNodeList and (not bDryRun): newDbNodeList = self.project._db.createNodes(newDbNodeList) for dbnode in newDbNodeList: library._addDbNodeToCache(dbnode) for rcDir in newDirList: rcDir.refresh(simple=True, dbNode=False) for drcDir, sSyncRuleList in reversed(ruledDirDct.items()): #print drcDir, "default_sync_rules:", sSyncRuleList if not bDryRun: drcDir.setSyncRules(sSyncRuleList, applyRules=True, refresh=False) if bLog and sDestPathList: sSep = "\n " sAction = "Created" if not bDryRun else "Missing" sMsg = '\n{} {} paths for "{}":'.format(sAction, sSpace.upper(), sEntityName) sMsg += sSep + sSep.join(sDestPathList) print sMsg, "\n" return sDestPathList