Skip to content

build

Build command module.

build_project(path, working_directory, template_repo_url, checkout, verify_config=True, no_kernel=False)

Installs dependencies and kernel for a given project.

Parameters:

Name Type Description Default
path Path | None

Path to project

required
working_directory Path

working directory

required
template_repo_url str

Template repository url

required
checkout str | None

The git reference to check against. Supports branches, tags and commit hashes.

required
verify_config bool

Determines if gitconfig is verified.

True
no_kernel bool

Determines if a kernel shall be generated or not.

False
Source code in ssb_project_cli/ssb_project/build/build.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def build_project(
    path: Path | None,
    working_directory: Path,
    template_repo_url: str,
    checkout: str | None,
    verify_config: bool = True,
    no_kernel: bool = False,
) -> None:
    """Installs dependencies and kernel for a given project.

    Args:
        path: Path to project
        working_directory: working directory
        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
    else:
        project_directory = working_directory / path

    project_name, project_root = get_project_name_and_root_path(project_directory)

    if project_name is None or project_root is None:
        print(
            ":x:\tCould not find project root. Please run ssb-project within a project directory."
        )
        sys.exit()

    if verify_config:
        validate_and_fix_git_config(
            template_repo_url, checkout, project_name, project_root
        )

    check_and_remove_onprem_source(project_root)

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

ipykernel_attach_bashrc(project_name)

Attaches bashrc to the project kernel by modifying ipykernel files.

Parameters:

Name Type Description Default
project_name str

path to the kernel directory

required
Source code in ssb_project_cli/ssb_project/build/build.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def ipykernel_attach_bashrc(project_name: str) -> None:
    """Attaches bashrc to the project kernel by modifying ipykernel files.

    Args:
        project_name: path to the kernel directory
    """
    kernels = get_kernels_dict()
    if project_name not in kernels:
        print(
            f":x:\tCould not mount .bashrc, '{project_name}' kernel was not found'."  # noqa: B907
        )
        sys.exit(1)

    project_kernel_path = kernels[project_name]["resource_dir"]
    if not Path(project_kernel_path).exists():
        print(
            f":x:\tCould not mount .bashrc, path: '{project_kernel_path}' does not exist."  # noqa: B907
        )
        sys.exit(1)

    kernel_json_file = f"{project_kernel_path}/kernel.json"
    if not Path(kernel_json_file).exists():
        print(
            f":x:\tCould not mount .bashrc, file: '{kernel_json_file}' does not exist."  # noqa: B907
        )
        sys.exit(1)

    with open(kernel_json_file, encoding="utf-8") as f:
        content_as_json = json.loads(f.read())

    python_executable_path = _get_python_executable_path(content_as_json["argv"])
    if python_executable_path is None:
        print(
            f":x:\tCould not mount .bashrc, cannot find python executable path in {kernel_json_file}"
        )  # noqa: B907
        sys.exit(1)

    if python_executable_path.endswith("/python.sh"):
        print(
            ":warning:\t.bashrc should already been mounted in your kernel, if you are in doubt do a 'clean' followed by a 'build'"
        )  # noqa: B907
        sys.exit(0)

    start_script_path = f"{project_kernel_path}/python.sh"
    content_as_json["argv"] = [
        start_script_path,
        "-m",
        "ipykernel_launcher",
        "-f",
        "{connection_file}",
    ]

    with open(kernel_json_file, "w", encoding="utf-8") as f:
        f.write(json.dumps(content_as_json))

    _write_start_script(start_script_path, python_executable_path)

    # set rx to everyone, required for jupyterlab to get permission to call start script
    os.chmod(start_script_path, 0o555)  # noqa: S103

validate_and_fix_git_config(template_repo_url, checkout, project_name, project_root)

Validate and fix the git config.

Parameters:

Name Type Description Default
template_repo_url str

Template repository url

required
checkout str | None

The git reference to check against. Supports branches, tags and commit hashes.

required
project_name str

The name of the project

required
project_root Path

The root directory of the project/repo.

required
Source code in ssb_project_cli/ssb_project/build/build.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
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.
    """
    valid_global_git_config: bool = try_if_file_exists(
        lambda: kvakk_git_tools.validate_git_config()
    ).get_or_else(False)
    valid_project_git_files: bool = try_if_file_exists(
        lambda: kvakk_git_tools.validate_local_git_files(cwd=Path(str(project_root)))
    ).get_or_else(False)

    if not (valid_global_git_config and valid_project_git_files):

        print(
            f"""
            :x:    Your project's Git configuration does not follow SSB recommendations,
            :x:    which may result in sensitive data being pushed to GitHub.

                Git file validation status:
            {":white_check_mark:" if valid_global_git_config else ":x:"}      - Global .gitconfig file
            {":white_check_mark:" if valid_project_git_files else ":x:"}      - Project .gitignore and .gitattributes files
            """
        )
        confirm_fix_ssb_git_config(
            project_name,
            template_repo_url,
            checkout,
            project_root,
            valid_global_git_config,
            valid_project_git_files,
        )