Skip to content

Scanning

modelmap.scanner

Run the providers and the tree scans and return one :class:Scan.

Order of work, which is also the order files are charged in:

  1. Each provider's stores, in the order of PROVIDERS.
  2. Paths the user named.
  3. With no paths named, a sweep of the home directory for loose models.

A directory is scanned once. A root that is the same directory as one already scanned, or lies inside one, is skipped: the ledger would keep the totals right, but every model would be listed a second time at size 0.

Options(paths=(), only=None, defaults=True, sweep=None, excludes=(), min_size=MIN_UNVERIFIED_SIZE, max_depth=None) dataclass

What to scan.

Parameters:

Name Type Description Default
paths Sequence[str]

Files or directories the user named.

()
only Sequence[str] | None

Provider names to run; None for all.

None
defaults bool

Scan the known stores.

True
sweep bool | None

Sweep the home directory for loose models. None means yes when neither paths nor providers are named.

None
excludes Sequence[str]

Glob patterns skipped in tree scans.

()
min_size int

Smallest file to list when only its extension says it is a model.

MIN_UNVERIFIED_SIZE
max_depth int | None

Levels to descend below a named path; None for all.

None

redact(scan, home)

Remove what identifies the machine and its user, in place.

The home directory becomes ~ wherever it appears: in paths, in warning and issue text, and in the names of loose records, which start with the name of their parent directory. The host name is dropped. Use before sending an inventory to someone else.

Parameters:

Name Type Description Default
scan Scan

The scan to change.

required
home str | PathLike[str]

The home directory to hide, as configured and as resolved.

required

Returns:

Type Description
Scan

The same scan.

Source code in src/modelmap/scanner.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def redact(scan: Scan, home: str | os.PathLike[str]) -> Scan:
    """Remove what identifies the machine and its user, in place.

    The home directory becomes ``~`` wherever it appears: in paths, in
    warning and issue text, and in the names of loose records, which start
    with the name of their parent directory. The host name is dropped. Use
    before sending an inventory to someone else.

    :param scan: The scan to change.
    :param home: The home directory to hide, as configured and as resolved.
    :return: The same scan.
    """
    prefixes = sorted({str(home), os.path.realpath(home)}, key=len, reverse=True)
    users = {os.path.basename(prefix) for prefix in prefixes}

    def hide(text: str) -> str:
        for prefix in prefixes:
            text = text.replace(prefix, "~")
        return text

    def hide_name(text: str) -> str:
        # "user/model", or an owner such as "generic:user/model".
        owner, colon, name = text.rpartition(":") if ":" in text.split("/")[0] else ("", "", text)
        first, slash, rest = name.partition("/")
        if slash and first in users:
            name = f"~/{rest}"
        return hide(f"{owner}{colon}{name}")

    scan.hostname = None
    for record in scan.records:
        record.path = hide(record.path)
        for entry in record.file_list:
            entry.path = hide(entry.path)
            entry.used_by = [hide_name(user) for user in entry.used_by]
        record.name = hide_name(record.name)
        record.aliases = [hide_name(a) for a in record.aliases]
        record.shared_with = [hide_name(o) for o in record.shared_with]
        record.issues = [hide(i) for i in record.issues]
    for root in scan.roots:
        root.path, root.resolved = hide(root.path), hide(root.resolved)
    for warning in scan.warnings:
        warning.path, warning.message = hide(warning.path), hide(warning.message)
    return scan

scan(ctx=None, options=None)

Scan a machine.

Parameters:

Name Type Description Default
ctx Context | None

The machine to scan; the running one by default.

None
options Options | None

What to scan; the known stores and a home sweep by default.

None

Returns:

Type Description
Scan

Everything found. If the scan is interrupted, what was found so far, with a warning.

Source code in src/modelmap/scanner.py
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
194
195
196
197
198
199
200
201
202
203
204
205
def scan(ctx: Context | None = None, options: Options | None = None) -> Scan:
    """Scan a machine.

    :param ctx: The machine to scan; the running one by default.
    :param options: What to scan; the known stores and a home sweep by
        default.
    :return: Everything found. If the scan is interrupted, what was found
        so far, with a warning.
    """
    # Each scan charges files, collects warnings and records uses afresh, so
    # one context can be scanned again (`modelmap serve` rescans).
    base = ctx or Context.current()
    ctx = dataclasses.replace(base, ledger=SizeLedger(), warnings=[], uses={})
    options = options or Options()
    run = _Run(ctx, options)
    start = time.perf_counter()
    try:
        if options.defaults:
            run.stores()
        run.paths()
        # Naming providers or paths says where to look; the sweep is for
        # the question "what is on this machine".
        wanted = not options.paths and options.only is None
        sweep = options.sweep if options.sweep is not None else wanted
        if sweep and options.defaults:
            run.sweep()
    except KeyboardInterrupt:
        ctx.warn("", "interrupted: the results are partial")
    # A file named on the command line is found again by the sweep. Keep the
    # first record of each (provider, path); the ledger charged it already.
    seen: set[tuple[str, str]] = set()
    records = []
    for record in run.records:
        key = (record.provider, os.path.realpath(record.path))
        if key not in seen:
            seen.add(key)
            records.append(record)
    finish(records)
    return Scan(
        records=records,
        roots=run.roots,
        warnings=ctx.warnings,
        elapsed_s=round(time.perf_counter() - start, 3),
        hostname=socket.gethostname(),
        tool_version=__version__,
    )