Skip to content

Commit

Permalink
Merge pull request #27 from yunstanford/add_uninstall
Browse files Browse the repository at this point in the history
Add uninstall
  • Loading branch information
toumorokoshi committed Mar 19, 2017
2 parents e5eaff5 + 0482892 commit cc1c883
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
30 changes: 30 additions & 0 deletions tests/test_packages_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ def main(build):
print("test")
""".strip()

URANIUM_PY_UNINSTALL = """
def main(build):
build.packages.install("pyyaml")
import yaml
assert yaml is not None
build.packages.uninstall("pyyaml")
import importlib
try:
importlib.reload(yaml)
exist = True
except Exception:
exist = False
assert exist == False
""".strip()

from uranium.scripts import execute_script
from .conftest import URANIUM_SOURCE_ROOT

Expand All @@ -31,6 +49,18 @@ def test_install(tmpdir):
assert code == 0


def test_uninstall(tmpdir):
# we need to create a virtualenv
tmpdir.join("ubuild.py").write(URANIUM_PY_UNINSTALL)
code, out, err = execute_script(
"uranium_standalone", "--uranium-dir", URANIUM_SOURCE_ROOT,
cwd=tmpdir.strpath
)
print("stdout:\n" + str(out))
print("stderr:\n" + str(err))
assert code == 0


def test_package_cache(tmpdir):
""" don't event attempt to call pip, if a package already exists. """
tmpdir.join("ubuild.py").write(URANIUM_PY)
Expand Down
12 changes: 11 additions & 1 deletion uranium/packages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ..lib.asserts import get_assert_function
from ..exceptions import PackageException
from .install_command import install
from .install_command import install, uninstall
from .versions import Versions

p_assert = get_assert_function(PackageException)
Expand Down Expand Up @@ -88,6 +88,16 @@ def install(self, name, version=None, develop=False, upgrade=False, install_opti
if req.installed_version:
self.versions[req.name] = ("==" + req.installed_version)

def uninstall(self, package_name):
"""
uninstall is used when uninstalling a python package from a environment.
"""
p_assert(
self._is_package_already_installed(package_name, None),
"package {package} doesn't exist".format(package=package_name)
)
uninstall(package_name)

@staticmethod
def _is_package_already_installed(name, version):
import pkg_resources
Expand Down
12 changes: 11 additions & 1 deletion uranium/packages/install_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pip.commands import InstallCommand
from pip.commands import InstallCommand, UninstallCommand
from pip.req.req_file import process_line
from ..lib.compat import urlparse

Expand Down Expand Up @@ -60,6 +60,16 @@ def install(package_name, constraint_dict=None, **kwargs):
return command.run(options, args)


def uninstall(package_name, **kwargs):
"""
a convenience function to uninstall a package.
"""
command = UninstallCommand()
args = ["--yes", package_name]
options, args = command.parse_args(args)
return command.run(options, args)


def _get_netloc(url):
parsed_url = urlparse(url)
return parsed_url.netloc
Expand Down

0 comments on commit cc1c883

Please sign in to comment.