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)
def load_projectfile(cls, path, create=True, data=None): """Given a path, load or create the necessary lockfile. :param str path: Path to the project root or lockfile :param bool create: Whether to create the lockfile if not found, defaults to True :raises OSError: Thrown if the project root directory doesn't exist :raises FileNotFoundError: Thrown if the lockfile doesn't exist and ``create=False`` :return: A project file instance for the supplied project :rtype: :class:`~requirementslib.models.project.ProjectFile` """ if not path: path = os.curdir path = Path(path).absolute() project_path = path if path.is_dir() else path.parent lockfile_path = path if path.is_file() else project_path / "Pipfile.lock" if not project_path.exists(): raise OSError("Project does not exist: %s" % project_path.as_posix()) elif not lockfile_path.exists() and not create: raise FileNotFoundError("Lockfile does not exist: %s" % lockfile_path.as_posix()) projectfile = cls.read_projectfile(lockfile_path.as_posix()) if not lockfile_path.exists(): if not data: path_str = lockfile_path.as_posix() if path_str[-5:] == ".lock": pipfile = Path(path_str[:-5]) else: pipfile = project_path.joinpath("Pipfile") lf = cls.lockfile_from_pipfile(pipfile) else: lf = plette.lockfiles.Lockfile(data) projectfile.model = lf return projectfile
def filter_pythons(path): """Return all valid pythons in a given path""" if not isinstance(path, Path): path = Path(str(path)) if not path.is_dir(): return path if path_is_python(path) else None return filter(lambda x: path_is_python(x), path.iterdir())
def ensure_setup_py(base_dir): if not base_dir: base_dir = create_tracked_tempdir(prefix="requirementslib-setup") base_dir = Path(base_dir) 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()
def load(cls, path): if not path: path = os.curdir path = Path(path).absolute() if path.is_dir(): path = path / "Pipfile.lock" elif path.name == "Pipfile": path = path.parent / "Pipfile.lock" if not path.exists(): raise OSError("Path does not exist: %s" % path) return cls.create(path.parent, lockfile_name=path.name)
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
def is_installable_file(path): """Determine if a path can potentially be installed""" from packaging import specifiers if hasattr(path, "keys") and any( key for key in path.keys() if key in ["file", "path"]): path = urlparse(path["file"]).path if "file" in path else path["path"] if not isinstance(path, six.string_types) or path == "*": return False # If the string starts with a valid specifier operator, test if it is a valid # specifier set before making a path object (to avoid breaking windows) if any(path.startswith(spec) for spec in "!=<>~"): try: specifiers.SpecifierSet(path) # If this is not a valid specifier, just move on and try it as a path except specifiers.InvalidSpecifier: pass else: return False parsed = urlparse(path) if parsed.scheme == "file": path = parsed.path if not os.path.exists(os.path.abspath(path)): return False lookup_path = Path(path) absolute_path = "{0}".format(lookup_path.absolute()) if lookup_path.is_dir() and is_installable_dir(absolute_path): return True elif lookup_path.is_file() and pip_shims.shims.is_archive_file( absolute_path): return True return False