Skip to content

Commit

Permalink
cut off work for now
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Jan 31, 2018
1 parent 629ff46 commit 2591288
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
17 changes: 15 additions & 2 deletions src/python/pants/backend/python/targets/python_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@

from pants.backend.python.targets.python_target import PythonTarget
from pants.base.payload import Payload
from pants.source.payload_fields import SourcesField
from pants.source.wrapped_globs import FilesetWithSpec
from pants.util.memo import memoized_property


class PythonDistribution(PythonTarget):
"""A Python distribution target that accepts a user-defined setup.py."""

@memoized_property
def _cpp_sources_field(self):
cpp_sources_field = self.payload.get_field('cpp_sources')
if cpp_sources_field is not None:
return cpp_sources_field
return SourcesField(sources=FilesetWithSpec.empty(self.address.spec_path))

def cpp_sources_relative_to_target_base(self):
return self._cpp_sources_field.sources

default_sources_globs = '*.py'

@classmethod
Expand All @@ -22,7 +35,7 @@ def __init__(self,
address=None,
payload=None,
sources=None,
c_sources=None,
cpp_sources=None,
**kwargs):
"""
:param address: The Address that maps to this Target in the BuildGraph.
Expand All @@ -36,7 +49,7 @@ def __init__(self,
"""
payload = payload or Payload()
payload.add_fields({
'c_sources': self.create_sources_field(sources, address.spec_path, key_arg='c_sources'),
'cpp_sources': self.create_sources_field(cpp_sources, address.spec_path, key_arg='cpp_sources'),
})
super(PythonDistribution, self).__init__(
address=address, payload=payload, sources=sources, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import glob
import os
import re
import shutil

from pex.interpreter import PythonInterpreter
Expand All @@ -17,9 +18,21 @@
from pants.base.exceptions import TargetDefinitionException, TaskError
from pants.base.fingerprint_strategy import DefaultFingerprintStrategy
from pants.task.task import Task
from pants.util.contextutil import environment_as, temporary_dir
from pants.util.dirutil import safe_mkdir


PANTSSETUP_IMPORT_BOILERPLATE = """
# DO NOT EDIT THIS FILE -- AUTOGENERATED BY PANTS
# Target: {setup_target}
from distutils.core import Extension
def find_external_modules():
return [Extension('native', [{native_sources_joined}])]
"""


class BuildLocalPythonDistributions(Task):
"""Create python distributions (.whl) from python_dist targets."""

Expand Down Expand Up @@ -66,17 +79,39 @@ def _create_dist(self, dist_tgt, dist_target_dir):
# Copy sources and setup.py over to vt results directory for packaging.
# NB: The directory structure of the destination directory needs to match 1:1
# with the directory structure that setup.py expects.
for src_relative_to_target_base in dist_tgt.sources_relative_to_target_base():
all_sources = (
list(dist_tgt.sources_relative_to_target_base()) +
list(dist_tgt.cpp_sources_relative_to_target_base())
)
for src_relative_to_target_base in all_sources:
src_rel_to_results_dir = os.path.join(dist_target_dir, src_relative_to_target_base)
safe_mkdir(os.path.dirname(src_rel_to_results_dir))
abs_src_path = os.path.join(get_buildroot(),
dist_tgt.address.spec_path,
src_relative_to_target_base)
shutil.copyfile(abs_src_path, src_rel_to_results_dir)
# Build a whl using SetupPyRunner and return its absolute path.
setup_runner = SetupPyRunner(dist_target_dir, 'bdist_wheel', interpreter=interpreter)
setup_runner.run()
return self._get_whl_from_dir(os.path.join(dist_target_dir, 'dist'))
with temporary_dir() as tmpdir:
native_sources =
"'{}'".format(x) for x in dist_tgt.cpp_sources_relative_to_target_base()
pantssetup_import_contents = PANTSSETUP_IMPORT_BOILERPLATE.format(
setup_target=repr(dist_tgt),
native_sources_joined=','.join(native_sources),
)
self.context.log.info(pantssetup_import_contents)
pantssetup_module_path = os.path.join(tmpdir, 'pantssetup.py')
with open(pantssetup_module_path, 'w') as pantssetup_module_fh:
pantssetup_module_fh.write(pantssetup_import_contents)
prev_pypath = os.environ.get('PYTHONPATH')
if prev_pypath is None:
new_pypath = tmpdir
else:
scrubbed_pypath = re.sub(':$', '', prev_pypath)
new_pypath = '{}:{}'.format(scrubbed_pypath, tmpdir)
with environment_as(PYTHONPATH=new_pypath):
# Build a whl using SetupPyRunner and return its absolute path.
setup_runner = SetupPyRunner(dist_target_dir, 'bdist_wheel', interpreter=interpreter)
setup_runner.run()
return self._get_whl_from_dir(os.path.join(dist_target_dir, 'dist'))

def _get_whl_from_dir(self, install_dir):
"""Return the absolute path of the whl in a setup.py install directory."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

python_dist(
name='fasthello',
sources=[
'super_greet.c',
'hello_package/hello.py',
'hello_package/__init__.py',
'setup.py'
]
sources=rglobs('*.py'),
cpp_sources=globs('*.c'),
)

python_binary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
unicode_literals, with_statement)

from setuptools import setup, find_packages
from distutils.core import Extension


c_module = Extension(str('super_greet'), sources=[str('super_greet.c')])
from pantssetup import find_external_modules

setup(
name='fasthello_test',
version='1.0.0',
ext_modules=[c_module],
ext_modules=find_external_modules(),
packages=find_packages(),
install_requires=['pycountry==17.1.2']
)

0 comments on commit 2591288

Please sign in to comment.