Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lock command #12

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plz/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__version__ = "0.0.0"
from .__main__ import cli

__version__ = "0.0.0"
16 changes: 12 additions & 4 deletions plz/__main__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from pathlib import Path

import click

from .commands import init
from .commands import init, lock


# noinspection PyTypeChecker
@click.group()
def cli() -> None:
pass
@click.pass_context
@click.option("-p", "--path", type=click.Path(exists=True, path_type=Path, file_okay=False))
def cli(ctx: click.Context, path: Path) -> None:
path = path or Path.cwd()
ctx.obj = {
"path": path
}


# noinspection PyTypeChecker
cli.add_command(init)

cli.add_command(lock, "lock")

if __name__ == "__main__":
cli()
1 change: 1 addition & 0 deletions plz/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .init import init
from .lock import lock
2 changes: 1 addition & 1 deletion plz/commands/consts.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REQUIREMENTS_FILE = "requirements.in"
DEFAULT_REQUIREMENTS_FILE = "requirements.in"
13 changes: 6 additions & 7 deletions plz/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from pathlib import Path
from typing import Optional
from typing import Optional, Dict

import click

from .consts import REQUIREMENTS_FILE
from .consts import DEFAULT_REQUIREMENTS_FILE


# noinspection PyTypeChecker
@click.command("init")
@click.option("-p", "--path", type=click.Path(exists=True, path_type=Path))
def init(path: Optional[Path] = None) -> None:
path = path or Path.cwd()
requirements_file = path / REQUIREMENTS_FILE
@click.pass_obj
def init(config: Dict) -> None:
requirements_file = config["path"] / DEFAULT_REQUIREMENTS_FILE
if requirements_file.exists():
raise click.BadParameter(
f"Path {requirements_file} already have {REQUIREMENTS_FILE},"
f"Path {requirements_file} already have {DEFAULT_REQUIREMENTS_FILE},"
f"did you meant to run `plz install`?"
)
requirements_file.touch()
Expand Down
17 changes: 17 additions & 0 deletions plz/commands/lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Dict

import click
from piptools.__main__ import compile as _compile


@click.command("lock")
@click.pass_obj
@click.pass_context
def lock(ctx: click.Context, config: Dict, *args, **kwargs):
ctx.forward(
_compile.cli, *args, **kwargs
)
click.secho("Successfully lock requirements.")


lock.params += _compile.cli.params
23 changes: 12 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from setuptools import setup
from plz import __version__

DEPENDENCIES = [
'pip-tools',
'virtualenv',
'click'
]
DEPENDENCIES = ["pip-tools", "virtualenv", "click"]

setup(
name='plz',
name="plz",
version=__version__,
author='Roy Reznik',
author_email='royreznik@gmail.com',
license='MIT License',
keywords='CLI wrapping over virtualenv and pip-tools',
packages=['plz'],
author="Roy Reznik",
author_email="royreznik@gmail.com",
license="MIT License",
keywords="CLI wrapping over virtualenv and pip-tools",
packages=["plz"],
install_requires=DEPENDENCIES,
entry_points={
"console_scripts": [
"plz = plz:cli",
],
},
)
10 changes: 5 additions & 5 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from click.testing import CliRunner

from plz.__main__ import cli
from plz.commands.consts import REQUIREMENTS_FILE
from plz.commands.consts import DEFAULT_REQUIREMENTS_FILE


def test_init_create_expected_files_in_cwd(
Expand All @@ -14,7 +14,7 @@ def test_init_create_expected_files_in_cwd(
result = runner.invoke(cli, "init")
assert "Requirements file created" in result.stdout

assert (isolated_dir / REQUIREMENTS_FILE).exists()
assert (isolated_dir / DEFAULT_REQUIREMENTS_FILE).exists()


def test_init_create_expected_files_in_path(
Expand All @@ -23,16 +23,16 @@ def test_init_create_expected_files_in_path(
new_dir = isolated_dir / "dir"
new_dir.mkdir()
# noinspection PyTypeChecker
result = runner.invoke(cli, shlex.split("init -p dir"))
result = runner.invoke(cli, shlex.split("-p dir init"))
assert "Requirements file created" in result.stdout

assert (new_dir / REQUIREMENTS_FILE).exists()
assert (new_dir / DEFAULT_REQUIREMENTS_FILE).exists()


def test_init_fails_on_files_already_exists(
runner: CliRunner, isolated_dir: Path
) -> None:
(isolated_dir / REQUIREMENTS_FILE).touch()
(isolated_dir / DEFAULT_REQUIREMENTS_FILE).touch()
# noinspection PyTypeChecker
result = runner.invoke(cli, "init")
assert result.exit_code != 0
Expand Down
26 changes: 26 additions & 0 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import shlex
from pathlib import Path

from click.testing import CliRunner

from plz.__main__ import cli
from plz.commands.consts import DEFAULT_REQUIREMENTS_FILE


# Sanity check
def test_lock_create_lock_file(runner: CliRunner, isolated_dir: Path) -> None:
(isolated_dir / DEFAULT_REQUIREMENTS_FILE).touch()
# noinspection PyTypeChecker
result = runner.invoke(cli, "lock")
assert "Successfully lock requirements" in result.stdout

assert (isolated_dir / "requirements.in").exists()


def test_lock_work_with_compile_argument(runner: CliRunner, isolated_dir: Path) -> None:
(isolated_dir / DEFAULT_REQUIREMENTS_FILE).touch()
# noinspection PyTypeChecker
result = runner.invoke(cli, shlex.split("lock --pip-args '--progress-bar off'"))
assert "Successfully lock requirements" in result.stdout

assert (isolated_dir / "requirements.in").exists()