def parse(code): """String -> AST Parse the string and return its AST representation. May raise a ParseError exception. """ added_newline = False if not code.endswith("\n"): code += "\n" added_newline = True try: drv = driver.Driver(pygram.python_grammar, pytree.convert) result = drv.parse_string(code, True) except ParseError: log.debug("Had problems parsing:\n%s\n" % quoted_block(code)) raise # Always return a Node, not a Leaf. if isinstance(result, Leaf): result = Node(syms.file_input, [result]) result.added_newline = added_newline return result
def clear_cache(self): if self._cached_code_tree: old_module_subpath, old_code_tree = self._cached_code_tree log.debug("Code tree for module %r gets out of cache, "\ "saving to a file..." % old_module_subpath) old_code_tree.save(self._code_tree_path(old_module_subpath)) self._cached_code_tree = None
def recall_code_tree(self, module_subpath): if self._is_cached(module_subpath): return self._cached_code_tree[1] try: log.debug("Loading code tree for module %r from a file and caching..." % \ module_subpath) code_tree = load_pickle_from(self._code_tree_path(module_subpath)) self._cache(code_tree, module_subpath) return code_tree except IOError: raise CodeTreeNotFound(module_subpath)
def add_and_update_modules(project): count = 0 for modpath in python_modules_below(project.path): try: module = project.find_module_by_full_path(modpath) if module.is_up_to_date(): log.debug("%s hasn't changed since last inspection, skipping." % module.subpath) continue except ModuleNotFound: pass log.info("Inspecting module %s." % project._extract_subpath(modpath)) static.inspect_module(project, modpath) count += 1 return count
def inspect_project_dynamically(project): if project.points_of_entry and hasattr(generator_has_ended, 'unreliable'): log.warning("Pure Python implementation of util.generator_has_ended is " "not reliable on Python 2.4 and lower. Please compile the " "_util module or use Python 2.5 or higher.") for poe in project.points_of_entry.values(): try: log.info("Inspecting point of entry %s." % poe.name) dynamic.inspect_point_of_entry(poe) except SyntaxError as err: log.warning("Point of entry contains a syntax error: %s" % err) except: log.warning("Point of entry exited with error: %s" % last_exception_as_string()) log.debug("Full traceback:\n" + last_traceback())
def add_and_update_modules(project): count = 0 for modpath in python_modules_below(project.path): try: module = project.find_module_by_full_path(modpath) if module.is_up_to_date(): log.debug( "%s hasn't changed since last inspection, skipping." % module.subpath) continue except ModuleNotFound: pass log.info("Inspecting module %s." % project._extract_subpath(modpath)) static.inspect_module(project, modpath) count += 1 return count
def save(self): # To avoid inconsistencies try to save all project's modules first. If # any of those saves fail, the pickle file won't get updated. for module in self.get_modules(): log.debug("Calling save() on module %r" % module.subpath) module.save() # We don't want to have a single AST in a Project instance. self.code_trees_manager.clear_cache() # Pickling the project after saving all of its modules, so any changes # made by Module instances during save() will be preserved as well. pickled_project = pickle.dumps(self) log.debug("Writing project pickle to disk...") write_content_to_file(pickled_project, self._get_pickle_path(), binary=True)
def _generate_test_method_descriptions_for_function(self, function, module): if testable_calls(function.calls): log.debug("Detected %s in function %s." % \ (pluralize("testable call", len(testable_calls(function.calls))), function.name)) # We're calling the function, so we have to make sure it will # be imported in the test self.ensure_import((module.locator, function.name)) # We have at least one call registered, so use it. return self._method_descriptions_from_function(function) else: # No calls were traced, so we'll go for a single test stub. log.debug("Detected _no_ testable calls in function %s." % function.name) name = name2testname(underscore(function.name)) return [TestMethodDescription(name, generate_test_case(function, self.template))]
def inspect_project_dynamically(project): if project.points_of_entry and hasattr(generator_has_ended, 'unreliable'): log.warning( "Pure Python implementation of util.generator_has_ended is " "not reliable on Python 2.4 and lower. Please compile the " "_util module or use Python 2.5 or higher.") for poe in project.points_of_entry.values(): try: log.info("Inspecting point of entry %s." % poe.name) dynamic.inspect_point_of_entry(poe) except SyntaxError as err: log.warning("Point of entry contains a syntax error: %s" % err) except: log.warning("Point of entry exited with error: %s" % last_exception_as_string()) log.debug("Full traceback:\n" + last_traceback())
count = 0 for path in python_modules_below(project.get_points_of_entry_path()): poe = ensure_point_of_entry(project, path) if poe.is_out_of_sync(): count += 1 return count def inspect_project_statically(project): return add_and_update_modules(project) + \ add_and_update_points_of_entry(project) def inspect_project_dynamically(project): if project.points_of_entry and hasattr(generator_has_ended, 'unreliable'): log.warning( "Pure Python implementation of util.generator_has_ended is " "not reliable on Python 2.4 and lower. Please compile the " "_util module or use Python 2.5 or higher.") for poe in project.points_of_entry.values(): try: log.info("Inspecting point of entry %s." % poe.name) dynamic.inspect_point_of_entry(poe) except SyntaxError, err: log.warning("Point of entry contains a syntax error: %s" % err) except: log.warning("Point of entry exited with error: %s" % last_exception_as_string()) log.debug("Full traceback:\n" + last_traceback())
project.add_point_of_entry(poe) return project.get_point_of_entry(name) def add_and_update_points_of_entry(project): count = 0 for path in python_modules_below(project.get_points_of_entry_path()): poe = ensure_point_of_entry(project, path) if poe.is_out_of_sync(): count += 1 return count def inspect_project_statically(project): return add_and_update_modules(project) + \ add_and_update_points_of_entry(project) def inspect_project_dynamically(project): if project.points_of_entry and hasattr(generator_has_ended, 'unreliable'): log.warning("Pure Python implementation of util.generator_has_ended is " "not reliable on Python 2.4 and lower. Please compile the " "_util module or use Python 2.5 or higher.") for poe in project.points_of_entry.values(): try: log.info("Inspecting point of entry %s." % poe.name) dynamic.inspect_point_of_entry(poe) except SyntaxError, err: log.warning("Point of entry contains a syntax error: %s" % err) except: log.warning("Point of entry exited with error: %s" % last_exception_as_string()) log.debug("Full traceback:\n" + last_traceback())
def remember_code_tree(self, code_tree, module_subpath): log.debug("Saving code tree for module %r to a file and caching..." % \ module_subpath) code_tree.save(self._code_tree_path(module_subpath)) self._cache(code_tree, module_subpath)