Skip to content

Commit

Permalink
Add --no-kernel option
Browse files Browse the repository at this point in the history
- Refactor build_project to reduce complexity (make flake8 tests pass)
  • Loading branch information
arneso-ssb committed Jan 10, 2024
1 parent e49e426 commit 0938b58
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/ssb_project_cli/ssb_project/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def create( # noqa: C901
None,
help="The git reference to check against. Supports branches, tags and commit hashes.",
),
no_kernel: bool = typer.Option( # noqa: B008
False,
"--no-kernel",
help="Do not create a kernel after the project is created (defaults to False).",
show_default=True,
),
) -> None:
""":sparkles: Create a project locally, and optionally on GitHub with the flag --github. The project will follow SSB's best practice for development."""
if not checkout and template_git_url is STAT_TEMPLATE_REPO_URL:
Expand All @@ -96,6 +102,7 @@ def create( # noqa: C901
template_git_url,
checkout,
verify_config,
no_kernel,
)


Expand Down
65 changes: 42 additions & 23 deletions src/ssb_project_cli/ssb_project/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def build_project(
template_repo_url: str,
checkout: str | None,
verify_config: bool = True,
no_kernel: bool = False,
) -> None:
"""Installs dependencies and kernel for a given project.
Expand All @@ -42,6 +43,7 @@ def build_project(
template_repo_url: Template repository url
checkout: The git reference to check against. Supports branches, tags and commit hashes.
verify_config: Determines if gitconfig is verified.
no_kernel: Determines if a kernel shall be generated or not.
"""
if path is None:
project_directory = working_directory
Expand All @@ -57,28 +59,9 @@ def build_project(
sys.exit()

if verify_config:
try:
valid_global_git_config = kvakk_git_tools.validate_git_config()
except FileExistsError:
# If gitconfig does not exist the configuration is invalid
valid_global_git_config = False
valid_project_git_config = verify_local_config(
template_repo_url,
checkout,
cwd=str(project_root),
validate_and_fix_git_config(
template_repo_url, checkout, project_name, project_root
)
if not valid_global_git_config or not valid_project_git_config:
print(
":x:\tYour project's Git configuration does not follow SSB recommendations,\n:x:\twhich may result in sensitive data being pushed to GitHub."
)
confirm_fix_ssb_git_config(
project_name,
template_repo_url,
checkout,
project_root,
valid_global_git_config,
valid_project_git_config,
)

if running_onprem(JUPYTER_IMAGE_SPEC):
print(
Expand All @@ -94,8 +77,44 @@ def build_project(
poetry_source_remove(project_root)

poetry_install(project_root)
install_ipykernel(project_root, project_name)
ipykernel_attach_bashrc(project_name)
if not no_kernel:
install_ipykernel(project_root, project_name)
ipykernel_attach_bashrc(project_name)


def validate_and_fix_git_config(
template_repo_url: str, checkout: str | None, project_name: str, project_root: Path
) -> None:
"""Validate and fix the git config.
Args:
template_repo_url: Template repository url
checkout: The git reference to check against. Supports branches, tags and commit hashes.
project_name: The name of the project
project_root: The root directory of the project/repo.
"""
try:
valid_global_git_config = kvakk_git_tools.validate_git_config()
except FileExistsError:
# If gitconfig does not exist the configuration is invalid
valid_global_git_config = False
valid_project_git_config = verify_local_config(
template_repo_url,
checkout,
cwd=str(project_root),
)
if not valid_global_git_config or not valid_project_git_config:
print(
":x:\tYour project's Git configuration does not follow SSB recommendations,\n:x:\twhich may result in sensitive data being pushed to GitHub."
)
confirm_fix_ssb_git_config(
project_name,
template_repo_url,
checkout,
project_root,
valid_global_git_config,
valid_project_git_config,
)


def ipykernel_attach_bashrc(project_name: str) -> None:
Expand Down
3 changes: 3 additions & 0 deletions src/ssb_project_cli/ssb_project/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def create_project( # noqa: C901
template_repo_url: str,
checkout: str | None,
verify_config: bool = True,
no_kernel: bool = False,
) -> None:
"""Create an SSB-project.
Expand All @@ -51,6 +52,7 @@ def create_project( # noqa: C901
template_repo_url: The Cookiecutter template URI.
checkout: The git reference to check against. Supports branches, tags and commit hashes.
verify_config: Determines if gitconfig is verified.
no_kernel: Determines if a kernel shall be generated or not.
"""
is_memory_full()

Expand Down Expand Up @@ -105,6 +107,7 @@ def create_project( # noqa: C901
template_repo_url,
checkout,
verify_config,
no_kernel,
)

git_repo_dir = Path(working_directory.joinpath(project_name))
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/create_test/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_create_copy_called(
False,
"",
None,
False,
)
assert mock_rmtree.call_count == 0

Expand All @@ -65,8 +66,9 @@ def test_default_checkout_value_set(
False,
STAT_TEMPLATE_REPO_URL,
None,
False,
)
assert _mock_build_project.call_args[-2][-2] == STAT_TEMPLATE_DEFAULT_REFERENCE
assert _mock_build_project.call_args[-2][-3] == STAT_TEMPLATE_DEFAULT_REFERENCE

def test_rmtree_template_error(
self,
Expand All @@ -89,6 +91,7 @@ def test_rmtree_template_error(
False,
"",
None,
False,
)
assert mock_rmtree.call_count == 1
assert mock_log.call_count == 1
Expand All @@ -114,6 +117,7 @@ def test_rmtree_git_error(
False,
"",
None,
False,
)
assert mock_rmtree.call_count == 1

Expand All @@ -136,6 +140,7 @@ def test_specify_template_uri(
False,
"https://github.com/statisticsnorway/ssb-minimal-template",
None,
False,
)
assert mock_rmtree.call_count == 0

Expand Down Expand Up @@ -164,6 +169,7 @@ def test_project_is_lowercase_should_fail(
False,
"",
None,
False,
)

assert mock_print.call_args[0][0] == expected_print_message
Expand All @@ -184,6 +190,7 @@ def test_project_dir_exists(mock_path_exists: Mock) -> None:
False,
"",
None,
False,
)
assert excinfo.value.code == 1

Expand Down

0 comments on commit 0938b58

Please sign in to comment.