Exemplo n.º 1
0
    def load_deprecated_from_module(self, module_name):
        module_path = resolve_module(module_name, self.file_path)

        if module_path is None:
            return None

        module_key = self.key_factory(module_path)

        if module_key in self.cache:
            data = self.cache[module_key]
            if data['version'] == Format.version:
                return dict(
                    load_deprecated(entry) for entry in data['deprecated'])
            elif data['generator'] == 'manual':
                warnings.warn(
                    ("skipping module {} because it has an obsolete, "
                     "manually generated, cache file: {}").format(
                         module_name, module_key.module_hash))
                return {}

        with open(module_path) as fd:
            try:
                module = ast.parse(fd.read())
            except UnicodeDecodeError:
                return []
            duc = SilentDefUseChains()
            duc.visit(module)
            anc = beniget.Ancestors()
            anc.visit(module)

            # Collect deprecated functions
            if self.recursive and module_path not in self.visited:
                self.visited.add(module_path)
                resolver = ImportResolver(self.decorator,
                                          self.reason_keyword,
                                          self.file_path,
                                          self.recursive,
                                          parent=self)
                resolver.visit(module)
                deprecated_imports = [
                    make_deprecated(d, reason)
                    for _, _, d, reason in resolver.get_deprecated_users(
                        duc, anc)
                ]
            else:
                deprecated_imports = []
            deprecated = self.collect_deprecated(module, duc, anc)
            deprecated.update(deprecated_imports)
            dl = {d[0].name: d[1] for d in deprecated if d is not None}
            data = {
                'generator': 'memestra',
                'deprecated': [store_deprecated(d, dl[d]) for d in sorted(dl)]
            }
            self.cache[module_key] = data
            return dl
Exemplo n.º 2
0
 def add_module(self, module_name):
     module_path = resolve_module(module_name)
     if module_path is not None:
         self.result.add(module_path)
Exemplo n.º 3
0
    def load_deprecated_from_module(self, module_name, level=None):
        # level may be none when it's taken from the ImportFrom node
        if level is None:
            level = 0

        # update module/pkg based on level
        rmodule_name = '.' * level + module_name

        # perform the module lookup
        try:
            module_name = resolve_name(rmodule_name, self.pkg_name)
        except (ImportError, ValueError):
            return None
        module_path = resolve_module(module_name, self.search_paths)

        # hopefully a module was found
        if module_path is None:
            return None

        module_key = self.key_factory(module_path, name_hint=module_name)

        # either find it in the cache
        if module_key in self.cache:
            data = self.cache[module_key]
            if data['version'] == Format.version:
                return dict(load_deprecated(entry)
                            for entry in data['deprecated'])
            elif data['generator'] == 'manual':
                warnings.warn(
                    ("skipping module {} because it has an obsolete, "
                     "manually generated, cache file: {}")
                    .format(module_name,
                            module_key.module_hash))
                return {}

        # or fill a new entry

        # To avoid loop, mark the key as in process
        self.cache[module_key] = {'generator': 'manual',
                                  'deprecated': []}

        with open(module_path) as fd:
            try:
                module, syntax_errors = frilouz.parse(ast.parse, fd.read())
            except UnicodeDecodeError:
                return []
            duc = SilentDefUseChains()
            duc.visit(module)
            anc = beniget.Ancestors()
            anc.visit(module)

            # Collect deprecated functions
            if self.recursive and module_path not in self.visited:
                self.visited.add(module_path)
                current_pkg = ".".join(module_name.split('.')[:-1])
                resolver = ImportResolver(self.decorator,
                                          self.reason_keyword,
                                          self.search_paths,
                                          self.recursive,
                                          parent=self,
                                          pkg_name=current_pkg)
                resolver.visit(module)
                deprecated_imports = [make_deprecated(d, reason)
                                      for _, _, d, reason in
                                      resolver.get_deprecated_users(duc, anc)]
            else:
                deprecated_imports = []
            deprecated = self.collect_deprecated(module, duc, anc,
                                                 pkg_name=module_name)
            deprecated.update(deprecated_imports)
            dl = {symbol_name(d[0]): d[1] for d in deprecated if d is not None}
            data = {'generator': 'memestra',
                    'deprecated': [store_deprecated(d, dl[d]) for d in
                                   sorted(dl)]}
            self.cache[module_key] = data
            return dl