Skip to content

Commit

Permalink
inspection: use pep517.meta.build from shell
Browse files Browse the repository at this point in the history
With change we execute pep517 metadata build from within a new venv
retaining old `python setup.py egg_info` behaviour. We fallback to the
old behaviour if pep517 metadata build fails.
  • Loading branch information
abn committed Jul 10, 2020
1 parent 35c0332 commit d82962b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 52 deletions.
93 changes: 50 additions & 43 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 51 additions & 8 deletions poetry/inspection/info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import glob
import logging
import os
import subprocess
import tarfile
import zipfile
Expand All @@ -12,8 +13,6 @@

import pkginfo

from pep517.build import compat_system as pep517_compat_system
from pep517.meta import build as pep517_metadata_build
from poetry.core.factory import Factory
from poetry.core.packages import Package
from poetry.core.packages import ProjectPackage
Expand All @@ -23,12 +22,24 @@
from poetry.core.utils.helpers import parse_requires
from poetry.core.utils.helpers import temporary_directory
from poetry.core.version.markers import InvalidMarker
from poetry.utils.env import EnvCommandError
from poetry.utils.env import EnvManager
from poetry.utils.env import VirtualEnv
from poetry.utils.setup_reader import SetupReader
from poetry.utils.toml_file import TomlFile


logger = logging.getLogger(__name__)

PEP517_MEATA_BUILD = """\
import pep517.build
import pep517.meta
path='{source}'
system=pep517.build.compat_system(path)
pep517.meta.build(source_dir=path, dest='{dest}', system=system)
"""


class PackageInfoError(ValueError):
def __init__(self, path): # type: (Union[Path, str]) -> None
Expand Down Expand Up @@ -255,6 +266,10 @@ def _from_sdist_file(cls, path): # type: (Path) -> PackageInfo

return info.update(new_info)

@staticmethod
def has_setup_files(path): # type: (Path) -> bool
return any((path / f).exists() for f in SetupReader.FILES)

@classmethod
def from_setup_files(cls, path): # type: (Path) -> PackageInfo
"""
Expand All @@ -264,7 +279,7 @@ def from_setup_files(cls, path): # type: (Path) -> PackageInfo
:param path: Path to `setup.py` file
"""
if not any((path / f).exists() for f in SetupReader.FILES):
if not cls.has_setup_files(path):
raise PackageInfoError(path)

try:
Expand Down Expand Up @@ -416,13 +431,39 @@ def _pep517_metadata(cls, path): # type (Path) -> PackageInfo
except PackageInfoError:
pass

cwd = Path.cwd()
os.chdir(path.as_posix())

try:
with temporary_directory() as tmp_dir:
pep517_metadata_build(
source_dir=path.as_posix(),
dest=tmp_dir,
system=pep517_compat_system(source_dir=path.as_posix()),
)
EnvManager.build_venv(tmp_dir)
venv = VirtualEnv(Path(tmp_dir), Path(tmp_dir))

try:
venv.run_pip(
"install",
"--disable-pip-version-check",
"--no-deps",
"pep517===0.8.2",
"toml==0.10.1",
)
venv.run(
"python",
"-c",
PEP517_MEATA_BUILD.format(source=path.as_posix(), dest=tmp_dir),
)
except EnvCommandError:
# something went wrong while attempting pep517 metadata build
# fallback to egg_info if setup.py available
setup_py = path / "setup.py"
if not setup_py.exists():
raise PackageInfoError(path)

try:
venv.run("python", "setup.py", "egg_info")
except EnvCommandError:
raise PackageInfoError(path)

return cls.from_metadata(Path(tmp_dir))
except subprocess.CalledProcessError:
cls._log("PEP 517 metadata build failed for {}".format(path), "debug")
Expand All @@ -431,6 +472,8 @@ def _pep517_metadata(cls, path): # type (Path) -> PackageInfo
"Falling back to parsed setup.py file for {}".format(path), "debug"
)
return info
finally:
os.chdir(cwd.as_posix())

# if we reach here, everything has failed and all hope is lost
raise PackageInfoError(path)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ keyring = [
# Use subprocess32 for Python 2.7
subprocess32 = { version = "^3.5", python = "~2.7" }
importlib-metadata = {version = "^1.6.0", python = "<3.8"}
pep517 = "^0.8.2"

[tool.poetry.dev-dependencies]
pytest = [
Expand Down

0 comments on commit d82962b

Please sign in to comment.