-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipenv_lock_pre_commit.py
More file actions
85 lines (72 loc) · 2.52 KB
/
pipenv_lock_pre_commit.py
File metadata and controls
85 lines (72 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
import os
import subprocess
import sys
from argparse import ArgumentParser
from pathlib import Path
def _get_env():
env = os.environ.copy()
env["PIPENV_IGNORE_VIRTUALENVS"] = "1"
return env
def _get_root_dirs(changed_pipfiles):
return set(
Path(path).parent.resolve()
for path in changed_pipfiles
)
def requirements():
parser = ArgumentParser(prog="pipenv_lock_pre_commit:requirements")
parser.add_argument(
"--requirements-file",
help="output file path relative to Pipfile",
default="requirements.txt",
)
# fetch list of changed files passed by pre-commit
parser.add_argument("changed_pipfiles", nargs="+")
args, extra_args = parser.parse_known_args()
env = _get_env()
return_code = 0
for root_dir in _get_root_dirs(args.changed_pipfiles):
with open(root_dir / args.requirements_file, "w") as stdout:
print(
"{root_dir}: pipenv requirements {extra_args}".format(
root_dir=root_dir,
extra_args=" ".join(extra_args),
)
)
out = subprocess.run(
["pipenv", "requirements", *extra_args],
stdout=stdout,
cwd=root_dir,
env=env,
)
return_code |= out.returncode
sys.exit(return_code)
def verify():
parser = ArgumentParser(prog="pipenv_lock_pre_commit:verify")
# fetch list of changed files passed by pre-commit
parser.add_argument("changed_pipfiles", nargs="+")
args = parser.parse_args()
env = _get_env()
return_code = 0
for root_dir in _get_root_dirs(args.changed_pipfiles):
print("{0}: pipenv verify".format(root_dir))
out = subprocess.run(["pipenv", "verify"], cwd=root_dir, env=env)
return_code |= out.returncode
sys.exit(return_code)
def lock():
parser = ArgumentParser(prog="pipenv_lock_pre_commit:lock")
# fetch list of changed files passed by pre-commit
parser.add_argument("changed_pipfiles", nargs="+")
args, extra_args = parser.parse_known_args()
env = _get_env()
return_code = 0
for root_dir in _get_root_dirs(args.changed_pipfiles):
print(
"{root_dir}: pipenv lock {extra_args}".format(
root_dir=root_dir,
extra_args=" ".join(extra_args),
)
)
out = subprocess.run(["pipenv", "lock"], cwd=root_dir, env=env)
return_code |= out.returncode
sys.exit(return_code)