| |
@@ -1,15 +1,16 @@
|
| |
- #!/usr/bin/env python
|
| |
+ #!/usr/bin/env python3.9
|
| |
|
| |
# Copyright (C) 2013 Red Hat Inc,
|
| |
- # SPDX-License-Identifier: GPL-2.0+
|
| |
+ # SPDX-License-Identifier: GPL-2.0+
|
| |
#
|
| |
# Author: Anthony Towns <atowns@redhat.com>
|
| |
#
|
| |
# Bencode parsing code from http://effbot.org/zone/bencode.htm
|
| |
|
| |
- from __future__ import print_function
|
| |
- import sys, re, time, os
|
| |
+ import sys
|
| |
import re
|
| |
+ import time
|
| |
+ import os
|
| |
|
| |
def tokenize(text, match=re.compile("([idel])|(\d+):|(-?\d+)").match):
|
| |
i = 0
|
| |
@@ -24,22 +25,22 @@
|
| |
else:
|
| |
yield s
|
| |
|
| |
- def decode_item(next, token):
|
| |
+ def decode_item(next_item, token):
|
| |
if token == "i":
|
| |
# integer: "i" value "e"
|
| |
- data = int(next())
|
| |
- if next() != "e":
|
| |
+ data = int(next_item())
|
| |
+ if next_item() != "e":
|
| |
raise ValueError
|
| |
elif token == "s":
|
| |
# string: "s" value (virtual tokens)
|
| |
- data = next()
|
| |
+ data = next_item()
|
| |
elif token == "l" or token == "d":
|
| |
# container: "l" (or "d") values "e"
|
| |
data = []
|
| |
- tok = next()
|
| |
+ tok = next_item()
|
| |
while tok != "e":
|
| |
- data.append(decode_item(next, tok))
|
| |
- tok = next()
|
| |
+ data.append(decode_item(next_item, tok))
|
| |
+ tok = next_item()
|
| |
if token == "d":
|
| |
data = dict(zip(data[0::2], data[1::2]))
|
| |
else:
|
| |
@@ -49,7 +50,7 @@
|
| |
def decode(text):
|
| |
try:
|
| |
src = tokenize(text)
|
| |
- data = decode_item(src.next, src.next())
|
| |
+ data = decode_item(src.__next__, next(src))
|
| |
for token in src: # look for more tokens
|
| |
raise SyntaxError("trailing junk")
|
| |
except (AttributeError, ValueError, StopIteration):
|
| |
@@ -65,7 +66,7 @@
|
| |
date = argv[2]
|
| |
else:
|
| |
date = time.strftime("%Y-%m-%d")
|
| |
- genini(sys.stdout, ".", group, date)
|
| |
+ genini(sys.stdout, ".", group, date)
|
| |
|
| |
def SIprefix(n):
|
| |
prefix = ["", "k", "M", "G", "T"]
|
| |
@@ -75,32 +76,33 @@
|
| |
x = "%.1f" % (n)
|
| |
prefix.pop(0)
|
| |
return "%s%sB" % (x, prefix[0])
|
| |
-
|
| |
+
|
| |
def torrentsize(filename):
|
| |
- torrentdict = decode(open(filename).read())
|
| |
+ with open(filename, 'rb') as file:
|
| |
+ torrentdict = decode(file.read().decode('latin1')) # Decode binary data using 'latin1'
|
| |
length = sum(y["length"] for y in torrentdict["info"]["files"])
|
| |
- return SIprefix(length)
|
| |
+ return SIprefix(length)
|
| |
|
| |
- def genini(output, path, group, date):
|
| |
+ def genini(output, path, group, date):
|
| |
for dirpath, dirnames, filenames in os.walk(path):
|
| |
- dirnames.sort()
|
| |
- filenames.sort()
|
| |
- for f in filenames:
|
| |
+ dirnames.sort()
|
| |
+ filenames.sort()
|
| |
+ for f in filenames:
|
| |
if not f.endswith(".torrent"):
|
| |
- continue
|
| |
- filepath = os.path.join(dirpath, f)
|
| |
+ continue
|
| |
+ filepath = os.path.join(dirpath, f)
|
| |
displaypath = filepath
|
| |
if displaypath.startswith(dirpath):
|
| |
displaypath = displaypath[len(dirpath):]
|
| |
if displaypath.startswith("/"):
|
| |
displaypath = displaypath[1:]
|
| |
- size = torrentsize(filepath)
|
| |
- output.write("[%s]\n" % (displaypath))
|
| |
- output.write("description=%s\n" % (f[:-8].replace("-", " ")))
|
| |
- output.write("size=%s\n" % (size))
|
| |
- output.write("releasedate=%s\n" % (date))
|
| |
- output.write("group=%s\n" % (group))
|
| |
- output.write("\n")
|
| |
+ size = torrentsize(filepath)
|
| |
+ output.write("[%s]\n" % displaypath)
|
| |
+ output.write("description=%s\n" % f[:-8].replace("-", " "))
|
| |
+ output.write("size=%s\n" % size)
|
| |
+ output.write("releasedate=%s\n" % date)
|
| |
+ output.write("group=%s\n" % group)
|
| |
+ output.write("\n")
|
| |
|
| |
if __name__ == "__main__":
|
| |
main(sys.argv)
|
| |
The issues I encountered while creating torrents for 39, after we migrated to rhel, there was encoding issues, and buffer in/output issues.
Signed-off-by: Samyak Jain samyak.jn11@gmail.com