def runGenerate(defaultConfig, moduleFileNames): """SQS generate command.""" # Assume Failure. moduleFile = None # We expect to process a list of file names. if not isinstance(moduleFileNames, list): moduleFileNames = [moduleFileNames] # Force list. for moduleFileName in moduleFileNames: if not moduleFileName is None and not moduleFileName == "": # Use passed Module File name. logger.debug("generate.runGenerate() - Module File: " + moduleFileName) try: moduleFile = open(moduleFileName) except: logger.exception( "generate.runGenerate() - Error reading Module File.") else: # TODO: Fix here and elsewhere - this won't be reached because we are # iterating a possibly empty list. # TODO: Copy STDIN to scratch directory before starting? # Use stdin if no file name. logger.info( "generate.runGenerate() - Reading module data from STDIN.") #logger.error("generate.runGenerate() - Currently STDIN not supported.") moduleFile = sys.stdin if not moduleFile is None: processModuleFile(defaultConfig, moduleFile)
def getSourceURL(urlSource): """Opens and returns a file-like object from the fully qualified url contained in the url source or None if no url source was specified or the file url not be opened.""" if urlSource != None and len(urlSource) > 0: try: sf = urllib.request.urlopen(urlSource) return sf except: logger.exception("common.getSourceURL() - Could not open URL '{0}'.".format(urlSource)) # Failed! return None
def getDestFile(fileDest): """Opens and returns the file with the path contained in the file destination or None if no file destination was specified or the file could not be opened. NOTE: File is opened in 'wb' (write/binary) mode.""" if fileDest: # PYTHON TIP: both None and an empty string evaluates to 'False' try: sf = open(fileDest, 'wb') return sf except: logger.exception("common.getDestFile() - Could not open file '{0}'.".format(fileDest)) # Failed! return None
def getSourceFile(fileSource): """Opens and returns the file with the path contained in the file source or None if no file source was specified or the file could not be opened. NOTE: File is opened in 'rb' (read/binary) mode.""" if fileSource: # PYTHON TIP: both None and an empty string evaluates to 'False' try: sf = open(fileSource, 'rb') return sf except: logger.exception("common.getSourceFile() - Could not open file '{0}'.".format(fileSource)) # Failed! return None
def processModuleFile(defaultConfig, moduleFile): """Loads JSON module data from a file-like object and processes it.""" # Assume failure. moduleData = None try: moduleData = json.load(moduleFile) except json.JSONDecodeError: logger.exception( "generate.processModuleFile() - Error loading Module File.") return if not moduleData is None: processModuleData(defaultConfig, moduleData)
def processModuleString(defaultConfig, moduleDataString): """Loads JSON Module Data from a string and processes it.""" # Assume failure. moduleData = None try: moduleData = json.loads(moduleDataString) except json.JSONDecodeError: logger.exception( "generate.processModuleString() - Could not load Module string.") return if not moduleData is None: processModuleData(defaultConfig, moduleData)
def processBuildFile(defaultConfig, buildFile): """Loads JSON Build data from a file-like object and processes it.""" # Assume failure. buildData = None try: moduleData = json.load(buildFile) except json.JSONDecodeError: logger.exception( "buildcommand.processBuildFile() - Error loading Build File.") return if not buildData is None: processBuildData(defaultConfig, buildData)
def processBuildString(defaultConfig, buildDataString): """Loads JSON Build Data from a string and processes it.""" # Assume failure. buildData = None try: buildData = json.loads(buildDataString) except json.JSONDecodeError: logger.exception( "buildcommand.processBuildString() - Could not load Build string.") return if not buildData is None: processBuildData(defaultConfig, buildData)
def copySourceToDestAndClose(sourceFile, destFile): """Writes the contents of a source file-like object to a destination file-like object and then attempts to close both if they support a close() method. Returns True on success, otherwise returns False. NOTE: Requires minimal memory because it doesn't read and write all of the source at one time. NOTE: Assumes both files were opened with the same text or binary mode. May still work otherwise.""" # Assume failure. result = False try: # Prime the pump chunk = sourceFile.read(4096) # Read 4kb at a time. # Write the data. while chunk: # PYTHON TIP: an empty string or binary object evaluates to 'False' destFile.write(chunk) chunk = sourceFile.read(4096) # Get next 4kb. # We are good! result = True except: logger.exception("common.copySourceToDest() - Copy failed with exception.") finally: # Close the files. try: sourceFile.close() except: pass try: destFile.close() except: pass # Done! return result