Skip to content

dependence.freeze

_iter_sort_dependents_last

_iter_sort_dependents_last(
    requirements: collections.abc.Iterable[str],
) -> collections.abc.Iterable[str]

Sort requirements such that dependents are first and dependencies are last.

Source code in src/dependence/freeze.py
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
65
66
67
68
69
70
71
72
73
74
def _iter_sort_dependents_last(requirements: Iterable[str]) -> Iterable[str]:
    """
    Sort requirements such that dependents are first and dependencies are last.
    """
    requirements = list(requirements)
    distribution_name: str
    distribution_requirement: dict[str, str] = {
        get_requirement_string_distribution_name(requirement): requirement
        for requirement in requirements
    }
    dependent_dependencies: dict[str, MutableSet[str]] = {
        distribution_name: get_required_distribution_names(requirement)
        for distribution_name, requirement in distribution_requirement.items()
    }
    while dependent_dependencies:
        dependent: str
        dependencies: MutableSet[str]
        item: tuple[str, MutableSet[str]]
        for dependent, dependencies in sorted(  # noqa: C414
            tuple(dependent_dependencies.items()),
            key=lambda item: item[0].lower(),
        ):

            def is_non_circular_requirement(
                dependency: str,
                dependent: str,
            ) -> bool:
                """
                Return `True` if the dependency is still among the unaccounted
                for requirements, and is not a circular reference
                """
                return (dependency in dependent_dependencies) and (
                    # Exclude interdependent distributions
                    # (circular references)
                    dependent not in dependent_dependencies[dependency]
                )

            if (not dependencies) or not any(
                map(
                    partial(is_non_circular_requirement, dependent=dependent),
                    dependencies,
                )
            ):
                yield distribution_requirement.pop(dependent)
                del dependent_dependencies[dependent]

get_frozen_requirements

get_frozen_requirements(
    requirements: collections.abc.Iterable[str] = (),
    *,
    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], 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
 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
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
def get_frozen_requirements(
    requirements: Iterable[str] = (),
    *,
    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.
    """
    # Separate requirement strings from requirement files
    if isinstance(requirements, str):
        requirements = {requirements}
    else:
        requirements = set(requirements)
    if isinstance(no_version, str):
        no_version = (no_version,)
    elif not isinstance(no_version, tuple):
        no_version = tuple(no_version)
    requirement_files: MutableSet[str] = set(
        filter(is_configuration_file, requirements)
    )
    requirement_strings: MutableSet[str] = cast(
        MutableSet[str], requirements - requirement_files
    )
    frozen_requirements: Iterable[str] = iter_distinct(
        chain(
            requirement_strings,
            *map(
                partial(
                    iter_configuration_file_requirement_strings,
                    include_pointers=include_pointers,
                    exclude_pointers=exclude_pointers,
                ),
                requirement_files,
            ),
        )
    )
    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] = (),
    *,
    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], 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/ignore

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

    One or more distributions to exclude/ignore. Note: Excluding a distribution here halts recursive discovery of requirements.

  • 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
244
245
246
247
248
249
250
251
252
253
254
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
def freeze(
    requirements: Iterable[str] = (),
    *,
    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/ignore
        exclude_recursive: One or more distributions to exclude/ignore.
            Note: Excluding a distribution here halts recursive
            discovery of requirements.
        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,
            )
        )
    )