Beispiel #1
0
def test_extract_tarbytes(mocker):
    """should extract tar file from memory"""
    test_bytes = bytearray("foobar", "utf-8")
    mock_io = mocker.patch.object(utils.helpers.io, "BytesIO")
    mock_io.return_value = io.BytesIO(test_bytes)
    mock_tarfile = mocker.patch.object(utils.helpers, "tarfile")
    mock_tar = mock_tarfile.open.return_value.__enter__.return_value
    utils.extract_tarbytes(test_bytes, "foobar")
    mock_tarfile.open.assert_called_once_with(fileobj=io.BytesIO(test_bytes),
                                              mode="r:gz")
    mock_tar.extractall.assert_called_once_with("foobar")
Beispiel #2
0
    def _fetch_package(self, url):
        """Fetch and stub package at url

        Args:
            url (str): URL to fetch

        Returns:
            Path: path to package
        """
        with tempfile.TemporaryDirectory() as tmp_dir:
            tmp_path = Path(tmp_dir)
            file_name = utils.get_url_filename(url)
            _file_name = "".join(self.log.iter_formatted(f"$B[{file_name}]"))
            content = utils.stream_download(
                url, desc=f"{self.log.get_service()} {_file_name}")
            pkg_path = utils.extract_tarbytes(content, tmp_path)
            ignore = ['setup.py', '__version__', 'test_']
            pkg_init = next(pkg_path.rglob("__init__.py"), None)
            py_files = [f for f in pkg_path.rglob(
                "*.py") if not any(i in f.name for i in ignore)]
            stubs = [utils.generate_stub(f) for f in py_files]
            if pkg_init:
                data_path = self.pkg_data / pkg_init.parent.name
                shutil.copytree(pkg_init.parent, data_path)
                return data_path
            for file, stub in stubs:
                shutil.copy2(file, (self.pkg_data / file.name))
                shutil.copy2(stub, (self.pkg_data / stub.name))
Beispiel #3
0
    def __enter__(self) -> Union[Path, List[Tuple[Path, Path]]]:
        """Prepare Pypi package for installation.

        Extracts the package into a temporary directory then
        generates stubs for type hinting.
        This helps with intellisense.

        If the dependency is a module, a list
        of tuples with the file and stub path, respectively,
        will be returned. Otherwise, the path to the package
        root will be returned.

        Returns:
            Root package path or list of files.

        """
        self.tmp_path = Path(mkdtemp())
        with self.handle_cleanup():
            path = utils.extract_tarbytes(self.fetch(), self.tmp_path)
            stubs = self.generate_stubs(path)
            pkg_root = self.get_root(path)
        return pkg_root or stubs