Skip to content

Commit

Permalink
we can produce dehydrated pex files with the --dehydrated flag!
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Nov 7, 2019
1 parent 7d59347 commit 6390b49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
19 changes: 17 additions & 2 deletions pex/bin/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ def configure_clp_pex_resolution(parser, builder):
callback_args=(builder,),
help='Whether to transitively resolve requirements. Default: True')

group.add_option(
'--dehydrated',
dest='dehydrated',
default=False,
action='callback',
callback=parse_bool,
help='[EXPERIMENTAL] Whether to avoid adding requirements to the generated pex file and '
'instead add an entry to PEX-INFO containing the transitive dependencies of all '
'requirements.'
)

# Set the pex tool to fetch from PyPI by default if nothing is specified.
parser.set_default('repos', [PyPIFetcher()])
parser.add_option_group(group)
Expand Down Expand Up @@ -647,8 +658,12 @@ def walk_and_do(fn, src_dir):
for resolved_dist in resolveds:
log(' %s -> %s' % (resolved_dist.requirement, resolved_dist.distribution),
V=options.verbosity)
pex_builder.add_distribution(resolved_dist.distribution)
pex_builder.add_requirement(resolved_dist.requirement)

if options.dehydrated:
pex_info.add_dehydrated_requirement(resolved_dist.requirement)
else:
pex_builder.add_distribution(resolved_dist.distribution)
pex_builder.add_requirement(resolved_dist.requirement)
except Unsatisfiable as e:
die(e)

Expand Down
15 changes: 15 additions & 0 deletions pex/pex_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PexInfo(object):
distributions: {dist_name: str} # map from distribution name (i.e. path in
# the internal cache) to its cache key (sha1)
requirements: list # list of requirements for this environment
dehydrated_requirements: list # list of requirements to resolve at bootstrap time.
# Environment options
pex_root: string # root of all pex-related files eg: ~/.pex
Expand Down Expand Up @@ -67,6 +68,7 @@ def make_build_properties(cls, interpreter=None):
def default(cls, interpreter=None):
pex_info = {
'requirements': [],
'dehydrated_requirements': [],
'distributions': {},
'build_properties': cls.make_build_properties(interpreter),
}
Expand Down Expand Up @@ -132,6 +134,10 @@ def __init__(self, info=None):
if not isinstance(requirements, (list, tuple)):
raise ValueError('Expected requirements to be a list, got %s' % type(requirements))
self._requirements = OrderedSet(self._parse_requirement_tuple(req) for req in requirements)
dehydrated_requirements = self._pex_info.get('dehydrated_requirements', [])
if not isinstance(dehydrated_requirements, (list, tuple)):
raise ValueError('Expected dehydrated_requirements to be a list, got %s' % type(dehydrated_requirements))
self._dehydrated_requirements = OrderedSet(self._parse_requirement_tuple(req) for req in dehydrated_requirements)

def _get_safe(self, key):
if key not in self._pex_info:
Expand Down Expand Up @@ -261,10 +267,17 @@ def script(self, value):
def add_requirement(self, requirement):
self._requirements.add(str(requirement))

def add_dehydrated_requirement(self, requirement):
self._dehydrated_requirements.add(str(requirement))

@property
def requirements(self):
return self._requirements

@property
def dehydrated_requirements(self):
return self._dehydrated_requirements

def add_distribution(self, location, sha):
self._distributions[location] = sha

Expand Down Expand Up @@ -307,10 +320,12 @@ def update(self, other):
self._distributions.update(other.distributions)
self._interpreter_constraints.update(other.interpreter_constraints)
self._requirements.update(other.requirements)
self._dehydrated_requirements.update(other.dehydrated_requirements)

def dump(self, **kwargs):
pex_info_copy = self._pex_info.copy()
pex_info_copy['requirements'] = sorted(self._requirements)
pex_info_copy['dehydrated_requirements'] = sorted(self._dehydrated_requirements)
pex_info_copy['interpreter_constraints'] = sorted(self._interpreter_constraints)
pex_info_copy['distributions'] = self._distributions.copy()
return json.dumps(pex_info_copy, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion pex/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

__version__ = '1.6.12'
__version__ = '1.6.12+dehydration'

0 comments on commit 6390b49

Please sign in to comment.