Skip to content

dependence.freeze

get_frozen_requirements

get_frozen_requirements(
    requirements: collections.abc.Iterable[
        str | pathlib.Path
    ] = (),
    *,
    exclude: collections.abc.Iterable[str] = (),
    exclude_recursive: collections.abc.Iterable[str] = (),
    no_version: collections.abc.Iterable[str] = (),
    dependency_order: bool = False,
    reverse: bool = False,
    depth: int | None = None,
    include_pointers: tuple[str, ...] = (),
    exclude_pointers: tuple[str, ...] = ()
) -> tuple[str, ...]

Get the (frozen) requirements for one or more specified distributions or configuration files.

Parameters:

  • requirements (collections.abc.Iterable[str | pathlib.Path], default: () ) –

    One or more requirement specifiers (for example: "requirement-name[extra-a,extra-b]" or ".[extra-a, extra-b]) and/or paths to a setup.cfg, pyproject.toml, tox.ini or requirements.txt file

  • exclude (collections.abc.Iterable[str], default: () ) –

    One or more distributions to exclude/ignore

  • exclude_recursive (collections.abc.Iterable[str], default: () ) –

    One or more distributions to exclude/ignore. Note: Excluding a distribution here excludes all requirements which would be identified through recursion.

  • no_version (collections.abc.Iterable[str], default: () ) –

    Exclude version numbers from the output (only return distribution names)

  • dependency_order (bool, default: False ) –

    Sort requirements so that dependents precede dependencies

  • depth (int | None, default: None ) –

    Depth of recursive requirement discovery

  • include_pointers (tuple[str, ...], default: () ) –

    A tuple of JSON pointers indicating elements to include (defaults to all elements). Only applies to TOML files.

  • exclude_pointers (tuple[str, ...], default: () ) –

    A tuple of JSON pointers indicating elements to exclude (defaults to no exclusions). Only applies to TOML files.

Source code in src/dependence/freeze.py
 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
105
106
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def get_frozen_requirements(  # noqa: C901
    requirements: Iterable[str | Path] = (),
    *,
    exclude: Iterable[str] = (),
    exclude_recursive: Iterable[str] = (),
    no_version: Iterable[str] = (),
    dependency_order: bool = False,
    reverse: bool = False,
    depth: int | None = None,
    include_pointers: tuple[str, ...] = (),
    exclude_pointers: tuple[str, ...] = (),
) -> tuple[str, ...]:
    """
    Get the (frozen) requirements for one or more specified distributions or
    configuration files.

    Parameters:
        requirements: One or more requirement specifiers (for example:
            "requirement-name[extra-a,extra-b]" or ".[extra-a, extra-b]) and/or
            paths to a setup.cfg, pyproject.toml, tox.ini or requirements.txt
            file
        exclude: One or more distributions to exclude/ignore
        exclude_recursive: One or more distributions to exclude/ignore.
            Note: Excluding a distribution here excludes all requirements which
            would be identified through recursion.
        no_version: Exclude version numbers from the output
            (only return distribution names)
        dependency_order: Sort requirements so that dependents
            precede dependencies
        depth: Depth of recursive requirement discovery
        include_pointers: A tuple of JSON pointers indicating elements to
            include (defaults to all elements). Only applies to TOML files.
        exclude_pointers: A tuple of JSON pointers indicating elements to
            exclude (defaults to no exclusions). Only applies to TOML files.
    """
    if isinstance(requirements, (str, Path)):
        requirements = {str(requirements)}
    else:
        requirements = set(map(str, requirements))
    if isinstance(no_version, str):
        no_version = (no_version,)
    elif not isinstance(no_version, tuple):
        no_version = tuple(no_version)
    # Separate requirement strings from requirement files
    configuration_files: MutableSet[str] = set()
    requirement_strings: MutableSet[str] = set()
    requirement: str | Path
    for requirement in requirements:
        if TYPE_CHECKING:
            assert isinstance(requirement, str)
        requirement_configuration_files: set[str] = set(
            iter_configuration_files(requirement)
        )
        if requirement_configuration_files:
            configuration_files |= requirement_configuration_files
        else:
            if requirement.startswith("setup.py"):
                raise ValueError(requirement)
            requirement_strings.add(requirement)
    frozen_requirements: Iterable[str] = iter_distinct(
        chain(
            requirement_strings,
            *map(
                partial(
                    iter_configuration_file_requirement_strings,
                    include_pointers=include_pointers,
                    exclude_pointers=exclude_pointers,
                ),
                configuration_files,
            ),
        )
    )
    frozen_requirements = tuple(frozen_requirements)
    if depth is not None:
        depth -= 1
    if (depth is None) or depth >= 0:
        frozen_requirements = _iter_frozen_requirements(
            frozen_requirements,
            exclude=set(
                chain(
                    # Exclude requirement strings which are *not*
                    # distribution names (such as editable package paths),
                    # as in these cases we are typically looking for this
                    # package's dependencies
                    (
                        set(
                            map(
                                get_requirement_string_distribution_name,
                                requirement_strings,
                            )
                        )
                        - set(map(normalize_name, requirement_strings))
                    ),
                    map(normalize_name, exclude),
                )
            ),
            exclude_recursive=set(map(normalize_name, exclude_recursive)),
            no_version=no_version,
            depth=depth,
        )
    if dependency_order:
        frozen_requirements = tuple(
            _iter_sort_dependents_last(frozen_requirements)
        )
        if not reverse:
            frozen_requirements = tuple(reversed(frozen_requirements))
    else:
        name: str
        frozen_requirements = tuple(
            sorted(
                frozen_requirements,
                key=lambda name: name.lower(),
                reverse=reverse,
            )
        )
    return frozen_requirements

freeze

freeze(
    requirements: collections.abc.Iterable[
        str | pathlib.Path
    ] = (),
    *,
    exclude: collections.abc.Iterable[str] = (),
    exclude_recursive: collections.abc.Iterable[str] = (),
    no_version: collections.abc.Iterable[str] = (),
    dependency_order: bool = False,
    reverse: bool = False,
    depth: int | None = None,
    include_pointers: tuple[str, ...] = (),
    exclude_pointers: tuple[str, ...] = ()
) -> None

Print the (frozen) requirements for one or more specified requirements or configuration files.

Parameters:

  • requirements (collections.abc.Iterable[str | pathlib.Path], default: () ) –

    One or more requirement specifiers (for example: "requirement-name[extra-a,extra-b]" or ".[extra-a, extra-b]) and/or paths to a setup.py, setup.cfg, pyproject.toml, tox.ini or requirements.txt file

  • exclude (collections.abc.Iterable[str], default: () ) –

    One or more distributions to exclude.

  • exclude_recursive (collections.abc.Iterable[str], default: () ) –

    One or more distributions to exclude. Recursive dependency discovery is also halted for these distributions, unlike those passed to exclude.

  • no_version (collections.abc.Iterable[str], default: () ) –

    Exclude version numbers from the output (only print distribution names) for package names matching any of these patterns

  • dependency_order (bool, default: False ) –

    Sort requirements so that dependents precede dependencies

  • depth (int | None, default: None ) –

    Depth of recursive requirement discovery

  • include_pointers (tuple[str, ...], default: () ) –

    If this not empty, only these TOML tables will inspected (for pyproject.toml files)

  • exclude_pointers (tuple[str, ...], default: () ) –

    If not empty, these TOML tables will not be inspected (for pyproject.toml files)

Source code in src/dependence/freeze.py
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
def freeze(
    requirements: Iterable[str | Path] = (),
    *,
    exclude: Iterable[str] = (),
    exclude_recursive: Iterable[str] = (),
    no_version: Iterable[str] = (),
    dependency_order: bool = False,
    reverse: bool = False,
    depth: int | None = None,
    include_pointers: tuple[str, ...] = (),
    exclude_pointers: tuple[str, ...] = (),
) -> None:
    """
    Print the (frozen) requirements for one or more specified requirements or
    configuration files.

    Parameters:
        requirements: One or more requirement specifiers (for example:
            "requirement-name[extra-a,extra-b]" or ".[extra-a, extra-b]) and/or
            paths to a setup.py, setup.cfg, pyproject.toml, tox.ini or
            requirements.txt file
        exclude: One or more distributions to exclude.
        exclude_recursive: One or more distributions to exclude. Recursive
            dependency discovery is also halted for these distributions,
            unlike those passed to `exclude`.
        no_version: Exclude version numbers from the output
            (only print distribution names) for package names matching any of
            these patterns
        dependency_order: Sort requirements so that dependents
            precede dependencies
        depth: Depth of recursive requirement discovery
        include_pointers: If this not empty, *only* these TOML tables will
            inspected (for pyproject.toml files)
        exclude_pointers: If not empty, these TOML tables will *not* be
            inspected (for pyproject.toml files)
    """
    print(  # noqa: T201
        "\n".join(
            get_frozen_requirements(
                requirements=requirements,
                exclude=exclude,
                exclude_recursive=exclude_recursive,
                no_version=no_version,
                dependency_order=dependency_order,
                reverse=reverse,
                depth=depth,
                include_pointers=include_pointers,
                exclude_pointers=exclude_pointers,
            )
        )
    )