Skip to content

Commit

Permalink
using entry-points instead of module and egg paths
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Feb 4, 2015
1 parent 1f66ad6 commit bb54a81
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 37 deletions.
4 changes: 4 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ can cause eggs to be installed incorrectly (due to a misdetected root path)
* support buildout extensions?
* support inheritance
* make virtualenv relocatable at the end of the build process

## Tests TODO

* test to allow user paths in uranium script.
3 changes: 3 additions & 0 deletions scripts/uranium
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def _install_virtualenv(install_dir):


def _install_uranium(virtualenv_dir, uranium_dir=None, version=None):
if uranium_dir:
uranium_dir = os.path.expanduser(uranium_dir)

log_file = os.path.join(virtualenv_dir, 'uranium_install_log.txt')
pip_executable = os.path.join(virtualenv_dir, 'bin', 'pip')
uranium_name = uranium_dir or 'uranium'
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]

setup(name='uranium',
version='0.0.48',
version='0.0.49',
description='a build system for python',
long_description='a build system for python',
author='Yusuke Tsutsumi',
Expand All @@ -45,6 +45,9 @@
entry_points={
'console_scripts': [
'uranium=uranium:main'
],
'uranium.plugin': [
'default = uranium.example_plugin:ExamplePlugin'
]
},
tests_require=tests_require
Expand Down
22 changes: 14 additions & 8 deletions uranium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"""
__import__('pkg_resources').declare_namespace(__name__)
import logging
import pkg_resources
from contextlib import contextmanager
from pip._vendor import pkg_resources
from pip._vendor import pkg_resources as pip_pkg_resources
from docopt import docopt
from virtualenv import make_environment_relocatable
from .uranium import Uranium
Expand Down Expand Up @@ -75,28 +76,33 @@ def _activate_virtualenv(uranium_dir):
# we modify the executable directly, because pip invokes this to install packages.
sys.executable = os.path.join(uranium_dir, 'bin', 'python')

for _pkg_resources in [pkg_resources, pip_pkg_resources]:
_clean_package_resources(_pkg_resources, old_prefix)

# in the past, an incorrect real_prefix directory was being
# generated when using uranium. it looks like sys.prefix
# works as a replacement, so let's use that.
sys.real_prefix = sys.prefix


def _clean_package_resources(_pkg_resources, old_prefix):
# this is a workaround for pip. Pip utilizes pkg_resources
# and the path to determine what's installed in the current
# sandbox
#
# we remove the requirements that are installed
# from the parent environment, so pip will detect
# the requirement from the current virtualenv
for name, req in list(pkg_resources.working_set.by_key.items()):
for name, req in list(_pkg_resources.working_set.by_key.items()):
if old_prefix in req.location:
del pkg_resources.working_set.by_key[name]
del _pkg_resources.working_set.by_key[name]

# ensure that pkg_resources only searches the
# existing sys.path. These variables are set on
# initialization, so we have to reset them
# when activating a sandbox.
pkg_resources.working_set.entries = sys.path

# in the past, an incorrect real_prefix directory was being
# generated when using uranium. it looks like sys.prefix
# works as a replacement, so let's use that.
sys.real_prefix = sys.prefix

LOGGING_NAMES = [__name__]


Expand Down
2 changes: 1 addition & 1 deletion uranium/buildout_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def install_part(part):
LOGGER.error(str(e))

def _get_recipe_class(self, recipe_name):
return self._classloader.get_class(recipe_name)
return self._classloader.get_entry_point(recipe_name, "zc.buildout")

def __getitem__(self, key):
if key == "buildout":
Expand Down
26 changes: 11 additions & 15 deletions uranium/classloader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import pkg_resources
from .compat import import_module
from .pip_manager import PackageNotFound

Expand All @@ -19,25 +20,20 @@ class ClassLoader(object):
def __init__(self, pip_manager):
self._pip = pip_manager

def get_class_from_spec(self, class_spec):
"""
resolve a spec string from one of the following options:
<egg_name>:<module>
<module>
"""
egg_name = None
module_path = class_spec
if ':' in class_spec:
egg_name, module_path = class_spec.split(':')
def get_entry_point(self, entry_point, group):
if ":" in entry_point:
dist, name = entry_point.split(":")
else:
dist, name = entry_point, "default"

try:
import_module(module_path)
import_module(dist)
except ImportError:
if egg_name:
self._install_egg(egg_name)
self._install_egg(dist)

return self.get_class(module_path)
return pkg_resources.load_entry_point(
dist, group, name
)

def get_class(self, class_module_path):
"""
Expand Down
4 changes: 3 additions & 1 deletion uranium/isotope_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ def remove_part(isotope):
pass

def _get_isotope_class(self, isotope_name):
return self._classloader.get_class_from_spec(isotope_name)
return self._classloader.get_entry_point(
isotope_name, "uranium.plugin"
)
13 changes: 3 additions & 10 deletions uranium/tests/test_classloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ class TestClassLoader(object):
def setUp(self):
self._classloader = ClassLoader(None)

def test_find_example_plugin(self):
eq_(self._classloader.get_class_from_spec('uranium.example_plugin'),
def test_get_entry_point(self):
eq_(self._classloader.get_entry_point("uranium",
"uranium.plugin"),
ExamplePlugin)

def test_find_example_plugin_with_eggname(self):
self._classloader._install_egg = Mock()
eq_(self._classloader.get_class_from_spec('uranium:uranium.example_plugin'),
ExamplePlugin)
# the behaviour here only downloads an egg if it doesn't already
# exist.
# self._classloader._install_egg.assert_called_with('uranium')
2 changes: 1 addition & 1 deletion uranium/tests/test_uranium.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
},
'parts': {
'platform-versions': {
'isotope': 'uranium.example_plugin',
'isotope': 'uranium',
'versions': {
'nose': '1.1.0'
}
Expand Down

0 comments on commit bb54a81

Please sign in to comment.