def writeFile(loctype, loclevel, contextname, filename, contents): localizationFile = getLocalizationFile(loctype, loclevel, contextname, filename) with File(localizationFile.getFile(), filename, 'w') as pythonFile: pythonFile.write(contents) localizationFile.save()
def readFile(loctype, loclevel, contextname, filename): localizationFile = getLocalizationFile(loctype, loclevel, contextname, filename) with File(localizationFile.getFile(), filename, 'r') as pythonFile: fileContents = pythonFile.read() return fileContents
def getFile(self, mode='r'): ''' @param mode: The mode. @summary: Return a file handle to the file ''' retrieveFile = True if mode == 'w': retrieveFile = False return File(self.jobj.getFile(retrieveFile), self.getName(), mode)
def _getFileContents(self, loctype, loclevel, locname, filename): pathManager = PathManagerFactory.getPathManager() context = pathManager.getContext(loctype, loclevel) context.setContextName(locname) localizationFile = pathManager.getLocalizationFile(context, filename) with File(localizationFile.getFile(), filename, 'r') as pythonFile: fileContents = pythonFile.read() return fileContents
def writeToServerFile(forecasts, outputFile, writeToSite): if outputFile: try: if writeToSite: ctx = PATH_MGR.getContext(LocalizationType.COMMON_STATIC, LocalizationLevel.SITE) else: ctx = PATH_MGR.getContext(LocalizationType.COMMON_STATIC, LocalizationLevel.USER) filePath = PATH_MGR.SEPARATOR.join(["gfe", "text", "PRODGEN", outputFile + ".PRODGEN"]) lFile = PATH_MGR.getLocalizationFile(ctx, filePath) logger.info("Writing forecast to " + str(lFile)) from LockingFile import File with File(lFile.getFile(), "", 'w') as outfile: outfile.write(forecasts) return lFile.save() except: logger.exception("Error writing forecast to " + str(lFile)) return 0 return 1
def _fileChanges(self, entry): fileChanges = entry.get("fileChanges", None) if not fileChanges: return False from LockingFile import File failed = 0 for fileName, fileType, changeType, strings, cleanUp in fileChanges: fileName = fileName.replace("<site>", self.getSiteID()) # Get the file lf = TextFileUtil.getTextFile(fileName, fileType) if lf.getName().endswith(".py"): if sys.modules.has_key(fileName): del sys.modules[fileName] try: with File(lf.getFile(), '', 'r') as pythonFile: text = pythonFile.read() except: failed = 1 print "FILE CHANGES failed reading from " + str(lf) raise #self.output("FILE CHANGES (initial) from " +str(lf) + "\n" + text, self._outFile) #DEBUG # Modify it if changeType == "add": text = text + strings elif changeType == "replace": # strings may be a tuple (orig, repl) or # a list of tuples for multiple changes to the same file if type(strings) == tuple: strings = [strings] for orig, repl in strings: strIndex = text.find(orig) text = text.replace(orig, repl) #self.output("FILE CHANGES (chg): " + orig + ' ' + repl, self._outFile) #DEBUG #self.output("FILE CHANGES (mod): " + text, self._outFile) #DEBUG if strIndex < 0: self.output("File change failed for " + orig, self._outFile) failed = 1 # Write it destLf = TextFileUtil.getUserTextFile(lf) try: with File(destLf.getFile(), '', 'w') as pythonFile: pythonFile.write(text) destLf.save() except: failed = 1 print "FILE CHANGES failed writing to " + str(destLf) raise #self.output("FILE CHANGES (saved) to " + str(destLf) + "\n" + text, self._outFile) #DEBUG if len(fileChanges) and not failed: if self._reportingMode not in ["Pretty"]: self.output("All File Changes successful", self._outFile) return True