Merge ~andersson123/ubuntu-archive-tools:torrent-testing-script into ubuntu-archive-tools:main

Proposed by Tim Andersson
Status: Needs review
Proposed branch: ~andersson123/ubuntu-archive-tools:torrent-testing-script
Merge into: ubuntu-archive-tools:main
Diff against target: 97 lines (+91/-0)
1 file modified
check-torrents (+91/-0)
Reviewer Review Type Date Requested Status
Ubuntu Package Archive Administrators Pending
Review via email: mp+465005@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Tim Andersson (andersson123) wrote :

Example output:

```
~/canonical/code/ubuntu-archive-tools$ ./check-torrents -r noble
INFO:root:checking hash: 0b33930759bb30a47663952af9c95f1fdd9c820b For image: ubuntu-24.04-live-server-arm64+largemem.iso.torrent
INFO:root:Found code: 0b33930759bb30a47663952af9c95f1fdd9c820b
INFO:root:checking hash: a958368d56536a1b568607171dca92b155e3994b For image: ubuntu-24.04-live-server-arm64.iso.torrent
INFO:root:Found code: a958368d56536a1b568607171dca92b155e3994b
INFO:root:checking hash: ead8c4756e522e8441818ff5ec397d6168f165f6 For image: ubuntu-24.04-live-server-ppc64el.iso.torrent
INFO:root:Found code: ead8c4756e522e8441818ff5ec397d6168f165f6
INFO:root:checking hash: 0f93fc8e83ec3b01865e4e1466d0a3f2889d5345 For image: ubuntu-24.04-live-server-s390x.iso.torrent
INFO:root:Found code: 0f93fc8e83ec3b01865e4e1466d0a3f2889d5345
INFO:root:{
  "ubuntu-24.04-live-server-arm64+largemem.iso.torrent": true,
  "ubuntu-24.04-live-server-arm64.iso.torrent": true,
  "ubuntu-24.04-live-server-ppc64el.iso.torrent": true,
  "ubuntu-24.04-live-server-s390x.iso.torrent": true
}
```

Unmerged commits

7fdda85... by Tim Andersson

feat: add torrent testing script

This script checks all the available .torrent images for a given
release.

It downloads the .torrent images, extracts the hash, and then check
that the image name and hash matches up with:
https://torrent.ubuntu.com/tracker_index

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/check-torrents b/check-torrents
2new file mode 100755
3index 0000000..6b11e57
4--- /dev/null
5+++ b/check-torrents
6@@ -0,0 +1,91 @@
7+#!/usr/bin/python3
8+
9+import argparse
10+import requests
11+import subprocess
12+import re
13+import json
14+import logging
15+import os
16+
17+
18+def parse_args():
19+ parser = argparse.ArgumentParser()
20+ parser.add_argument(
21+ "--release",
22+ "-r",
23+ required=True,
24+ help="Release to test torrents for (codename)",
25+ )
26+ return parser.parse_args()
27+
28+
29+def get_url(release):
30+ return f"rsync://cdimage.ubuntu.com/cdimage/releases/{release}/release/"
31+
32+
33+def rsync_list_torrent_images(walk_me_url):
34+ format = ".iso.torrent"
35+ rsync = subprocess.check_output(
36+ [
37+ "rsync",
38+ "--dry-run",
39+ "--out-format='%f'",
40+ f"{walk_me_url}*{format}",
41+ ],
42+ text=True,
43+ )
44+ t_files = []
45+ for t_file in rsync.splitlines():
46+ t_files.append(t_file.split(" ")[-1])
47+ return t_files
48+
49+
50+def get_torrent_hash(base_url, torrent_image_name):
51+ _ = subprocess.check_call(
52+ [
53+ "rsync",
54+ f"{base_url}{torrent_image_name}",
55+ f"/tmp/{torrent_image_name}"
56+ ]
57+ )
58+ show_transmission = subprocess.check_output(
59+ [
60+ "transmission-show",
61+ f"/tmp/{torrent_image_name}",
62+ ],
63+ text=True,
64+ )
65+ return re.search(r'Hash: (.*)\n', show_transmission).group(1)
66+
67+
68+def check_hash_is_correct(hash, image_name):
69+ logging.info("checking hash: %s For image: %s" % (hash, image_name))
70+ r = requests.get("https://torrent.ubuntu.com/tracker_index").content.decode("utf-8")
71+ for line in r.splitlines():
72+ if image_name.replace(".torrent", "") in line:
73+ code = re.search(r'<code>(.*)</code>', line).group(1)
74+ logging.info("Found code: %s" % code)
75+ return code == hash
76+ return False
77+
78+
79+def check_torrent(base_url, torrent_image_name):
80+ hash = get_torrent_hash(base_url, torrent_image_name)
81+ return check_hash_is_correct(hash, torrent_image_name)
82+
83+
84+def main():
85+ logging.basicConfig(level=logging.INFO)
86+ args = parse_args()
87+ url_to_walk = get_url(args.release)
88+ images_to_check = rsync_list_torrent_images(url_to_walk)
89+ torrent_results = {}
90+ for torrent_img in images_to_check:
91+ torrent_results[torrent_img] = check_torrent(url_to_walk, torrent_img)
92+ os.remove(f"/tmp/{torrent_img}")
93+ logging.info(json.dumps(torrent_results, indent=2))
94+
95+
96+if __name__ == "__main__":
97+ main()

Subscribers

People subscribed via source and target branches