Ejemplo n.º 1
0
    def check_module(module):
        try:
            possible_stmts = module.used_names['path']
        except KeyError:
            return builtin.get_sys_path()

        sys_path = list(builtin.get_sys_path())  # copy
        for p in possible_stmts:
            try:
                call = p.get_assignment_calls().get_only_subelement()
            except AttributeError:
                continue
            n = call.name
            if not isinstance(n, parsing.Name) or len(n.names) != 3:
                continue
            if n.names[:2] != ('sys', 'path'):
                continue
            array_cmd = n.names[2]
            if call.execution is None:
                continue
            exe = call.execution
            if not (array_cmd == 'insert' and len(exe) == 2
                    or array_cmd == 'append' and len(exe) == 1):
                continue

            if array_cmd == 'insert':
                exe_type, exe.type = exe.type, parsing.Array.NOARRAY
                exe_pop = exe.values.pop(0)
                res = execute_code(exe.get_code())
                if res is not None:
                    sys_path.insert(0, res)
                    debug.dbg('sys path inserted: %s' % res)
                exe.type = exe_type
                exe.values.insert(0, exe_pop)
            elif array_cmd == 'append':
                res = execute_code(exe.get_code())
                if res is not None:
                    sys_path.append(res)
                    debug.dbg('sys path added: %s' % res)
        return sys_path
Ejemplo n.º 2
0
    def check_module(module):
        try:
            possible_stmts = module.used_names['path']
        except KeyError:
            return builtin.get_sys_path()

        sys_path = list(builtin.get_sys_path())  # copy
        for p in possible_stmts:
            try:
                call = p.get_assignment_calls().get_only_subelement()
            except AttributeError:
                continue
            n = call.name
            if not isinstance(n, parsing.Name) or len(n.names) != 3:
                continue
            if n.names[:2] != ('sys', 'path'):
                continue
            array_cmd = n.names[2]
            if call.execution is None:
                continue
            exe = call.execution
            if not (array_cmd == 'insert' and len(exe) == 2
                    or array_cmd == 'append' and len(exe) == 1):
                continue

            if array_cmd == 'insert':
                exe_type, exe.type = exe.type, parsing.Array.NOARRAY
                exe_pop = exe.values.pop(0)
                res = execute_code(exe.get_code())
                if res is not None:
                    sys_path.insert(0, res)
                    debug.dbg('sys path inserted: %s' % res)
                exe.type = exe_type
                exe.values.insert(0, exe_pop)
            elif array_cmd == 'append':
                res = execute_code(exe.get_code())
                if res is not None:
                    sys_path.append(res)
                    debug.dbg('sys path added: %s' % res)
        return sys_path
Ejemplo n.º 3
0
    def _follow_file_system(self):
        """
        Find a module with a path (of the module, like usb.backend.libusb10).
        """
        def follow_str(ns_path, string):
            debug.dbg('follow_module', ns_path, string)
            path = None
            if ns_path:
                path = ns_path
            elif self.import_stmt.relative_count:
                path = self.get_relative_path()

            global imports_processed
            imports_processed += 1
            if path is not None:
                return imp.find_module(string, [path])
            else:
                debug.dbg('search_module', string, self.file_path)
                # Override the sys.path. It works only good that way.
                # Injecting the path directly into `find_module` did not work.
                sys.path, temp = sys_path_mod, sys.path
                try:
                    i = imp.find_module(string)
                except ImportError:
                    sys.path = temp
                    raise
                sys.path = temp
                return i

        if self.file_path:
            sys_path_mod = list(self.sys_path_with_modifications())
            sys_path_mod.insert(0, self.file_path)
        else:
            sys_path_mod = list(builtin.get_sys_path())

        current_namespace = (None, None, None)
        # now execute those paths
        rest = []
        for i, s in enumerate(self.import_path):
            try:
                current_namespace = follow_str(current_namespace[1], s)
            except ImportError:
                if self.import_stmt.relative_count \
                                and len(self.import_path) == 1:
                    # follow `from . import some_variable`
                    rel_path = self.get_relative_path()
                    try:
                        current_namespace = follow_str(rel_path, '__init__')
                    except ImportError:
                        pass
                if current_namespace[1]:
                    rest = self.import_path[i:]
                else:
                    raise ModuleNotFound(
                            'The module you searched has not been found')

        sys_path_mod.pop(0)  # TODO why is this here?
        path = current_namespace[1]
        is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY

        f = None
        if is_package_directory or current_namespace[0]:
            # is a directory module
            if is_package_directory:
                path += '/__init__.py'
                with open(path) as f:
                    source = f.read()
            else:
                source = current_namespace[0].read()
                current_namespace[0].close()
            if path.endswith('.py'):
                f = modules.Module(path, source)
            else:
                f = builtin.Parser(path=path)
        else:
            f = builtin.Parser(name=path)

        return f.parser.module, rest
Ejemplo n.º 4
0
    def _follow_file_system(self):
        """
        Find a module with a path (of the module, like usb.backend.libusb10).
        """
        def follow_str(ns, string):
            debug.dbg('follow_module', ns, string)
            path = None
            if ns:
                path = ns[1]
            elif self.import_stmt.relative_count:
                module = self.import_stmt.get_parent_until()
                path = os.path.abspath(module.path)
                for i in range(self.import_stmt.relative_count):
                    path = os.path.dirname(path)

            global imports_processed
            imports_processed += 1
            if path is not None:
                return imp.find_module(string, [path])
            else:
                debug.dbg('search_module', string, self.file_path)
                # Override the sys.path. It works only good that way.
                # Injecting the path directly into `find_module` did not work.
                sys.path, temp = sys_path_mod, sys.path
                try:
                    i = imp.find_module(string)
                except ImportError:
                    sys.path = temp
                    raise
                sys.path = temp
                return i

        if self.file_path:
            sys_path_mod = list(self.sys_path_with_modifications())
            sys_path_mod.insert(0, self.file_path)
        else:
            sys_path_mod = list(builtin.get_sys_path())

        current_namespace = None
        # now execute those paths
        rest = []
        for i, s in enumerate(self.import_path):
            try:
                current_namespace = follow_str(current_namespace, s)
            except ImportError:
                if current_namespace:
                    rest = self.import_path[i:]
                else:
                    raise ModuleNotFound(
                        'The module you searched has not been found')

        sys_path_mod.pop(0)  # TODO why is this here?
        path = current_namespace[1]
        is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY

        f = None
        if is_package_directory or current_namespace[0]:
            # is a directory module
            if is_package_directory:
                path += '/__init__.py'
                with open(path) as f:
                    source = f.read()
            else:
                source = current_namespace[0].read()
                current_namespace[0].close()
            if path.endswith('.py'):
                f = modules.Module(path, source)
            else:
                f = builtin.Parser(path=path)
        else:
            f = builtin.Parser(name=path)

        return f.parser.module, rest
Ejemplo n.º 5
0
    def _follow_file_system(self):
        """
        Find a module with a path (of the module, like usb.backend.libusb10).
        """
        def follow_str(ns_path, string):
            debug.dbg('follow_module', ns_path, string)
            path = None
            if ns_path:
                path = ns_path
            elif self.import_stmt.relative_count:
                path = self.get_relative_path()

            if path and not isinstance(path, list):
                path = [path]

            global imports_processed
            imports_processed += 1
            if path is not None:
                return imp.find_module(string, path)
            else:
                debug.dbg('search_module', string, self.file_path)
                # Override the sys.path. It works only good that way.
                # Injecting the path directly into `find_module` did not work.
                sys.path, temp = sys_path_mod, sys.path
                try:
                    i = imp.find_module(string)
                except ImportError:
                    sys.path = temp
                    raise
                sys.path = temp
                return i

        if self.file_path:
            sys_path_mod = list(self.sys_path_with_modifications())
            sys_path_mod.insert(0, self.file_path)
        else:
            sys_path_mod = list(builtin.get_sys_path())

        current_namespace = (None, None, None)
        # now execute those paths
        rest = []
        for i, s in enumerate(self.import_path):
            try:
                current_namespace = follow_str(current_namespace[1], s)
                if not current_namespace[1] and is_py33:
                    loader = importlib.find_loader(s)
                    if loader.__name__ == 'BuiltinImporter':
                        current_namespace = (current_namespace[0], s,
                                             current_namespace[2])

            except ImportError as ex:
                if not i and is_py33:
                    loader = importlib.find_loader(s)
                    if 'NamespaceLoader' in repr(loader):
                        try:
                            m = __import__(s)
                        except ImportError:
                            pass
                        else:
                            current_namespace = (None, list(m.__path__), None)
                            continue

                if self.import_stmt.relative_count \
                                and len(self.import_path) == 1:
                    # follow `from . import some_variable`
                    rel_path = self.get_relative_path()
                    try:
                        current_namespace = follow_str(rel_path, '__init__')
                    except ImportError:
                        pass
                if current_namespace[1]:
                    rest = self.import_path[i:]
                else:
                    raise ModuleNotFound(
                        'The module you searched has not been found')

        sys_path_mod.pop(0)  # TODO why is this here?
        path = current_namespace[1]
        is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY

        f = None
        if is_package_directory or current_namespace[0]:
            # is a directory module
            if is_package_directory:
                path += '/__init__.py'
                with open(path) as f:
                    source = f.read()
            else:
                source = current_namespace[0].read()
                current_namespace[0].close()
            if path.endswith('.py'):
                f = modules.Module(path, source)
            else:
                f = builtin.Parser(path=path)
        else:
            f = builtin.Parser(name=path)

        return f.parser.module, rest