thousandproblems 0.3.0__cp315-cp315-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,856 @@
1
+ """The ``thousandproblems`` command (also ``python -m thousandproblems``).
2
+
3
+ Subcommands::
4
+
5
+ fetch download problems (and the axioms they include), or print them
6
+ download download and unpack the whole TPTP library
7
+ validate check that a folder or tarball is a usable TPTP library
8
+ discover find TPTP libraries on this machine and set $TPTP
9
+ find locate problems or axiom files in a local library
10
+ stats print statistics about a problem: dialect, symbols, variables ...
11
+ search find problems by their properties, like TPTP2T on tptp.org
12
+ refresh check for a new TPTP release and clear outdated cached downloads
13
+
14
+ Commands that need a library take ``-L/--library`` (a folder, a tarball or a server URL).
15
+ Without it the folder named by ``$TPTP`` is used, and, for ``fetch`` and ``stats``, the TPTP
16
+ website if ``$TPTP`` is not set.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import argparse
22
+ import fnmatch
23
+ import json
24
+ import os
25
+ import sys
26
+ from pathlib import Path
27
+ from typing import Optional, Sequence
28
+
29
+ from . import __version__, parse_file
30
+ from .library import (DirectoryLibrary, Library, LibraryError, OnlineLibrary, ProblemNotFound, RateLimited,
31
+ TarballLibrary, open_library, problem_path)
32
+ from .nodes import ParseError
33
+
34
+ _GLOB = set("*?[")
35
+
36
+
37
+ class CommandError(Exception):
38
+ """A failure to report as ``thousandproblems: error: ...`` with exit status 1."""
39
+
40
+
41
+ # ---------------------------------------------------------------------------------
42
+ # Helpers
43
+ # ---------------------------------------------------------------------------------
44
+
45
+
46
+ def _open(source: Optional[str], *, local: bool) -> Library:
47
+ if source is None and local and not os.environ.get("TPTP"):
48
+ raise CommandError("no TPTP library given: use -L/--library PATH or set $TPTP "
49
+ "(find one with 'thousandproblems discover', or get one with 'thousandproblems download')")
50
+ try:
51
+ library = open_library(source)
52
+ except (FileNotFoundError, LibraryError) as e:
53
+ raise CommandError(str(e)) from None
54
+ if local and isinstance(library, OnlineLibrary):
55
+ raise CommandError("this command needs a local copy of the TPTP library (a folder or tarball), "
56
+ "not the website; get one with 'thousandproblems download'")
57
+ return library
58
+
59
+
60
+ def _progress(label: str, enabled: bool):
61
+ if not enabled or not sys.stderr.isatty():
62
+ return None
63
+ state = {"last": -1}
64
+
65
+ def report(done: int, total: Optional[int]) -> None:
66
+ mark = done * 100 // total if total else done // 1000
67
+ if mark != state["last"]:
68
+ state["last"] = mark
69
+ text = f"{done}/{total} ({mark}%)" if total else f"{done}"
70
+ sys.stderr.write(f"\r{label}: {text} ")
71
+ sys.stderr.flush()
72
+ if total and done >= total:
73
+ sys.stderr.write("\n")
74
+ return report
75
+
76
+
77
+ def _bytes_progress(enabled: bool):
78
+ if not enabled or not sys.stderr.isatty():
79
+ return None
80
+ state = {"last": -1}
81
+
82
+ def report(done: int, total: Optional[int]) -> None:
83
+ mb = done >> 20
84
+ if mb != state["last"]:
85
+ state["last"] = mb
86
+ text = f"{mb} / {total >> 20} MB ({done * 100 // total}%)" if total else f"{mb} MB"
87
+ sys.stderr.write(f"\rdownloading: {text} ")
88
+ sys.stderr.flush()
89
+ return report
90
+
91
+
92
+ def _expand_names(library: Library, names: Sequence[str]) -> list[str]:
93
+ """Problem names, with ``-`` read from stdin and wildcards matched against the library."""
94
+ result: list[str] = []
95
+ for name in names:
96
+ if name == "-":
97
+ result.extend(_expand_names(library, [line.split()[0] for line in sys.stdin if line.strip()]))
98
+ continue
99
+ name = name[:-2] if name.endswith(".p") else name
100
+ if _GLOB & set(name):
101
+ try:
102
+ domain = name[:3] if len(name) >= 3 and not (_GLOB & set(name[:3])) else None
103
+ matches = [n for n in library.problem_names(domain) if fnmatch.fnmatchcase(n, name)]
104
+ except NotImplementedError:
105
+ raise CommandError(f"wildcards ({name!r}) need a local library; the website cannot be listed") from None
106
+ if not matches:
107
+ raise CommandError(f"no problem matches {name!r} in {library.label}")
108
+ result.extend(matches)
109
+ else:
110
+ result.append(name)
111
+ return list(dict.fromkeys(result))
112
+
113
+
114
+ def _location(library: Library, rel: str) -> str:
115
+ if isinstance(library, DirectoryLibrary):
116
+ return str(library.root / rel)
117
+ if isinstance(library, TarballLibrary):
118
+ return f"{library.path}:{rel}"
119
+ return f"{library.label}/{rel}"
120
+
121
+
122
+ # ---------------------------------------------------------------------------------
123
+ # fetch
124
+ # ---------------------------------------------------------------------------------
125
+
126
+
127
+ def cmd_fetch(args) -> int:
128
+ from .download import save_files
129
+
130
+ library = _open(args.library, local=False)
131
+ with library:
132
+ names = _expand_names(library, args.names)
133
+ if not names:
134
+ raise CommandError("no problem names given")
135
+ for name in names:
136
+ try:
137
+ if args.output is None:
138
+ if args.expand:
139
+ parsed = library.problem(name, expand_includes=True)
140
+ sys.stdout.write("\n".join(str(f) for f in parsed.formulae) + "\n")
141
+ else:
142
+ sys.stdout.write(library.problem_text(name))
143
+ continue
144
+ files = library.problem_files(name) if not args.no_axioms else \
145
+ {problem_path(name): library.problem_text(name)}
146
+ written = save_files(files, args.output, overwrite=not args.keep_existing)
147
+ if not args.quiet:
148
+ for path in written:
149
+ print(path)
150
+ except ValueError as e: # bad name (ParseError is a ValueError too)
151
+ raise CommandError(str(e)) from None
152
+ return 0
153
+
154
+
155
+ # ---------------------------------------------------------------------------------
156
+ # download
157
+ # ---------------------------------------------------------------------------------
158
+
159
+
160
+ def cmd_download(args) -> int:
161
+ from . import download
162
+ from .validate import validate_library
163
+
164
+ if args.list:
165
+ current = download._versions(download.DISTRIBUTION_URL)
166
+ archived = download._versions(download.ARCHIVE_URL)
167
+ for v in sorted(set(current) | set(archived), key=lambda v: tuple(map(int, v.split(".")))):
168
+ print(f"{v}{' (current)' if v in current else ''}")
169
+ return 0
170
+ url = args.url or download.release_url(args.version)
171
+ if not args.quiet:
172
+ print(f"downloading {url} into {Path(args.output).resolve()}", file=sys.stderr)
173
+ result = download.download_library(args.output, url=url, extract=not args.no_extract,
174
+ keep_tarball=args.keep_tarball,
175
+ progress=_bytes_progress(not args.quiet))
176
+ if not args.quiet and sys.stderr.isatty():
177
+ sys.stderr.write("\n")
178
+ if args.no_extract:
179
+ print(result)
180
+ return 0
181
+ report = validate_library(result)
182
+ if not args.version and not args.url: # the latest release: as good as a check
183
+ from . import updates
184
+ import time
185
+ state = updates.load_state()
186
+ state.update(last_check=time.time(), latest_version=(report.version or "").lstrip("v") or None)
187
+ state.pop("quiet_until", None)
188
+ updates.save_state(state)
189
+ if not args.quiet:
190
+ print(report.summary(), file=sys.stderr)
191
+ print(result)
192
+ if report.ok and not args.quiet:
193
+ print(f"\nTo use it by default:\n export TPTP={result.resolve()}", file=sys.stderr)
194
+ return 0 if report.ok else 1
195
+
196
+
197
+ # ---------------------------------------------------------------------------------
198
+ # validate
199
+ # ---------------------------------------------------------------------------------
200
+
201
+
202
+ def cmd_validate(args) -> int:
203
+ from .validate import validate_library
204
+
205
+ source = args.path or args.library or os.environ.get("TPTP")
206
+ if not source:
207
+ raise CommandError("give the library folder or tarball to check (or set $TPTP)")
208
+ report = validate_library(source, check_includes=args.includes, parse=args.parse,
209
+ progress=_progress("checking", not args.json))
210
+ if args.json:
211
+ print(json.dumps({"source": report.source, "ok": report.ok, "version": report.version,
212
+ "domains": report.domains, "problems": report.problems, "axioms": report.axioms,
213
+ "errors": report.errors, "warnings": report.warnings}, indent=2))
214
+ else:
215
+ print(report.summary())
216
+ failed = not report.ok or (args.strict and report.warnings)
217
+ return 1 if failed else 0
218
+
219
+
220
+ # ---------------------------------------------------------------------------------
221
+ # find
222
+ # ---------------------------------------------------------------------------------
223
+
224
+
225
+ def cmd_find(args) -> int:
226
+ library = _open(args.library, local=True)
227
+ missing = 0
228
+
229
+ def exists(rel: str) -> bool:
230
+ if isinstance(library, DirectoryLibrary):
231
+ return (library.root / rel).is_file()
232
+ return library.read_optional(rel) is not None
233
+
234
+ def show(rel: str) -> None:
235
+ print(rel if args.relative else _location(library, rel))
236
+
237
+ with library:
238
+ for name in args.names:
239
+ if name.endswith(".ax"): # an axiom file, SET001-0.ax
240
+ if exists(f"Axioms/{name}"):
241
+ show(f"Axioms/{name}")
242
+ continue
243
+ print(f"thousandproblems: Axioms/{name} is not in {library.label}", file=sys.stderr)
244
+ missing += 1
245
+ continue
246
+ if _GLOB & set(name):
247
+ try:
248
+ for problem in _expand_names(library, [name]):
249
+ show(problem_path(problem))
250
+ except CommandError as e:
251
+ print(f"thousandproblems: {e}", file=sys.stderr)
252
+ missing += 1
253
+ continue
254
+ try:
255
+ rel = problem_path(name)
256
+ except ValueError as e:
257
+ print(f"thousandproblems: {e}", file=sys.stderr)
258
+ missing += 1
259
+ continue
260
+ if exists(rel):
261
+ show(rel)
262
+ elif exists(f"Axioms/{name.removesuffix('.p')}.ax"): # SET001-0 names an axiom file
263
+ show(f"Axioms/{name.removesuffix('.p')}.ax")
264
+ else:
265
+ print(f"thousandproblems: {name} is not in {library.label}", file=sys.stderr)
266
+ missing += 1
267
+ return 1 if missing else 0
268
+
269
+
270
+ # ---------------------------------------------------------------------------------
271
+ # stats
272
+ # ---------------------------------------------------------------------------------
273
+
274
+
275
+ def _load_for_stats(args):
276
+ target = args.target
277
+ path = Path(target)
278
+ if path.is_file():
279
+ root = args.library or os.environ.get("TPTP")
280
+ try:
281
+ return parse_file(path, expand_includes=args.expand, tptp_root=root), str(path), None
282
+ except ParseError as e:
283
+ if not args.expand or "not found" not in e.message:
284
+ raise
285
+ print(f"thousandproblems: warning: {e.message}; statistics cover this file only "
286
+ "(give -L/--library or set $TPTP to resolve includes)", file=sys.stderr)
287
+ return parse_file(path), str(path), None
288
+ library = _open(args.library, local=False)
289
+ with library:
290
+ try:
291
+ parsed = library.problem(target, expand_includes=args.expand)
292
+ except ValueError as e:
293
+ if isinstance(e, ParseError):
294
+ raise
295
+ raise CommandError(f"{target!r} is neither a file nor a TPTP problem name") from None
296
+ return parsed, problem_path(target), library
297
+
298
+
299
+ def _fmt_details(stat) -> str:
300
+ parts = []
301
+ for key, value in stat.details.items():
302
+ text = f"{value[0]}-{value[1]}" if isinstance(value, tuple) else f"{value}"
303
+ parts.append(f"{text} {key}")
304
+ return f"({'; '.join(parts)})" if parts else ""
305
+
306
+
307
+ def cmd_stats(args) -> int:
308
+ from .stats import DOMAINS, SPC, compute_stats, syntax_stats
309
+
310
+ parsed, where, library = _load_for_stats(args)
311
+ header = parsed.header
312
+ declared = syntax_stats(header)
313
+ computed = compute_stats(parsed)
314
+ spc = SPC(header.spc) if header and header.spc else None
315
+
316
+ if args.json:
317
+ print(json.dumps({
318
+ "file": where,
319
+ "problem": header.problem if header else None,
320
+ "domain": (header.problem or "")[:3] if header and header.problem else None,
321
+ "status": header.status if header else None,
322
+ "rating": header.rating if header else None,
323
+ "spc": str(spc) if spc else None,
324
+ "form": spc.form if spc else None,
325
+ "declared": {k: {"value": s.value, **{d: (list(v) if isinstance(v, tuple) else v)
326
+ for d, v in s.details.items()}}
327
+ for k, s in declared.items()},
328
+ "computed": computed.as_dict(),
329
+ "expanded": args.expand,
330
+ }, indent=2))
331
+ return 0
332
+
333
+ rows: list[tuple[str, str]] = []
334
+ name = header.problem if header and header.problem else Path(where).stem
335
+ rows.append(("Problem", f"{name} ({where})"))
336
+ if header:
337
+ domain = name[:3]
338
+ if header.get("Domain") or domain in DOMAINS:
339
+ rows.append(("Domain", f"{domain} {header.get('Domain') or DOMAINS.get(domain, '')}".rstrip()))
340
+ if header.get("Problem"):
341
+ rows.append(("Title", header.get("Problem")))
342
+ if header.status:
343
+ rows.append(("Status", header.status))
344
+ if header.ratings:
345
+ rating, version = header.ratings[0]
346
+ rows.append(("Rating", f"{rating:.2f}" + (f" (v{version})" if version else "")))
347
+ if spc:
348
+ rows.append(("SPC", f"{spc} ({spc.describe()})"))
349
+ dialect = ", ".join(computed.dialects) or "-"
350
+ if spc:
351
+ dialect += f" [{spc.form}]"
352
+ if computed.polymorphic:
353
+ dialect += " polymorphic"
354
+ rows.append(("Dialect", dialect))
355
+ if computed.includes:
356
+ rows.append(("Includes", ", ".join(computed.includes) + " (not expanded)"))
357
+
358
+ roles = ", ".join(f"{n} {r}" for r, n in sorted(computed.roles.items(), key=lambda x: -x[1]))
359
+ rows.append(("Formulae", f"{computed.formulae}" + (f" ({roles})" if roles else "")))
360
+ if computed.unit_formulae is not None:
361
+ rows.append(("Unit formulae", str(computed.unit_formulae)))
362
+ if computed.type_declarations:
363
+ rows.append(("Type decls", f"{computed.type_declarations}"
364
+ + (f" (user types: {', '.join(computed.user_types)})" if computed.user_types else "")))
365
+ if computed.atoms is not None:
366
+ rows.append(("Atoms", f"{computed.atoms} ({computed.equality_atoms} equality)"))
367
+ elif computed.equality_atoms:
368
+ rows.append(("Equalities", str(computed.equality_atoms)))
369
+
370
+ def symbol_line(symbols: tuple[str, ...]) -> str:
371
+ if not symbols:
372
+ return "0"
373
+ arities = sorted(int(s.rsplit("/", 1)[1]) for s in symbols)
374
+ text = f"{len(symbols)} (arity {arities[0]}-{arities[-1]})"
375
+ if args.symbols:
376
+ text += "\n" + _wrap(symbols)
377
+ return text
378
+
379
+ rows.append(("Symbols", symbol_line(computed.symbols)))
380
+ if computed.atoms is not None:
381
+ rows.append((" predicates", symbol_line(computed.predicates)))
382
+ rows.append((" functions", symbol_line(computed.functions)))
383
+ rows.append((" constants", symbol_line(computed.constants)))
384
+ if computed.defined_symbols:
385
+ rows.append((" interpreted", f"{len(computed.defined_symbols)}"
386
+ + (f"\n{_wrap(computed.defined_symbols)}" if args.symbols else
387
+ f" ({', '.join(computed.defined_symbols[:8])}{' ...' if len(computed.defined_symbols) > 8 else ''})")))
388
+ binders = ", ".join(f"{n} {q}" for q, n in computed.bound_variables.items())
389
+ variables = f"{computed.variables}" + (f" (bound: {binders})" if binders else "")
390
+ if "cnf" in computed.dialects:
391
+ variables += f" ({computed.singleton_variables} singleton)"
392
+ rows.append(("Variables", variables))
393
+ if not declared: # the header's figures, when present, are the authoritative ones
394
+ rows.append(("Max depth", f"formula {computed.max_formula_depth}, term {computed.max_term_depth}"))
395
+
396
+ width = max(len(k) for k, _ in rows) + 2
397
+ for key, value in rows:
398
+ lines = value.split("\n")
399
+ print(f"{key:<{width}}: {lines[0]}")
400
+ for more in lines[1:]:
401
+ print(f"{'':<{width}} {more}")
402
+ if declared:
403
+ print("\nDeclared in the header (% Syntax), covering the problem with its includes:")
404
+ label_width = max(len(k) for k in declared) + 2
405
+ for key, stat in declared.items():
406
+ print(f" {key.replace('_', ' '):<{label_width}}: {stat.value:>6} {_fmt_details(stat)}")
407
+ if computed.includes and not args.expand:
408
+ print("\nNote: the computed figures cover this file only; its includes were not read.")
409
+ return 0
410
+
411
+
412
+ def _wrap(items: Sequence[str], width: int = 88, indent: str = " ") -> str:
413
+ lines, line = [], indent
414
+ for item in items:
415
+ if len(line) + len(item) + 1 > width and line.strip():
416
+ lines.append(line.rstrip())
417
+ line = indent
418
+ line += item + " "
419
+ if line.strip():
420
+ lines.append(line.rstrip())
421
+ return "\n".join(lines)
422
+
423
+
424
+ # ---------------------------------------------------------------------------------
425
+ # search
426
+ # ---------------------------------------------------------------------------------
427
+
428
+
429
+ def cmd_search(args) -> int:
430
+ from .search import CRITERIA, Query, build_index
431
+
432
+ query = Query()
433
+ try:
434
+ for name in CRITERIA:
435
+ for value in getattr(args, name) or ():
436
+ query.add(name, value)
437
+ except ValueError as e:
438
+ raise CommandError(str(e)) from None
439
+ source = args.library
440
+ if source is None and not os.environ.get("TPTP"):
441
+ raise CommandError("no TPTP library given: use -L/--library PATH or set $TPTP "
442
+ "(find one with 'thousandproblems discover', or get one with 'thousandproblems download')")
443
+ try:
444
+ records = build_index(source, refresh=args.refresh,
445
+ progress=_progress("indexing problem headers", not args.quiet))
446
+ except (FileNotFoundError, LibraryError) as e:
447
+ raise CommandError(str(e)) from None
448
+ if not args.quiet and sys.stderr.isatty():
449
+ sys.stderr.write("\r" + " " * 60 + "\r")
450
+ hits = [r for r in records if query.matches(r)]
451
+ if args.limit is not None:
452
+ shown = hits[:args.limit]
453
+ else:
454
+ shown = hits
455
+
456
+ if args.count:
457
+ print(len(hits))
458
+ return 0 if hits else 1
459
+ if args.format == "json":
460
+ print(json.dumps([r.as_dict() for r in shown], indent=2))
461
+ elif args.format == "paths":
462
+ library = open_library(source)
463
+ with library:
464
+ for r in shown:
465
+ print(_location(library, r.path))
466
+ elif args.format == "table":
467
+ print(f"{'Problem':<18} {'Form':<4} {'Status':<4} {'Rating':>6} {'Formulae':>8} {'Atoms':>8} "
468
+ f"{'Symbols':>7} {'Vars':>6} SPC")
469
+ for r in shown:
470
+ symbols = r.stat("symbols")
471
+ if symbols is None and (r.stat("predicates") is not None or r.stat("functors") is not None):
472
+ symbols = (r.stat("predicates") or 0) + (r.stat("functors") or 0)
473
+ cell = lambda v, w: f"{'-' if v is None else v:>{w}}" # noqa: E731
474
+ rating = "-" if r.rating is None else f"{r.rating:.2f}"
475
+ print(f"{r.name:<18} {r.form or '-':<4} {r.status_code or '-':<4} {rating:>6} "
476
+ f"{cell(r.stat('formulae', 'clauses'), 8)} {cell(r.stat('atoms', 'literals'), 8)} "
477
+ f"{cell(symbols, 7)} {cell(r.stat('variables'), 6)} {r.spc or ''}")
478
+ else:
479
+ for r in shown:
480
+ print(r.name)
481
+ if not args.quiet:
482
+ more = f" (showing {len(shown)})" if len(shown) != len(hits) else ""
483
+ print(f"{len(hits)} of {len(records)} problems match{more}", file=sys.stderr)
484
+ return 0 if hits else 1
485
+
486
+
487
+ # ---------------------------------------------------------------------------------
488
+ # refresh, and the reminder to run it
489
+ # ---------------------------------------------------------------------------------
490
+
491
+
492
+ def _local_source(args) -> Optional[str]:
493
+ source = getattr(args, "library", None) or os.environ.get("TPTP")
494
+ if source and not source.startswith(("http://", "https://")) and Path(source).exists():
495
+ return source
496
+ return None
497
+
498
+
499
+ def cmd_refresh(args) -> int:
500
+ from .updates import check_for_updates
501
+
502
+ local = _local_source(args)
503
+ report = check_for_updates(local)
504
+ if args.index and local:
505
+ from .search import build_index
506
+ records = build_index(local, refresh=True, progress=_progress("indexing problem headers", not args.json))
507
+ if sys.stderr.isatty() and not args.json:
508
+ sys.stderr.write("\r" + " " * 60 + "\r")
509
+ report.notes.append(f"search index : rebuilt ({len(records)} problems)")
510
+ if args.json:
511
+ print(json.dumps({"latest": report.latest, "cache_version": report.cache_version,
512
+ "cache_cleared": report.cache_cleared, "local_path": report.local_path,
513
+ "local_version": report.local_version, "local_outdated": report.local_outdated},
514
+ indent=2))
515
+ else:
516
+ print(report.summary())
517
+ return 0
518
+
519
+
520
+ _REMINDER_SKIPPED = {"refresh", "download"}
521
+
522
+
523
+ def _remind(args) -> None:
524
+ """Every 30 days, offer to check for a new TPTP release (or warn, without a terminal)."""
525
+ from . import updates
526
+
527
+ if args.command in _REMINDER_SKIPPED:
528
+ return
529
+ days = updates.check_due()
530
+ if days is None:
531
+ return
532
+ message = f"You have not checked for a new TPTP library version in {days} days"
533
+ interactive = sys.stdin is not None and sys.stdin.isatty() and sys.stderr.isatty()
534
+ if not interactive:
535
+ print(f"thousandproblems: {message.lower()[0]}{message[1:]}; run 'thousandproblems refresh' "
536
+ "(or set THOUSANDPROBLEMS_NO_UPDATE_CHECK=1)", file=sys.stderr)
537
+ updates.postpone()
538
+ return
539
+ while True:
540
+ sys.stderr.write(f"{message}, check now? [y]es / [n]o / [s]nooze for thirty days: ")
541
+ sys.stderr.flush()
542
+ answer = sys.stdin.readline()
543
+ if not answer: # end of input
544
+ answer = "n"
545
+ answer = answer.strip().lower()
546
+ if answer in ("y", "yes"):
547
+ try:
548
+ report = updates.check_for_updates(_local_source(args))
549
+ except LibraryError as e:
550
+ print(f"thousandproblems: could not check: {e}", file=sys.stderr)
551
+ updates.postpone()
552
+ else:
553
+ print(report.summary(), file=sys.stderr)
554
+ return
555
+ if answer in ("n", "no"):
556
+ updates.postpone() # ask again tomorrow, not on every command
557
+ return
558
+ if answer in ("s", "snooze"):
559
+ updates.snooze()
560
+ return
561
+
562
+
563
+ # ---------------------------------------------------------------------------------
564
+ # discover
565
+ # ---------------------------------------------------------------------------------
566
+
567
+
568
+ def _interactive() -> bool:
569
+ return sys.stdin is not None and sys.stdin.isatty() and sys.stderr.isatty()
570
+
571
+
572
+ def _ask(question: str, default: str = "n") -> str:
573
+ """Ask on the terminal; the answer, lower-cased, or ``default`` on an empty line or end of input."""
574
+ sys.stderr.write(question)
575
+ sys.stderr.flush()
576
+ answer = sys.stdin.readline()
577
+ return (answer.strip().lower() or default) if answer else default
578
+
579
+
580
+ def cmd_discover(args) -> int:
581
+ from . import discover
582
+
583
+ shell = args.shell or discover.detect_shell()
584
+ current = os.environ.get("TPTP")
585
+ found = discover.find_libraries(args.dirs or None, depth=args.depth)
586
+ usable = [f for f in found if f.valid and not f.packed]
587
+
588
+ if args.json:
589
+ chosen = usable[0] if usable else None
590
+ print(json.dumps({
591
+ "current": current,
592
+ "found": [{"path": str(f.path), "version": f.version, "valid": f.valid, "packed": f.packed,
593
+ "problems": f.problems, "errors": f.errors} for f in found],
594
+ "chosen": str(chosen.path) if chosen else None,
595
+ "command": discover.export_command(chosen.path, shell) if chosen else None,
596
+ }, indent=2))
597
+ return 0 if usable else 1
598
+
599
+ where = ", ".join(args.dirs) if args.dirs else "the usual places (here, your home folder, /opt ...)"
600
+ if not found:
601
+ print(f"No TPTP library found in {where}.")
602
+ print("Search somewhere else with 'thousandproblems discover DIR', or get one with "
603
+ "'thousandproblems download -o DIR'.")
604
+ return 1
605
+
606
+ print(f"TPTP libraries found in {where}:")
607
+ for number, f in enumerate(found, 1):
608
+ version = f"v{f.version}" if f.version else "v?"
609
+ if f.packed:
610
+ state = "tarball: unpack it to use it"
611
+ elif f.valid:
612
+ state = f"{f.problems} problems" + ("" if f.complete else ", partial: only Problems/ and Axioms/")
613
+ else:
614
+ state = f"INVALID: {f.errors[0] if f.errors else 'see thousandproblems validate'}"
615
+ same = " <- current $TPTP" if current and _same_path(current, f.path) else ""
616
+ print(f" {number:>2}. {version:<9} {f.path} ({state}){same}")
617
+ if not usable:
618
+ print("\nNone of them can be used as $TPTP. 'thousandproblems validate PATH' explains what is wrong.")
619
+ return 1
620
+
621
+ chosen = usable[0]
622
+ if len(usable) > 1 and _interactive() and not args.yes:
623
+ numbers = {i: f for i, f in enumerate(found, 1) if f in usable}
624
+ default = next(i for i, f in numbers.items() if f is chosen)
625
+ while True:
626
+ answer = _ask(f"Use which one? [{default}] ", str(default))
627
+ if answer.isdigit() and int(answer) in numbers:
628
+ chosen = numbers[int(answer)]
629
+ break
630
+ print(f" choose one of {', '.join(map(str, numbers))}", file=sys.stderr)
631
+ elif len(usable) > 1:
632
+ print(f"\nUsing the first: {chosen.path}")
633
+
634
+ already = current is not None and _same_path(current, chosen.path)
635
+ command = discover.export_command(chosen.path, shell)
636
+ if already:
637
+ print(f"\n$TPTP is already set to it.")
638
+ else:
639
+ print(f"\nTo use it in this terminal, run:\n\n {command}\n")
640
+
641
+ if shell in ("powershell", "cmd"):
642
+ return _set_windows(args, chosen.path)
643
+ target = discover.startup_file(shell)
644
+ text = target.read_text(encoding="utf-8") if target and target.exists() else ""
645
+ if discover.MARK_BEGIN in text and command in text:
646
+ print(f"{target} already sets it for new terminals.")
647
+ return 0
648
+ if not args.set:
649
+ if not _interactive() or args.yes:
650
+ print(f"Add that line to {target} to make it permanent, or run "
651
+ f"'thousandproblems discover --set'.")
652
+ return 0
653
+ if _ask(f"Add it to {target} so that new terminals use it? [y/N] ") not in ("y", "yes"):
654
+ return 0
655
+ elif not args.yes:
656
+ if not _interactive():
657
+ raise CommandError(f"--set would change {target}; add --yes to allow that without asking")
658
+ if _ask(f"Add it to {target}? [y/N] ") not in ("y", "yes"):
659
+ return 0
660
+ discover.install(chosen.path, shell, target)
661
+ replaced = discover.MARK_BEGIN in text
662
+ print(f"{'Updated' if replaced else 'Added'} in {target}:\n\n"
663
+ f" {discover.MARK_BEGIN}\n {command}\n {discover.MARK_END}\n")
664
+ for line in discover.other_settings(text):
665
+ print(f"warning: {target} also has '{line}'; the block above comes later, so it wins")
666
+ print(f"New terminals will use it. For this one, run: {command}"
667
+ if not already else "New terminals will use it.")
668
+ return 0
669
+
670
+
671
+ def _set_windows(args, path) -> int:
672
+ import subprocess
673
+
674
+ command = ["setx", "TPTP", str(path)]
675
+ if not args.set:
676
+ print(f"To make it permanent, run: setx TPTP \"{path}\" (or 'thousandproblems discover --set')")
677
+ return 0
678
+ if not args.yes:
679
+ if not _interactive():
680
+ raise CommandError("--set would change your user environment; add --yes to allow that without asking")
681
+ if _ask("Set TPTP for your user account with setx? [y/N] ") not in ("y", "yes"):
682
+ return 0
683
+ result = subprocess.run(command, capture_output=True, text=True)
684
+ if result.returncode != 0:
685
+ raise CommandError(f"setx failed: {(result.stderr or result.stdout).strip()}")
686
+ print("Set for your user account. Programs started from now on will see it.")
687
+ return 0
688
+
689
+
690
+ def _same_path(a, b) -> bool:
691
+ try:
692
+ return os.path.samefile(a, b)
693
+ except OSError:
694
+ return False
695
+
696
+
697
+ # ---------------------------------------------------------------------------------
698
+ # Argument parsing
699
+ # ---------------------------------------------------------------------------------
700
+
701
+
702
+ def build_parser() -> argparse.ArgumentParser:
703
+ from .search import CRITERIA
704
+
705
+ library_opt = argparse.ArgumentParser(add_help=False)
706
+ library_opt.add_argument("-L", "--library", metavar="SOURCE",
707
+ help="TPTP library: a folder, a tarball, or a server URL (default: $TPTP)")
708
+
709
+ parser = argparse.ArgumentParser(
710
+ prog="thousandproblems",
711
+ description="Fetch, find, check, describe and search TPTP problems.",
712
+ epilog="Run 'thousandproblems COMMAND --help' for the options of each command.")
713
+ parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
714
+ sub = parser.add_subparsers(dest="command", metavar="COMMAND", required=True)
715
+
716
+ p = sub.add_parser("fetch", parents=[library_opt], help="download or print problems",
717
+ description="Download problems together with the axiom files they include, or print "
718
+ "them. Without -L and $TPTP, problems come from the TPTP website, which "
719
+ "allows only a few requests a minute (downloads are cached).")
720
+ p.add_argument("names", nargs="+", metavar="NAME",
721
+ help="problem names such as SET001-1; wildcards (SET00*) need a local library; "
722
+ "'-' reads names from stdin, e.g. from 'search'")
723
+ p.add_argument("-o", "--output", metavar="DIR",
724
+ help="save into DIR in the standard layout (Problems/..., Axioms/...); "
725
+ "without it the problem is printed")
726
+ p.add_argument("--expand", action="store_true", help="when printing, replace includes by their formulae")
727
+ p.add_argument("--no-axioms", action="store_true", help="with -o, save only the problem files")
728
+ p.add_argument("--keep-existing", action="store_true", help="with -o, do not overwrite files")
729
+ p.add_argument("-q", "--quiet", action="store_true", help="do not list the files written")
730
+ p.set_defaults(func=cmd_fetch)
731
+
732
+ p = sub.add_parser("download", help="download the whole TPTP library",
733
+ description="Download a TPTP release (about 1 GB) from tptp.org and unpack it.")
734
+ p.add_argument("-o", "--output", default=".", metavar="DIR", help="where to put it (default: here)")
735
+ p.add_argument("--version", dest="version", metavar="X.Y.Z", help="release to get (default: the latest)")
736
+ p.add_argument("--list", action="store_true", help="list the releases available and exit")
737
+ p.add_argument("--no-extract", action="store_true", help="keep the tarball packed")
738
+ p.add_argument("--keep-tarball", action="store_true", help="keep the tarball after unpacking it")
739
+ p.add_argument("--url", help="download from this URL instead")
740
+ p.add_argument("-q", "--quiet", action="store_true")
741
+ p.set_defaults(func=cmd_download)
742
+
743
+ p = sub.add_parser("refresh", parents=[library_opt], help="check for a new TPTP release",
744
+ description="Look up the latest TPTP release on tptp.org. Problems cached from the "
745
+ "website are deleted if they come from an older release, so they are "
746
+ "fetched again, and a local library (-L or $TPTP) is compared with the "
747
+ "latest release. Resets the 30-day reminder.")
748
+ p.add_argument("--index", action="store_true", help="also rebuild the search index of the local library")
749
+ p.add_argument("--json", action="store_true", help="print JSON")
750
+ p.set_defaults(func=cmd_refresh)
751
+
752
+ p = sub.add_parser("validate", parents=[library_opt], help="check a local TPTP library",
753
+ description="Check that a folder or tarball is a TPTP library in the standard "
754
+ "layout. Exits with status 1 if it is not.")
755
+ p.add_argument("path", nargs="?", help="the library folder or tarball (default: -L or $TPTP)")
756
+ p.add_argument("--includes", action="store_true", help="also check that every problem's includes exist")
757
+ p.add_argument("--parse", action="store_true", help="also parse every problem and axiom file (slow)")
758
+ p.add_argument("--strict", action="store_true", help="fail on warnings too")
759
+ p.add_argument("--json", action="store_true", help="print the report as JSON")
760
+ p.set_defaults(func=cmd_validate)
761
+
762
+ p = sub.add_parser("discover", help="find TPTP libraries on this machine and set $TPTP",
763
+ description="Look for TPTP libraries (folders with Problems/ and Axioms/) and check "
764
+ "them. Prints the command that sets $TPTP to the best one, and can add it "
765
+ "to your shell's startup file (--set; on Windows, setx).")
766
+ p.add_argument("dirs", nargs="*", metavar="DIR",
767
+ help="where to look (default: here, your home folder, and /opt, /usr/local/share, "
768
+ "/usr/share, /srv, /data)")
769
+ p.add_argument("--depth", type=int, default=4, metavar="N",
770
+ help="how many folder levels to search below each DIR (default: 4)")
771
+ p.add_argument("--set", action="store_true", help="make it permanent: add it to your shell's startup file")
772
+ p.add_argument("-y", "--yes", action="store_true", help="don't ask: take the first one listed, and allow --set")
773
+ p.add_argument("--shell", choices=("bash", "zsh", "fish", "sh", "powershell", "cmd"),
774
+ help="the shell to write the command for (default: yours)")
775
+ p.add_argument("--json", action="store_true", help="print JSON (changes nothing)")
776
+ p.set_defaults(func=cmd_discover)
777
+
778
+ p = sub.add_parser("find", parents=[library_opt], help="locate problems in a local library",
779
+ description="Print where problems (SET001-1, wildcards such as 'SET00*-1') or "
780
+ "axiom files (SET001-0, SET001-0.ax) are in a local library.")
781
+ p.add_argument("names", nargs="+", metavar="NAME")
782
+ p.add_argument("-r", "--relative", action="store_true", help="print paths relative to the library root")
783
+ p.set_defaults(func=cmd_find)
784
+
785
+ p = sub.add_parser("stats", parents=[library_opt], help="print statistics about a problem",
786
+ description="Print the dialect, status, symbols, variables and other statistics of "
787
+ "a problem (a name such as SET001-1, or a file).")
788
+ p.add_argument("target", metavar="NAME_OR_FILE")
789
+ p.add_argument("--no-expand", dest="expand", action="store_false",
790
+ help="do not read the included axiom files")
791
+ p.add_argument("--symbols", action="store_true", help="list every symbol")
792
+ p.add_argument("--json", action="store_true", help="print JSON")
793
+ p.set_defaults(func=cmd_stats)
794
+
795
+ p = sub.add_parser(
796
+ "search", parents=[library_opt], help="find problems by their properties (like TPTP2T)",
797
+ formatter_class=argparse.RawDescriptionHelpFormatter,
798
+ description="Find the problems of a local library that have every property given, like the "
799
+ "TPTP2T form at tptp.org/cgi-bin/TPTP2T.\n\n"
800
+ "Lists are comma-separated and match any item. Numbers take N, MIN..MAX, MIN..,\n"
801
+ "..MAX, >N, >=N, <N or <=N. Prefix a value with 'not:' to negate it. An option given\n"
802
+ "twice must hold both times. The problem headers are indexed on first use and the\n"
803
+ "index is cached until the library changes.",
804
+ epilog="examples:\n"
805
+ " thousandproblems search --form FOF --status THM --rating 0.5..1 --equality none\n"
806
+ " thousandproblems search --domain SET,SEU --form THF --format table\n"
807
+ " thousandproblems search --form not:CNF --predicates '>1' --function-arity 0 --count\n"
808
+ " thousandproblems search --form TF0 --arithmetic yes | thousandproblems fetch - -o out/")
809
+ criteria = p.add_argument_group("problem properties")
810
+ for name, (_, text) in CRITERIA.items():
811
+ criteria.add_argument(f"--{name.replace('_', '-')}", dest=name, action="append", metavar="VALUE", help=text)
812
+ out = p.add_argument_group("output")
813
+ out.add_argument("--format", choices=("names", "table", "paths", "json"), default="names",
814
+ help="names (default), a table of properties, file paths, or JSON")
815
+ out.add_argument("--count", action="store_true", help="print only the number of matches")
816
+ out.add_argument("--limit", type=int, metavar="N", help="show at most N matches")
817
+ out.add_argument("--refresh", action="store_true", help="rebuild the header index")
818
+ out.add_argument("-q", "--quiet", action="store_true", help="no progress or totals on stderr")
819
+ p.set_defaults(func=cmd_search)
820
+ return parser
821
+
822
+
823
+ def main(argv: Optional[Sequence[str]] = None) -> int:
824
+ parser = build_parser()
825
+ args = parser.parse_args(argv)
826
+ try:
827
+ _remind(args)
828
+ return args.func(args)
829
+ except CommandError as e:
830
+ print(f"thousandproblems: error: {e}", file=sys.stderr)
831
+ return 1
832
+ except RateLimited as e:
833
+ print(f"thousandproblems: error: {e}", file=sys.stderr)
834
+ return 75 # EX_TEMPFAIL
835
+ except ProblemNotFound as e:
836
+ print(f"thousandproblems: error: {e.args[0] if e.args else e}", file=sys.stderr)
837
+ return 1
838
+ except ParseError as e:
839
+ print(f"thousandproblems: error: {e}", file=sys.stderr)
840
+ return 1
841
+ except (LibraryError, OSError) as e:
842
+ print(f"thousandproblems: error: {e}", file=sys.stderr)
843
+ return 1
844
+ except KeyboardInterrupt:
845
+ print(file=sys.stderr)
846
+ return 130
847
+ except BrokenPipeError:
848
+ try:
849
+ sys.stdout = open(os.devnull, "w")
850
+ except OSError:
851
+ pass
852
+ return 0
853
+
854
+
855
+ if __name__ == "__main__":
856
+ sys.exit(main())