Exemple #1
0
def get_pyproject(path):
    from vistir.compat import Path
    if not path:
        return
    if not isinstance(path, Path):
        path = Path(path)
    if not path.is_dir():
        path = path.parent
    pp_toml = path.joinpath("pyproject.toml")
    setup_py = path.joinpath("setup.py")
    if not pp_toml.exists():
        if setup_py.exists():
            return None
    else:
        pyproject_data = {}
        with io.open(pp_toml.as_posix(), encoding="utf-8") as fh:
            pyproject_data = tomlkit.loads(fh.read())
        build_system = pyproject_data.get("build-system", None)
        if build_system is None:
            if setup_py.exists():
                requires = ["setuptools", "wheel"]
                backend = "setuptools.build_meta"
            else:
                requires = ["setuptools>=38.2.5", "wheel"]
                backend = "setuptools.build_meta"
            build_system = {"requires": requires, "build-backend": backend}
            pyproject_data["build_system"] = build_system
        else:
            requires = build_system.get("requires")
            backend = build_system.get("build-backend")
        return (requires, backend)
Exemple #2
0
    def load_projectfile(cls, path, create=False):
        # type: (Text, bool) -> ProjectFile
        """
        Given a path, load or create the necessary pipfile.

        :param Text path: Path to the project root or pipfile
        :param bool create: Whether to create the pipfile if not found, defaults to True
        :raises OSError: Thrown if the project root directory doesn't exist
        :raises FileNotFoundError: Thrown if the pipfile doesn't exist and ``create=False``
        :return: A project file instance for the supplied project
        :rtype: :class:`~requirementslib.models.project.ProjectFile`
        """
        if not path:
            raise RuntimeError(
                "Must pass a path to classmethod 'Pipfile.load'")
        if not isinstance(path, Path):
            path = Path(path).absolute()
        pipfile_path = path if path.is_file() else path.joinpath("Pipfile")
        project_path = pipfile_path.parent
        if not project_path.exists():
            raise FileNotFoundError("%s is not a valid project path!" % path)
        elif not pipfile_path.exists() or not pipfile_path.is_file():
            if not create:
                raise RequirementError("%s is not a valid Pipfile" %
                                       pipfile_path)
        return cls.read_projectfile(pipfile_path.as_posix())
Exemple #3
0
def get_pyproject(path):
    # type: (Union[STRING_TYPE, Path]) -> Optional[Tuple[List[STRING_TYPE], STRING_TYPE]]
    """
    Given a base path, look for the corresponding ``pyproject.toml`` file and return its
    build_requires and build_backend.

    :param AnyStr path: The root path of the project, should be a directory (will be truncated)
    :return: A 2 tuple of build requirements and the build backend
    :rtype: Optional[Tuple[List[AnyStr], AnyStr]]
    """

    if not path:
        return
    from vistir.compat import Path

    if not isinstance(path, Path):
        path = Path(path)
    if not path.is_dir():
        path = path.parent
    pp_toml = path.joinpath("pyproject.toml")
    setup_py = path.joinpath("setup.py")
    if not pp_toml.exists():
        if not setup_py.exists():
            return None
        requires = ["setuptools>=40.8", "wheel"]
        backend = get_default_pyproject_backend()
    else:
        pyproject_data = {}
        with io.open(pp_toml.as_posix(), encoding="utf-8") as fh:
            pyproject_data = tomlkit.loads(fh.read())
        build_system = pyproject_data.get("build-system", None)
        if build_system is None:
            if setup_py.exists():
                requires = ["setuptools>=40.8", "wheel"]
                backend = get_default_pyproject_backend()
            else:
                requires = ["setuptools>=40.8", "wheel"]
                backend = get_default_pyproject_backend()
            build_system = {"requires": requires, "build-backend": backend}
            pyproject_data["build_system"] = build_system
        else:
            requires = build_system.get("requires",
                                        ["setuptools>=40.8", "wheel"])
            backend = build_system.get("build-backend",
                                       get_default_pyproject_backend())
    return requires, backend
Exemple #4
0
    def create(cls, base_dir, subdirectory=None, ireq=None, kwargs=None):
        if not base_dir or base_dir is None:
            return

        creation_kwargs = {"extra_kwargs": kwargs}
        if not isinstance(base_dir, Path):
            base_dir = Path(base_dir)
        creation_kwargs["base_dir"] = base_dir.as_posix()
        pyproject = base_dir.joinpath("pyproject.toml")

        if subdirectory is not None:
            base_dir = base_dir.joinpath(subdirectory)
        setup_py = base_dir.joinpath("setup.py")
        setup_cfg = base_dir.joinpath("setup.cfg")
        creation_kwargs["pyproject"] = pyproject
        creation_kwargs["setup_py"] = setup_py
        creation_kwargs["setup_cfg"] = setup_cfg
        if ireq:
            creation_kwargs["ireq"] = ireq
        return cls(**creation_kwargs)
Exemple #5
0
def is_installable_dir(path):
    if _is_installable_dir(path):
        return True
    path = Path(path)
    pyproject = path.joinpath("pyproject.toml")
    if pyproject.exists():
        pyproject_toml = tomlkit.loads(pyproject.read_text())
        build_system = pyproject_toml.get("build-system", {}).get("build-backend", "")
        if build_system:
            return True
    return False
Exemple #6
0
def ensure_setup_py(base_dir):
    if not base_dir:
        base_dir = create_tracked_tempdir(prefix="requirementslib-setup")
    base_dir = Path(base_dir)
    setup_py = base_dir.joinpath("setup.py")
    is_new = False if setup_py.exists() else True
    if not setup_py.exists():
        setup_py.write_text(u"")
    try:
        yield
    finally:
        if is_new:
            setup_py.unlink()
Exemple #7
0
    def create(cls, base_dir, subdirectory=None, ireq=None, kwargs=None):
        # type: (AnyStr, Optional[AnyStr], Optional[InstallRequirement], Optional[Dict[AnyStr, AnyStr]]) -> Optional[SetupInfo]
        if not base_dir or base_dir is None:
            return

        creation_kwargs = {"extra_kwargs": kwargs}
        if not isinstance(base_dir, Path):
            base_dir = Path(base_dir)
        creation_kwargs["base_dir"] = base_dir.as_posix()
        pyproject = base_dir.joinpath("pyproject.toml")

        if subdirectory is not None:
            base_dir = base_dir.joinpath(subdirectory)
        setup_py = base_dir.joinpath("setup.py")
        setup_cfg = base_dir.joinpath("setup.cfg")
        creation_kwargs["pyproject"] = pyproject
        creation_kwargs["setup_py"] = setup_py
        creation_kwargs["setup_cfg"] = setup_cfg
        if ireq:
            creation_kwargs["ireq"] = ireq
        created = cls(**creation_kwargs)
        created.get_info()
        return created
Exemple #8
0
 def egg_base(self):
     # type: () -> S
     base = None  # type: Optional[STRING_TYPE]
     if self.setup_py.exists():
         base = self.setup_py.parent
     elif self.pyproject.exists():
         base = self.pyproject.parent
     elif self.setup_cfg.exists():
         base = self.setup_cfg.parent
     if base is None:
         base = Path(self.base_dir)
     if base is None:
         base = Path(self.extra_kwargs["src_dir"])
     egg_base = base.joinpath("reqlib-metadata")
     if not egg_base.exists():
         atexit.register(rmtree, egg_base.as_posix())
     egg_base.mkdir(parents=True, exist_ok=True)
     return egg_base.as_posix()
Exemple #9
0
def ensure_setup_py(base):
    # type: (STRING_TYPE) -> Generator[None, None, None]
    if not base:
        base = create_tracked_tempdir(prefix="requirementslib-setup")
    base_dir = Path(base)
    if base_dir.exists() and base_dir.name == "setup.py":
        base_dir = base_dir.parent
    elif not (base_dir.exists() and base_dir.is_dir()):
        base_dir = base_dir.parent
        if not (base_dir.exists() and base_dir.is_dir()):
            base_dir = base_dir.parent
    setup_py = base_dir.joinpath("setup.py")

    is_new = False if setup_py.exists() else True
    if not setup_py.exists():
        setup_py.write_text(u"")
    try:
        yield
    finally:
        if is_new:
            setup_py.unlink()