summaryrefslogtreecommitdiff
path: root/lib/spack/spack/util/format.py
blob: c42597a8c3f1611732098f34ba7d33badd9bf500 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from typing import Optional


def get_version_lines(version_hashes_dict: dict, url_dict: Optional[dict] = None) -> str:
    """
    Renders out a set of versions like those found in a package's
    package.py file for a given set of versions and hashes.

    Args:
        version_hashes_dict (dict): A dictionary of the form: version -> checksum.
        url_dict (dict): A dictionary of the form: version -> URL.

    Returns:
        (str): Rendered version lines.
    """
    version_lines = []

    for v, h in version_hashes_dict.items():
        expand_arg = ""

        # Extract the url for a version if url_dict is provided.
        url = ""
        if url_dict is not None and v in url_dict:
            url = url_dict[v]

        # Add expand_arg since wheels should not be expanded during stanging
        if url.endswith(".whl") or ".whl#" in url:
            expand_arg = ", expand=False"
        version_lines.append(f'    version("{v}", sha256="{h}"{expand_arg})')

    return "\n".join(version_lines)