diff --git a/setup.py b/setup.py index 83c65e1d..706a7ff4 100644 --- a/setup.py +++ b/setup.py @@ -90,6 +90,7 @@ "cookiecutter>=2.2.3", "gcovr>=6.0", "urllib3<2.0.0", + "requests>=2.31.0", ], extras_require={ "dev": [ diff --git a/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/cookiecutter.json b/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/cookiecutter.json index 57aff861..108b5656 100644 --- a/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/cookiecutter.json +++ b/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/cookiecutter.json @@ -1,13 +1,8 @@ { - "__default_branch": "devel", "project_name": "MyProject", - "fprime_branch_or_tag": "{{ cookiecutter.__default_branch }}", "install_venv": ["yes", "no"], - "venv_install_path": "{% if cookiecutter.install_venv == 'yes' %}./venv{% else %}None{% endif %}", "__prompts__": { "project_name": "Project name", - "fprime_branch_or_tag": "F´ version (select branch or tag)", - "install_venv": "Install virtual environment?", - "venv_install_path": "Virtual environment install path" + "install_venv": "Install F´ development tools in current virtual environment?" } } diff --git a/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/hooks/post_gen_project.py b/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/hooks/post_gen_project.py index 59a6046e..9971f121 100644 --- a/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/hooks/post_gen_project.py +++ b/src/fprime/cookiecutter_templates/cookiecutter-fprime-project/hooks/post_gen_project.py @@ -4,56 +4,68 @@ It does the following: - Initializes a git repository - Adds F' as a submodule -- Checks out the requested branch/tag +- Checks out the latest release of F' - Installs the virtual environment if requested @author thomas-bc """ import subprocess import sys +import requests +from pathlib import Path -# Fail-safe in case user input is invalid branch/tag -DEFAULT_BRANCH = "{{ cookiecutter.__default_branch }}" +response = requests.get("https://api.github.com/repos/nasa/fprime/releases/latest") +latest_tag_name = response.json()["tag_name"] + +PRINT_VENV_WARNING = False # Add F' as a submodule subprocess.run(["git", "init"]) +print(f"[INFO] Checking out F' submodule at latest release: {latest_tag_name}") subprocess.run( [ "git", "submodule", "add", - "-b", - DEFAULT_BRANCH, + "--depth", + "1", "https://github.com/nasa/fprime.git", ] ) - +subprocess.run( + ["git", "fetch", "origin", "--depth", "1", "tag", latest_tag_name], + cwd="./fprime", + capture_output=True, +) # Checkout requested branch/tag res = subprocess.run( - ["git", "checkout", "{{cookiecutter.fprime_branch_or_tag}}"], + ["git", "checkout", latest_tag_name], cwd="./fprime", capture_output=True, ) + if res.returncode != 0: - print( - "[WARNING] Unable to checkout branch/tag: {{cookiecutter.fprime_branch_or_tag}}" - ) - print(f"[WARNING] Reverted to default branch: {DEFAULT_BRANCH}") -else: - print( - "[INFO] F' submodule checked out to branch/tag: {{cookiecutter.fprime_branch_or_tag}}" - ) + print(f"[ERROR] Unable to checkout tag: {latest_tag_name}. Exit...") + sys.exit(1) # sys.exit(1) indicates failure to cookiecutter # Install venv if requested if "{{cookiecutter.install_venv}}" == "yes": - subprocess.run([sys.executable, "-m", "venv", "{{cookiecutter.venv_install_path}}"]) - subprocess.run( - [ - "{{cookiecutter.venv_install_path}}/bin/pip", - "install", - "-r", - "fprime/requirements.txt", - ] + if sys.prefix != sys.base_prefix: + subprocess.run( + [ + Path(sys.prefix) / "bin" / "pip", + "install", + "-Ur", + Path("fprime") / "requirements.txt", + ] + ) + else: + # Print warning after the following message so users do not miss it + PRINT_VENV_WARNING = True +else: + print( + "[INFO] requirements.txt has not been installed because you did not request it.", + "Install with `pip install -Ur fprime/requirements.txt`", ) print( @@ -67,9 +79,6 @@ Get started with your F' project: --- Activate the virtual environment -- -Linux/MacOS: source venv/bin/activate - -- Generate a new component -- fprime-util new --component @@ -80,12 +89,8 @@ """ ) -if res.returncode != 0: - print( - "[WARNING] Unable to checkout branch/tag: {{cookiecutter.fprime_branch_or_tag}}" - ) - print(f"[WARNING] Reverted to default branch: {DEFAULT_BRANCH}") -else: +if PRINT_VENV_WARNING: print( - "[INFO] F' submodule checked out to branch/tag: {{cookiecutter.fprime_branch_or_tag}}" + "[WARNING] requirements.txt has not been installed because you are not running in a virtual environment.", + "Install with `pip install -Ur fprime/requirements.txt`", ) diff --git a/src/fprime/util/cookiecutter_wrapper.py b/src/fprime/util/cookiecutter_wrapper.py index 6fa129fe..ea8b6665 100644 --- a/src/fprime/util/cookiecutter_wrapper.py +++ b/src/fprime/util/cookiecutter_wrapper.py @@ -307,7 +307,6 @@ def new_project(parsed_args): file=sys.stderr, ) return 1 - print(f"[INFO] New project successfully created: {gen_path}") return 0