Skip to content

Commit

Permalink
Finalized initial approach at packaging wheels w/ c sources into a pex
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Livingston committed Jan 30, 2018
1 parent 4e2b1b9 commit 74bfb8c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@

from hello_package import hello


if __name__ == '__main__':
hello.hello()
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

# Like Hello world, but built with Pants. This time, with both C++ and Py sources

# The BUILD file must be in the directory of the Python Distribution, and there must
# only be a single BUILD file in a given directory.

python_distribution(
name='superhello'
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
import super_greet

def hello():
print(super_greet.super_greet())
print(super_greet.super_greet())
29 changes: 2 additions & 27 deletions src/python/pants/backend/python/targets/python_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,16 @@ class PythonDistribution(PythonTarget):
def alias(cls):
return 'python_distribution'

default_sources_globs = '*.(c|cpp|h|py)'


def __init__(self,
source=None,
setup_file=None,
repositories=None,
package_dir=None,
platforms=(),
**kwargs):
payload = Payload()
payload.add_fields({
'setup_file': PrimitiveField(setup_file),
'repositories': PrimitiveField(maybe_list(repositories or [])),
'platforms': PrimitiveField(tuple(maybe_list(platforms or []))),
'package_dir': PrimitiveField(package_dir),
'platforms': PrimitiveField(tuple(maybe_list(platforms or [])))
})

sources = [] if source is None else [source]
super(PythonDistribution, self).__init__(sources=sources, payload=payload, **kwargs)

@property
def setup_file(self):
return self.payload.setup_file
super(PythonDistribution, self).__init__(sources=[], payload=payload, **kwargs)

@property
def platforms(self):
return self.payload.platforms

@property
def repositories(self):
return self.payload.repositories

def package_dir(self):
return self.payload.package_dir



27 changes: 17 additions & 10 deletions src/python/pants/backend/python/tasks/pex_build_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,30 @@ def build_python_distribution_from_target(target, workdir):
pydist_workdir = os.path.join(workdir, '.pydistworkdir')
safe_mkdir(pydist_workdir)
pex_name = "%s.pex" % target.name

args = ['--disable-cache', '/Users/clivingston/workspace/pants/examples/src/python/example/python_distribution/hello/superhello', '-o', pex_name]
path_to_target = os.path.dirname(target.address.rel_path)

args = ['--disable-cache', path_to_target, '-o', os.path.join(pydist_workdir, pex_name)]
try:
pex_main.main(args=args)
except SystemExit as e:
raise TaskError(e)
except Exception as e:
raise TaskError(e)

# unzip into a chroot within the python dist workdir
zip_ref = zipfile.ZipFile(os.path.join(pydist_workdir, pex_name), 'r')
safe_mkdir(os.path.join(pydist_workdir, pex_name + '_chroot'))
zip_ref.extractall(os.path.join(pydist_workdir, pex_name + '_chroot'))
fingerprint = target.payload.fingerprint()
safe_mkdir(os.path.join(pydist_workdir, fingerprint))
zip_ref.extractall(os.path.join(pydist_workdir, fingerprint))
zip_ref.close()

contents = os.listdir(os.path.join(pydist_workdir, pex_name + '_chroot', '.deps'))
whl_file = [wheel for wheel in contents if target.name in wheel]
whl_location = os.path.join(pydist_workdir, pex_name + '_chroot', '.deps', whl_file[0])

return whl_location
# read the contents from .deps of the chroot to obtain the whl location
chroot_deps_contents = os.listdir(os.path.join(pydist_workdir, fingerprint, '.deps'))
if chroot_deps_contents:
whl_dist = chroot_deps_contents[0] # TODO: find better way to grab .whl from chroot
whl_location = os.path.join(pydist_workdir, fingerprint, '.deps', whl_dist)
return whl_location
return None


def dump_python_distibutions(builder, dist_targets, workdir, log):
Expand All @@ -165,7 +170,9 @@ def dump_python_distibutions(builder, dist_targets, workdir, log):
# build whl for target using pex wheel installer
locations = set()
for tgt in dist_targets_set:
locations.add(build_python_distribution_from_target(tgt, workdir))
whl_location = build_python_distribution_from_target(tgt, workdir)
if whl_location:
locations.add(whl_location)

# dump prebuilt wheels into pex builder
for location in locations:
Expand Down

0 comments on commit 74bfb8c

Please sign in to comment.