Skip to content

Commit f1295e0

Browse files
committed
Apply suggestions from code review
This commit applies suggestions from the code review in #6
1 parent 18c7394 commit f1295e0

File tree

16 files changed

+407
-79
lines changed

16 files changed

+407
-79
lines changed

.cookiecutter/includes/HACKING.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
Testing Manually
3+
----------------
4+
5+
Normally if you wanted to test a command manually in dev you'd do so through
6+
tox, for example:
7+
8+
```terminal
9+
$ tox -qe dev --run-command 'pip-sync-faster --help'
10+
usage: pip-sync-faster [-h] [-v]
11+
12+
options:
13+
-h, --help show this help message and exit
14+
-v, --version
15+
```
16+
17+
But there's a problem with running `pip-sync-faster` commands in this way: a
18+
command like `tox -e dev --run-command 'pip-sync-faster requirements.txt'` will
19+
run `pip-sync requirements.txt` and `pip-sync` will sync the
20+
current virtualenv (`.tox/dev/`) with the `requirements.txt` file. Everything
21+
in `requirements.txt` will get installed into `.tox/dev/`, which you probably
22+
don't want. Even worse everything _not_ in `requirements.txt` will get
23+
_removed_ from `.tox/dev/` including `pip-sync-faster` itself!
24+
25+
To avoid this problem run `pip-sync-faster` in a temporary virtualenv instead.
26+
This installs the contents of `requirements.txt` into the temporary venv so
27+
your `.tox/dev/` env doesn't get messed up. And it does not install
28+
`pip-sync-faster` into the temporary venv so there's no issue with `pip-sync`
29+
uninstalling `pip-sync-faster`:
30+
31+
```terminal
32+
# Make a temporary directory.
33+
tempdir=$(mktemp -d)
34+
35+
# Create a virtualenv in the temporary directory.
36+
python3 -m venv $tempdir
37+
38+
# Activate the virtualenv.
39+
source $tempdir/bin/activate
40+
41+
# Install pip-tools in the virtualenv (pip-sync-faster needs pip-tools).
42+
pip install pip-tools
43+
44+
# Call pip-sync-faster to install a requirements file into the temporary virtualenv.
45+
PYTHONPATH=src python3 -m pip_sync_faster /path/to/requirements.txt
46+
47+
# When you're done testing deactivate the temporary virtualenv.
48+
deactivate
49+
```

.cookiecutter/includes/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ If any of the given requirements files doesn't have a matching cached hash then
2727
pip-sync-faster calls pip-sync forwarding all command line arguments and
2828
options.
2929

30+
## You need to add `pip-sync-faster` to your requirements file
31+
32+
A command like `pip-sync-faster requirements.txt` will call
33+
`pip-sync requirements.txt` which will uninstall anything not in
34+
`requirements.txt` from the active venv, including `pip-sync-faster` itself!
35+
36+
You can add `pip-sync-faster` to `requirements.txt` so that it doesn't get
37+
uninstalled.
38+
39+
### Running `pip-sync-faster` directly instead
40+
41+
Alternatively as long as `pip-tools` is installed in the active venv you can
42+
run `pip-sync-faster` directly with a command like:
43+
44+
```bash
45+
PYTHONPATH=/path/to/pip-sync-faster/src python3 -m pip_sync_faster requirements.txt
46+
```
47+
48+
This doesn't rely on `pip-sync-faster` being installed so there's no issue with
49+
`pip-sync` uninstalling it.
50+
3051
## pip-sync-faster doesn't sync modified virtualenvs
3152

3253
If you modify your requirements files pip-sync-faster will notice the change

.cookiecutter/includes/tox/deps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lint,tests: pytest-mock

HACKING.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,52 @@ To change the project's formatting, linting and test dependencies:
100100
```
101101

102102
3. Commit everything to git and send a pull request
103+
104+
Testing Manually
105+
----------------
106+
107+
Normally if you wanted to test a command manually in dev you'd do so through
108+
tox, for example:
109+
110+
```terminal
111+
$ tox -qe dev --run-command 'pip-sync-faster --help'
112+
usage: pip-sync-faster [-h] [-v]
113+
114+
options:
115+
-h, --help show this help message and exit
116+
-v, --version
117+
```
118+
119+
But there's a problem with running `pip-sync-faster` commands in this way: a
120+
command like `tox -e dev --run-command 'pip-sync-faster requirements.txt'` will
121+
run `pip-sync requirements.txt` as a subprocess and `pip-sync` will sync the
122+
current virtualenv (`.tox/dev/`) with the `requirements.txt` file. Everything
123+
in `requirements.txt` will get installed into `.tox/dev/`, which you probably
124+
don't want. Even worse everything _not_ in `requirements.txt` will get
125+
_removed_ from `.tox/dev/` including `pip-sync-faster` itself!
126+
127+
To avoid this problem run `pip-sync-faster` in a temporary virtualenv instead.
128+
This installs the contents of `requirements.txt` into the temporary venv so
129+
your `.tox/dev/` env doesn't get messed up. And it does not install
130+
`pip-sync-faster` into the temporary venv so there's no issue with `pip-sync`
131+
uninstalling `pip-sync-faster`:
132+
133+
```terminal
134+
# Make a temporary directory.
135+
tempdir=$(mktemp -d)
136+
137+
# Create a virtualenv in the temporary directory.
138+
python3 -m venv $tempdir
139+
140+
# Activate the virtualenv.
141+
source $tempdir/bin/activate
142+
143+
# Install pip-tools in the virtualenv (pip-sync-faster needs pip-tools).
144+
pip install pip-tools
145+
146+
# Call pip-sync-faster to install a requirements file into the temporary virtualenv.
147+
PYTHONPATH=src python3 -m pip_sync_faster /path/to/requirements.txt
148+
149+
# When you're done testing deactivate the temporary virtualenv.
150+
deactivate
151+
```

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,34 @@ user 0m0.029s
3535
sys 0m0.008s
3636
```
3737

38-
pip-sync-faster does this by saving hashes of the given requirements files in a
38+
`pip-sync-faster` does this by saving hashes of the given requirements files in a
3939
JSON file within the virtualenv and not calling pip-sync if the hashes haven't
4040
changed.
4141
If any of the given requirements files doesn't have a matching cached hash then
4242
pip-sync-faster calls pip-sync forwarding all command line arguments and
4343
options.
4444

45+
## You need to add `pip-sync-faster` to your requirements file
46+
47+
A command like `pip-sync-faster requirements.txt` will call
48+
`pip-sync requirements.txt` which will uninstall anything not in
49+
`requirements.txt` from the active venv, including `pip-sync-faster` itself!
50+
51+
You can add `pip-sync-faster` to `requirements.txt` so that it doesn't get
52+
uninstalled.
53+
54+
### Running `pip-sync-faster` directly instead
55+
56+
Alternatively as long as `pip-tools` is installed in the active venv you can
57+
run `pip-sync-faster` directly with a command like:
58+
59+
```bash
60+
PYTHONPATH=/path/to/pip-sync-faster/src python3 -m pip_sync_faster requirements.txt
61+
```
62+
63+
This doesn't rely on `pip-sync-faster` being installed so there's no issue with
64+
`pip-sync` uninstalling it.
65+
4566
## pip-sync-faster doesn't sync modified virtualenvs
4667

4768
If you modify your requirements files pip-sync-faster will notice the change
@@ -54,4 +75,4 @@ Calling pip-sync directly in this case would re-sync your virtualenv with your
5475
requirements files, but calling pip-sync-faster won't.
5576

5677
If you can live with this limitation then you can use pip-sync-faster and save
57-
yourself a few hundred milliseconds. If not you should just use pip-sync.
78+
yourself a few hundred milliseconds. If not you should just use pip-sync.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ ignore = [
4343
branch = true
4444
parallel = true
4545
source = ["pip_sync_faster", "tests/unit"]
46+
omit = [
47+
"*/pip_sync_faster/__main__.py",
48+
]
4649

4750
[tool.coverage.paths]
4851
source = ["src", ".tox/*tests/lib/python*/site-packages"]

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where = src
2525

2626
[options.entry_points]
2727
console_scripts =
28-
pip-sync-faster = pip_sync_faster.main:entry_point
28+
pip-sync-faster = pip_sync_faster.cli:cli
2929

3030
[pycodestyle]
3131
ignore =

src/pip_sync_faster/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
3+
from pip_sync_faster.cli import cli
4+
5+
sys.exit(cli())

src/pip_sync_faster/cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from argparse import ArgumentParser
2+
from importlib.metadata import version
3+
from subprocess import CalledProcessError
4+
5+
from pip_sync_faster.sync import sync
6+
7+
8+
def cli(_argv=None): # pylint:disable=inconsistent-return-statements
9+
parser = ArgumentParser(
10+
description="Synchronize the active venv with requirements.txt files."
11+
)
12+
parser.add_argument(
13+
"--version", action="store_true", help="show the version and exit"
14+
)
15+
parser.add_argument(
16+
"src_files", nargs="*", help="the requirements.txt files to synchronize"
17+
)
18+
19+
args = parser.parse_known_args(_argv)
20+
21+
if args[0].version:
22+
print(f"pip-sync-faster, version {version('pip-sync-faster')}")
23+
return
24+
25+
try:
26+
sync(args[0].src_files)
27+
except CalledProcessError as err:
28+
return err.returncode

src/pip_sync_faster/main.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)