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,239 @@
1
+ """Find TPTP libraries on this machine, and make one the default by setting ``$TPTP``.
2
+
3
+ >>> from thousandproblems.discover import find_libraries
4
+ >>> for found in find_libraries(["~"]): # doctest: +SKIP
5
+ ... print(found.path, found.version, found.valid)
6
+ /Users/me/tptp/TPTP-v10.0.0 10.0.0 True
7
+
8
+ A library is a folder holding ``Problems/`` and ``Axioms/``; each one found is checked with
9
+ :func:`thousandproblems.validate.validate_library`. Tarballs named like ``TPTP-v10.0.0.tgz``
10
+ are reported too, but ``$TPTP`` must name an unpacked folder.
11
+
12
+ :func:`export_command` gives the line that sets ``$TPTP`` in a given shell, and
13
+ :func:`install` writes it into the shell's startup file, between marker comments, so that
14
+ running it again replaces the earlier line instead of adding another.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import os
20
+ import re
21
+ import sys
22
+ from dataclasses import dataclass, field
23
+ from pathlib import Path
24
+ from typing import Iterable, Optional, Union
25
+
26
+ __all__ = ["Found", "find_libraries", "default_roots", "detect_shell", "export_command",
27
+ "startup_file", "install", "MARK_BEGIN", "MARK_END"]
28
+
29
+ MARK_BEGIN = "# >>> thousandproblems: TPTP library >>>"
30
+ MARK_END = "# <<< thousandproblems: TPTP library <<<"
31
+
32
+ #: Folders never searched inside: big, and never where a TPTP library lives.
33
+ _SKIP = {"node_modules", "Library", "Applications", "__pycache__", "site-packages", "venv",
34
+ "proc", "sys", "dev", "System", "Windows", "Program Files", "Program Files (x86)", "AppData"}
35
+ _TARBALL_RE = re.compile(r"TPTP-v(\d+\.\d+\.\d+)\.(?:tgz|tar\.gz|tar\.bz2|tar\.xz|tar)")
36
+
37
+
38
+ @dataclass
39
+ class Found:
40
+ """One library found: a folder, or a tarball (``packed``)."""
41
+
42
+ path: Path
43
+ version: Optional[str] = None
44
+ valid: bool = False
45
+ packed: bool = False
46
+ #: Has every folder of a distribution, not only Problems/ and Axioms/ (as ``fetch -o`` makes).
47
+ complete: bool = False
48
+ problems: int = 0
49
+ errors: list[str] = field(default_factory=list)
50
+
51
+ @property
52
+ def sort_key(self):
53
+ version = tuple(int(x) for x in self.version.split(".")) if self.version else ()
54
+ usable = 2 if self.valid and not self.packed else 1 if self.packed else 0 # a tarball can be unpacked
55
+ return (usable, self.complete, version, self.problems, str(self.path))
56
+
57
+
58
+ def default_roots() -> list[Path]:
59
+ """Where to look when no folder is given: here, the home folder, the usual install places."""
60
+ roots = [Path.cwd(), Path.home()]
61
+ if sys.platform == "win32":
62
+ roots += [Path(os.environ.get("SystemDrive", "C:") + "\\")]
63
+ else:
64
+ roots += [Path(p) for p in ("/opt", "/usr/local/share", "/usr/share", "/srv", "/data")]
65
+ unique: dict[str, Path] = {}
66
+ for root in roots:
67
+ if root.is_dir():
68
+ unique.setdefault(str(root.resolve()), root)
69
+ return list(unique.values())
70
+
71
+
72
+ def _is_library(path: Path) -> bool:
73
+ return (path / "Problems").is_dir() and (path / "Axioms").is_dir()
74
+
75
+
76
+ def _walk(root: Path, depth: int, budget: list[int]) -> Iterable[tuple[Path, bool]]:
77
+ """Yield (path, is_tarball) for each library-looking thing under ``root``."""
78
+ if _is_library(root):
79
+ yield root, False
80
+ return
81
+ stack = [(root, 0)]
82
+ while stack:
83
+ folder, level = stack.pop()
84
+ if budget[0] <= 0:
85
+ return
86
+ budget[0] -= 1
87
+ try:
88
+ entries = sorted(os.scandir(folder), key=lambda e: e.name)
89
+ except OSError:
90
+ continue
91
+ below = []
92
+ for entry in entries:
93
+ name = entry.name
94
+ try:
95
+ if entry.is_file() and _TARBALL_RE.fullmatch(name):
96
+ yield Path(entry.path), True
97
+ continue
98
+ if not entry.is_dir() or name.startswith((".", "$")) or name in _SKIP:
99
+ continue
100
+ symlink = entry.is_symlink()
101
+ except OSError:
102
+ continue
103
+ path = Path(entry.path)
104
+ if _is_library(path):
105
+ yield path, False
106
+ elif level + 1 < depth and not symlink: # a symlink may be a library, but isn't crawled
107
+ below.append((path, level + 1))
108
+ stack.extend(reversed(below))
109
+
110
+
111
+ def find_libraries(roots: Optional[Iterable[Union[str, os.PathLike]]] = None, *, depth: int = 4,
112
+ max_folders: int = 200_000, check: bool = True) -> list[Found]:
113
+ """TPTP libraries under ``roots`` (default: :func:`default_roots`), best first: valid
114
+ folders, then tarballs, then invalid folders; complete distributions before partial copies,
115
+ then newer versions and more problems first.
116
+
117
+ ``depth`` is how many folder levels below each root are searched, and ``max_folders``
118
+ caps the folders looked at in all. With ``check``, each folder is validated.
119
+ """
120
+ from .updates import detect_version
121
+ from .validate import validate_library
122
+
123
+ paths = [Path(os.path.expanduser(os.fspath(r))) for r in roots] if roots else default_roots()
124
+ budget = [max_folders]
125
+ seen: dict[str, Found] = {}
126
+ for root in paths:
127
+ if not root.exists():
128
+ continue
129
+ for path, packed in _walk(root, depth, budget):
130
+ key = os.path.normcase(str(path.resolve()))
131
+ if key in seen:
132
+ continue
133
+ # keep a symlink as it is, so $TPTP follows it when it is pointed at a new release
134
+ found = Found(path.absolute(), detect_version(path.resolve()), packed=packed)
135
+ if packed:
136
+ found.errors.append("a tarball: unpack it to use it as $TPTP")
137
+ elif check:
138
+ report = validate_library(path)
139
+ found.valid, found.problems = report.ok, report.problems
140
+ found.complete = report.ok and not any(w.startswith("no ") and w.endswith("folder (not needed for reading problems)")
141
+ for w in report.warnings)
142
+ found.version = found.version or (report.version or "").lstrip("v") or None
143
+ found.errors = list(report.errors)
144
+ else:
145
+ found.valid = found.complete = True
146
+ seen[key] = found
147
+ return sorted(seen.values(), key=lambda f: f.sort_key, reverse=True)
148
+
149
+
150
+ # ---------------------------------------------------------------------------------
151
+ # Shells
152
+ # ---------------------------------------------------------------------------------
153
+
154
+ SHELLS = ("bash", "zsh", "fish", "sh", "powershell", "cmd")
155
+
156
+
157
+ def detect_shell() -> str:
158
+ """The user's shell: ``bash``, ``zsh``, ``fish``, ``sh``, ``powershell`` or ``cmd``."""
159
+ if sys.platform == "win32":
160
+ return "powershell" if os.environ.get("PSModulePath") else "cmd"
161
+ name = Path(os.environ.get("SHELL", "")).name
162
+ for shell in ("zsh", "bash", "fish"):
163
+ if name.startswith(shell):
164
+ return shell
165
+ return "sh"
166
+
167
+
168
+ def _quote_posix(text: str) -> str:
169
+ return "'" + text.replace("'", "'\\''") + "'"
170
+
171
+
172
+ def export_command(path: Union[str, os.PathLike], shell: str) -> str:
173
+ """The command that sets ``$TPTP`` to ``path`` in ``shell``."""
174
+ text = str(path)
175
+ if shell == "fish":
176
+ return f"set -gx TPTP {_quote_posix(text)}"
177
+ if shell == "powershell":
178
+ return f"$env:TPTP = '{text.replace(chr(39), chr(39) * 2)}'"
179
+ if shell == "cmd":
180
+ return f'set "TPTP={text}"'
181
+ return f"export TPTP={_quote_posix(text)}"
182
+
183
+
184
+ def startup_file(shell: str, home: Optional[Path] = None) -> Optional[Path]:
185
+ """The file ``shell`` runs at startup, where ``install`` puts the setting.
186
+
187
+ ``None`` for Windows shells, where the setting is made with ``setx`` instead.
188
+ """
189
+ home = home or Path.home()
190
+ if shell == "zsh":
191
+ return Path(os.environ.get("ZDOTDIR") or home) / ".zshrc"
192
+ if shell == "bash":
193
+ # macOS terminals start login shells, which read .bash_profile, not .bashrc
194
+ return home / (".bash_profile" if sys.platform == "darwin" else ".bashrc")
195
+ if shell == "fish":
196
+ config = Path(os.environ.get("XDG_CONFIG_HOME") or home / ".config")
197
+ return config / "fish" / "conf.d" / "thousandproblems.fish"
198
+ if shell == "sh":
199
+ return home / ".profile"
200
+ return None
201
+
202
+
203
+ def _block(path: Union[str, os.PathLike], shell: str) -> str:
204
+ return f"{MARK_BEGIN}\n{export_command(path, shell)}\n{MARK_END}\n"
205
+
206
+
207
+ def planned_text(current: str, path: Union[str, os.PathLike], shell: str) -> str:
208
+ """``current`` (a startup file's text) with the TPTP block added or replaced."""
209
+ block = _block(path, shell)
210
+ pattern = re.compile(re.escape(MARK_BEGIN) + r".*?" + re.escape(MARK_END) + r"\n?", re.DOTALL)
211
+ if pattern.search(current):
212
+ return pattern.sub(lambda _: block, current, count=1)
213
+ separator = "" if not current or current.endswith("\n\n") else ("\n" if current.endswith("\n") else "\n\n")
214
+ return current + separator + block
215
+
216
+
217
+ def other_settings(text: str) -> list[str]:
218
+ """Lines outside our block that also set TPTP, which the user should know about."""
219
+ outside = re.sub(re.escape(MARK_BEGIN) + r".*?" + re.escape(MARK_END), "", text, flags=re.DOTALL)
220
+ return [line.strip() for line in outside.splitlines()
221
+ if re.match(r"\s*(export\s+TPTP=|TPTP=|set\s+-\w*x\w*\s+TPTP\b|setenv\s+TPTP\b)", line)]
222
+
223
+
224
+ def install(path: Union[str, os.PathLike], shell: str, file: Optional[Path] = None) -> Path:
225
+ """Write the TPTP setting into ``shell``'s startup file (or ``file``); returns the file."""
226
+ target = file or startup_file(shell)
227
+ if target is None:
228
+ raise ValueError(f"{shell} has no startup file; use setx")
229
+ current = target.read_text(encoding="utf-8") if target.exists() else ""
230
+ target.parent.mkdir(parents=True, exist_ok=True)
231
+ temporary = target.with_name(f".{target.name}.{os.getpid()}.tmp")
232
+ temporary.write_text(planned_text(current, path, shell), encoding="utf-8")
233
+ if target.exists():
234
+ try:
235
+ os.chmod(temporary, target.stat().st_mode)
236
+ except OSError:
237
+ pass
238
+ os.replace(temporary, target)
239
+ return target
@@ -0,0 +1,148 @@
1
+ """Download the whole TPTP library from tptp.org.
2
+
3
+ >>> from thousandproblems.download import download_library
4
+ >>> root = download_library("/data") # doctest: +SKIP
5
+ >>> root # doctest: +SKIP
6
+ PosixPath('/data/TPTP-v10.0.0')
7
+
8
+ The current release is at ``https://tptp.org/TPTP/Distribution/TPTP-v<version>.tgz`` and
9
+ older ones are under ``https://tptp.org/TPTP/Archive/``. The tarball is close to 1 GB, and
10
+ unpacked the library takes several GB.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import os
16
+ import re
17
+ import tarfile
18
+ import urllib.error
19
+ import urllib.request
20
+ from pathlib import Path
21
+ from typing import Callable, Optional, Union
22
+
23
+ from .library import LibraryError
24
+
25
+ __all__ = ["DISTRIBUTION_URL", "ARCHIVE_URL", "latest_version", "release_url", "download_library", "save_files"]
26
+
27
+ DISTRIBUTION_URL = "https://tptp.org/TPTP/Distribution/"
28
+ ARCHIVE_URL = "https://tptp.org/TPTP/Archive/"
29
+
30
+ _TARBALL_RE = re.compile(r"TPTP-v(\d+\.\d+\.\d+)\.tgz")
31
+
32
+
33
+ def _user_agent() -> dict[str, str]:
34
+ from . import __version__
35
+ return {"User-Agent": f"thousandproblems/{__version__}"}
36
+
37
+
38
+ def _get(url: str, timeout: float = 60.0):
39
+ try:
40
+ return urllib.request.urlopen(urllib.request.Request(url, headers=_user_agent()), timeout=timeout)
41
+ except urllib.error.HTTPError as e:
42
+ e.close()
43
+ raise LibraryError(f"{url}: HTTP {e.code} {e.reason}") from e
44
+ except (urllib.error.URLError, OSError) as e:
45
+ raise LibraryError(f"cannot reach {url}: {getattr(e, 'reason', e)}") from e
46
+
47
+
48
+ def _versions(listing_url: str) -> list[str]:
49
+ with _get(listing_url) as response:
50
+ page = response.read().decode("utf-8", errors="replace")
51
+ found = set(_TARBALL_RE.findall(page))
52
+ return sorted(found, key=lambda v: tuple(int(x) for x in v.split(".")))
53
+
54
+
55
+ def latest_version(base_url: str = DISTRIBUTION_URL) -> str:
56
+ """The version of the current release, e.g. ``'10.0.0'``, read from the download page."""
57
+ versions = _versions(base_url)
58
+ if not versions:
59
+ raise LibraryError(f"no TPTP-v*.tgz found at {base_url}")
60
+ return versions[-1]
61
+
62
+
63
+ def release_url(version: Optional[str] = None, *, distribution_url: str = DISTRIBUTION_URL,
64
+ archive_url: str = ARCHIVE_URL) -> str:
65
+ """The tarball URL for ``version`` (``'9.0.0'`` or ``'v9.0.0'``), or the latest release."""
66
+ if version in (None, "", "latest"):
67
+ return f"{distribution_url}TPTP-v{latest_version(distribution_url)}.tgz"
68
+ version = version.lstrip("vV")
69
+ if not re.fullmatch(r"\d+\.\d+\.\d+", version):
70
+ raise ValueError(f"{version!r} is not a TPTP version such as 9.0.0")
71
+ if version in _versions(distribution_url):
72
+ return f"{distribution_url}TPTP-v{version}.tgz"
73
+ return f"{archive_url}TPTP-v{version}.tgz"
74
+
75
+
76
+ def _safe_extract(tar: tarfile.TarFile, dest: Path) -> None:
77
+ if hasattr(tarfile, "data_filter"): # Python 3.11.4+
78
+ tar.extractall(dest, filter="data")
79
+ return
80
+ root = dest.resolve()
81
+ for member in tar.getmembers():
82
+ target = (dest / member.name).resolve()
83
+ if not (member.isfile() or member.isdir()) or not target.is_relative_to(root):
84
+ continue
85
+ tar.extract(member, dest)
86
+
87
+
88
+ def download_library(dest: Union[str, os.PathLike] = ".", version: Optional[str] = None, *,
89
+ extract: bool = True, keep_tarball: bool = False, url: Optional[str] = None,
90
+ progress: Optional[Callable[[int, Optional[int]], None]] = None) -> Path:
91
+ """Download a TPTP release into the folder ``dest``.
92
+
93
+ Returns the unpacked library folder (e.g. ``dest/TPTP-v10.0.0``), or the tarball with
94
+ ``extract=False``. ``progress(bytes_done, bytes_total)`` is called while downloading.
95
+ ``url`` overrides where the tarball comes from.
96
+ """
97
+ dest = Path(dest)
98
+ dest.mkdir(parents=True, exist_ok=True)
99
+ url = url or release_url(version)
100
+ tarball = dest / url.rsplit("/", 1)[-1]
101
+ partial = tarball.with_name(tarball.name + ".part")
102
+ with _get(url, timeout=120.0) as response:
103
+ total = response.headers.get("Content-Length")
104
+ total = int(total) if total and total.isdigit() else None
105
+ done = 0
106
+ with open(partial, "wb") as out:
107
+ while chunk := response.read(1 << 20):
108
+ out.write(chunk)
109
+ done += len(chunk)
110
+ if progress:
111
+ progress(done, total)
112
+ if total is not None and done != total:
113
+ raise LibraryError(f"download of {url} stopped after {done} of {total} bytes")
114
+ os.replace(partial, tarball)
115
+ if not extract:
116
+ return tarball
117
+ try:
118
+ with tarfile.open(tarball, "r:*") as tar:
119
+ names = (m.name[2:] if m.name.startswith("./") else m.name for m in tar.getmembers())
120
+ tops = {n.split("/")[0] for n in names if n not in ("", ".")}
121
+ _safe_extract(tar, dest)
122
+ except (tarfile.TarError, EOFError) as e:
123
+ raise LibraryError(f"{tarball} is not a readable tarball: {e}") from e
124
+ if not keep_tarball:
125
+ tarball.unlink()
126
+ if len(tops) == 1:
127
+ return dest / tops.pop()
128
+ return dest
129
+
130
+
131
+ def save_files(files: dict[str, str], dest: Union[str, os.PathLike], *, overwrite: bool = True) -> list[Path]:
132
+ """Write ``{library path: text}`` (from :meth:`Library.problem_files`) under ``dest``,
133
+ keeping the standard layout. Returns the files written."""
134
+ from .library import _clean
135
+
136
+ root = Path(dest)
137
+ written = []
138
+ for rel, text in files.items():
139
+ clean = _clean(rel)
140
+ if clean is None:
141
+ continue
142
+ target = root / clean
143
+ if target.exists() and not overwrite:
144
+ continue
145
+ target.parent.mkdir(parents=True, exist_ok=True)
146
+ target.write_bytes(text.encode("utf-8"))
147
+ written.append(target)
148
+ return written