Commit 408eef79 by Sébastien Eustace

Add the new command

parent dde73522
...@@ -165,7 +165,7 @@ The `install` command reads the `poetry.toml` file from the current directory, r ...@@ -165,7 +165,7 @@ The `install` command reads the `poetry.toml` file from the current directory, r
and installs them. and installs them.
```bash ```bash
poet install poetry install
``` ```
If there is a `poetry.lock` file in the current directory, If there is a `poetry.lock` file in the current directory,
...@@ -178,14 +178,14 @@ You can specify to the command that yo do not want the development dependencies ...@@ -178,14 +178,14 @@ You can specify to the command that yo do not want the development dependencies
the `--no-dev` option. the `--no-dev` option.
```bash ```bash
poet install --no-dev poetry install --no-dev
``` ```
You can also specify the features you want installed by passing the `--f|--features` option (See [Features](#features) for more info) You can also specify the features you want installed by passing the `--f|--features` option (See [Features](#features) for more info)
```bash ```bash
poet install --features "mysql pgsql" poetry install --features "mysql pgsql"
poet install -f mysql -f pgsql poetry install -f mysql -f pgsql
``` ```
#### Options #### Options
...@@ -193,14 +193,13 @@ poet install -f mysql -f pgsql ...@@ -193,14 +193,13 @@ poet install -f mysql -f pgsql
* `--no-dev`: Do not install dev dependencies. * `--no-dev`: Do not install dev dependencies.
* `-f|--features`: Features to install (multiple values allowed). * `-f|--features`: Features to install (multiple values allowed).
### update ### update
In order to get the latest versions of the dependencies and to update the `poetry.lock` file, In order to get the latest versions of the dependencies and to update the `poetry.lock` file,
you should use the `update` command. you should use the `update` command.
```bash ```bash
poet update poetry update
``` ```
This will resolve all dependencies of the project and write the exact versions into `poetry.lock`. This will resolve all dependencies of the project and write the exact versions into `poetry.lock`.
...@@ -208,7 +207,7 @@ This will resolve all dependencies of the project and write the exact versions i ...@@ -208,7 +207,7 @@ This will resolve all dependencies of the project and write the exact versions i
If you just want to update a few packages and not all, you can list them as such: If you just want to update a few packages and not all, you can list them as such:
```bash ```bash
poet update requests toml poetry update requests toml
``` ```
#### Options #### Options
...@@ -216,6 +215,15 @@ poet update requests toml ...@@ -216,6 +215,15 @@ poet update requests toml
* `--no-progress`: Removes the progress display that can mess with some terminals or scripts which don't handle backspace characters. * `--no-progress`: Removes the progress display that can mess with some terminals or scripts which don't handle backspace characters.
### add
The `add` command adds new packages to the `poetry.toml` file from the current directory.
```bash
poetry add requests pendulum
```
### package ### package
The `package` command builds the source and wheels archives. The `package` command builds the source and wheels archives.
...@@ -241,7 +249,7 @@ It will automatically register the package before uploading if this is the first ...@@ -241,7 +249,7 @@ It will automatically register the package before uploading if this is the first
This command searches for packages on a remote index. This command searches for packages on a remote index.
```bash ```bash
poet search requests pendulum poetry search requests pendulum
``` ```
#### Options #### Options
...@@ -253,20 +261,7 @@ poet search requests pendulum ...@@ -253,20 +261,7 @@ poet search requests pendulum
This command locks (without installing) the dependencies specified in `poetry.toml`. This command locks (without installing) the dependencies specified in `poetry.toml`.
```bash ```bash
poet lock poetry lock
```
#### Options
* `-f|--force`: Force locking.
### check
The `check` command will check if the `poetry.toml` file is valid.
```bash
poet check
``` ```
......
...@@ -9,6 +9,7 @@ from .commands import AboutCommand ...@@ -9,6 +9,7 @@ from .commands import AboutCommand
from .commands import AddCommand from .commands import AddCommand
from .commands import InstallCommand from .commands import InstallCommand
from .commands import LockCommand from .commands import LockCommand
from .commands import NewCommand
from .commands import UpdateCommand from .commands import UpdateCommand
...@@ -44,6 +45,7 @@ class Application(BaseApplication): ...@@ -44,6 +45,7 @@ class Application(BaseApplication):
AddCommand(), AddCommand(),
InstallCommand(), InstallCommand(),
LockCommand(), LockCommand(),
NewCommand(),
UpdateCommand(), UpdateCommand(),
] ]
......
...@@ -2,4 +2,5 @@ from .about import AboutCommand ...@@ -2,4 +2,5 @@ from .about import AboutCommand
from .add import AddCommand from .add import AddCommand
from .install import InstallCommand from .install import InstallCommand
from .lock import LockCommand from .lock import LockCommand
from .new import NewCommand
from .update import UpdateCommand from .update import UpdateCommand
from pathlib import Path
from poetry.layouts import layout
from .command import Command
class NewCommand(Command):
"""
Creates a new Python project at <path>
new
{ path : The path to create the project at. }
{ --name : Set the resulting package name. }
"""
def handle(self):
layout_ = layout('standard')
path = Path.cwd() / Path(self.argument('path'))
name = self.option('name')
if not name:
name = path.name
if path.exists():
if list(path.glob('*')):
# Directory is not empty. Aborting.
raise RuntimeError(
'Destination <fg=yellow;bg=red>{}</>'
'exists and is not empty'.format(
path
)
)
readme_format = 'rst'
layout_ = layout_(name, '0.1.0', readme_format=readme_format)
layout_.create(path)
self.line(
'Created package <info>{}</> in <fg=blue>{}</>'
.format(name, path.relative_to(Path.cwd()))
)
from typing import Type
from .layout import Layout
from .standard import StandardLayout
_LAYOUTS = {
'standard': StandardLayout
}
def layout(name: str) -> Type[Layout]:
if name not in _LAYOUTS:
raise ValueError('Invalid layout')
return _LAYOUTS[name]
# -*- coding: utf-8 -*-
import re
import toml
from poetry.vcs.git_config import GitConfig
_canonicalize_regex = re.compile(r"[-_.]+")
TESTS_DEFAULT = """from {package_name} import __version__
def test_version():
assert '{version}' == __version__
"""
class Layout(object):
def __init__(self, project, version='0.1.0', readme_format='md', author=None):
self._project = project
self._package_name = self._canonicalize_name(project).replace('-', '_')
self._version = version
self._readme_format = readme_format
self._dependencies = {}
self._dev_dependencies = {}
self._include = []
self._git_config = GitConfig()
if not author:
if (
self._git_config.get('user.name')
and self._git_config.get('user.email')
):
author = '{} <{}>'.format(
self._git_config['user.name'],
self._git_config['user.email']
)
else:
author = 'Your Name <you@example.com>'
self._author = author
def create(self, path, with_tests=True):
self._dependencies = {}
self._dev_dependencies = {}
self._include = []
path.mkdir(parents=True, exist_ok=True)
self._create_default(path)
self._create_readme(path)
if with_tests:
self._create_tests(path)
self._write_poetry(path)
def _create_default(self, path, src=True):
raise NotImplementedError()
def _create_readme(self, path):
if self._readme_format == 'rst':
readme_file = path / 'README.rst'
else:
readme_file = path / 'README.md'
readme_file.touch()
def _create_tests(self, path):
self._dev_dependencies['pytest'] = '^3.0'
tests = path / 'tests'
tests_init = tests / '__init__.py'
tests_default = tests / 'test_{}.py'.format(self._package_name)
tests.mkdir()
tests_init.touch(exist_ok=False)
with tests_default.open('w') as f:
f.write(
TESTS_DEFAULT.format(
package_name=self._package_name,
version=self._version
)
)
def _write_poetry(self, path):
output = {
'package': {
'name': self._project,
'version': self._version,
'authors': [self._author],
}
}
content = toml.dumps(output, preserve=True)
output = {
'dependencies': {},
'dev-dependencies': {
'pytest': '^3.4'
}
}
content += '\n' + toml.dumps(output, preserve=True)
poetry = path / 'poetry.toml'
with poetry.open('w') as f:
f.write(content)
def _canonicalize_name(self, name: str) -> str:
return _canonicalize_regex.sub("-", name).lower()
# -*- coding: utf-8 -*-
from .layout import Layout
DEFAULT = """__version__ = '{version}'
"""
class StandardLayout(Layout):
def _create_default(self, path):
package_path = path / self._package_name
package_init = package_path / '__init__.py'
package_path.mkdir()
with package_init.open('w') as f:
f.write(DEFAULT.format(version=self._version))
import re
import subprocess
class GitConfig(object):
def __init__(self):
config_list = subprocess.check_output(
['git', 'config', '-l'],
stderr=subprocess.STDOUT
).decode()
self._config = {}
m = re.findall('(?ms)^([^=]+)=(.*?)$', config_list)
if m:
for group in m:
self._config[group[0]] = group[1]
def get(self, key, default=None):
return self._config.get(key, default)
def __getitem__(self, item):
return self._config[item]
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