Commit 186a8309 by Sébastien Eustace

Add an actual pip installer

parent 2a104075
...@@ -7,6 +7,7 @@ from poetry.utils.venv import Venv ...@@ -7,6 +7,7 @@ from poetry.utils.venv import Venv
from .commands import AboutCommand from .commands import AboutCommand
from .commands import InstallCommand from .commands import InstallCommand
from .commands import LockCommand
class Application(BaseApplication): class Application(BaseApplication):
...@@ -35,7 +36,8 @@ class Application(BaseApplication): ...@@ -35,7 +36,8 @@ class Application(BaseApplication):
return commands + [ return commands + [
AboutCommand(), AboutCommand(),
InstallCommand() InstallCommand(),
LockCommand(),
] ]
def do_run(self, i, o) -> int: def do_run(self, i, o) -> int:
......
from .about import AboutCommand from .about import AboutCommand
from .install import InstallCommand from .install import InstallCommand
from .lock import LockCommand
...@@ -17,7 +17,7 @@ class InstallCommand(Command): ...@@ -17,7 +17,7 @@ class InstallCommand(Command):
help = """The <info>install</info> command reads the <comment>poetry.lock</> file from help = """The <info>install</info> command reads the <comment>poetry.lock</> file from
the current directory, processes it, and downloads and installs all the the current directory, processes it, and downloads and installs all the
libraries and dependencies outlined in that file. If the file does not libraries and dependencies outlined in that file. If the file does not
exist it will look for <comment>poetry.json</> and do the same. exist it will look for <comment>poetry.toml</> and do the same.
<info>poetry install</info> <info>poetry install</info>
""" """
......
from poetry.installation import Installer
from poetry.repositories.pypi_repository import PyPiRepository
from .command import Command
class LockCommand(Command):
"""
Locks the project dependencies.
lock
{ --no-dev : Do not install dev dependencies. }
"""
help = """The <info>lock</info> command reads the <comment>poetry.toml</> file from
the current directory, processes it, and locks the depdencies in the <comment>poetry.lock</> file.
<info>poetry lock</info>
"""
def handle(self):
installer = Installer(
self.output,
self.poetry.package,
self.poetry.locker,
PyPiRepository()
)
installer.update(True)
installer.execute_operations(False)
return installer.run()
...@@ -10,6 +10,8 @@ from poetry.puzzle.operations.operation import Operation ...@@ -10,6 +10,8 @@ from poetry.puzzle.operations.operation import Operation
from poetry.repositories import Repository from poetry.repositories import Repository
from poetry.repositories.installed_repository import InstalledRepository from poetry.repositories.installed_repository import InstalledRepository
from .pip_installer import PipInstaller
class Installer: class Installer:
...@@ -30,6 +32,8 @@ class Installer: ...@@ -30,6 +32,8 @@ class Installer:
self._dev_mode = True self._dev_mode = True
self._execute_operations = True self._execute_operations = True
self._installer = PipInstaller(self._io.venv, self._io)
def run(self): def run(self):
# Force update if there is no lock file present # Force update if there is no lock file present
if not self._update and not self._locker.is_locked(): if not self._update and not self._locker.is_locked():
...@@ -78,6 +82,19 @@ class Installer: ...@@ -78,6 +82,19 @@ class Installer:
def is_dev_mode(self) -> bool: def is_dev_mode(self) -> bool:
return self._dev_mode return self._dev_mode
def update(self, update=True) -> 'Installer':
self._update = update
return self
def is_updating(self) -> bool:
return self._update
def execute_operations(self, execute=True) -> 'Installer':
self._execute_operations = execute
return self
def _do_install(self, local_repo): def _do_install(self, local_repo):
locked_repository = Repository() locked_repository = Repository()
# initialize locked repo if we are installing from lock # initialize locked repo if we are installing from lock
...@@ -113,7 +130,7 @@ class Installer: ...@@ -113,7 +130,7 @@ class Installer:
# if they are present in the local repo # if they are present in the local repo
# TODO # TODO
if ops: if ops and self._execute_operations:
installs = [] installs = []
updates = [] updates = []
uninstalls = [] uninstalls = []
...@@ -149,7 +166,8 @@ class Installer: ...@@ -149,7 +166,8 @@ class Installer:
elif op.job_type == 'update': elif op.job_type == 'update':
local_repo.add_package(op.target_package) local_repo.add_package(op.target_package)
self._execute(op) if self._execute_operations:
self._execute(op)
def _execute(self, operation: Operation) -> None: def _execute(self, operation: Operation) -> None:
""" """
...@@ -160,27 +178,16 @@ class Installer: ...@@ -160,27 +178,16 @@ class Installer:
getattr(self, f'_execute_{method}')(operation) getattr(self, f'_execute_{method}')(operation)
def _execute_install(self, operation: Install) -> None: def _execute_install(self, operation: Install) -> None:
self._io.writeln( self._installer.install(operation.package)
f' - Installing <info>{operation.package.name}</> '
f'(<comment>{operation.package.full_pretty_version}</>)'
)
def _execute_update(self, operation: Update) -> None: def _execute_update(self, operation: Update) -> None:
name = operation.target_package.name self._installer.update(
original = operation.initial_package.pretty_version operation.initial_package,
target = operation.target_package.pretty_version operation.target_package
self._io.writeln(
f' - Updating <info>{name}</> '
f'(<comment>{original}</>'
f' -> <comment>{target}</>)'
) )
def _execute_uninstall(self, operation: Uninstall) -> None: def _execute_uninstall(self, operation: Uninstall) -> None:
self._io.writeln( self._installer.remove(operation.package)
f' - Removing <info>{operation.package.name}</> '
f'(<comment>{operation.package.full_pretty_version}</>)'
)
def _get_operations_from_lock(self, def _get_operations_from_lock(self,
locked_repository: Repository locked_repository: Repository
......
from poetry.utils.venv import Venv
class PipInstaller:
def __init__(self, venv: Venv, io):
self._venv = venv
self._io = io
def install(self, package):
self._io.writeln(
f' - Installing <info>{package.name}</> '
f'(<comment>{package.full_pretty_version}</>)'
)
self.run('install', self.requirement(package), '--no-deps')
def update(self, source, target):
self._io.writeln(
f' - Updating <info>{target.name}</> '
f'(<comment>{source.pretty_version}</>'
f' -> <comment>{target.pretty_version}</>)'
)
self.run('install', self.requirement(target), '--no-deps', '-U')
def remove(self, package):
self._io.writeln(
f' - Removing <info>{package.name}</> '
f'(<comment>{package.full_pretty_version}</>)'
)
self.run('uninstall', package.name)
def run(self, *args) -> str:
return self._venv.run('pip', *args)
def requirement(self, package) -> str:
return f'{package.name}=={package.version}'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment