def _setRecordFiles(): for (target, pocname, pocid, component, version, status, r_time, result) in kb.results: if type(status) != str: status = status[1] outputPath = os.path.join(getUnicode(paths.POCSUITE_OUTPUT_PATH), normalizeUnicode(getUnicode(target))) if not os.path.isdir(outputPath): try: os.makedirs(outputPath, 0755) except (OSError, IOError), ex: try: tempDir = tempfile.mkdtemp(prefix="pocsuitetoutput") except Exception, _: errMsg = "unable to write to the temporary directory ('%s'). " % _ errMsg += "Please make sure that your disk is not full and " errMsg += "that you have sufficient write permissions to " errMsg += "create temporary files and/or directories" raise PocsuiteSystemException(errMsg) warnMsg = "unable to create output directory " warnMsg += "'%s' (%s). " % (outputPath, getUnicode(ex)) warnMsg += "Using temporary directory '%s' instead" % getUnicode(tempDir) logger.warn(warnMsg) outputPath = tempDir
def _createTargetDirs(): """ Create the output directory. """ if not os.path.isdir(paths.POCSUITE_OUTPUT_PATH): try: if not os.path.isdir(paths.POCSUITE_OUTPUT_PATH): os.makedirs(paths.POCSUITE_OUTPUT_PATH, 0755) warnMsg = "using '%s' as the output directory" % paths.POCSUITE_OUTPUT_PATH logger.log(CUSTOM_LOGGING.WARNING, warnMsg) except (OSError, IOError), ex: try: tempDir = tempfile.mkdtemp(prefix="pocsuiteoutput") except Exception, _: errMsg = "unable to write to the temporary directory ('%s'). " % _ errMsg += "Please make sure that your disk is not full and " errMsg += "that you have sufficient write permissions to " errMsg += "create temporary files and/or directories" raise PocsuiteSystemException(errMsg) warnMsg = "unable to create regular output directory " warnMsg += "'%s' (%s). " % (paths.POCSUITE_OUTPUT_PATH, getUnicode(ex)) warnMsg += "Using temporary directory '%s' instead" % getUnicode(tempDir) logger.log(CUSTOM_LOGGING.WARNING, warnMsg) paths.POCUSITE_OUTPUT_PATH = tempDir
def _setRecordFiles(): for (target, pocname, pocid, component, version, status, r_time, result) in kb.results: if type(status) != str: status = status[1] outputPath = os.path.join(getUnicode(paths.POCSUITE_OUTPUT_PATH), normalizeUnicode(getUnicode(target))) # colon is illegal in windows file system if "Windows" in platform.platform(): # replace colon between ip and port as "-", avoid unicode replace error # bypass the first colon in drive letter like "C:\" outputPath = outputPath[:4] + outputPath[4:].replace(":", "-") if not os.path.isdir(outputPath): try: os.makedirs(outputPath, 0755) except (OSError, IOError), ex: try: tempDir = tempfile.mkdtemp(prefix="pocsuitetoutput") except Exception, _: errMsg = "unable to write to the temporary directory ('%s'). " % _ errMsg += "Please make sure that your disk is not full and " errMsg += "that you have sufficient write permissions to " errMsg += "create temporary files and/or directories" raise PocsuiteSystemException(errMsg) warnMsg = "unable to create output directory " warnMsg += "'%s' (%s). " % (outputPath, getUnicode(ex)) warnMsg += "Using temporary directory '%s' instead" % getUnicode( tempDir) logger.warn(warnMsg) outputPath = tempDir
def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, unique=False): """ @function returns newline delimited items contained inside file """ retVal = list() if not unique else OrderedDict() checkFile(filename) try: with open(filename, 'r') as f: for line in (f.readlines() if unicode_ else f.xreadlines()): # xreadlines doesn't return unicode strings when codecs.open() is used if commentPrefix and line.find(commentPrefix) != -1: line = line[:line.find(commentPrefix)] line = line.strip() if not unicode_: try: line = str.encode(line) except UnicodeDecodeError: continue if line: if lowercase: line = line.lower() if unique and line in retVal: continue if unique: retVal[line] = True else: retVal.append(line) except (IOError, OSError, MemoryError), ex: errMsg = "something went wrong while trying " errMsg += "to read the content of file '%s' ('%s')" % (filename, ex) raise PocsuiteSystemException(errMsg)
def checkFile(filename): """ @function Checks for file existence and readability """ valid = True if filename is None or not os.path.isfile(filename): valid = False if valid: try: with open(filename, "rb"): pass except: valid = False if not valid: raise PocsuiteSystemException("unable to read file '%s'" % filename)