def processFiles(files, cfg = None, pre_process_cb = None) : # insert this here, so we find files in the local dir before std library if sys.path[0] != '' : sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None : _cfg = cfg elif _cfg is None : _cfg = Config.Config() if _cfg.ignoreImportErrors: install_ignore__import__() warnings = [] utils.initConfig(_cfg) for file, (moduleName, moduleDir) in zip(files, getModules(files)) : if callable(pre_process_cb) : pre_process_cb("module %s (%s)" % (moduleName, file)) oldsyspath = sys.path[:] sys.path.insert(0, moduleDir) module = PyCheckerModule(moduleName, moduleDir=moduleDir) if not module.load() : w = Warning(module.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) sys.path = oldsyspath utils.popConfig() return warnings
def processFiles(files, cfg = None, pre_process_cb = None) : # insert this here, so we find files in the local dir before std library if sys.path[0] != '' : sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None : _cfg = cfg elif _cfg is None : _cfg = Config.Config() warnings = [] utils.initConfig(_cfg) for moduleName, filename in getModules(files) : if callable(pre_process_cb) : pre_process_cb(moduleName) module = Module(moduleName, fullpath = filename) # reload the given module, otherwise won't get new syntax errors. sysModule = sys.modules.get(moduleName) if sysModule: try: reload(sysModule) except: pass module.load(warnings) utils.popConfig() return warnings
def find(moduleList, initialCfg, suppressions = None) : "Return a list of warnings found in the module list" if suppressions is None : suppressions = {}, {} utils.initConfig(initialCfg) warnings = [] for module in moduleList : if module.moduleName in cfg().blacklist : continue modSuppress = getSuppression(module.moduleName, suppressions, warnings) globalRefs, classCodes = {}, {} # main_code can be null if there was a syntax error if module.main_code != None : funcInfo = _updateFunctionWarnings(module, module.main_code, None, warnings, globalRefs, 1) for code in funcInfo[1] : classCodes[code.co_name] = code _findFunctionWarnings(module, globalRefs, warnings, suppressions) for c in module.classes.values() : _findClassWarnings(module, c, classCodes.get(c.name), globalRefs, warnings, suppressions) if cfg().noDocModule and \ module.module != None and module.module.__doc__ == None : warnings.append(Warning(module.filename(), 1, msgs.NO_MODULE_DOC)) if cfg().allVariablesUsed or cfg().privateVariableUsed : prefix = None if not cfg().allVariablesUsed : prefix = "_" for ignoreVar in cfg().variablesToIgnore + cfg().unusedNames : globalRefs[ignoreVar] = ignoreVar warnings.extend(_getUnused(module, globalRefs, module.variables, msgs.VAR_NOT_USED, prefix)) if cfg().importUsed : if module.moduleName != utils.INIT or cfg().packageImportUsed : # always ignore readline module, if [raw_]input() is used if globalRefs.has_key('input') or \ globalRefs.has_key('raw_input'): globalRefs['readline'] = 0 warnings.extend(_getUnused(module, globalRefs, module.modules, msgs.IMPORT_NOT_USED)) if module.main_code != None : utils.popConfig() if modSuppress is not None : utils.popConfig() std_lib = None if cfg().ignoreStandardLibrary : std_lib = getStandardLibraries() return removeWarnings(warnings, getBlackList(cfg().blacklist), std_lib, cfg())
def processFiles(files, cfg=None, pre_process_cb=None): # insert this here, so we find files in the local dir before std library if sys.path[0] != '': sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None: _cfg = cfg elif _cfg is None: _cfg = Config.Config() warnings = [] utils.initConfig(_cfg) for moduleName, filename in getModules(files): if callable(pre_process_cb): pre_process_cb(moduleName) module = Module(moduleName, fullpath=filename) # reload the given module, otherwise won't get new syntax errors. sysModule = sys.modules.get(moduleName) if sysModule: try: reload(sysModule) except: pass module.load(warnings) utils.popConfig() return warnings
def processFiles(files, cfg=None, pre_process_cb=None): # insert this here, so we find files in the local dir before std library if sys.path[0] != '': sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None: _cfg = cfg elif _cfg is None: _cfg = Config.Config() if _cfg.ignoreImportErrors: install_ignore__import__() warnings = [] utils.initConfig(_cfg) for file, (moduleName, moduleDir) in zip(files, getModules(files)): if callable(pre_process_cb): pre_process_cb("module %s (%s)" % (moduleName, file)) oldsyspath = sys.path[:] sys.path.insert(0, moduleDir) module = PyCheckerModule(moduleName, moduleDir=moduleDir) if not module.load(): w = Warning(module.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) sys.path = oldsyspath utils.popConfig() return warnings
def _check(files, cfg=None, suppressions=None, printProcessing=False): # snapshot modules before and after processing, so that we only warn # about the modules loaded because of these files. # preferable to clearing the loaded modules because we don't have to # reprocess previously handled modules beforePCModules = getAllPCModules() beforeModules = dict(sys.modules.items()) utils.initConfig(cfg) utils.debug('main: Checking %d files', len(files)) utils.debug('main: Finding import warnings') importWarnings = processFiles( files, cfg, printProcessing and _print_processing or None) utils.debug('main: Found %d import warnings' % len(importWarnings)) utils.debug('main: %d modules in sys.modules' % len(sys.modules.keys())) fixupBuiltinModules() afterPCModules = getAllPCModules() newPCModules = afterPCModules[:] for m in beforePCModules: if m in newPCModules: newPCModules.remove(m) newModules = dict(sys.modules.items()) for k, v in beforeModules.items(): if k in newModules: del newModules[k] if cfg.printParse: for module in newPCModules: printer.module(module) utils.debug('main: %d Pychecker modules and %d python modules loaded', len(newPCModules), len(newModules)) # remove all sys.modules suspected of being sibling imports; they now # pollute the global namespace of sys.modules for k, v in newModules.items(): if v and _mightBeSiblingModule(v): utils.debug('main: unloading python module %s', v) del sys.modules[k] utils.debug('main: Finding warnings') # suppressions is a tuple of suppressions, suppressionRegexs dicts warnings = warn.find(newPCModules, cfg, suppressions) utils.debug('main: Found %d warnings in %d files and %d modules', len(importWarnings) + len(warnings), len(files), len(newPCModules)) # FIXME: any way to assert we are popping the one we pushed ? utils.popConfig() return importWarnings + warnings
def _check(files, cfg=None, suppressions=None, printProcessing=False): # snapshot modules before and after processing, so that we only warn # about the modules loaded because of these files. # preferable to clearing the loaded modules because we don't have to # reprocess previously handled modules beforePCModules = getAllPCModules() beforeModules = dict(sys.modules.items()) utils.initConfig(cfg) utils.debug('main: Checking %d files', len(files)) utils.debug('main: Finding import warnings') importWarnings = processFiles(files, cfg, printProcessing and _print_processing or None) utils.debug('main: Found %d import warnings' % len(importWarnings)) utils.debug('main: %d modules in sys.modules' % len(sys.modules.keys())) fixupBuiltinModules() afterPCModules = getAllPCModules() newPCModules = afterPCModules[:] for m in beforePCModules: if m in newPCModules: newPCModules.remove(m) newModules = dict(sys.modules.items()) for k, v in beforeModules.items(): if k in newModules: del newModules[k] if cfg.printParse : for module in newPCModules: printer.module(module) utils.debug('main: %d Pychecker modules and %d python modules loaded', len(newPCModules), len(newModules)) # remove all sys.modules suspected of being sibling imports; they now # pollute the global namespace of sys.modules for k, v in newModules.items(): if v and _mightBeSiblingModule(v): utils.debug('main: unloading python module %s', v) del sys.modules[k] utils.debug('main: Finding warnings') # suppressions is a tuple of suppressions, suppressionRegexs dicts warnings = warn.find(newPCModules, cfg, suppressions) utils.debug('main: Found %d warnings in %d files and %d modules', len(importWarnings) + len(warnings), len(files), len(newPCModules)) # FIXME: any way to assert we are popping the one we pushed ? utils.popConfig() return importWarnings + warnings
def _init() : global _cfg, _suppressions, _orig__import__ args = string.split(os.environ.get('PYCHECKER', '')) _cfg, files, _suppressions = Config.setupFromArgs(args) utils.initConfig(_cfg) fixupBuiltinModules(1) # keep the orig __import__ around so we can call it import __builtin__ _orig__import__ = __builtin__.__import__ __builtin__.__import__ = __import__
def _init(): global _cfg, _suppressions, _orig__import__ args = string.split(os.environ.get('PYCHECKER', '')) _cfg, files, _suppressions = Config.setupFromArgs(args) utils.initConfig(_cfg) fixupBuiltinModules(1) # keep the orig __import__ around so we can call it import __builtin__ _orig__import__ = __builtin__.__import__ __builtin__.__import__ = __import__
def processFiles(files, cfg=None, pre_process_cb=None): """ @type files: list of str @type cfg: L{Config.Config} @param pre_process_cb: callable notifying of module name, filename @type pre_process_cb: callable taking (str, str) """ warnings = [] # insert this here, so we find files in the local dir before std library if sys.path[0] != '' : sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None: _cfg = cfg elif _cfg is None: _cfg = Config.Config() if _cfg.ignoreImportErrors: install_ignore__import__() utils.initConfig(_cfg) utils.debug('Processing %d files' % len(files)) for file, (moduleName, moduleDir) in zip(files, getModules(files)): if callable(pre_process_cb): pre_process_cb("module %s (%s)" % (moduleName, file)) # create and load the PyCheckerModule, tricking sys.path temporarily oldsyspath = sys.path[:] if moduleDir is not None: sys.path.insert(0, moduleDir) pcmodule = pcmodules.PyCheckerModule(moduleName, moduleDir=moduleDir) loaded = pcmodule.load() sys.path = oldsyspath if not loaded: w = Warning(pcmodule.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) utils.debug('Processed %d files' % len(files)) utils.popConfig() return warnings
def loadFile(self, filename): """ Load suppressions from the given file. @type filename: str @rtype: tuple of (dict, dict) """ suppressions = {} suppressionRegexs = {} try: tmpGlobals, tmpLocals = {}, {} execfile(filename, tmpGlobals, tmpLocals) suppressions = _getSuppressions('suppressions', tmpLocals, filename) regexs = _getSuppressions('suppressionRegexs', tmpLocals, filename) # debug them here, since the options in tmpLocals can turn off # debugging again # We don't have an active config here yet. Push ourselves, # since we first got loaded with command line arguments, # and so -d shows these suppression messages from pychecker import utils utils.initConfig(self) if suppressions: utils.debug('Loaded %d suppressions from %s', len(suppressions), filename) if suppressionRegexs: utils.debug('Loaded %d suppression regexs from %s', len(suppressionRegexs), filename) utils.popConfig() # now set our attributes based on the locals for key, value in tmpLocals.items(): if self.__dict__.has_key(key): self.__dict__[key] = value elif key not in ('suppressions', 'suppressionRegexs') and \ key[0] != '_': print "Warning, option (%s) doesn't exist, ignoring" % key for regex_str in regexs.keys(): regex = re.compile(regex_str) suppressionRegexs[regex] = regexs[regex_str] except IOError: pass # ignore if no file except Exception, detail: print "Warning, error loading defaults file:", filename, detail
def processFiles(files, cfg=None, pre_process_cb=None): """ @type files: list of str @type cfg: L{Config.Config} @param pre_process_cb: callable notifying of module name, filename @type pre_process_cb: callable taking (str, str) """ warnings = [] # insert this here, so we find files in the local dir before std library if sys.path[0] != '': sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None: _cfg = cfg elif _cfg is None: _cfg = Config.Config() if _cfg.ignoreImportErrors: install_ignore__import__() utils.initConfig(_cfg) utils.debug('Processing %d files' % len(files)) for file, (moduleName, moduleDir) in zip(files, getModules(files)): if callable(pre_process_cb): pre_process_cb("module %s (%s)" % (moduleName, file)) # create and load the PyCheckerModule, tricking sys.path temporarily oldsyspath = sys.path[:] if moduleDir is not None: sys.path.insert(0, moduleDir) pcmodule = pcmodules.PyCheckerModule(moduleName, moduleDir=moduleDir) loaded = pcmodule.load() sys.path = oldsyspath if not loaded: w = Warning(pcmodule.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) utils.debug('Processed %d files' % len(files)) utils.popConfig() return warnings
def loadFile(self, filename): """ Load suppressions from the given file. @type filename: str @rtype: tuple of (dict, dict) """ suppressions = {} suppressionRegexs = {} try: tmpGlobals, tmpLocals = {}, {} execfile(filename, tmpGlobals, tmpLocals) suppressions = _getSuppressions("suppressions", tmpLocals, filename) regexs = _getSuppressions("suppressionRegexs", tmpLocals, filename) # debug them here, since the options in tmpLocals can turn off # debugging again # We don't have an active config here yet. Push ourselves, # since we first got loaded with command line arguments, # and so -d shows these suppression messages from pychecker import utils utils.initConfig(self) if suppressions: utils.debug("Loaded %d suppressions from %s", len(suppressions), filename) if suppressionRegexs: utils.debug("Loaded %d suppression regexs from %s", len(suppressionRegexs), filename) utils.popConfig() # now set our attributes based on the locals for key, value in tmpLocals.items(): if self.__dict__.has_key(key): self.__dict__[key] = value elif key not in ("suppressions", "suppressionRegexs") and key[0] != "_": print "Warning, option (%s) doesn't exist, ignoring" % key for regex_str in regexs.keys(): regex = re.compile(regex_str) suppressionRegexs[regex] = regexs[regex_str] except IOError: pass # ignore if no file except Exception, detail: print "Warning, error loading defaults file:", filename, detail
def processFiles(files, cfg = None, pre_process_cb = None) : # insert this here, so we find files in the local dir before std library if sys.path[0] != '' : sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None : _cfg = cfg elif _cfg is None : _cfg = Config.Config() warnings = [] utils.initConfig(_cfg) for moduleName in getModules(files) : if callable(pre_process_cb) : pre_process_cb(moduleName) module = PyCheckerModule(moduleName) if not module.load() : w = Warning(module.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) utils.popConfig() return warnings
def processFiles(files, cfg=None, pre_process_cb=None): # insert this here, so we find files in the local dir before std library if sys.path[0] != '': sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None: _cfg = cfg elif _cfg is None: _cfg = Config.Config() warnings = [] utils.initConfig(_cfg) for moduleName in getModules(files): if callable(pre_process_cb): pre_process_cb(moduleName) module = PyCheckerModule(moduleName) if not module.load(): w = Warning(module.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) utils.popConfig() return warnings
def find(moduleList, initialCfg, suppressions=None): "Return a list of warnings found in the module list" if suppressions is None: suppressions = {}, {} utils.initConfig(initialCfg) warnings = [] for module in moduleList: if module.moduleName in cfg().blacklist: continue modSuppress = getSuppression(module.moduleName, suppressions, warnings) globalRefs, classCodes = {}, {} # main_code can be null if there was a syntax error if module.main_code != None: funcInfo = _updateFunctionWarnings(module, module.main_code, None, warnings, globalRefs, 1) for code in funcInfo[1]: classCodes[code.co_name] = code _findFunctionWarnings(module, globalRefs, warnings, suppressions) for c in module.classes.values(): _findClassWarnings(module, c, classCodes.get(c.name), globalRefs, warnings, suppressions) if cfg().noDocModule and \ module.module != None and module.module.__doc__ == None : warnings.append(Warning(module.filename(), 1, msgs.NO_MODULE_DOC)) if cfg().allVariablesUsed or cfg().privateVariableUsed: prefix = None if not cfg().allVariablesUsed: prefix = "_" for ignoreVar in cfg().variablesToIgnore + cfg().unusedNames: globalRefs[ignoreVar] = ignoreVar warnings.extend( _getUnused(module, globalRefs, module.variables, msgs.VAR_NOT_USED, prefix)) if cfg().importUsed: if module.moduleName != utils.INIT or cfg().packageImportUsed: # always ignore readline module, if [raw_]input() is used if globalRefs.has_key('input') or \ globalRefs.has_key('raw_input'): globalRefs['readline'] = 0 warnings.extend( _getUnused(module, globalRefs, module.modules, msgs.IMPORT_NOT_USED)) if module.main_code != None: utils.popConfig() if modSuppress is not None: utils.popConfig() std_lib = None if cfg().ignoreStandardLibrary: std_lib = getStandardLibraries() return removeWarnings(warnings, getBlackList(cfg().blacklist), std_lib, cfg())
def find(moduleList, initialCfg, suppressions=None): "Return a list of warnings found in the module list" if suppressions is None : suppressions = {}, {} utils.initConfig(initialCfg) utils.debug('Finding warnings in %d modules' % len(moduleList)) warnings = [] before = 0 for module in moduleList : if module.moduleName in cfg().blacklist : continue modSuppress = getSuppression(module.moduleName, suppressions, warnings) globalRefs, classCodes = {}, {} # mainCode can be null if there was a syntax error if module.mainCode != None : utils.debug("module:", module) before = len(warnings) funcInfo = _updateFunctionWarnings(module, module.mainCode, None, warnings, globalRefs, 1) if before != len(warnings): utils.debug("module: %r __main__ triggered %d warnings", module, len(warnings) - before) for code in funcInfo[1] : classCodes[code.co_name] = code before = len(warnings) _findFunctionWarnings(module, globalRefs, warnings, suppressions) if before != len(warnings): utils.debug("module: %r functions triggered %d warnings", module, len(warnings) - before) before = len(warnings) for c in module.classes.values(): _findClassWarnings(module, c, classCodes.get(c.name), globalRefs, warnings, suppressions) if before != len(warnings): utils.debug("module: %r classes triggered %d warnings", module, len(warnings) - before) if cfg().noDocModule and \ module.module != None and module.module.__doc__ == None: warnings.append(Warning(module.filename(), 1, msgs.NO_MODULE_DOC)) utils.debug("module: %r module doc triggered 1 warning") before = len(warnings) if cfg().allVariablesUsed or cfg().privateVariableUsed: prefix = None if not cfg().allVariablesUsed: prefix = "_" for ignoreVar in cfg().variablesToIgnore + cfg().unusedNames: globalRefs[ignoreVar] = ignoreVar warnings.extend(_getUnused(module, globalRefs, module.variables, msgs.VAR_NOT_USED, prefix)) if before != len(warnings): utils.debug("module: %r unused variables triggered %d warnings", module, len(warnings) - before) before = len(warnings) if cfg().importUsed: if module.moduleName != utils.INIT or cfg().packageImportUsed: # always ignore readline module, if [raw_]input() is used if globalRefs.has_key('input') or \ globalRefs.has_key('raw_input'): globalRefs['readline'] = 0 warnings.extend(_getUnused(module, globalRefs, module.modules, msgs.IMPORT_NOT_USED)) if before != len(warnings): utils.debug("module: %r unused imports triggered %d warnings", module, len(warnings) - before) # we have to do this here, b/c checkFunction doesn't popConfig for # classes this allows us to have __pychecker__ apply to all methods # when defined at class scope if module.mainCode != None: utils.popConfig() if modSuppress is not None: utils.popConfig() std_lib = None if cfg().ignoreStandardLibrary: std_lib = getStandardLibraries() ret = removeWarnings(warnings, getBlackList(cfg().blacklist), std_lib, cfg()) utils.debug('Found %d warnings in %d modules' % (len(ret), len(moduleList))) return ret
command_file = argv[1][1:] try: f = open(command_file, 'r') command_line = f.read() f.close() except IOError, err: sys.stderr.write("Unable to read commands from file: %s\n %s\n" % \ (command_file, err)) sys.exit(101) # convert to an argv list, keeping argv[0] and the files to process argv = argv[:1] + string.split(command_line) + argv[2:] global _cfg _cfg, files, suppressions = Config.setupFromArgs(argv[1:]) utils.initConfig(_cfg) if not files: return 0 # Now that we've got the args, update the list of evil C objects for evil_doer in _cfg.evil: pcmodules.EVIL_C_OBJECTS[evil_doer] = None # insert this here, so we find files in the local dir before std library sys.path.insert(0, '') # import here, because sys.path is not set up at the top for pychecker dir from pychecker import check warnings = check._check(files, cfg=_cfg, suppressions=suppressions,
command_file = argv[1][1:] try: f = open(command_file, 'r') command_line = f.read() f.close() except IOError, err: sys.stderr.write("Unable to read commands from file: %s\n %s\n" % \ (command_file, err)) sys.exit(101) # convert to an argv list, keeping argv[0] and the files to process argv = argv[:1] + string.split(command_line) + argv[2:] global _cfg _cfg, files, suppressions = Config.setupFromArgs(argv[1:]) utils.initConfig(_cfg) if not files : return 0 # Now that we've got the args, update the list of evil C objects for evil_doer in _cfg.evil: pcmodules.EVIL_C_OBJECTS[evil_doer] = None # insert this here, so we find files in the local dir before std library sys.path.insert(0, '') utils.debug('main: Finding import warnings') importWarnings = processFiles(files, _cfg, _print_processing) utils.debug('main: Found %d import warnings' % len(importWarnings)) fixupBuiltinModules()