Skip to content

Commit

Permalink
Updates cookie-cutter to incorporate new metadata fields in the templ…
Browse files Browse the repository at this point in the history
…ate (#113)

* adds `version_display` in cookie-cutter's query (defaults to version)
    * if `version_display`==`version`, it gets removed from the final `metadata.yml` since it is optional
* adds activation of `.ospac` environment (analogous to python virtual environments)
* renames executables
  • Loading branch information
pcrespov authored Jun 6, 2024
1 parent b501eea commit 1b9c930
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"git_username": "Yourusername",
"default_docker_registry": "itisfoundation",
"release_date": "{% now 'utc', '%Y' %}",
"version": "0.1.0",
"version": "0.1.0",
"version_display": "{{ cookiecutter.version }}",
"_extensions": [
"jinja2_time.TimeExtension"
]
Expand Down
61 changes: 42 additions & 19 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@
#
# SEE https://cookiecutter.readthedocs.io/en/stable/advanced/hooks.html

# pylint: disable=missing-function-docstring
# pylint: disable=unspecified-encoding

import os
import re
import shutil
import sys
from pathlib import Path
import os
from contextlib import contextmanager
from pathlib import Path

import yaml

SELECTED_DOCKER_BASE = "{{ cookiecutter.docker_base }}"
SELECTED_GIT_REPO = "{{ cookiecutter.git_repo }}"
METADATA_PATH = (
"{{ cookiecutter._output_dir }}/{{cookiecutter.project_slug}}/.osparc/metadata.yml"
)


@contextmanager
def _context_print(msg: str):
print(msg, end="...", flush=True)
yield
print("DONE")


def create_dockerfile():
folder_name = Path("docker") / SELECTED_DOCKER_BASE.split(":")[0]
def _create_dockerfile():
folder_name = Path("docker") / SELECTED_DOCKER_BASE.split(":", maxsplit=1)[0]

# list folders
# NOTE: it needs to be a list as we delete the folders
Expand All @@ -25,7 +40,7 @@ def create_dockerfile():
shutil.rmtree(folder)


def create_ignore_listings():
def _create_ignore_listings():
# .gitignore
common_gitignore = Path("Common.gitignore")
python_gitignore = Path("Python.gitignore")
Expand Down Expand Up @@ -56,33 +71,41 @@ def create_ignore_listings():
common_dockerignore.unlink()


def create_repo_folder():
def _create_repo_folder():
if SELECTED_GIT_REPO != "github":
shutil.rmtree(".github")
if SELECTED_GIT_REPO != "gitlab":
shutil.rmtree(".gitlab")


@contextmanager
def context_print(
msg,
):
print("-", msg, end="...", flush=True)
yield
print("DONE")
def _postpro_osparc_metadata():
metadata_path = Path(METADATA_PATH)
content = metadata_path.read_text()
metadata = yaml.safe_load(content)
if metadata.get("version") == metadata.get("version_display", "UNDEFINED"):
with _context_print(
"metadata: version_display==version, removing version_display"
):
# NOTE: prefer to substitue than re-serialize with yaml to avoid risk of
# reformatting values
pattern = re.compile(r"^version_display:.*\n?", re.MULTILINE)
metadata_path.write_text(pattern.sub("", content))


def main():
print("Starting post-gen-project hook:", flush=True)
try:
with context_print("Pruning docker/ folder to selection"):
create_dockerfile()
with _context_print("Pruning docker/ folder to selection"):
_create_dockerfile()

with _context_print("Updating .gitignore and .dockerignore configs"):
_create_ignore_listings()

with context_print("Updating .gitignore and .dockerignore configs"):
create_ignore_listings()
with _context_print("Adding config for selected external repository"):
_create_repo_folder()

with context_print("Adding config for selected external repository"):
create_repo_folder()

_postpro_osparc_metadata()

except Exception as exc: # pylint: disable=broad-except
print("ERROR", exc)
Expand Down
4 changes: 2 additions & 2 deletions {{cookiecutter.project_slug}}/.osparc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SHELL := /bin/bash
REPO_DIR := $(abspath $(CURDIR)/..)

# NOTE that IMAGES variable can change when docker-compose.yml gets rebuilt. Do NOT use := !!!
IMAGES = $(shell ./bin/yq.bash eval '.services.*.image' docker-compose.yml)
IMAGES = $(shell ./bin/yq eval '.services.*.image' docker-compose.yml)

export DOCKER_REGISTRY ?= registry:5000

Expand Down Expand Up @@ -36,7 +36,7 @@ update-version:
compose: update-version ## creates docker-compose.yml
# creating compose specs
cd $(REPO_DIR) \
&& .osparc/bin/ooil.bash compose -f .osparc/docker-compose.yml
&& .osparc/bin/ooil compose -f .osparc/docker-compose.yml


.PHONY: build build-nc
Expand Down
67 changes: 67 additions & 0 deletions {{cookiecutter.project_slug}}/.osparc/bin/activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# Store the current PATH
export _OLD_OSPARC_PATH=$PATH


# Get the directory of the currently running script
BIN_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
OSPARC_ENV=$(dirname "${BIN_DIR}")


# Add .osparc/bin to the PATH
export PATH="$BIN_DIR/.osparc/bin:$PATH"


deactivate () {
# Unset the OLD_PATH variable
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_OSPARC_PATH:+_}" ] ; then
PATH="$_OLD_OSPARC_PATH"
export PATH
unset _OLD_OSPARC_PATH
fi

# The hash command must be called to get it to forget past
# commands. Without forgetting past commands the $PATH changes
# we made may not be respected
hash -r 2>/dev/null


# Removes (osparc) in prompt
if ! [ -z "${_OLD_OSPARC_PS1+_}" ] ; then
PS1="$_OLD_OSPARC_PS1"
export PS1
unset _OLD_OSPARC_PS1
fi

unset OSPARC_ENV
unset OSPARC_ENV_PROMPT
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
echo "osparc environment deactivated"
fi
}

# unset irrelevant variables
deactivate nondestructive


# Adds (osparc) in prompt
if [ "xosparc" != x ] ; then
OSPARC_ENV_PROMPT=".osparc"
else
OSPARC_ENV_PROMPT=$(basename "$OSPARC_ENV")
fi
export OSPARC_ENV_PROMPT


if [ -z "${OSPARC_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_OSPARC_PS1="${PS1-}"
PS1="(${OSPARC_ENV_PROMPT}) ${PS1-}"
export PS1
fi

# Inform the user
echo "Environment activated. To deactivate, type 'deactivate'"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WORKDIR="$(pwd)"
#
# NOTE: with --interactive --tty the command below will
# produce colors in the outputs. The problem is that
# . ooil.bash >VERSION will insert special color codes
# . ooil >VERSION will insert special color codes
# . in the VERSION file which make it unusable as a variable
# . when cat VERSION !!
#
Expand All @@ -34,7 +34,7 @@ run() {
# MAIN
#
# USAGE
# ./scripts/ooil.bash --help
# ooil --help

run "$@"
# ----------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'

# Define variables
YQ_IMAGE="mikefarah/yq"
Expand Down
1 change: 1 addition & 0 deletions {{cookiecutter.project_slug}}/.osparc/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ key: simcore/services/{%- if cookiecutter.project_type == "computational" -%}com
type: {{ cookiecutter.project_type }}
integration-version: 1.0.0
version: {{ cookiecutter.version }}
version_display: {{ cookiecutter.version_display }}
description: {{ cookiecutter.project_short_description }}
contact: {{ cookiecutter.contact_email }}
thumbnail: https://github.com/ITISFoundation/osparc-assets/blob/cb43207b6be2f4311c93cd963538d5718b41a023/assets/default-thumbnail-cookiecutter-osparc-service.png?raw=true
Expand Down
12 changes: 6 additions & 6 deletions {{cookiecutter.project_slug}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ METADATA := .osparc/metadata.yml
.PHONY: VERSION
VERSION: $(METADATA) ## generates VERSION from metadata
# updating $@ from $<
@$(OSPARC_DIR)/bin/ooil.bash get-version --metadata-file $< > $@
@$(OSPARC_DIR)/bin/ooil get-version --metadata-file $< > $@

service.cli/run: $(METADATA) ## generates run from metadata
# Updates adapter script from metadata in $<
@$(OSPARC_DIR)/bin/ooil.bash run-creator --metadata $< --runscript $@
@$(OSPARC_DIR)/bin/ooil run-creator --metadata $< --runscript $@

docker-compose.yml: $(METADATA) ## generates docker-compose
# Injects metadata from $< as labels
@$(OSPARC_DIR)/bin/ooil.bash compose --to-spec-file $@ --metadata $<
@$(OSPARC_DIR)/bin/ooil compose --to-spec-file $@ --metadata $<



Expand Down Expand Up @@ -72,15 +72,15 @@ info-build: ## displays info on the built image
# TESTS-----------------------------------------------------------------
.PHONY: test tests
test tests: ## runs validation tests
@$(OSPARC_DIR)/bin/ooil.bash test .
@$(OSPARC_DIR)/bin/ooil test .



# PUBLISHING -----------------------------------------------------------------

.PHONY: version-service-patch version-service-minor version-service-major
version-service-patch version-service-minor version-service-major: $(METADATA) ## kernel/service versioning as patch
$(OSPARC_DIR)/bin/ooil.bash bump-version --metadata-file $< --upgrade $(subst version-service-,,$@)
$(OSPARC_DIR)/bin/ooil bump-version --metadata-file $< --upgrade $(subst version-service-,,$@)
# syncing metadata upstream
@$(MAKE) VERSION

Expand Down Expand Up @@ -156,7 +156,7 @@ info: ## general info
@echo ' docker buildx : $(shell docker buildx version)'
@echo ' docker-compose : $(shell docker-compose --version)'
# exe: integration tools
@echo ' ooil version : $(shell $(OSPARC_DIR)/bin/ooil.bash --version)'
@echo ' ooil version : $(shell $(OSPARC_DIR)/bin/ooil --version)'



Expand Down

0 comments on commit 1b9c930

Please sign in to comment.