import libtorrent as lt, os, hashlib, shutil root = "tests/fixtures" data = os.path.join(root, "data") shutil.rmtree(data, ignore_errors=True) os.makedirs(os.path.join(data, "multi", "sub"), exist_ok=True) # deterministic content with open(os.path.join(data, "single.bin"), "wb") as f: f.write(bytes((i*131+7) & 0xff for i in range(200000))) with open(os.path.join(data, "multi", "a.txt"), "wb") as f: f.write(b"hello naut " * 5000) with open(os.path.join(data, "multi", "sub", "b.dat"), "wb") as f: f.write(bytes((i*7) & 0xff for i in range(90000))) def make(name, src, flags): fs = lt.file_storage() lt.add_files(fs, src) t = lt.create_torrent(fs, piece_size=16384, flags=flags) parent = os.path.dirname(src) if os.path.isfile(src) else os.path.dirname(src.rstrip("/")) t.add_tracker("http://tracker.example.com:8080/announce", 0) t.add_tracker("udp://tracker.example.com:8080", 1) lt.set_piece_hashes(t, parent) ent = t.generate() blob = lt.bencode(ent) path = os.path.join(root, name) with open(path, "wb") as f: f.write(blob) ti = lt.torrent_info(ent) ih = ti.info_hashes() v1 = str(ih.v1) if ih.has_v1() else "-" v2 = str(ih.v2) if ih.has_v2() else "-" print(f"{name}\tv1={v1}\tv2={v2}\tpieces={ti.num_pieces()}\tsize={ti.total_size()}") return path V1 = lt.create_torrent.v1_only V2 = lt.create_torrent.v2_only make("single_v1.torrent", os.path.join(data, "single.bin"), V1) make("multi_v1.torrent", os.path.join(data, "multi"), V1) make("hybrid.torrent", os.path.join(data, "multi"), 0) make("v2.torrent", os.path.join(data, "multi"), V2)