btmc 0.0.0__py3-none-any.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.
btmc/__init__.py ADDED
@@ -0,0 +1,12 @@
1
+ from btmc.config import Config
2
+ from btmc.protocol import Client, Peer, TorrentState
3
+ from btmc.files import TorrentWACZ, TorrentZip
4
+
5
+ __all__ = [
6
+ "Client",
7
+ "Config",
8
+ "Peer",
9
+ "TorrentState",
10
+ "TorrentWACZ",
11
+ "TorrentZip",
12
+ ]
btmc/cli/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """The ``btmc`` command. Needs the ``cli`` extra."""
btmc/cli/common.py ADDED
@@ -0,0 +1,87 @@
1
+ """What the commands that work on one torrent have in common"""
2
+
3
+ import contextlib
4
+ from collections.abc import AsyncGenerator, Callable
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ import click
9
+ from torrent_models import Torrent
10
+
11
+ from btmc.config import Config
12
+ from btmc.protocol.client import Client
13
+ from btmc.protocol.torrent import TorrentState
14
+ from btmc.types import AnyAddr, parse_addr
15
+
16
+
17
+ def echo(line: str) -> None:
18
+ """
19
+ Print a line of a command's output to stdout. Not with ``click.echo``,
20
+ which writes past the progress bars' redirect of stdout and ends up on top of them.
21
+ """
22
+ print(line)
23
+
24
+
25
+ def _read_torrent(ctx: click.Context, param: click.Parameter, value: Path) -> Torrent[Any]:
26
+ try:
27
+ return Torrent.read(value, context={"limits": Config.from_toml().torrent_limits})
28
+ except Exception as e:
29
+ raise click.BadParameter(f"can't read {value}: {e}") from e
30
+
31
+
32
+ def _parse_addrs(
33
+ ctx: click.Context, param: click.Parameter, values: tuple[str, ...]
34
+ ) -> list[AnyAddr]:
35
+ try:
36
+ return [parse_addr(value) for value in values]
37
+ except ValueError as e:
38
+ raise click.BadParameter(str(e)) from e
39
+
40
+
41
+ torrent_argument = click.argument(
42
+ "torrent", type=click.Path(exists=True, dir_okay=False, path_type=Path), callback=_read_torrent
43
+ )
44
+ """The .torrent file to fetch from, read into a :class:`.Torrent`"""
45
+
46
+
47
+ def torrent_options(fn: Callable[..., Any]) -> Callable[..., Any]:
48
+ """The arguments and options for fetching from one torrent, see :func:`.running_client`"""
49
+ fn = client_options(fn)
50
+ fn = click.argument("dest", type=click.Path(file_okay=False, path_type=Path))(fn)
51
+ return torrent_argument(fn)
52
+
53
+
54
+ def client_options(fn: Callable[..., Any]) -> Callable[..., Any]:
55
+ """The options for how the client finds peers, see :func:`.running_client`"""
56
+ options = [
57
+ click.option(
58
+ "--peer",
59
+ multiple=True,
60
+ callback=_parse_addrs,
61
+ help="HOST:PORT of a peer to connect to. Can be used multiple times.",
62
+ ),
63
+ click.option("--listen", type=int, help="Port to take incoming connections on"),
64
+ click.option(
65
+ "--no-trackers", is_flag=True, help="Don't announce to the torrent's trackers"
66
+ ),
67
+ ]
68
+ for option in reversed(options):
69
+ fn = option(fn)
70
+ return fn
71
+
72
+
73
+ @contextlib.asynccontextmanager
74
+ async def running_client(
75
+ torrent: Torrent[Any], dest: Path, peer: list[AnyAddr], listen: int | None, no_trackers: bool
76
+ ) -> AsyncGenerator[tuple[Client, TorrentState]]:
77
+ """A running client, with the torrent added, from :func:`.torrent_options`"""
78
+ async with Client(config=client_config(listen)) as client:
79
+ t = client.add(torrent, dest, known_peers=peer, trackers=[] if no_trackers else None)
80
+ yield client, t
81
+
82
+
83
+ def client_config(listen: int | None) -> Config:
84
+ """Settings from btmc.toml, listening on all interfaces on the ``--listen`` port if given"""
85
+ if listen is None:
86
+ return Config.from_toml()
87
+ return Config.from_toml(listen_host="0.0.0.0", port=listen)
btmc/cli/fetch.py ADDED
@@ -0,0 +1,47 @@
1
+ """``btmc fetch``: fetching pieces of a torrent"""
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+
6
+ import anyio
7
+ import click
8
+ from rich.console import Console
9
+ from torrent_models import Torrent
10
+
11
+ from btmc.cli.common import running_client, torrent_options
12
+ from btmc.cli.progress import TorrentProgress
13
+ from btmc.types import AnyAddr
14
+
15
+
16
+ @click.command("fetch")
17
+ @torrent_options
18
+ @click.argument("pieces", type=int, nargs=-1)
19
+ @click.pass_obj
20
+ def fetch(
21
+ console: Console,
22
+ torrent: Torrent[Any],
23
+ dest: Path,
24
+ peer: list[AnyAddr],
25
+ listen: int | None,
26
+ no_trackers: bool,
27
+ pieces: tuple[int, ...],
28
+ ) -> None:
29
+ """Fetch pieces of TORRENT into DEST, by their index, or all of them without PIECES"""
30
+ n_pieces = torrent.layout.n_pieces
31
+ for index in pieces:
32
+ if not 0 <= index < n_pieces:
33
+ raise click.BadParameter(
34
+ f"there's no piece {index}, the torrent has {n_pieces}", param_hint="PIECES"
35
+ )
36
+
37
+ async def run() -> None:
38
+ # around the whole client, so the bars stay below what's logged while it starts and stops
39
+ with TorrentProgress(console) as progress:
40
+ async with running_client(torrent, dest, peer, listen, no_trackers) as (client, t):
41
+ progress.attach(client)
42
+ await client.fetch(t, pieces or None)
43
+ # stopping removes the torrent, which would take its bar away
44
+ progress.detach()
45
+ console.print(f"downloaded {t.downloaded:,} bytes, {t.hash_failures} hash failures")
46
+
47
+ anyio.run(run)
btmc/cli/main.py ADDED
@@ -0,0 +1,41 @@
1
+ """The ``btmc`` command, which every other command is under"""
2
+
3
+ import logging
4
+
5
+ import click
6
+ from rich.console import Console
7
+ from rich.logging import RichHandler
8
+
9
+ from btmc.cli.fetch import fetch
10
+ from btmc.cli.mount import mount
11
+ from btmc.cli.zip import zip_
12
+
13
+
14
+ @click.group("btmc")
15
+ @click.option(
16
+ "-v",
17
+ "--verbose",
18
+ count=True,
19
+ help="Log more: -v for HTTP tracker requests and the like, -vv for announce results and peers",
20
+ )
21
+ @click.pass_context
22
+ def main(ctx: click.Context, verbose: int) -> None:
23
+ """
24
+ btmc - bittorrent microclient
25
+
26
+ Settings are read from btmc.toml in the current directory, if there is one.
27
+ """
28
+ # progress, logs and status go to stderr, so what commands print to stdout can be piped
29
+ console = Console(stderr=True)
30
+ levels = [logging.WARNING, logging.INFO, logging.DEBUG]
31
+ logging.basicConfig(
32
+ level=levels[min(verbose, len(levels) - 1)],
33
+ format="%(message)s",
34
+ handlers=[RichHandler(console=console, show_path=False)],
35
+ )
36
+ ctx.obj = console
37
+
38
+
39
+ main.add_command(fetch)
40
+ main.add_command(zip_)
41
+ main.add_command(mount)
btmc/cli/mount.py ADDED
@@ -0,0 +1,133 @@
1
+ """``btmc mount``: a torrent as a directory"""
2
+
3
+ import dataclasses
4
+ import glob
5
+ import shutil
6
+ from collections.abc import Callable
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ import click
11
+ from rich.console import Console
12
+ from torrent_models import Torrent
13
+
14
+ from btmc.cli.common import client_config, client_options, torrent_argument
15
+ from btmc.cli.progress import TorrentProgress
16
+ from btmc.types import AnyAddr
17
+ from btmc.utils import torrent_name
18
+
19
+
20
+ @click.command("mount")
21
+ @torrent_argument
22
+ @click.argument("mountpoint", type=click.Path(exists=True, file_okay=False, path_type=Path))
23
+ @click.option(
24
+ "--mount-zip",
25
+ "zips",
26
+ multiple=True,
27
+ metavar="PATH",
28
+ help="A file in the torrent to show as a zip, like a .wacz. Can be used multiple times.",
29
+ )
30
+ @click.option(
31
+ "--download-dir",
32
+ type=click.Path(file_okay=False, path_type=Path),
33
+ help=(
34
+ "Where to keep fetched pieces, in a directory for each torrent. "
35
+ "Defaults to download_dir in btmc.toml, or the user cache directory."
36
+ ),
37
+ )
38
+ @click.option(
39
+ "--fetch",
40
+ # FetchMode's values, which can't be imported without the mount extra
41
+ type=click.Choice(["as-read", "whole-file"]),
42
+ default="as-read",
43
+ show_default=True,
44
+ help="Fetch an opened file's pieces as they're read, or all of them as soon as it's opened",
45
+ )
46
+ @click.option(
47
+ "--clear-cache",
48
+ is_flag=True,
49
+ help="Delete the pieces already fetched for this torrent, and start from nothing",
50
+ )
51
+ @client_options
52
+ @click.pass_obj
53
+ def mount(
54
+ console: Console,
55
+ torrent: Torrent[Any],
56
+ mountpoint: Path,
57
+ zips: tuple[str, ...],
58
+ download_dir: Path | None,
59
+ fetch: str,
60
+ clear_cache: bool,
61
+ peer: list[AnyAddr],
62
+ listen: int | None,
63
+ no_trackers: bool,
64
+ ) -> None:
65
+ """
66
+ Mount TORRENT as a read-only directory at MOUNTPOINT, until Ctrl-C.
67
+
68
+ Files are fetched a piece at a time as they're read, or with --fetch whole-file,
69
+ all at once as soon as they're opened.
70
+ Zip files are also shown as a directory next to them, named with .d on the end,
71
+ with what's in them in it. Files ending in .zip are shown like this,
72
+ and so are any given with --mount-zip.
73
+
74
+ On macOS, a file's Get Info in Finder has a tag for whether it's on disk.
75
+
76
+ Needs the mount extra and FUSE: on macOS, macFUSE 5.2 or newer, with its file system
77
+ extension switched on in System Settings, under General > Login Items & Extensions.
78
+ """
79
+ try:
80
+ from fsspec.asyn import sync
81
+
82
+ from btmc.fsspec.filesystem import FetchMode, TorrentFileSystem
83
+ from btmc.fsspec.fuse import mount as fuse_mount
84
+ except (ImportError, OSError) as e:
85
+ raise click.ClickException(f"can't mount without FUSE and the mount extra: {e}") from e
86
+ if missing := set(zips) - {f.path for f in torrent.files}:
87
+ raise click.BadParameter(
88
+ f"not files in the torrent: {sorted(missing)}", param_hint="--mount-zip"
89
+ )
90
+
91
+ config = client_config(listen)
92
+ if download_dir is not None:
93
+ config = dataclasses.replace(config, download_dir=download_dir.expanduser())
94
+ dest = config.download_dir / torrent_name(torrent)
95
+ if clear_cache:
96
+ # the torrent's resume files too, including ones for other directories,
97
+ # which only means those get checked again
98
+ for path in (dest, config.state_dir / "resume" / torrent_name(torrent)):
99
+ if path.exists():
100
+ shutil.rmtree(path)
101
+ console.print(f"cleared {path}", markup=False)
102
+ fs = TorrentFileSystem(
103
+ torrents=[torrent],
104
+ zips=["*.zip", *(glob.escape(path) for path in zips)],
105
+ trackers=[] if no_trackers else None,
106
+ peers=peer,
107
+ config=config,
108
+ # this command closes it, so it can't be one fsspec handed out before
109
+ skip_instance_cache=True,
110
+ )
111
+ # around the whole client, so the bars stay below what's logged while it starts and stops
112
+ with TorrentProgress(console) as progress:
113
+ try:
114
+ client = sync(fs.loop, fs.get_client)
115
+ sync(fs.loop, _on_loop, progress.attach, client)
116
+ # so it starts finding peers before anything's read
117
+ sync(fs.loop, fs.torrent_state, torrent)
118
+ try:
119
+ root = torrent_name(torrent)
120
+ fuse_mount(fs, root, mountpoint, torrent.info.name, FetchMode(fetch))
121
+ except RuntimeError as e:
122
+ # FUSE prints why before failing
123
+ raise click.ClickException(f"couldn't mount at {mountpoint}") from e
124
+ finally:
125
+ # stopping removes the torrent, which would take its bar away
126
+ sync(fs.loop, _on_loop, progress.detach)
127
+ finally:
128
+ fs.close()
129
+
130
+
131
+ async def _on_loop(fn: Callable[..., None], *args: Any) -> None:
132
+ """Call ``fn`` on the event loop's thread, through :func:`fsspec.asyn.sync`"""
133
+ fn(*args)
btmc/cli/progress.py ADDED
@@ -0,0 +1,184 @@
1
+ """Progress bars for the torrents a client is fetching"""
2
+
3
+ import sys
4
+ from typing import Self
5
+
6
+ from rich import filesize
7
+ from rich.console import Console
8
+ from rich.progress import (
9
+ BarColumn,
10
+ DownloadColumn,
11
+ Progress,
12
+ ProgressColumn,
13
+ Task,
14
+ TaskID,
15
+ TaskProgressColumn,
16
+ TextColumn,
17
+ TimeRemainingColumn,
18
+ )
19
+ from rich.text import Text
20
+
21
+ from btmc.protocol.client import Client
22
+ from btmc.protocol.peer import Peer
23
+ from btmc.protocol.picker import PieceState
24
+ from btmc.protocol.torrent import TorrentState
25
+ from btmc.types import PieceIndex
26
+
27
+ NAME_WIDTH = 30
28
+ BINARY_UNITS = ["bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
29
+ """Base 2 sizes, like :class:`.DownloadColumn` does"""
30
+
31
+
32
+ class _BinarySpeedColumn(ProgressColumn):
33
+ """Download speed in base 2 units, to match the sizes"""
34
+
35
+ def render(self, task: Task) -> Text:
36
+ speed = task.finished_speed or task.speed
37
+ if speed is None:
38
+ return Text("?", style="progress.data.speed")
39
+ unit, suffix = filesize.pick_unit_and_suffix(int(speed), BINARY_UNITS, 1024)
40
+ precision = 0 if unit == 1 else 1
41
+ return Text(f"{speed / unit:,.{precision}f} {suffix}/s", style="progress.data.speed")
42
+
43
+
44
+ class TorrentProgress:
45
+ """
46
+ A progress bar for each torrent in a client: how many of the pieces asked for have
47
+ arrived, and how many bytes, how fast they're arriving, how long until the rest do,
48
+ and how many peers the torrent is connected to.
49
+
50
+ Show the bars with ``with``, and :meth:`.attach` them to a client to follow its torrents
51
+ as they're added and removed. Only pieces asked for after attaching are counted.
52
+ Leaving the ``with`` block detaches, and leaves the bars on screen as they were.
53
+
54
+ Args:
55
+ console: where to draw the bars
56
+ """
57
+
58
+ def __init__(self, console: Console | None = None):
59
+ self._progress = Progress(
60
+ TextColumn("{task.description}"),
61
+ BarColumn(),
62
+ TaskProgressColumn(),
63
+ TextColumn("{task.fields[pieces]}"),
64
+ DownloadColumn(binary_units=True),
65
+ _BinarySpeedColumn(),
66
+ TextColumn("{task.fields[peers]} peers"),
67
+ TimeRemainingColumn(),
68
+ console=console,
69
+ # shown above the bars when it's on the same terminal, and left alone when piped
70
+ redirect_stdout=sys.stdout.isatty(),
71
+ )
72
+ self._client: Client | None = None
73
+ self._bars: dict[TorrentState, _TorrentBar] = {}
74
+
75
+ def __enter__(self) -> Self:
76
+ self._progress.start()
77
+ return self
78
+
79
+ def __exit__(self, *exc: object) -> None:
80
+ self.detach()
81
+ self._progress.stop()
82
+
83
+ def attach(self, client: Client) -> None:
84
+ """
85
+ Show a bar for each of the client's torrents, now and as they're added.
86
+ Raises RuntimeError if it's attached to a client already.
87
+ """
88
+ if self._client is not None:
89
+ raise RuntimeError("already attached to a client")
90
+ self._client = client
91
+ client.add_callback("added", self._added)
92
+ client.add_callback("removed", self._removed)
93
+ for t in client.torrents:
94
+ self._added(t)
95
+
96
+ def detach(self) -> None:
97
+ """Stop following the client's torrents, leaving their bars as they are"""
98
+ if self._client is None:
99
+ return
100
+ self._client.remove_callback("added", self._added)
101
+ self._client.remove_callback("removed", self._removed)
102
+ for bar in self._bars.values():
103
+ bar.close()
104
+ self._bars.clear()
105
+ self._client = None
106
+
107
+ def _added(self, t: TorrentState) -> None:
108
+ self._bars[t] = _TorrentBar(self._progress, t)
109
+
110
+ def _removed(self, t: TorrentState) -> None:
111
+ bar = self._bars.pop(t)
112
+ bar.close()
113
+ self._progress.remove_task(bar.task)
114
+
115
+
116
+ class _TorrentBar:
117
+ """One torrent's bar, kept up to date by the torrent's events until it's closed"""
118
+
119
+ def __init__(self, progress: Progress, t: TorrentState):
120
+ self._progress = progress
121
+ self._t = t
122
+ self._requested: set[PieceIndex] = set()
123
+ self._requested_bytes = 0
124
+ self._arrived = 0
125
+ self._arrived_bytes = 0
126
+ self._peers = 0
127
+ name = t.torrent.info.name or t.name
128
+ if len(name) > NAME_WIDTH:
129
+ name = name[: NAME_WIDTH - 1] + "…"
130
+ self.task: TaskID = progress.add_task(name, total=None, pieces=self._pieces(), peers=0)
131
+ t.picker.add_callback(PieceState.OPEN, self._opened)
132
+ t.picker.add_callback(PieceState.HAVE, self._had)
133
+ t.swarm.add_callback("connected", self._connected)
134
+ t.swarm.add_callback("disconnected", self._disconnected)
135
+
136
+ def close(self) -> None:
137
+ """Stop following the torrent"""
138
+ self._t.picker.remove_callback(PieceState.OPEN, self._opened)
139
+ self._t.picker.remove_callback(PieceState.HAVE, self._had)
140
+ self._t.swarm.remove_callback("connected", self._connected)
141
+ self._t.swarm.remove_callback("disconnected", self._disconnected)
142
+
143
+ def _opened(self, pieces: list[PieceIndex]) -> None:
144
+ for index in pieces:
145
+ self._request(index)
146
+ self._update()
147
+
148
+ def _had(self, pieces: list[PieceIndex]) -> None:
149
+ for index in pieces:
150
+ # asked for before we were attached, or found on disk
151
+ self._request(index)
152
+ self._arrived += 1
153
+ self._arrived_bytes += self._size(index)
154
+ self._update()
155
+
156
+ def _request(self, index: PieceIndex) -> None:
157
+ # pieces that fail their check open again, and are only counted once
158
+ if index not in self._requested:
159
+ self._requested.add(index)
160
+ self._requested_bytes += self._size(index)
161
+
162
+ def _size(self, index: PieceIndex) -> int:
163
+ """Bytes of the piece that are downloaded, which leaves out padding"""
164
+ piece = self._t.layout.piece_range(index)
165
+ return piece.size - piece.padding
166
+
167
+ def _connected(self, peer: Peer) -> None:
168
+ self._peers += 1
169
+ self._progress.update(self.task, peers=self._peers)
170
+
171
+ def _disconnected(self, peer: Peer) -> None:
172
+ self._peers -= 1
173
+ self._progress.update(self.task, peers=self._peers)
174
+
175
+ def _pieces(self) -> str:
176
+ return f"{self._arrived}/{len(self._requested)} pieces"
177
+
178
+ def _update(self) -> None:
179
+ self._progress.update(
180
+ self.task,
181
+ total=self._requested_bytes,
182
+ completed=self._arrived_bytes,
183
+ pieces=self._pieces(),
184
+ )
btmc/cli/zip.py ADDED
@@ -0,0 +1,84 @@
1
+ """``btmc zip``: listing and extracting members of a zip in a torrent"""
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+
6
+ import anyio
7
+ import click
8
+ from rich.console import Console
9
+ from torrent_models import Torrent, safe_join
10
+
11
+ from btmc.cli.common import echo, running_client, torrent_options
12
+ from btmc.cli.progress import TorrentProgress
13
+ from btmc.files.zip import TorrentZip
14
+ from btmc.types import AnyAddr
15
+
16
+
17
+ @click.command("zip")
18
+ @torrent_options
19
+ @click.option(
20
+ "-f",
21
+ "--file",
22
+ "path",
23
+ help="Path of the zip file within the torrent. Needed if the torrent has more than one file.",
24
+ )
25
+ @click.option(
26
+ "-x",
27
+ "--extract",
28
+ type=click.Path(file_okay=False, path_type=Path),
29
+ help="Directory to extract MEMBERS into",
30
+ )
31
+ @click.argument("members", nargs=-1)
32
+ @click.pass_obj
33
+ def zip_(
34
+ console: Console,
35
+ torrent: Torrent[Any],
36
+ dest: Path,
37
+ peer: list[AnyAddr],
38
+ listen: int | None,
39
+ no_trackers: bool,
40
+ path: str | None,
41
+ extract: Path | None,
42
+ members: tuple[str, ...],
43
+ ) -> None:
44
+ """
45
+ List the members of a zip file in TORRENT, or extract some of them,
46
+ fetching only the pieces they're in. Pieces are stored in DEST.
47
+ """
48
+ zip_path = path if path is not None else _only_file(torrent)
49
+
50
+ async def run() -> None:
51
+ # around the whole client, so the bars stay below what's logged while it starts and stops
52
+ with TorrentProgress(console) as progress:
53
+ async with running_client(torrent, dest, peer, listen, no_trackers) as (client, t):
54
+ progress.attach(client)
55
+ tzip = TorrentZip(client, t, zip_path)
56
+ zf = await tzip.open()
57
+ if not members:
58
+ for info in zf.infolist():
59
+ echo(f"{info.file_size:>14,} {info.filename}")
60
+ for name in members:
61
+ data = await tzip.read(name)
62
+ if extract is None:
63
+ echo(f"{len(data):>14,} {name}")
64
+ else:
65
+ out = safe_join(extract, name)
66
+ out.parent.mkdir(parents=True, exist_ok=True)
67
+ out.write_bytes(data)
68
+ console.print(f"extracted {name} to {out}", markup=False)
69
+ # stopping removes the torrent, which would take its bar away
70
+ progress.detach()
71
+ console.print(
72
+ f"fetched {t.have.count()} of {t.n_pieces} pieces, {t.downloaded:,} bytes",
73
+ markup=False,
74
+ )
75
+
76
+ anyio.run(run)
77
+
78
+
79
+ def _only_file(torrent: Torrent[Any]) -> str:
80
+ """The torrent's file, when it only has one, for when the zip isn't named"""
81
+ files = torrent.files
82
+ if len(files) != 1:
83
+ raise click.UsageError(f"the torrent has {len(files)} files, so pick one with --file")
84
+ return files[0].path