Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic discovery entry_points configuration #309

Merged
merged 1 commit into from
Dec 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions src/main/python/pybuilder/plugins/python/distutils_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import string
import subprocess
import sys

import os
import re

try:
from StringIO import StringIO
except ImportError as e:
Expand Down Expand Up @@ -79,7 +78,7 @@ def run(self):
packages = $packages,
py_modules = $modules,
classifiers = $classifiers,
entry_points = $console_scripts,
entry_points = $entry_points,
data_files = $data_files,
package_data = $package_data,
install_requires = $dependencies,
Expand Down Expand Up @@ -139,7 +138,7 @@ def render_setup_script(project):
"packages": build_packages_string(project),
"modules": build_modules_string(project),
"classifiers": build_classifiers_string(project),
"console_scripts": build_console_scripts_string(project),
"entry_points": build_entry_points_string(project),
"data_files": build_data_files_string(project),
"package_data": build_package_data_string(project),
"dependencies": build_install_dependencies_string(project),
Expand Down Expand Up @@ -391,18 +390,32 @@ def build_modules_string(project):
return build_string_from_array([mod for mod in project.list_modules()])


def build_console_scripts_string(project):
console_scripts = project.get_property('distutils_console_scripts', [])
def build_entry_points_string(project):
console_scripts = project.get_property('distutils_console_scripts')
entry_points = project.get_property('distutils_entry_points')
if console_scripts is not None and entry_points is not None:
raise BuildFailedException("'distutils_console_scripts' cannot be combined with 'distutils_entry_points'")

if len(console_scripts) == 0:
return "{}"
if entry_points is None:
entry_points = dict()

if console_scripts is not None:
entry_points['console_scripts'] = console_scripts

if len(entry_points) == 0:
return '{}'

indent = 12
string = "{'console_scripts': "
string += build_string_from_array(console_scripts, indent)
string += "}"
indent = 8
result = "{\n"

for k in sorted(entry_points.keys()):
result += " " * (indent + 4)
result += "'%s': %s" % (k, build_string_from_array(entry_points[k], indent + 8)) + ",\n"

return string
result = result[:-2] + "\n"
result += (" " * indent) + "}"

return result


def build_classifiers_string(project):
Expand Down Expand Up @@ -446,13 +459,13 @@ def build_string_from_dict(d, indent=12):
element_separator += " " * indent
map_elements = []

for k, v in d:
for k, v in d.items():
map_elements.append("'%s': '%s'" % (k, v))

result = ""

if len(map_elements) > 0:
result += "\n"
result += "{\n"
result += " " * indent
result += element_separator.join(map_elements)
result += "\n"
Expand Down
45 changes: 38 additions & 7 deletions src/unittest/python/plugins/python/distutils_plugin_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
from mock import patch, MagicMock, ANY

from pybuilder.core import Project, Author, Logger
from pybuilder.errors import BuildFailedException
from pybuilder.plugins.python.distutils_plugin import (build_data_files_string,
build_dependency_links_string,
build_install_dependencies_string,
build_package_data_string,
build_console_scripts_string,
build_entry_points_string,
default,
render_manifest_file,
build_scripts_string,
Expand Down Expand Up @@ -504,12 +505,42 @@ def test_should_render_console_scripts_when_property_is_set(self):
self.project.set_property("distutils_console_scripts", ["release = zest.releaser.release:main",
"prerelease = zest.releaser.prerelease:main"])

actual_setup_script = build_console_scripts_string(self.project)

self.assertEquals("{'console_scripts': [\n"
" 'release = zest.releaser.release:main',\n"
" 'prerelease = zest.releaser.prerelease:main'\n"
" ]}", actual_setup_script)
actual_setup_script = build_entry_points_string(self.project)
self.assertEquals("{\n"
" 'console_scripts': [\n"
" 'release = zest.releaser.release:main',\n"
" 'prerelease = zest.releaser.prerelease:main'\n"
" ]\n"
" }", actual_setup_script)

def test_should_render_console_script_when_property_is_set(self):
self.project.set_property("distutils_console_scripts", ["release = zest.releaser.release:main"])

actual_setup_script = build_entry_points_string(self.project)
self.assertEquals("{\n"
" 'console_scripts': ['release = zest.releaser.release:main']\n"
" }", actual_setup_script)

def test_should_render_entry_points_when_property_is_set(self):
self.project.set_property("distutils_entry_points", {'foo_entry': ["release = zest.releaser.release:main",
"release1 = zest.releaser.release1:main"],
'bar_entry': ["prerelease = zest.releaser.prerelease:main"]
})

actual_setup_script = build_entry_points_string(self.project)
self.assertEquals("{\n"
" 'bar_entry': ['prerelease = zest.releaser.prerelease:main'],\n"
" 'foo_entry': [\n"
" 'release = zest.releaser.release:main',\n"
" 'release1 = zest.releaser.release1:main'\n"
" ]\n"
" }", actual_setup_script)

def test_should_fail_with_entry_points_and_console_scripts_set(self):
self.project.set_property("distutils_console_scripts", object())
self.project.set_property("distutils_entry_points", object())

self.assertRaises(BuildFailedException, build_entry_points_string, self.project)

@patch("pybuilder.plugins.python.distutils_plugin.open", create=True)
def test_should_render_runtime_dependencies_when_requirements_file_used(self, mock_open):
Expand Down