-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from hypothesis/remove-non-boilerplate-files
Apply suggestions from code review
- Loading branch information
Showing
16 changed files
with
407 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
Testing Manually | ||
---------------- | ||
|
||
Normally if you wanted to test a command manually in dev you'd do so through | ||
tox, for example: | ||
|
||
```terminal | ||
$ tox -qe dev --run-command 'pip-sync-faster --help' | ||
usage: pip-sync-faster [-h] [-v] | ||
options: | ||
-h, --help show this help message and exit | ||
-v, --version | ||
``` | ||
|
||
But there's a problem with running `pip-sync-faster` commands in this way: a | ||
command like `tox -e dev --run-command 'pip-sync-faster requirements.txt'` will | ||
run `pip-sync requirements.txt` and `pip-sync` will sync the | ||
current virtualenv (`.tox/dev/`) with the `requirements.txt` file. Everything | ||
in `requirements.txt` will get installed into `.tox/dev/`, which you probably | ||
don't want. Even worse everything _not_ in `requirements.txt` will get | ||
_removed_ from `.tox/dev/` including `pip-sync-faster` itself! | ||
|
||
To avoid this problem run `pip-sync-faster` in a temporary virtualenv instead. | ||
This installs the contents of `requirements.txt` into the temporary venv so | ||
your `.tox/dev/` env doesn't get messed up. And it does not install | ||
`pip-sync-faster` into the temporary venv so there's no issue with `pip-sync` | ||
uninstalling `pip-sync-faster`: | ||
|
||
```terminal | ||
# Make a temporary directory. | ||
tempdir=$(mktemp -d) | ||
# Create a virtualenv in the temporary directory. | ||
python3 -m venv $tempdir | ||
# Activate the virtualenv. | ||
source $tempdir/bin/activate | ||
# Install pip-tools in the virtualenv (pip-sync-faster needs pip-tools). | ||
pip install pip-tools | ||
# Call pip-sync-faster to install a requirements file into the temporary virtualenv. | ||
PYTHONPATH=src python3 -m pip_sync_faster /path/to/requirements.txt | ||
# When you're done testing deactivate the temporary virtualenv. | ||
deactivate | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lint,tests: pytest-mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import sys | ||
|
||
from pip_sync_faster.cli import cli | ||
|
||
sys.exit(cli()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from argparse import ArgumentParser | ||
from importlib.metadata import version | ||
from subprocess import CalledProcessError | ||
|
||
from pip_sync_faster.sync import sync | ||
|
||
|
||
def cli(_argv=None): # pylint:disable=inconsistent-return-statements | ||
parser = ArgumentParser( | ||
description="Synchronize the active venv with requirements.txt files." | ||
) | ||
parser.add_argument( | ||
"--version", action="store_true", help="show the version and exit" | ||
) | ||
parser.add_argument( | ||
"src_files", nargs="*", help="the requirements.txt files to synchronize" | ||
) | ||
|
||
args = parser.parse_known_args(_argv) | ||
|
||
if args[0].version: | ||
print(f"pip-sync-faster, version {version('pip-sync-faster')}") | ||
return | ||
|
||
try: | ||
sync(args[0].src_files) | ||
except CalledProcessError as err: | ||
return err.returncode |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import hashlib | ||
import json | ||
import sys | ||
from os import environ | ||
from os.path import abspath | ||
from pathlib import Path | ||
from subprocess import run | ||
|
||
|
||
def get_hash(path): | ||
"""Return the hash of the given file.""" | ||
hashobj = hashlib.sha512() | ||
|
||
with open(path, "rb") as file: | ||
hashobj.update(file.read()) | ||
|
||
return hashobj.hexdigest() | ||
|
||
|
||
def get_hashes(paths): | ||
"""Return a dict mapping the given files to their hashes.""" | ||
return {abspath(path): get_hash(abspath(path)) for path in paths} | ||
|
||
|
||
def sync(src_files): | ||
cached_hashes_path = Path(environ["VIRTUAL_ENV"]) / "pip_sync_faster.json" | ||
|
||
try: | ||
with open(cached_hashes_path, "r", encoding="utf-8") as handle: | ||
cached_hashes = json.load(handle) | ||
except FileNotFoundError: | ||
cached_hashes = {} | ||
|
||
hashes = get_hashes(src_files) | ||
|
||
if hashes == cached_hashes: | ||
return | ||
|
||
# The hashes did not match the cached ones. This can happen if: | ||
# | ||
# * This is the first time that pip-sync-faster has been called for this venv | ||
# * One or more of the requirements files has changed | ||
# * pip-sync-faster was called with a different set of requirements files | ||
|
||
run(["pip-sync", *sys.argv[1:]], check=True) | ||
|
||
# Replace the cached hashes file with one containing the correct hashes for | ||
# the requirements files that pip-sync-faster was called with this time. | ||
with open(cached_hashes_path, "w", encoding="utf-8") as handle: | ||
json.dump(hashes, handle) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from importlib.metadata import version | ||
from subprocess import CalledProcessError | ||
|
||
import pytest | ||
|
||
from pip_sync_faster.cli import cli | ||
|
||
|
||
def test_cli(sync): | ||
exit_code = cli(["requirements/dev.txt", "--foo", "bar"]) | ||
|
||
sync.assert_called_once_with(["requirements/dev.txt"]) | ||
assert not exit_code | ||
|
||
|
||
def test_version(capsys): | ||
exit_code = cli(["--version"]) | ||
|
||
assert ( | ||
capsys.readouterr().out.strip() | ||
== f"pip-sync-faster, version {version('pip-sync-faster')}" | ||
) | ||
assert not exit_code | ||
|
||
|
||
def test_if_pip_sync_fails(sync): | ||
sync.side_effect = CalledProcessError(23, ["pip-sync"]) | ||
|
||
exit_code = cli(["requirements/dev.txt"]) | ||
|
||
# It echoes pip-sync's exit code. | ||
assert exit_code == 23 | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def sync(mocker): | ||
return mocker.patch("pip_sync_faster.cli.sync", autospec=True) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.