def handle(self) -> int: from pathlib import Path from poetry.core.vcs.git import GitConfig from poetry.layouts import layout from poetry.utils.env import SystemEnv if self.option("src"): layout_cls = layout("src") else: layout_cls = layout("standard") path = Path(self.argument("path")) if not path.is_absolute(): # we do not use resolve here due to compatibility issues # for path.resolve(strict=False) path = Path.cwd().joinpath(path) name = self.option("name") if not name: name = path.name if path.exists() and list(path.glob("*")): # Directory is not empty. Aborting. raise RuntimeError( f"Destination <fg=yellow>{path}</> exists and is not empty") readme_format = self.option("readme") or "md" config = GitConfig() author = None if config.get("user.name"): author = config["user.name"] author_email = config.get("user.email") if author_email: author += f" <{author_email}>" current_env = SystemEnv(Path(sys.executable)) default_python = "^" + ".".join( str(v) for v in current_env.version_info[:2]) layout_ = layout_cls( name, "0.1.0", author=author, readme_format=readme_format, python=default_python, ) layout_.create(path) path = path.resolve() with suppress(ValueError): path = path.relative_to(Path.cwd()) self.line(f"Created package <info>{layout_._package_name}</> in" f" <fg=blue>{path.as_posix()}</>") return 0
def build_sdist(sdist_directory, config_settings=None): """Builds an sdist, places it in sdist_directory""" poetry = Factory().create_poetry(Path(".")) path = SdistBuilder(poetry, SystemEnv(Path(sys.prefix)), NullIO()).build(Path(sdist_directory)) return unicode(path.name)
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): """Builds a wheel, places it in wheel_directory""" poetry = Factory().create_poetry(Path(".")) return unicode( WheelBuilder.make_in(poetry, SystemEnv(Path(sys.prefix)), NullIO(), Path(wheel_directory)))
def test_system_env_has_correct_paths(): env = SystemEnv(Path(sys.prefix)) paths = env.paths assert paths.get("purelib") is not None assert paths.get("platlib") is not None assert paths.get("scripts") is not None assert env.site_packages.path == Path(paths["purelib"])
def handle(self): from pathlib import Path from poetry.core.semver import parse_constraint from poetry.core.vcs.git import GitConfig from poetry.layouts import layout from poetry.utils.env import SystemEnv if self.option("src"): layout_ = layout("src") else: layout_ = layout("standard") path = Path.cwd() / Path(self.argument("path")) name = self.option("name") if not name: name = path.name if path.exists(): if list(path.glob("*")): # Directory is not empty. Aborting. raise RuntimeError("Destination <fg=yellow>{}</> " "exists and is not empty".format(path)) readme_format = "rst" config = GitConfig() author = None if config.get("user.name"): author = config["user.name"] author_email = config.get("user.email") if author_email: author += " <{}>".format(author_email) current_env = SystemEnv(Path(sys.executable)) default_python = "^{}".format(".".join( str(v) for v in current_env.version_info[:2])) dev_dependencies = {} python_constraint = parse_constraint(default_python) if parse_constraint("<3.5").allows_any(python_constraint): dev_dependencies["pytest"] = "^4.6" if parse_constraint(">=3.5").allows_all(python_constraint): dev_dependencies["pytest"] = "^5.2" layout_ = layout_( name, "0.1.0", author=author, readme_format=readme_format, python=default_python, dev_dependencies=dev_dependencies, ) layout_.create(path) self.line("Created package <info>{}</> in <fg=blue>{}</>".format( module_name(name), path.relative_to(Path.cwd())))
def handle(self): from poetry.layouts import layout from poetry.utils._compat import Path from poetry.utils.env import SystemEnv from poetry.vcs.git import GitConfig if self.option("src"): layout_ = layout("src") else: layout_ = layout("standard") path = Path.cwd() / Path(self.argument("path")) name = self.option("name") if not name: name = path.name if path.exists(): if list(path.glob("*")): # Directory is not empty. Aborting. raise RuntimeError( "Destination <fg=yellow>{}</> " "exists and is not empty".format(path) ) readme_format = "rst" config = GitConfig() author = None if config.get("user.name"): author = config["user.name"] author_email = config.get("user.email") if author_email: author += " <{}>".format(author_email) current_env = SystemEnv(Path(sys.executable)) default_python = "^{}".format( ".".join(str(v) for v in current_env.version_info[:2]) ) layout_ = layout_( name, "0.1.0", author=author, readme_format=readme_format, python=default_python, ) layout_.create(path) self.line( "Created package <info>{}</> in <fg=blue>{}</>".format( name, path.relative_to(Path.cwd()) ) )
def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None): poetry = Factory().create_poetry(Path(".")) builder = WheelBuilder(poetry, SystemEnv(Path(sys.prefix)), NullIO()) dist_info = Path(metadata_directory, builder.dist_info) dist_info.mkdir(parents=True, exist_ok=True) if "scripts" in poetry.local_config or "plugins" in poetry.local_config: with (dist_info / "entry_points.txt").open("w", encoding="utf-8") as f: builder._write_entry_points(f) with (dist_info / "WHEEL").open("w", encoding="utf-8") as f: builder._write_wheel_file(f) with (dist_info / "METADATA").open("w", encoding="utf-8") as f: builder._write_metadata_file(f) return dist_info.name
def _zip(zip_file, build_dir, *more_root_files): "Zip the files up." version_info = SystemEnv(Path(sys.prefix)).get_version_info() python_minor = Version(*version_info[:2]) with ZipFile(zip_file, mode="w", compression=ZIP_DEFLATED, allowZip64=False) as zf: for root_file in more_root_files: zf.write(root_file, root_file.name) for lib in ["lib", "lib64"]: target_path = build_dir / lib / f"python{python_minor}" / "site-packages" if not target_path.is_dir(): continue for root, dirs, files in os.walk(target_path): root_path = Path(root) dirs.sort() tail_path = root_path.relative_to(target_path) if tail_path.parts: zf.write(root_path, tail_path) for filename in files: zf.write(root_path / filename, tail_path / filename)
def current_env() -> SystemEnv: return SystemEnv(Path(sys.executable))
def handle(self) -> int: from pathlib import Path from poetry.core.vcs.git import GitConfig from poetry.layouts import layout from poetry.utils.env import SystemEnv pyproject = PyProjectTOML(Path.cwd() / "pyproject.toml") if pyproject.file.exists(): if pyproject.is_poetry_project(): self.line( "<error>A pyproject.toml file with a poetry section already exists.</error>" ) return 1 if pyproject.data.get("build-system"): self.line( "<error>A pyproject.toml file with a defined build-system already exists.</error>" ) return 1 vcs_config = GitConfig() self.line("") self.line( "This command will guide you through creating your <info>pyproject.toml</> config." ) self.line("") name = self.option("name") if not name: name = Path.cwd().name.lower() question = self.create_question( "Package name [<comment>{}</comment>]: ".format(name), default=name ) name = self.ask(question) version = "0.1.0" question = self.create_question( "Version [<comment>{}</comment>]: ".format(version), default=version ) version = self.ask(question) description = self.option("description") or "" question = self.create_question( "Description [<comment>{}</comment>]: ".format(description), default=description, ) description = self.ask(question) author = self.option("author") if not author and vcs_config and vcs_config.get("user.name"): author = vcs_config["user.name"] author_email = vcs_config.get("user.email") if author_email: author += " <{}>".format(author_email) question = self.create_question( "Author [<comment>{}</comment>, n to skip]: ".format(author), default=author ) question.set_validator(lambda v: self._validate_author(v, author)) author = self.ask(question) if not author: authors = [] else: authors = [author] license = self.option("license") or "" question = self.create_question( "License [<comment>{}</comment>]: ".format(license), default=license ) question.set_validator(self._validate_license) license = self.ask(question) python = self.option("python") if not python: current_env = SystemEnv(Path(sys.executable)) default_python = "^{}".format( ".".join(str(v) for v in current_env.version_info[:2]) ) question = self.create_question( "Compatible Python versions [<comment>{}</comment>]: ".format( default_python ), default=default_python, ) python = self.ask(question) self.line("") requirements = {} if self.option("dependency"): requirements = self._format_requirements( self._determine_requirements(self.option("dependency")) ) question = "Would you like to define your main dependencies interactively?" help_message = ( "You can specify a package in the following forms:\n" " - A single name (<b>requests</b>)\n" " - A name and a constraint (<b>requests@^2.23.0</b>)\n" " - A git url (<b>git+https://github.com/python-poetry/poetry.git</b>)\n" " - A git url with a revision (<b>git+https://github.com/python-poetry/poetry.git#develop</b>)\n" " - A file path (<b>../my-package/my-package.whl</b>)\n" " - A directory (<b>../my-package/</b>)\n" " - A url (<b>https://example.com/packages/my-package-0.1.0.tar.gz</b>)\n" ) help_displayed = False if self.confirm(question, True): self.line(help_message) help_displayed = True requirements.update( self._format_requirements(self._determine_requirements([])) ) self.line("") dev_requirements = {} if self.option("dev-dependency"): dev_requirements = self._format_requirements( self._determine_requirements(self.option("dev-dependency")) ) question = ( "Would you like to define your development dependencies interactively?" ) if self.confirm(question, True): if not help_displayed: self.line(help_message) dev_requirements.update( self._format_requirements(self._determine_requirements([])) ) self.line("") layout_ = layout("standard")( name, version, description=description, author=authors[0] if authors else None, license=license, python=python, dependencies=requirements, dev_dependencies=dev_requirements, ) content = layout_.generate_poetry_content(original=pyproject) if self.io.is_interactive(): self.line("<info>Generated file</info>") self.line("") self.line(content) self.line("") if not self.confirm("Do you confirm generation?", True): self.line("<error>Command aborted</error>") return 1 with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f: f.write(content)
def test_system_env_usersite(mocker, enabled): mocker.patch("site.check_enableusersite", return_value=enabled) env = SystemEnv(Path(sys.prefix)) assert (enabled and env.usersite is not None) or (not enabled and env.usersite is None)