Example #1
0
    def __init__(self, name, directory="."):
        self._name = module_name(name)
        self._in_src = False

        # It must exist either as a .py file or a directory, but not both
        pkg_dir = Path(directory, self._name)
        py_file = Path(directory, self._name + ".py")
        if pkg_dir.is_dir() and py_file.is_file():
            raise ValueError("Both {} and {} exist".format(pkg_dir, py_file))
        elif pkg_dir.is_dir():
            self._path = pkg_dir
            self._is_package = True
        elif py_file.is_file():
            self._path = py_file
            self._is_package = False
        else:
            # Searching for a src module
            src_pkg_dir = Path(directory, "src", self._name)
            src_py_file = Path(directory, "src", self._name + ".py")

            if src_pkg_dir.is_dir() and src_py_file.is_file():
                raise ValueError("Both {} and {} exist".format(
                    pkg_dir, py_file))
            elif src_pkg_dir.is_dir():
                self._in_src = True
                self._path = src_pkg_dir
                self._is_package = True
            elif src_py_file.is_file():
                self._in_src = True
                self._path = src_py_file
                self._is_package = False
            else:
                raise ValueError(
                    "No file/folder found for package {}".format(name))
Example #2
0
    def __init__(self, name, directory=".", packages=None, includes=None):
        self._name = module_name(name)
        self._in_src = False
        self._is_package = False
        self._path = Path(directory)
        self._includes = []
        packages = packages or []
        includes = includes or []

        if not packages:
            # It must exist either as a .py file or a directory, but not both
            pkg_dir = Path(directory, self._name)
            py_file = Path(directory, self._name + ".py")
            if pkg_dir.is_dir() and py_file.is_file():
                raise ValueError("Both {} and {} exist".format(
                    pkg_dir, py_file))
            elif pkg_dir.is_dir():
                packages = [{"include": str(pkg_dir.relative_to(self._path))}]
            elif py_file.is_file():
                packages = [{"include": str(py_file.relative_to(self._path))}]
            else:
                # Searching for a src module
                src = Path(directory, "src")
                src_pkg_dir = src / self._name
                src_py_file = src / (self._name + ".py")

                if src_pkg_dir.is_dir() and src_py_file.is_file():
                    raise ValueError("Both {} and {} exist".format(
                        pkg_dir, py_file))
                elif src_pkg_dir.is_dir():
                    packages = [{
                        "include": str(src_pkg_dir.relative_to(src)),
                        "from": str(src.relative_to(self._path)),
                    }]
                elif src_py_file.is_file():
                    packages = [{
                        "include": str(src_py_file.relative_to(src)),
                        "from": str(src.relative_to(self._path)),
                    }]
                else:
                    raise ModuleOrPackageNotFound(
                        "No file/folder found for package {}".format(name))

        for package in packages:
            formats = package.get("format")
            if formats and not isinstance(formats, list):
                formats = [formats]

            self._includes.append(
                PackageInclude(
                    self._path,
                    package["include"],
                    formats=formats,
                    source=package.get("from"),
                ))

        for include in includes:
            self._includes.append(Include(self._path, include))
Example #3
0
    def __init__(self, name, directory=".", packages=None, includes=None):
        self._name = module_name(name)
        self._in_src = False
        self._is_package = False
        self._path = Path(directory)
        self._includes = []
        packages = packages or []
        includes = includes or []

        if not packages:
            # It must exist either as a .py file or a directory, but not both
            pkg_dir = Path(directory, self._name)
            py_file = Path(directory, self._name + ".py")
            if pkg_dir.is_dir() and py_file.is_file():
                raise ValueError("Both {} and {} exist".format(pkg_dir, py_file))
            elif pkg_dir.is_dir():
                packages = [{"include": str(pkg_dir.relative_to(self._path))}]
            elif py_file.is_file():
                packages = [{"include": str(py_file.relative_to(self._path))}]
            else:
                # Searching for a src module
                src = Path(directory, "src")
                src_pkg_dir = src / self._name
                src_py_file = src / (self._name + ".py")

                if src_pkg_dir.is_dir() and src_py_file.is_file():
                    raise ValueError("Both {} and {} exist".format(pkg_dir, py_file))
                elif src_pkg_dir.is_dir():
                    packages = [
                        {
                            "include": str(src_pkg_dir.relative_to(src)),
                            "from": str(src.relative_to(self._path)),
                        }
                    ]
                elif src_py_file.is_file():
                    packages = [
                        {
                            "include": str(src_py_file.relative_to(src)),
                            "from": str(src.relative_to(self._path)),
                        }
                    ]
                else:
                    raise ValueError("No file/folder found for package {}".format(name))

        for package in packages:
            self._includes.append(
                PackageInclude(self._path, package["include"], package.get("from"))
            )

        for include in includes:
            self._includes.append(Include(self._path, include))
Example #4
0
    def __init__(self, name, directory='.'):
        self._name = module_name(name)

        # It must exist either as a .py file or a directory, but not both
        pkg_dir = Path(directory, self._name)
        py_file = Path(directory, self._name + '.py')
        if pkg_dir.is_dir() and py_file.is_file():
            raise ValueError("Both {} and {} exist".format(pkg_dir, py_file))
        elif pkg_dir.is_dir():
            self._path = pkg_dir
            self._is_package = True
        elif py_file.is_file():
            self._path = py_file
            self._is_package = False
        else:
            raise ValueError("No file/folder found for package {}".format(name))
Example #5
0
    def find_excluded_files(self):  # type: () -> Set[str]
        # Checking VCS
        vcs = get_vcs(self._path)
        if not vcs:
            vcs_ignored_files = set()
        else:
            vcs_ignored_files = set(vcs.get_ignored_files())

        explicitely_excluded = set()
        for excluded_glob in self._package.exclude:
            excluded_path = Path(self._path, excluded_glob)

            try:
                is_dir = excluded_path.is_dir()
            except OSError:
                # On Windows, testing if a path with a glob is a directory will raise an OSError
                is_dir = False
            if is_dir:
                excluded_glob = Path(excluded_glob, "**/*")

            for excluded in glob(Path(self._path, excluded_glob).as_posix(),
                                 recursive=True):
                explicitely_excluded.add(
                    Path(excluded).relative_to(self._path).as_posix())

        ignored = vcs_ignored_files | explicitely_excluded
        result = set()
        for file in ignored:
            result.add(file)

        # The list of excluded files might be big and we will do a lot
        # containment check (x in excluded).
        # Returning a set make those tests much much faster.
        return result
Example #6
0
    def remove_venv(cls, path):  # type: (Union[Path,str]) -> None
        if isinstance(path, str):
            path = Path(path)
        assert path.is_dir()
        try:
            shutil.rmtree(str(path))
            return
        except OSError as e:
            # Continue only if e.errno == 16
            if e.errno != 16:  # ERRNO 16: Device or resource busy
                raise e

        # Delete all files and folders but the toplevel one. This is because sometimes
        # the venv folder is mounted by the OS, such as in a docker volume. In such
        # cases, an attempt to delete the folder itself will result in an `OSError`.
        # See https://github.com/python-poetry/poetry/pull/2064
        for file_path in path.iterdir():
            if file_path.is_file() or file_path.is_symlink():
                file_path.unlink()
            elif file_path.is_dir():
                shutil.rmtree(str(file_path))