Skip to content

Commit

Permalink
Fixup old style --preserve-pip-download-log handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Oct 24, 2024
1 parent 5c41a32 commit 7872b3d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
6 changes: 4 additions & 2 deletions pex/resolve/abbreviated_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def parse_tags(output):
)
job = SpawnedJob.stdout(
job=pip.spawn_debug(
platform_spec=platform_spec, manylinux=manylinux, log=pip_configuration.log
platform_spec=platform_spec,
manylinux=manylinux,
log=pip_configuration.log.path if pip_configuration.log else None,
),
result_func=parse_tags,
)
Expand Down Expand Up @@ -179,7 +181,7 @@ def create(
# call to `pip -v debug ...` in _calculate_tags above. We do the same for the cached case
# since this can be very useful information when investigating why Pip did not select a
# particular wheel for an abbreviated --platform.
with safe_open(pip_configuration.log, "a") as fp:
with safe_open(pip_configuration.log.path, "a") as fp:
print(
"Read {count} compatible tags for abbreviated --platform {platform} from:".format(
count=len(compatibility_tags), platform=platform
Expand Down
4 changes: 2 additions & 2 deletions pex/resolve/lockfile/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pex.resolve.lockfile.model import Lockfile
from pex.resolve.requirement_configuration import RequirementConfiguration
from pex.resolve.resolved_requirement import ArtifactURL, Fingerprint
from pex.resolve.resolver_configuration import PipConfiguration, ReposConfiguration
from pex.resolve.resolver_configuration import PipConfiguration, PipLog, ReposConfiguration
from pex.result import Error, ResultError, catch, try_
from pex.sorted_tuple import SortedTuple
from pex.targets import Target, Targets
Expand Down Expand Up @@ -619,7 +619,7 @@ def create(
max_jobs, # type: int
use_pip_config, # type: bool
dependency_configuration, # type: DependencyConfiguration
pip_log, # type: Optional[str]
pip_log, # type: Optional[PipLog]
):
# type: (...) -> LockUpdater

Expand Down
8 changes: 7 additions & 1 deletion pex/resolve/resolver_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ def allow_wheel(self, project_name):
return self.allow_wheels and project_name not in self.only_builds


@attr.s(frozen=True)
class PipLog(object):
path = attr.ib() # type: str
user_specified = attr.ib() # type: bool


@attr.s(frozen=True)
class PipConfiguration(object):
repos_configuration = attr.ib(default=ReposConfiguration()) # type: ReposConfiguration
Expand All @@ -188,7 +194,7 @@ class PipConfiguration(object):
allow_prereleases = attr.ib(default=False) # type: bool
transitive = attr.ib(default=True) # type: bool
max_jobs = attr.ib(default=DEFAULT_MAX_JOBS) # type: int
log = attr.ib(default=None) # type: Optional[str]
log = attr.ib(default=None) # type: Optional[PipLog]
version = attr.ib(default=None) # type: Optional[PipVersionValue]
resolver_version = attr.ib(default=None) # type: Optional[ResolverVersion.Value]
allow_version_fallback = attr.ib(default=True) # type: bool
Expand Down
15 changes: 11 additions & 4 deletions pex/resolve/resolver_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
LockRepositoryConfiguration,
PexRepositoryConfiguration,
PipConfiguration,
PipLog,
PreResolvedConfiguration,
ReposConfiguration,
ResolverVersion,
Expand Down Expand Up @@ -326,6 +327,7 @@ def __init__(self, *args, **kwargs):
super(HandlePipDownloadLogAction, self).__init__(*args, **kwargs)

def __call__(self, parser, namespace, value, option_str=None):
pip_log = None # type: Optional[PipLog]
if option_str.startswith("--no"):
if value:
raise ArgumentError(
Expand All @@ -336,8 +338,13 @@ def __call__(self, parser, namespace, value, option_str=None):
),
)
elif not value:
value = os.path.join(tempfile.mkdtemp(prefix="pex-pip-log."), "pip.log")
setattr(namespace, self.dest, value)
pip_log = PipLog(
path=os.path.join(tempfile.mkdtemp(prefix="pex-pip-log."), "pip.log"),
user_specified=False,
)
else:
pip_log = PipLog(path=value, user_specified=True)
setattr(namespace, self.dest, pip_log)


def register_pip_log(parser):
Expand All @@ -354,8 +361,8 @@ def register_pip_log(parser):


def get_pip_log(options):
# type: (Namespace) -> Optional[str]
return cast("Optional[str]", options.pip_log)
# type: (Namespace) -> Optional[PipLog]
return cast("Optional[PipLog]", options.pip_log)


def register_use_pip_config(parser):
Expand Down
33 changes: 21 additions & 12 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from pex.requirements import LocalProjectRequirement
from pex.resolve.downloads import get_downloads_dir
from pex.resolve.requirement_configuration import RequirementConfiguration
from pex.resolve.resolver_configuration import BuildConfiguration, ResolverVersion
from pex.resolve.resolver_configuration import BuildConfiguration, PipLog, ResolverVersion
from pex.resolve.resolvers import (
ResolvedDistribution,
Resolver,
Expand Down Expand Up @@ -81,20 +81,27 @@ class PipLogManager(object):
@classmethod
def create(
cls,
log, # type: Optional[str]
log, # type: Optional[PipLog]
targets, # type: Sequence[Target]
):
# type: (...) -> PipLogManager
log_by_target = {} # type: Dict[Target, str]
if log:
log_dir = safe_mkdtemp(prefix="pex-pip-log.")
log_by_target.update(
(target, os.path.join(log_dir, "pip.{target}.log".format(target=target.id)))
(
target,
(
os.path.join(log_dir, "pip.{target}.log".format(target=target.id))
if log.user_specified
else log.path
),
)
for target in targets
)
return cls(log=log, log_by_target=log_by_target)

log = attr.ib() # type: Optional[str]
log = attr.ib() # type: Optional[PipLog]
_log_by_target = attr.ib() # type: Mapping[Target, str]

@staticmethod
Expand All @@ -112,12 +119,14 @@ def _target_id(target):
def finalize_log(self):
# type: () -> None
target_count = len(self._log_by_target)
if target_count == 0:
if target_count == 0 or not self.log:
return

if self.log and target_count == 1:
log = next(iter(self._log_by_target.values()))
shutil.move(log, self.log)
if target_count == 1:
if self.log.user_specified:
# Move the log to the path the user asked for.
log = next(iter(self._log_by_target.values()))
shutil.move(log, self.log.path)
return

with safe_open(self.log, "a") as out_fp:
Expand Down Expand Up @@ -153,7 +162,7 @@ class DownloadRequest(object):
package_index_configuration = attr.ib(default=None) # type: Optional[PackageIndexConfiguration]
build_configuration = attr.ib(default=BuildConfiguration()) # type: BuildConfiguration
observer = attr.ib(default=None) # type: Optional[ResolveObserver]
pip_log = attr.ib(default=None) # type: Optional[str]
pip_log = attr.ib(default=None) # type: Optional[PipLog]
pip_version = attr.ib(default=None) # type: Optional[PipVersionValue]
resolver = attr.ib(default=None) # type: Optional[Resolver]
dependency_configuration = attr.ib(
Expand Down Expand Up @@ -1050,7 +1059,7 @@ def resolve(
max_parallel_jobs=None, # type: Optional[int]
ignore_errors=False, # type: bool
verify_wheels=True, # type: bool
pip_log=None, # type: Optional[str]
pip_log=None, # type: Optional[PipLog]
pip_version=None, # type: Optional[PipVersionValue]
resolver=None, # type: Optional[Resolver]
use_pip_config=False, # type: bool
Expand Down Expand Up @@ -1224,7 +1233,7 @@ def _download_internal(
dest=None, # type: Optional[str]
max_parallel_jobs=None, # type: Optional[int]
observer=None, # type: Optional[ResolveObserver]
pip_log=None, # type: Optional[str]
pip_log=None, # type: Optional[PipLog]
pip_version=None, # type: Optional[PipVersionValue]
resolver=None, # type: Optional[Resolver]
dependency_configuration=DependencyConfiguration(), # type: DependencyConfiguration
Expand Down Expand Up @@ -1303,7 +1312,7 @@ def download(
dest=None, # type: Optional[str]
max_parallel_jobs=None, # type: Optional[int]
observer=None, # type: Optional[ResolveObserver]
pip_log=None, # type: Optional[str]
pip_log=None, # type: Optional[PipLog]
pip_version=None, # type: Optional[PipVersionValue]
resolver=None, # type: Optional[Resolver]
use_pip_config=False, # type: bool
Expand Down

0 comments on commit 7872b3d

Please sign in to comment.