def copyDirOrFile(self, src, dst, configPath): if src != dst: if os.path.isfile(src): dstDir = os.path.dirname(dst) self.createDir(configPath, dstDir) copy2(src, dst) sMode, sUid, sGid = getModeFile(src) os.chown(dst, sUid, sGid) os.chmod(dst, sMode) elif os.path.isdir(src): self.createDir(configPath, dst) sMode, sUid, sGid = getModeFile(src) os.chown(dst, sUid, sGid) os.chmod(dst, sMode)
def save(self): path = os.path.dirname(self.fileName) if not os.path.exists(path): try: os.makedirs(path) except: self.printERROR(_("Failed to create directory %s")%path) return False if not os.path.exists(self.fileName): try: open(self.fileName, "w") except: self.printERROR(_("Failed to create file %s")%self.fileName) return False if self.getFileAccess(perm="WRITE"): modeFile = 0600 if getModeFile(self.fileName, mode="mode") != modeFile: os.chmod(self.fileName, modeFile) buff = "\n".join(map(lambda x: ":".join(x), self.data)) + "\n" FD = open(self.fileName, "w+") FD.write(buff) FD.close() return True else: return False
def createDir(self, configPath, dstDir): """Создание директории в случае необходимости""" if os.path.exists(dstDir): return True def splPath(path): listPath = [] if path in ("","/"): return [] base, p = os.path.split(path) listPath.append(p) while(not base in ("","/")): base, p = os.path.split(base) listPath.append(p) listPath.reverse() return listPath notFoundPaths = [] path = "/" for p in splPath(dstDir): path = os.path.join(path,p) if not os.path.exists(path): notFoundPaths.append(path) for mkPath in notFoundPaths: srcPath = pathJoin(configPath, mkPath) dMode, dUid, dGid = getModeFile(srcPath) os.mkdir(mkPath, dMode) os.chown(mkPath, dUid, dGid) return True
def copyConfigFiles(self, configPath): """Копирование конфигурационных файлов""" configDstFiles = self.scanProtectDirs(configPath) if configDstFiles: self.logger.warn(_("Replaced files:")) for dst in configDstFiles: src = pathJoin(configPath, dst) if src != dst: dstDir = os.path.dirname(dst) self.createDir(configPath, dstDir) copy2(src, dst) sMode, sUid, sGid = getModeFile(src) os.chown(dst, sUid, sGid) os.chmod(dst, sMode) self.logger.warn(" "*5 + dst) return True