Skip to content

Commit

Permalink
Install requirements into existing venv (#161)
Browse files Browse the repository at this point in the history
* Install into existing venv

* format

* Retrieve latest release

* format

* review changes

* formatting
  • Loading branch information
thomas-bc authored Aug 16, 2023
1 parent c2c4e45 commit e1b7b6a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"cookiecutter>=2.2.3",
"gcovr>=6.0",
"urllib3<2.0.0",
"requests>=2.31.0",
],
extras_require={
"dev": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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?"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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`",
)
1 change: 0 additions & 1 deletion src/fprime/util/cookiecutter_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit e1b7b6a

Please sign in to comment.