Skip to content

Commit

Permalink
Merge with origin.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterS committed Apr 29, 2020
2 parents 71c1886 + 201044c commit 2bd022f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 20 deletions.
7 changes: 7 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ the `--no-dev` option.
poetry install --no-dev
```

If you want to remove old dependencies no longer present in the lock file, use the
`--remove-untracked` option.

```bash
poetry install --remove-untracked
```

You can also specify the extras you want installed
by passing the `--E|--extras` option (See [Extras](#extras) for more info)

Expand Down
6 changes: 2 additions & 4 deletions poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class InstallCommand(EnvCommand):
"(implicitly enables --verbose).",
),
option(
"keep-untracked",
None,
"Does not remove packages not present in the lock file.",
"remove-untracked", None, "Removes packages not present in the lock file.",
),
option(
"extras",
Expand Down Expand Up @@ -63,7 +61,7 @@ def handle(self):
installer.extras(extras)
installer.dev_mode(not self.option("no-dev"))
installer.dry_run(self.option("dry-run"))
installer.keep_untracked(self.option("keep-untracked"))
installer.remove_untracked(self.option("remove-untracked"))
installer.verbose(self.option("verbose"))

return_code = installer.run()
Expand Down
61 changes: 45 additions & 16 deletions poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from poetry.puzzle.operations import Uninstall
from poetry.puzzle.operations import Update
from poetry.puzzle.operations.operation import Operation
from poetry.puzzle.provider import Provider
from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.repositories.installed_repository import InstalledRepository
Expand Down Expand Up @@ -38,7 +39,7 @@ def __init__(
self._pool = pool

self._dry_run = False
self._keep_untracked = False
self._remove_untracked = False
self._update = False
self._verbose = False
self._write_lock = True
Expand Down Expand Up @@ -83,13 +84,13 @@ def dry_run(self, dry_run=True): # type: (bool) -> Installer
def is_dry_run(self): # type: () -> bool
return self._dry_run

def keep_untracked(self, keep_untracked=True): # type: (bool) -> Installer
self._keep_untracked = keep_untracked
def remove_untracked(self, remove_untracked=True): # type: (bool) -> Installer
self._remove_untracked = remove_untracked

return self

def is_keep_untracked(self): # type: () -> bool
return self._keep_untracked
def is_remove_untracked(self): # type: () -> bool
return self._remove_untracked

def verbose(self, verbose=True): # type: (bool) -> Installer
self._verbose = verbose
Expand Down Expand Up @@ -205,6 +206,45 @@ def _do_install(self, local_repo):
root = root.clone()
del root.dev_requires[:]

with root.with_python_versions(
".".join([str(i) for i in self._env.version_info[:3]])
):
# We resolve again by only using the lock file
pool = Pool(ignore_repository_names=True)

# Making a new repo containing the packages
# newly resolved and the ones from the current lock file
repo = Repository()
for package in local_repo.packages + locked_repository.packages:
if not repo.has_package(package):
repo.add_package(package)

pool.add_repository(repo)

# We whitelist all packages to be sure
# that the latest ones are picked up
whitelist = []
for pkg in locked_repository.packages:
whitelist.append(pkg.name)

solver = Solver(
root, pool, self._installed_repository, locked_repository, NullIO()
)

ops = solver.solve(use_latest=whitelist)

if self._remove_untracked:
locked_names = {locked.name for locked in locked_repository.packages}

for installed in self._installed_repository.packages:
if installed.name == self._package.name:
continue
if installed.name in Provider.UNSAFE_PACKAGES:
# Never remove pip, setuptools etc.
continue
if installed.name not in locked_names:
ops.append(Uninstall(installed))

# We need to filter operations so that packages
# not compatible with the current system,
# or optional and not requested, are dropped
Expand Down Expand Up @@ -405,17 +445,6 @@ def _get_operations_from_lock(

ops.append(op)

if not self._keep_untracked:
for installed in installed_repo.packages:
is_in_lock_file = False
for locked in locked_repository.packages:
if locked.name == installed.name:
is_in_lock_file = True
break

if not is_in_lock_file:
ops.append(Uninstall(installed))

return ops

def _filter_operations(
Expand Down
53 changes: 53 additions & 0 deletions tests/installation/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,59 @@ def test_run_install_no_dev(installer, locker, repo, package, installed):
assert len(removals) == 1


def test_run_install_remove_untracked(installer, locker, repo, package, installed):
locker.locked(True)
locker.mock_lock_data(
{
"package": [
{
"name": "a",
"version": "1.0",
"category": "main",
"optional": False,
"platform": "*",
"python-versions": "*",
"checksum": [],
}
],
"metadata": {
"python-versions": "*",
"platform": "*",
"content-hash": "123456789",
"hashes": {"a": []},
},
}
)
package_a = get_package("a", "1.0")
package_b = get_package("b", "1.1")
package_c = get_package("c", "1.2")
package_pip = get_package("pip", "20.0.0")
repo.add_package(package_a)
repo.add_package(package_b)
repo.add_package(package_c)
repo.add_package(package_pip)

installed.add_package(package_a)
installed.add_package(package_b)
installed.add_package(package_c)
installed.add_package(package_pip) # Always required and never removed.
installed.add_package(package) # Root package never removed.

package.add_dependency("A", "~1.0")

installer.dev_mode(True).remove_untracked(True)
installer.run()

installs = installer.installer.installs
assert len(installs) == 0

updates = installer.installer.updates
assert len(updates) == 0

removals = installer.installer.removals
assert set(r.name for r in removals) == {"b", "c"}


def test_run_whitelist_add(installer, locker, repo, package):
locker.locked(True)
locker.mock_lock_data(
Expand Down

0 comments on commit 2bd022f

Please sign in to comment.