Ejemplo n.º 1
0
    def discover(self, *, args, identification_extensions):  # noqa: D102
        if args.paths is None:
            return set()

        # on Windows manually check for wildcards and expand them
        if sys.platform == 'win32':
            _expand_wildcards(args.paths)

        logger.log(1, 'PathPackageDiscovery.discover(%s)', args.paths)

        visited_paths = set()
        descs = set()
        for path in args.paths:
            real_path = os.path.realpath(path)
            # avoid recrawling same paths
            if real_path in visited_paths:
                continue
            visited_paths.add(real_path)

            try:
                result = identify(identification_extensions, path)
            except IgnoreLocationException:
                continue
            if result:
                descs.add(result)

        return descs
Ejemplo n.º 2
0
    def discover(self, *, args, identification_extensions):  # noqa: D102
        if args.paths is None:
            return set()

        # manually check for wildcards and expand them in case
        # the values were not provided through the shell
        expand_dir_wildcards(args.paths)

        logger.log(1, 'PathPackageDiscovery.discover(%s)', args.paths)

        visited_paths = set()
        descs = set()
        for path in args.paths:
            real_path = os.path.realpath(path)
            # avoid recrawling same paths
            if real_path in visited_paths:
                continue
            visited_paths.add(real_path)

            try:
                result = identify(identification_extensions, path)
            except IgnoreLocationException:
                continue
            if result:
                descs.add(result)

        return descs
Ejemplo n.º 3
0
def test_identify():
    path = '/some/path'
    context = EntryPointContext(extension1=Extension1,
                                extension2=Extension2,
                                extension3=Extension3,
                                extension4=Extension4)

    with context:
        # no identification
        desc = identify({}, path)
        assert desc is None

        # no complete identification
        extensions = get_package_identification_extensions()
        extensions[80]['extension1'].identify = Mock(side_effect=identify_name)
        desc = identify(extensions, path)
        assert desc is None

        # valid result combined across priority groups
        extensions = get_package_identification_extensions()
        extensions[100]['extension4'].identify = Mock(
            side_effect=identify_type)
        desc = identify(extensions, path)
        assert isinstance(desc, PackageDescriptor)
        assert str(desc.path) == '/some/path'.replace('/', os.sep)
        assert desc.name == 'name'
        assert desc.type == 'type'

        # skip location
        extensions = get_package_identification_extensions()
        extensions[90]['extension3'].identify = Mock(
            side_effect=IgnoreLocationException())
        with pytest.raises(IgnoreLocationException):
            identify(extensions, path)

        # valid result from first priority group
        # lower priority groups are not even invoked
        extensions = get_package_identification_extensions()
        extensions[100]['extension4'].identify.side_effect = \
            identify_name_and_type
        desc = identify(extensions, path)
        assert isinstance(desc, PackageDescriptor)
        assert str(desc.path) == '/some/path'.replace('/', os.sep)
        assert desc.name == 'name'
        assert desc.type == 'type'

    with context:
        # multiple different results result in skipping the location
        extensions = get_package_identification_extensions()
        extensions[100]['extension2'].identify = Mock(
            side_effect=identify_name)
        extensions[100]['extension4'].identify = Mock(
            side_effect=identify_type)
        with pytest.raises(IgnoreLocationException):
            identify(extensions, path)
Ejemplo n.º 4
0
    def discover(self, *, args, identification_extensions):  # noqa: D102
        if args.base_paths is None:
            return set()

        # manually check for wildcards and expand them in case
        # the values were not provided through the shell
        expand_dir_wildcards(args.base_paths)

        logger.info(
            'Crawling recursively for packages in %s',
            ', '.join(["'%s'" % os.path.abspath(p) for p in args.base_paths]))

        visited_paths = set()
        descs = set()
        for base_path in args.base_paths:
            for dirpath, dirnames, filenames in os.walk(
                    base_path,
                    followlinks=True,
            ):
                real_dirpath = os.path.realpath(dirpath)
                # avoid recrawling same paths
                if real_dirpath in visited_paths:
                    del dirnames[:]
                    continue
                visited_paths.add(real_dirpath)

                try:
                    result = identify(identification_extensions, dirpath)
                except IgnoreLocationException:
                    del dirnames[:]
                    continue
                if result:
                    descs.add(result)
                    del dirnames[:]
                    continue

                # skip subdirectories starting with a dot
                dirnames[:] = filter(lambda d: not d.startswith('.'), dirnames)
                dirnames.sort()
        return descs