Skip to content

Commit

Permalink
Merge branch 'feature/RESEARCH-192' of github.com:gridsingularity/gsy…
Browse files Browse the repository at this point in the history
…-e into feature/RESEARCH-192
  • Loading branch information
hannesdiedrich committed May 21, 2024
2 parents f20706c + 4db7966 commit 05d3d79
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gsy-e_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
run: |
export SOLC_BINARY=$HOME/solc/usr/bin/solc
export LD_LIBRARY_PATH=$HOME/solc/usr/lib:$LD_LIBRARY_PATH
pip install tox==3.24.4
pip install tox==4.15.0
tox -e $TOXENV --verbose
- name: Archive artifacts
Expand Down
122 changes: 58 additions & 64 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,120 +16,114 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
from pathlib import Path

from fabric.colors import blue, green, yellow
from fabric.context_managers import hide
from fabric.decorators import task, hosts
from fabric.operations import local
from fabric.tasks import execute
from fabric.utils import abort, puts
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())
captainhook_installed = "CAPTAINHOOK IDENTIFIER" in hook.read_text(encoding="utf-8")
if captainhook_installed:
puts(yellow("Removing obsolete captainhook"))
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:
puts(yellow("Configuring 'pre-commit' git hooks"))
with hide("running", "stdout"):
local("pre-commit install --overwrite")
print("Configuring 'pre-commit' git hooks")
cnx.local("pre-commit install --overwrite", hide=True)
else:
with hide("running", "stdout"):
local("pre-commit autoupdate")
cnx.local("pre-commit autoupdate", hide=True)


def _ensure_venv():
if "VIRTUAL_ENV" not in os.environ:
abort("No active virtualenv found. Please create / activate one before continuing.")
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:
with hide("running", "stdout"):
puts(yellow("Installing 'pip-tools'"), show_prefix=True)
local("pip install pip-tools")
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):
puts(blue(" - {}".format(file.name.replace(".in", ""))))
local("pip-compile --max-rounds 100 --no-emit-index-url {}{} --rebuild {}".format(
"--upgrade" if upgrade or package else "",
"-package {}".format(package) if package else "",
file.relative_to(HERE)
))
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(upgrade="", package=None):
"""Update list of requirements"""
@task(hosts=["localhost"])
def compile_requirements(_ctx, upgrade="", package=None):
"""Update list of requirements."""

if upgrade and package:
abort("Can only specify one of `upgrade` or `package`")
sys.exit("Can only specify one of `upgrade` or `package`")
if package:
puts(blue("Upgrading spec for {}".format(package)))
print(f"Upgrading spec for {package}")
elif upgrade:
puts(blue("Upgrading all package specs"))
print("Upgrading all package specs")
_pre_check()
upgrade = (upgrade.lower() in {"true", "upgrade", "1", "yes", "up"})
with hide("running", "stdout"):
puts(green("Updating requirements"), show_prefix=True)
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(default=True)
@hosts("localhost")
def sync():
"""Ensure installed packages match requirements"""
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()
with hide("running"):
puts(green("Syncing requirements to local packages"), show_prefix=True)
local(
"pip-sync {}".format(
" ".join(
str(f.relative_to(HERE))
for f in REQ_DIR.glob("*.txt")
)
)
)
local("pip install -e .")
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()
write_default_settings_file(ctx)


@task
@hosts("localhost")
def reqs():
"""'compile' then 'sync'"""
execute(compile)
execute(sync)
@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():
@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()
Loading

0 comments on commit 05d3d79

Please sign in to comment.