diff --git a/cookiecutter.json b/cookiecutter.json index 5c06bc13..c866af15 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -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" ] diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index d4e238c8..a9368f91 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -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 @@ -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") @@ -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) diff --git a/{{cookiecutter.project_slug}}/.osparc/Makefile b/{{cookiecutter.project_slug}}/.osparc/Makefile index e21d95f8..57dbc757 100644 --- a/{{cookiecutter.project_slug}}/.osparc/Makefile +++ b/{{cookiecutter.project_slug}}/.osparc/Makefile @@ -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 @@ -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 diff --git a/{{cookiecutter.project_slug}}/.osparc/bin/activate b/{{cookiecutter.project_slug}}/.osparc/bin/activate new file mode 100755 index 00000000..1bcfd52f --- /dev/null +++ b/{{cookiecutter.project_slug}}/.osparc/bin/activate @@ -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'" diff --git a/{{cookiecutter.project_slug}}/.osparc/bin/ooil.bash b/{{cookiecutter.project_slug}}/.osparc/bin/ooil similarity index 91% rename from {{cookiecutter.project_slug}}/.osparc/bin/ooil.bash rename to {{cookiecutter.project_slug}}/.osparc/bin/ooil index b67ab7ac..fb85469d 100755 --- a/{{cookiecutter.project_slug}}/.osparc/bin/ooil.bash +++ b/{{cookiecutter.project_slug}}/.osparc/bin/ooil @@ -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 !! # @@ -34,7 +34,7 @@ run() { # MAIN # # USAGE -# ./scripts/ooil.bash --help +# ooil --help run "$@" # ---------------------------------------------------------------------- diff --git a/{{cookiecutter.project_slug}}/.osparc/bin/yq.bash b/{{cookiecutter.project_slug}}/.osparc/bin/yq similarity index 81% rename from {{cookiecutter.project_slug}}/.osparc/bin/yq.bash rename to {{cookiecutter.project_slug}}/.osparc/bin/yq index 43be19e0..99b23150 100755 --- a/{{cookiecutter.project_slug}}/.osparc/bin/yq.bash +++ b/{{cookiecutter.project_slug}}/.osparc/bin/yq @@ -1,4 +1,8 @@ #!/bin/bash +set -o errexit +set -o nounset +set -o pipefail +IFS=$'\n\t' # Define variables YQ_IMAGE="mikefarah/yq" diff --git a/{{cookiecutter.project_slug}}/.osparc/metadata.yml b/{{cookiecutter.project_slug}}/.osparc/metadata.yml index 3af3a0e6..2f465269 100644 --- a/{{cookiecutter.project_slug}}/.osparc/metadata.yml +++ b/{{cookiecutter.project_slug}}/.osparc/metadata.yml @@ -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 diff --git a/{{cookiecutter.project_slug}}/Makefile b/{{cookiecutter.project_slug}}/Makefile index 0cf16bed..6f2d23b5 100644 --- a/{{cookiecutter.project_slug}}/Makefile +++ b/{{cookiecutter.project_slug}}/Makefile @@ -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 $< @@ -72,7 +72,7 @@ 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 . @@ -80,7 +80,7 @@ test tests: ## runs validation tests .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 @@ -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)'