Skip to content

poetry

This module contains functions used to install poetry dependecies and kernels.

check_and_fix_onprem_source(project_root)

Check if running onprem and fix source in pyproject.toml if so.

Parameters:

Name Type Description Default
project_root Path

Path to the root of the project

required
Source code in ssb_project_cli/ssb_project/build/poetry.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def check_and_fix_onprem_source(project_root: Path) -> None:
    """Check if running onprem and fix source in pyproject.toml if so.

    Args:
        project_root: Path to the root of the project
    """
    if running_onprem(JUPYTER_IMAGE_SPEC):
        print(
            ":twisted_rightwards_arrows:\tDetected onprem environment, using proxy for package installation"
        )
        if poetry_source_includes_source_name(project_root):
            poetry_source_remove(project_root, lock_update=False)
        poetry_source_add(PIP_INDEX_URL, project_root)
    elif poetry_source_includes_source_name(project_root):
        print(
            ":twisted_rightwards_arrows:\tDetected non-onprem environment, removing proxy for package installation"
        )
        poetry_source_remove(project_root)

install_ipykernel(project_directory, project_name)

Installs ipykernel.

Parameters:

Name Type Description Default
project_directory Path

Path of project

required
project_name str

Name of project

required
Source code in ssb_project_cli/ssb_project/build/poetry.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def install_ipykernel(project_directory: Path, project_name: str) -> None:
    """Installs ipykernel.

    Args:
        project_directory: Path of project
        project_name: Name of project
    """
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        transient=True,
    ) as progress:
        progress.add_task(description="Installing Jupyter kernel...", total=None)
        kernel_cmd = f"poetry run python3 -m ipykernel install --user --name {project_name}".split(
            " "
        )

        execute_command(
            kernel_cmd,
            "install-ipykernel",
            f":white_check_mark:\tInstalled Jupyter Kernel ({project_name})",
            "Something went wrong while installing ipykernel.",
            project_directory,
        )

poetry_install(project_directory)

Call poetry install in project_directory.

Parameters:

Name Type Description Default
project_directory Path

Path of project

required
Source code in ssb_project_cli/ssb_project/build/poetry.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def poetry_install(project_directory: Path) -> None:
    """Call poetry install in project_directory.

    Args:
        project_directory: Path of project
    """
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        transient=True,
    ) as progress:
        progress.add_task(
            description="Installing dependencies... This may take a few minutes",
            total=None,
        )

        execute_command(
            "poetry install".split(" "),
            "poetry-install",
            ":white_check_mark:\tInstalled dependencies in the virtual environment",
            "Error: Something went wrong when installing packages with Poetry.",
            project_directory,
        )

poetry_source_add(source_url, cwd, source_name=NEXUS_SOURCE_NAME)

Add a package installation source for this project.

Parameters:

Name Type Description Default
source_url str

URL of 'simple' package API of package server

required
cwd Path

Path of project to add source to

required
source_name str

Name of source to add

NEXUS_SOURCE_NAME
Source code in ssb_project_cli/ssb_project/build/poetry.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def poetry_source_add(
    source_url: str, cwd: Path, source_name: str = NEXUS_SOURCE_NAME
) -> None:
    """Add a package installation source for this project.

    Args:
        source_url: URL of 'simple' package API of package server
        cwd: Path of project to add source to
        source_name: Name of source to add
    """
    print("Adding package installation source for poetry...")
    execute_command(
        f"poetry source add --priority=primary {source_name} {source_url}".split(" "),
        "poetry-source-add",
        "Poetry source successfully added!",
        "Failed to add poetry source.",
        cwd=cwd,
    )

    # If the lock is created off-prem, we need to refresh the lock.
    if should_update_lock_file(source_url, cwd):
        update_lock(cwd)

poetry_source_includes_source_name(cwd, source_name=NEXUS_SOURCE_NAME)

Check whether this source is already added to the project.

Parameters:

Name Type Description Default
cwd Path

Path of project to add source to

required
source_name str

Name of source to check

NEXUS_SOURCE_NAME

Returns:

Type Description
bool

True if the source exists in the list

Source code in ssb_project_cli/ssb_project/build/poetry.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def poetry_source_includes_source_name(
    cwd: Path, source_name: str = NEXUS_SOURCE_NAME
) -> bool:
    """Check whether this source is already added to the project.

    Args:
        cwd: Path of project to add source to
        source_name: Name of source to check

    Returns:
        True if the source exists in the list
    """
    result = execute_command(
        "poetry source show".split(" "),
        "poetry-source-show",
        "",
        "Error showing Poetry source.",
        cwd=cwd,
    )

    return source_name in result.stdout.decode("utf-8")

poetry_source_remove(cwd, lock_update=True, source_name=NEXUS_SOURCE_NAME)

Remove a package installation source for this project.

Parameters:

Name Type Description Default
cwd Path

Path of project to add source to

required
lock_update bool

Bool used to decide whether to run update_lock

True
source_name str

Name of source to be removed

NEXUS_SOURCE_NAME
Source code in ssb_project_cli/ssb_project/build/poetry.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def poetry_source_remove(
    cwd: Path, lock_update: bool = True, source_name: str = NEXUS_SOURCE_NAME
) -> None:
    """Remove a package installation source for this project.

    Args:
        cwd: Path of project to add source to
        lock_update: Bool used to decide whether to run update_lock
        source_name: Name of source to be removed
    """
    print("Removing Poetry source...")
    execute_command(
        f"poetry source remove {source_name}".split(" "),
        "source-remove",
        "Poetry source successfully removed!",
        "Failed to remove Poetry source.",
        cwd=cwd,
    )
    if lock_update:
        update_lock(cwd)

poetry_update_lockfile_dependencies(project_directory)

Call poetry update --lock in project_directory.

Update the lock file dependencies without installing packages.

Parameters:

Name Type Description Default
project_directory Path

Path of project

required
Source code in ssb_project_cli/ssb_project/build/poetry.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def poetry_update_lockfile_dependencies(project_directory: Path) -> None:
    """Call poetry update --lock in project_directory.

    Update the lock file dependencies without installing packages.

    Args:
        project_directory: Path of project
    """
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        transient=True,
    ) as progress:
        progress.add_task(
            description="Updating lock file dependencies... This may take some time.",
            total=None,
        )

        execute_command(
            "poetry update --lock".split(" "),
            "poetry-update-lock-deps",
            ":white_check_mark:\tUpdated lock file dependencies",
            "Error: Something went wrong when updating lock file dependencies with Poetry.",
            project_directory,
        )

should_update_lock_file(source_url, cwd)

Checks if poetry.lock exists and if nexus source is set there.

Parameters:

Name Type Description Default
source_url str

URL of 'simple' package API of package server

required
cwd Path

Path of project to add source to

required

Returns: True if source url is not set in lock file, else False.

Source code in ssb_project_cli/ssb_project/build/poetry.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def should_update_lock_file(source_url: str, cwd: Path) -> bool:
    """Checks if poetry.lock exists and if nexus source is set there.

    Args:
        source_url: URL of 'simple' package API of package server
        cwd: Path of project to add source to
    Returns:
        True if source url is not set in lock file, else False.
    """
    lock_file_path = cwd / Path("poetry.lock")
    if os.path.isfile(lock_file_path):
        with open(lock_file_path) as lock_file:
            if source_url in lock_file.read():
                return False
    else:
        return False

    return True

update_lock(cwd)

Runs poetry lock --no-update command in CWD.

Parameters:

Name Type Description Default
cwd Path

Path of project to add source to.

required
Source code in ssb_project_cli/ssb_project/build/poetry.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def update_lock(cwd: Path) -> None:
    """Runs poetry lock --no-update command in CWD.

    Args:
        cwd: Path of project to add source to.
    """
    print("Refreshing lock file...")
    execute_command(
        "poetry lock --no-update".split(" "),
        "update_lock",
        "Poetry successfully refreshed lock file!",
        "Poetry failed to refresh lock file.",
        cwd=cwd,
    )