Unverified Commit d7ad5b04 authored by Sébastien Eustace's avatar Sébastien Eustace
Browse files

Improve TOML file management

parent 8b195ed1
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGELOG.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,10 +5,12 @@
### Added

- Add `show` command. 
- Added the `--dry-run` option to the `add` command.

### Changed

- Dependencies of each package is now stored in `poetry.lock`.
- Improved TOML file management.

### Fixed

+1 −0
Changes for README.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -302,6 +302,7 @@ poetry add requests pendulum

* `--D|dev`: Add package as development dependency.
* `--optional` : Add as an optional dependency.
* `--dry-run` : Outputs the operations but will not execute anything (implicitly enables --verbose).


### remove
+1 −1
Changes for poetry.toml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -25,9 +25,9 @@ include = ['poetry/**/*', 'LICENSE']
[dependencies]
cleo = "^0.6"
requests = "^2.18"
toml = "^0.9"
cachy = "^0.1.0"
pip-tools = "^1.11"
toml = "^0.9.4"

[dev-dependencies]
pytest = "~3.4"
+40 −51
Changes for poetry/console/commands/add.py: 40 added lines, 51 removed lines.
Original line number Diff line number Diff line
import re

import toml

from typing import List
from typing import Tuple

@@ -20,6 +18,8 @@ class AddCommand(Command):
        { name* : Packages to add. }
        {--D|dev : Add package as development dependency. }
        {--optional : Add as an optional dependency. }
        {--dry-run : Outputs the operations but will not execute anything
                     (implicitly enables --verbose). }
    """

    help = """The add command adds required packages to your <comment>poetry.toml</> and installs them.
@@ -28,10 +28,22 @@ If you do not specify a version constraint, poetry will choose a suitable one ba
"""

    def handle(self):
        names = self.argument('name')
        packages = self.argument('name')
        is_dev = self.option('dev')

        requirements = self._determine_requirements(names)
        section = 'dependencies'
        if is_dev:
            section = 'dev-dependencies'

        original_content = self.poetry.locker.original.read()
        content = self.poetry.locker.original.read()

        for name in packages:
            for key in content[section]:
                if key.lower() == name.lower():
                    raise ValueError(f'Package {name} is already present')

        requirements = self._determine_requirements(packages)
        requirements = self._format_requirements(requirements)

        # validate requirements format
@@ -39,53 +51,11 @@ If you do not specify a version constraint, poetry will choose a suitable one ba
        for constraint in requirements.values():
            parser.parse_constraints(constraint)

        # Trying to figure out where to add our dependencies
        # If we find a toml library that keeps comments
        # We could remove this whole section
        section = '[dependencies]'
        if is_dev:
            section = '[dev-dependencies]'

        new_content = None
        with self.poetry.locker.original.path.open() as fd:
            content = fd.read().split('\n')

        in_section = False
        index = None
        for i, line in enumerate(content):
            line = line.strip()

            if line == section:
                in_section = True
                continue

            if in_section and not line:
                index = i
                break

        if index is not None:
            for i, require in enumerate(requirements.items()):
                name, version = require
                if '.' in name:
                    name = f'"{name}"'

                content.insert(
                    index + i,
                    f'{name} = "{version}"'
                )
        for name, constraint in requirements.items():
            content[section][name] = constraint

            new_content = '\n'.join(content)

        if new_content is not None:
            with self.poetry.locker.original.path.open('w') as fd:
                fd.write(new_content)
        else:
            # We could not find where to put the dependencies
            # We raise an warning
            self.warning('Unable to automatically add dependencies')
            self.warning('Add them manually to your poetry.toml')

            return 1
        # Write new content
        self.poetry.locker.original.write(content)

        # Cosmetic new line
        self.line('')
@@ -100,10 +70,29 @@ If you do not specify a version constraint, poetry will choose a suitable one ba
            self.poetry.pool
        )

        installer.dry_run(self.option('dry-run'))
        installer.update(True)
        installer.whitelist(requirements)

        installer.run()
        try:
            status = installer.run()
        except Exception:
            self.poetry.locker.original.write(original_content)

            raise

        if status != 0 or self.option('dry-run'):
            # Revert changes
            if not self.option('dry-run'):
                self.error(
                    '\n'
                    'Addition failed, reverting poetry.toml '
                    'to its original content.'
                )

            self.poetry.locker.original.write(original_content)

        return status

    def _determine_requirements(self, requires: List[str]) -> List[str]:
        if not requires:
+20 −50
Changes for poetry/console/commands/remove.py: 20 added lines, 50 removed lines.
Original line number Diff line number Diff line
import toml

from poetry.installation import Installer

from .command import Command
@@ -22,59 +20,33 @@ list of installed packages
<info>poetry remove</info>"""

    def handle(self):
        packages = [p.lower() for p in self.argument('packages')]
        packages = self.argument('packages')
        is_dev = self.option('dev')

        with self.poetry.locker.original.path.open() as fd:
            content = fd.read().split('\n')

        # Trying to figure out where are our dependencies
        # If we find a toml library that keeps comments
        # We could remove this whole section
        section = '[dependencies]'
        original_content = self.poetry.locker.original.read()
        content = self.poetry.locker.original.read()
        section = 'dependencies'
        if is_dev:
            section = '[dev-dependencies]'
            section = 'dev-dependencies'

        # Searching for package in
        in_section = False
        indices = []
        # Deleting entries
        requirements = {}
        for i, line in enumerate(content):
            line = line.strip()

            if line == section:
                in_section = True
                continue

            if in_section:
                if not line:
                    # End of section
        for name in packages:
            found = False
            for key in content[section]:
                if key.lower() == name.lower():
                    found = True
                    requirements[name] = content[section][name]
                    break

                requirement = toml.loads(line)
                name = list(requirement.keys())[0].lower()
                version = requirement[name]

                if name in packages:
                    requirements[name] = version
                    indices.append(i)
                    break

        if not indices or len(indices) != len(packages):
            raise RuntimeError(
                'Packages are not present in your poetry.toml file'
            )

        new_content = []
        for i, line in enumerate(content):
            if i in indices:
                continue
            if not found:
                raise ValueError(f'Package {name} not found')

            new_content.append(line)
        for key in requirements:
            del content[section][key]

        new_content = '\n'.join(new_content)
        with self.poetry.locker.original.path.open('w') as fd:
            fd.write(new_content)
        # Write the new content back
        self.poetry.locker.original.write(content)

        # Update packages
        self.reset_poetry()
@@ -93,8 +65,7 @@ list of installed packages
        try:
            status = installer.run()
        except Exception:
            with self.poetry.locker.original.path.open('w') as fd:
                fd.write('\n'.join(content))
            self.poetry.locker.original.write(original_content)

            raise

@@ -107,7 +78,6 @@ list of installed packages
                    'to its original content.'
                )

            with self.poetry.locker.original.path.open('w') as fd:
                fd.write('\n'.join(content))
            self.poetry.locker.original.write(original_content)

        return status
Loading