-
Notifications
You must be signed in to change notification settings - Fork 21
/
fabfile.py
129 lines (103 loc) · 4.29 KB
/
fabfile.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Copyright 2018 Grid Singularity
This file is part of Grid Singularity Exchange.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
from pathlib import Path
from fabric.connection import Connection
from fabric.tasks import task
HERE = Path().resolve()
REQ_DIR = HERE / "requirements"
cnx = Connection("localhost")
def _ensure_pre_commit():
hook_dir = Path(".git/hooks")
hook = hook_dir.joinpath("pre-commit")
captainhook_installed = False
pre_commit_installed = False
if hook.exists():
captainhook_installed = "CAPTAINHOOK IDENTIFIER" in hook.read_text(encoding="utf-8")
if captainhook_installed:
print("Removing obsolete captainhook")
checkers_dir = hook_dir.joinpath("checkers")
for file in checkers_dir.glob("*"):
file.unlink()
checkers_dir.rmdir()
if not pre_commit_installed:
print("Configuring 'pre-commit' git hooks")
cnx.local("pre-commit install --overwrite", hide=True)
else:
cnx.local("pre-commit autoupdate", hide=True)
def _ensure_venv():
if "VIRTUAL_ENV" not in os.environ:
sys.exit("No active virtualenv found. Please create / activate one before continuing.")
def _ensure_pip_tools():
try:
# pylint: disable=import-outside-toplevel,unused-import
import piptools # noqa
except ImportError:
print("Installing 'pip-tools'")
cnx.local("pip install pip-tools", hide=True)
def _pre_check():
_ensure_venv()
_ensure_pip_tools()
def _fab_compile_requirements_file(file, upgrade, package):
print(f" - {file.name.replace('.in', '')}")
upgrade_option = "--upgrade" if upgrade or package else ""
package_option = f"-package {package}" if package else ""
cnx.local(f"pip-compile --max-rounds 100 --no-emit-index-url "
f"{upgrade_option}{package_option} "
f"--rebuild {file.relative_to(HERE)}")
@task(hosts=["localhost"])
def compile_requirements(_ctx, upgrade="", package=None):
"""Update list of requirements."""
if upgrade and package:
sys.exit("Can only specify one of `upgrade` or `package`")
if package:
print(f"Upgrading spec for {package}")
elif upgrade:
print("Upgrading all package specs")
_pre_check()
upgrade = upgrade.lower() in {"true", "upgrade", "1", "yes", "up"}
print("Updating requirements")
cnx.local(f"rm -rf {REQ_DIR / 'base.txt'}")
cnx.local(f"rm -rf {REQ_DIR / 'dev.txt'}")
cnx.local(f"rm -rf {REQ_DIR / 'tests.txt'}")
_fab_compile_requirements_file(REQ_DIR / "base.in", upgrade, package)
_fab_compile_requirements_file(REQ_DIR / "dev.in", upgrade, package)
_fab_compile_requirements_file(REQ_DIR / "tests.in", upgrade, package)
@task(hosts=["localhost"], default=True)
def sync(ctx):
"""Ensure installed packages match requirements."""
_pre_check()
print("Syncing requirements to local packages")
joined_req_paths = " ".join(
str(f.relative_to(HERE))
for f in REQ_DIR.glob("*.txt")
)
cnx.local(f"pip-sync {joined_req_paths}", hide=True)
cnx.local("pip install -e .", hide=True)
_ensure_pre_commit()
write_default_settings_file(ctx)
@task(hosts=["localhost"])
def build_all(ctx):
"""Build everything, including recompiling the dependencies and installing them locally."""
compile_requirements(ctx)
sync(ctx)
@task(hosts=["localhost"])
def write_default_settings_file(_ctx):
"""Fab task that creates or updates the default settings file."""
# This lazy import has stay in order to avoid import errors when running 'fab sync'
# pylint: disable=import-outside-toplevel
from gsy_e.gsy_e_core.util import export_default_settings_to_json_file
export_default_settings_to_json_file()