sciop-cli 0.1.1__tar.gz → 0.1.3__tar.gz

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.
Files changed (30) hide show
  1. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/PKG-INFO +1 -1
  2. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/pyproject.toml +1 -1
  3. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/cli/torrent.py +31 -6
  4. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/const.py +1 -1
  5. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/data.py +1 -0
  6. sciop_cli-0.1.3/src/sciop_cli/exceptions.py +42 -0
  7. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/torrent.py +58 -3
  8. sciop_cli-0.1.3/tests/data/basic_v2.torrent +3 -0
  9. sciop_cli-0.1.3/tests/test_cli/test_torrent.py +126 -0
  10. sciop_cli-0.1.3/tests/test_data.py +26 -0
  11. sciop_cli-0.1.1/tests/test_cli/test_torrent.py +0 -40
  12. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/LICENSE +0 -0
  13. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/README.md +0 -0
  14. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/__init__.py +0 -0
  15. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/__main__.py +0 -0
  16. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/cli/__init__.py +0 -0
  17. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/cli/main.py +0 -0
  18. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/cli/manifest.py +0 -0
  19. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/file.py +0 -0
  20. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/models/__init__.py +0 -0
  21. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/schema.py +0 -0
  22. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/src/sciop_cli/types.py +0 -0
  23. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/.DS_Store +0 -0
  24. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/__init__.py +0 -0
  25. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/conftest.py +0 -0
  26. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/data/basic/part_1/subdir/part_1 +0 -0
  27. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/data/basic/part_2/subdir/part_2 +0 -0
  28. /sciop_cli-0.1.1/tests/data/basic.torrent → /sciop_cli-0.1.3/tests/data/basic_hybrid.torrent +0 -0
  29. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/test_cli/__init__.py +0 -0
  30. {sciop_cli-0.1.1 → sciop_cli-0.1.3}/tests/test_temp.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sciop-cli
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: A CLI for sciop
5
5
  Author-Email: cosmicexplorer <dmcC2@hypnicjerk.ai>, sneakers-the-rat <sneakers-the-rat@protonmail.com>
6
6
  License: EUPL-1.2
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sciop-cli"
3
- version = "0.1.1"
3
+ version = "0.1.3"
4
4
  description = "A CLI for sciop"
5
5
  authors = [
6
6
  { name = "cosmicexplorer", email = "dmcC2@hypnicjerk.ai" },
@@ -46,10 +46,15 @@ def pack() -> None:
46
46
  "If not present, use the default trackers from https://sciop.net/docs/uploading/default_trackers.txt",
47
47
  )
48
48
  @click.option(
49
- "--no-default-trackers",
49
+ "--default-trackers/--no-default-trackers",
50
50
  is_flag=True,
51
- default=False,
52
- help="Do not use the default trackers if no trackers are provided",
51
+ default=None,
52
+ help="If trackers are specified with --tracker, "
53
+ "--default-trackers appends the default trackers to that list, "
54
+ "otherwise just use the supplied trackers (--no-default-trackers has no effect). "
55
+ "If no trackers are specified, "
56
+ "--no-default-trackers prevents adding the default tracker list,"
57
+ "which is done by default (--default-trackers has no effect).",
53
58
  )
54
59
  @click.option(
55
60
  "-s",
@@ -89,6 +94,13 @@ def pack() -> None:
89
94
  "Similar torrents are torrents who have files in common with this torrent, "
90
95
  "clients are able to reuse files from the other torrents if they already have them downloaded.",
91
96
  )
97
+ @click.option(
98
+ "-2",
99
+ "--v2",
100
+ is_flag=True,
101
+ default=False,
102
+ help="Make a v2-only torrent (otherwise, hybrid v1/v2)",
103
+ )
92
104
  @click.option("--progress/--no-progress", default=True, help="Enable progress bar (default True)")
93
105
  @click.option(
94
106
  "-o",
@@ -100,13 +112,14 @@ def pack() -> None:
100
112
  )
101
113
  def create(
102
114
  path: Path,
103
- tracker: list[str] | None = None,
104
- no_default_trackers: bool = False,
115
+ tracker: list[str] | tuple[str] | None = None,
116
+ default_trackers: bool | None = None,
105
117
  piece_size: PieceSize = 512 * (2**10),
106
118
  comment: str | None = None,
107
119
  creator: str = DEFAULT_TORRENT_CREATOR,
108
120
  webseed: list[str] | None = None,
109
121
  similar: list[str] | None = None,
122
+ v2: bool = False,
110
123
  progress: bool = True,
111
124
  output: Path | None = None,
112
125
  ) -> None:
@@ -120,8 +133,19 @@ def create(
120
133
  form details on fields, all input here is passed through to
121
134
  libtorrent's creation methods.
122
135
  """
123
- if tracker is None and not no_default_trackers:
136
+ # recast tuple to list or none rather than tuple or empty tuple
137
+ tracker = list(tracker) if tracker else None
138
+
139
+ if not tracker and (default_trackers is None or default_trackers):
140
+ click.echo(
141
+ "No trackers specified, using default trackers from "
142
+ "sciop.net/docs/uploading/default_trackers.txt"
143
+ )
124
144
  tracker = get_default_trackers()
145
+ elif tracker and default_trackers:
146
+ default_tracker_list = get_default_trackers()
147
+ tracker.extend(default_tracker_list)
148
+
125
149
  result = create_torrent(
126
150
  path,
127
151
  trackers=tracker,
@@ -130,6 +154,7 @@ def create(
130
154
  creator=creator,
131
155
  webseeds=webseed,
132
156
  similar=similar,
157
+ version="v2" if v2 else "hybrid",
133
158
  pbar=progress,
134
159
  bencode=True,
135
160
  )
@@ -1,4 +1,4 @@
1
1
  from importlib.metadata import version
2
2
 
3
3
  DEFAULT_TORRENT_CREATOR = f"sciop-cli {version('sciop-cli')}"
4
- EXCLUDE_FILES = ".DS_Store"
4
+ EXCLUDE_FILES = (".DS_Store", "Thumbs.db")
@@ -16,6 +16,7 @@ DEFAULT_TRACKER_SOURCE = "https://sciop.net/docs/uploading/default_trackers.txt"
16
16
 
17
17
  def get_default_trackers(tracker_source: str = DEFAULT_TRACKER_SOURCE) -> list[str]:
18
18
  if not DEFAULT_TRACKERS.exists():
19
+ DATA_DIR.mkdir(parents=True, exist_ok=True)
19
20
  res = requests.get(tracker_source)
20
21
  res.raise_for_status()
21
22
  with open(DEFAULT_TRACKERS, "w") as f:
@@ -0,0 +1,42 @@
1
+ """
2
+ Exception and warning classes.
3
+ """
4
+
5
+ # --------------------------------------------------
6
+ # Exceptions
7
+ # --------------------------------------------------
8
+
9
+
10
+ class SciopException(Exception):
11
+ """
12
+ Generic top-level exception class.
13
+ Mixin other base exception classes as needed.
14
+ """
15
+
16
+
17
+ class TorrentCreateException(SciopException):
18
+ """Exception during torrent creation"""
19
+
20
+
21
+ class NoFilesException(TorrentCreateException, ValueError, FileNotFoundError):
22
+ """No files found for a created torrent, e.g. from an empty directory"""
23
+
24
+
25
+ # --------------------------------------------------
26
+ # Warnings
27
+ # --------------------------------------------------
28
+
29
+
30
+ class SciopWarning(UserWarning):
31
+ """
32
+ Generic top-level warning class.
33
+ Mixin other base warning classes as needed
34
+ """
35
+
36
+
37
+ class TorrentCreateWarning(SciopWarning, RuntimeWarning):
38
+ """A warning emitted during torrent creation"""
39
+
40
+
41
+ class NoTrackersWarning(TorrentCreateWarning):
42
+ """No trackers passed while creating a torrent"""
@@ -1,5 +1,7 @@
1
+ import warnings
1
2
  from math import floor
2
3
  from pathlib import Path
4
+ from typing import Literal as L
3
5
 
4
6
  import bencode_rs
5
7
  import libtorrent
@@ -7,6 +9,7 @@ from pydantic import TypeAdapter
7
9
  from tqdm import tqdm
8
10
 
9
11
  from sciop_cli.const import DEFAULT_TORRENT_CREATOR, EXCLUDE_FILES
12
+ from sciop_cli.exceptions import NoTrackersWarning
10
13
  from sciop_cli.types import PieceSize
11
14
 
12
15
 
@@ -38,10 +41,44 @@ def create_torrent(
38
41
  creator: str = DEFAULT_TORRENT_CREATOR,
39
42
  webseeds: list[str] | None = None,
40
43
  similar: list[str] | None = None,
44
+ version: L["v1", "v2", "hybrid"] = "hybrid",
41
45
  bencode: bool = True,
42
46
  piece_size: PieceSize = 512 * (2**10),
43
47
  pbar: bool = False,
44
48
  ) -> dict | bytes:
49
+ """
50
+ Create a hybrid v1/v2 torrent with libtorrent
51
+
52
+ Args:
53
+ path: File or directory to create a torrent for
54
+ trackers: list of trackers to add to the torrent.
55
+ Each torrent is added on a separate tier, in order,
56
+ so that by default clients announce to all trackers
57
+ comment: Additional comment string embedded in the torrent
58
+ creator: Annotation of what tool was used to create the torrent,
59
+ defaults to `sciop-cli-{version}`
60
+ webseeds: List of HTTP urls where the content can also be found.
61
+ If `path` is a directory, the files on the server
62
+ must match the directory structure of the torrent and their content exactly.
63
+ similar: Infohashes of torrents that have identical files to those in this torrent.
64
+ Clients can use this to deduplicate downloads.
65
+ version: Torrent version to create. Default ``"hybrid"`` creates v1/v2 compatible torrents.
66
+ v1 is *not recommended.*
67
+ bencode: If ``True`` ( default ) return the bencoded bytestring.
68
+ Otherwise return a python dictionary
69
+ piece_size: The size of data chunks to hash.
70
+ Choosing a piece size can be complicated, but ideally you want
71
+ to pick a piece size that yields 20-50k pieces, and less than <100k.
72
+ Smaller torrents can have fewer pieces, in that case match the piece size
73
+ to being slightly smaller than the median file size.
74
+ Once torrents start to have >200k pieces, clients suffer to efficiently
75
+ track which peers have what pieces, and also struggle to cache the data.
76
+ pbar: It ``True``, show a progress bar while hashing pieces.
77
+
78
+ Returns:
79
+ bytes: bencoded torrent ready for writing
80
+ dict: python-formatted torrent file dict
81
+ """
45
82
  path = Path(path)
46
83
  fs = libtorrent.file_storage()
47
84
 
@@ -62,13 +99,31 @@ def create_torrent(
62
99
  else:
63
100
  fs.add_file(path.name, path.stat().st_size)
64
101
 
102
+ if fs.num_files() == 0:
103
+ raise
104
+
65
105
  piece_size = TypeAdapter(PieceSize).validate_python(piece_size)
66
106
 
67
- torrent = libtorrent.create_torrent(fs, piece_size=piece_size)
107
+ flags = 0
108
+ if version == "v1":
109
+ flags = libtorrent.create_torrent.v1_only
110
+ elif version == "v2":
111
+ flags = libtorrent.create_torrent.v2_only
112
+
113
+ torrent = libtorrent.create_torrent(fs, piece_size=piece_size, flags=flags)
68
114
 
69
115
  if trackers:
70
- for tracker in trackers:
71
- torrent.add_tracker(tracker)
116
+ for tier, tracker in enumerate(trackers):
117
+ torrent.add_tracker(tracker, tier)
118
+ else:
119
+ warnings.warn(
120
+ "No trackers passed when creating a torrent. "
121
+ "This torrent will likely not be able to be seeded efficiently. "
122
+ "Consider adding trackers, or use the default trackers "
123
+ "(with `sciop_cli.data.get_default_trackers()`)",
124
+ NoTrackersWarning,
125
+ stacklevel=2,
126
+ )
72
127
 
73
128
  if webseeds:
74
129
  for webseed in webseeds:
@@ -0,0 +1,3 @@
1
+ d8:announce22:udp://example.com:696913:announce-listll22:udp://example.com:696931:http://example.com/announce.phpee7:comment4:test10:created by11:sciop-tests4:infod9:file treed6:part_1d6:subdird6:part_1d0:d6:lengthi65536e11:pieces root32:El���/���U����d�Pk*U�7ܼ�9eeee6:part_2d6:subdird6:part_2d0:d6:lengthi65536e11:pieces root32:
2
+ ��:H� �w�2$L����Ѵ�ioUјT��eeeee12:meta versioni2e4:name5:basic12:piece lengthi16384ee12:piece layersd32:El���/���U����d�Pk*U�7ܼ�9128:�i�q�ˆ��U:e���g�e����T΄�:7�K(�Mၤ�y�LfTj� G�M�&�"0�O%�{�Rdĺ �V��j �A���Y2U
3
+ ��:H� �w�2$L����Ѵ�ioUјT��128:���fY_E~"ic6U;��?�'��&L+L��l��Cˮ`���{1���w�1� ���ߕ��_�D����l���L�Ս���
@@ -0,0 +1,126 @@
1
+ import bencode_rs
2
+ import pytest
3
+ from click.testing import CliRunner
4
+
5
+ from sciop_cli.cli import torrent
6
+ from sciop_cli.data import get_default_trackers
7
+ from sciop_cli.exceptions import NoTrackersWarning
8
+
9
+ from ..conftest import DATA_DIR
10
+
11
+
12
+ @pytest.mark.parametrize("version", ["hybrid", "v2"])
13
+ def test_create_torrent(tmp_path, version):
14
+ trackers = ["udp://example.com:6969", "http://example.com/announce.php"]
15
+ webseeds = ["https://example.com/files"]
16
+ output = tmp_path / "basic.torrent"
17
+ args = [
18
+ "-p",
19
+ str(DATA_DIR / "basic"),
20
+ "-o",
21
+ str(output),
22
+ "--creator",
23
+ "sciop-tests",
24
+ "--comment",
25
+ "test",
26
+ "-s",
27
+ 16 * (2**10),
28
+ ]
29
+ for tracker in trackers:
30
+ args.extend(["--tracker", tracker])
31
+ for webseed in webseeds:
32
+ args.extend(["--webseed", webseed])
33
+ if version == "v2":
34
+ args.append("--v2")
35
+
36
+ runner = CliRunner()
37
+ result = runner.invoke(torrent.create, args)
38
+ assert result.exit_code == 0
39
+ expected = bencode_rs.bdecode((DATA_DIR / f"basic_{version}.torrent").read_bytes())
40
+ created = bencode_rs.bdecode(output.read_bytes())
41
+
42
+ # remove creation_date, which can't be set from python libtorrent
43
+ del created[b"creation date"]
44
+ if b"creation date" in expected:
45
+ del expected[b"creation date"]
46
+
47
+ # qbittorrent assigns trackers to the same tier by default,
48
+ # while we assign each to a different tier.
49
+ expected[b"announce-list"] = [[t] for t in expected[b"announce-list"][0]]
50
+
51
+ assert bencode_rs.bencode(expected) == bencode_rs.bencode(created)
52
+
53
+
54
+ @pytest.mark.parametrize(
55
+ "tracker",
56
+ [
57
+ ["udp://otherexample.com/:6969"],
58
+ ["udp://otherexample.com/:6969", "udp://thirdexample.com/:6969"],
59
+ None,
60
+ ],
61
+ )
62
+ @pytest.mark.parametrize("default_trackers", [True, False, None])
63
+ def test_create_torrent_with_default_trackers(tmp_path, tracker, default_trackers, capsys):
64
+ """
65
+ - If we aren't given any trackers, and we don't specify default trackers, we add them
66
+ - If we aren't given any trackers, and we specify no default trackers,
67
+ we don't and emit a warning
68
+ - If we are given trackers, and we don't specify default trackers, we don't add them
69
+ - If we are given trackers, and we specify default trackers, we add them.
70
+ """
71
+ output = tmp_path / "basic.torrent"
72
+ args = [
73
+ "-p",
74
+ str(DATA_DIR / "basic"),
75
+ "-o",
76
+ str(output),
77
+ ]
78
+ if tracker:
79
+ for t in tracker:
80
+ args.extend(["--tracker", t])
81
+
82
+ if default_trackers:
83
+ args.append("--default-trackers")
84
+ elif default_trackers is False:
85
+ # aka it's False
86
+ args.append("--no-default-trackers")
87
+
88
+ runner = CliRunner()
89
+ if default_trackers is False and not tracker:
90
+ # ensure we warn when creating torrents with no trackers
91
+ with pytest.warns(NoTrackersWarning):
92
+ result = runner.invoke(torrent.create, args)
93
+ else:
94
+ result = runner.invoke(torrent.create, args)
95
+
96
+ assert result.exit_code == 0
97
+
98
+ created = bencode_rs.bdecode(output.read_bytes())
99
+
100
+ # if creating a torrent with no trackers, return early
101
+ if default_trackers is False and not tracker:
102
+ assert b"announce" not in created
103
+ assert b"announce-list" not in created
104
+ return
105
+ # otherwise if we have a single tracker and no defaults, there isn't an announce list
106
+ elif (tracker and len(tracker) == 1) and (
107
+ default_trackers is None or default_trackers is False
108
+ ):
109
+ assert created[b"announce"] == tracker[0].encode("utf-8")
110
+ assert b"announce-list" not in created
111
+ return
112
+
113
+ default_tracker_list = get_default_trackers()
114
+ flat_tracker_list = []
115
+ for tier in created[b"announce-list"]:
116
+ flat_tracker_list.extend([t.decode("utf-8") for t in tier])
117
+
118
+ # check if we have default trackers (if we should)
119
+ if default_trackers or (not tracker and default_trackers is None):
120
+ assert all([t in flat_tracker_list for t in default_tracker_list])
121
+ else:
122
+ assert not any([t in flat_tracker_list for t in default_tracker_list])
123
+
124
+ # if we supplied a tracker, it should be included and it should be first
125
+ if tracker:
126
+ assert flat_tracker_list[0] == tracker[0]
@@ -0,0 +1,26 @@
1
+ from time import sleep
2
+
3
+ from pydantic import AnyUrl, TypeAdapter
4
+
5
+ from sciop_cli.data import DEFAULT_TRACKERS, get_default_trackers
6
+
7
+
8
+ def test_get_default_trackers():
9
+ """
10
+ We should be able to get default trackers,
11
+ and only get them once if they already exist
12
+ """
13
+ DEFAULT_TRACKERS.unlink(missing_ok=True)
14
+ trackers = get_default_trackers()
15
+ assert DEFAULT_TRACKERS.exists()
16
+ # we got some trackers
17
+ assert len(trackers) > 0
18
+ # they are all URIs
19
+ validator = TypeAdapter(AnyUrl)
20
+ assert all([validator.validate_python(t) for t in trackers])
21
+
22
+ # ensure we don't get it twice
23
+ first_mtime = DEFAULT_TRACKERS.stat().st_mtime_ns
24
+ sleep(0.001)
25
+ trackers2 = get_default_trackers()
26
+ assert DEFAULT_TRACKERS.stat().st_mtime_ns == first_mtime
@@ -1,40 +0,0 @@
1
- import bencode_rs
2
- from click.testing import CliRunner
3
-
4
- from sciop_cli.cli import torrent
5
-
6
- from ..conftest import DATA_DIR
7
-
8
-
9
- def test_create_torrent(tmp_path):
10
- trackers = ["udp://example.com:6969", "http://example.com/announce.php"]
11
- webseeds = ["https://example.com/files"]
12
- output = tmp_path / "basic.torrent"
13
- args = [
14
- "-p",
15
- str(DATA_DIR / "basic"),
16
- "-o",
17
- str(output),
18
- "--creator",
19
- "sciop-tests",
20
- "--comment",
21
- "test",
22
- "-s",
23
- 16 * (2**10),
24
- ]
25
- for tracker in trackers:
26
- args.extend(["--tracker", tracker])
27
- for webseed in webseeds:
28
- args.extend(["--webseed", webseed])
29
-
30
- runner = CliRunner()
31
- result = runner.invoke(torrent.create, args)
32
- assert result.exit_code == 0
33
- expected = bencode_rs.bdecode((DATA_DIR / "basic.torrent").read_bytes())
34
- created = bencode_rs.bdecode(output.read_bytes())
35
-
36
- # remove creation_date, which can't be set from python libtorrent
37
- del expected[b"creation date"]
38
- del created[b"creation date"]
39
-
40
- assert bencode_rs.bencode(expected) == bencode_rs.bencode(created)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes