Skip to content

Commit

Permalink
updates to the rebuild process, added progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpdev committed Oct 6, 2022
1 parent 572ab3a commit ff39d99
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ htmlcov/
*.cover
.pytest_cache/
*.log
# Jupyter Notebook
.codacy-coverage/
# Jupyter Notebook
.env
env/
# IDE settings
Expand Down
354 changes: 135 additions & 219 deletions docs/Source/rebuild/index.html

Large diffs are not rendered by default.

Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/search/search_index.json

Large diffs are not rendered by default.

Binary file modified docs/sitemap.xml.gz
Binary file not shown.
44 changes: 34 additions & 10 deletions torrentfile/rebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import pyben

from torrentfile.hasher import HasherV2
from torrentfile.mixins import CbMixin
from torrentfile.mixins import CbMixin, ProgMixin
from torrentfile.utils import copypath

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -117,7 +117,7 @@ class PieceNode(CbMixin):
Base class representing a single SHA1 hash block of data from a torrent.
"""

def __init__(self, piece: bytes):
def __init__(self, piece: bytes, parent: "Metadata"):
"""
Store information about an individual SHA1 hash for a torrent file.
Expand All @@ -127,7 +127,10 @@ def __init__(self, piece: bytes):
----------
piece : bytes
SHA1 hash bytes
parent : Metadata
Parent node
"""
self.parent = parent
self.piece = piece
self.paths = []
self.result = None
Expand Down Expand Up @@ -177,6 +180,7 @@ def _find_matches(self, filemap: dict, paths: list, data: bytes) -> bool:
if val:
dest_path = os.path.join(self.dest, pathnode.full)
copypath(loc, dest_path)
self.parent.prog_update(1)
self.cb(filename, dest_path)
return val
return False
Expand All @@ -196,7 +200,7 @@ def find_matches(self, filemap: dict, dest: str):
self.result = self._find_matches(filemap, self.paths[:], bytes())


class Metadata(CbMixin):
class Metadata(CbMixin, ProgMixin):
"""
Class containing the metadata contents of a torrent file.
"""
Expand Down Expand Up @@ -270,7 +274,7 @@ def _map_pieces(self):
current = {}
for i in range(total_pieces):
begin = SHA1 * i
piece = PieceNode(self.pieces[begin: begin + SHA1])
piece = PieceNode(self.pieces[begin: begin + SHA1], self)
target = self.piece_length
if remainder:
start = current["length"] - remainder
Expand Down Expand Up @@ -332,7 +336,7 @@ def _parse_tree(self, tree: dict, partials: list):
else:
self._parse_tree(val, partials + [key])

def find_matches(self, filemap: dict, dest: str):
def _match_v1(self, filemap: dict, dest: str):
"""
Check each of the nodes against the filemap dictionary for matches.
Expand All @@ -347,7 +351,7 @@ def find_matches(self, filemap: dict, dest: str):
for node in self.piece_nodes:
node.find_matches(filemap, dest)

def rebuild(self, filemap: dict, dest: str):
def _match_v2(self, filemap: dict, dest: str):
"""
Rebuild method for torrent v2 files.
Expand All @@ -370,9 +374,32 @@ def rebuild(self, filemap: dict, dest: str):
if entry["root"] == hasher.root:
dest_path = os.path.join(dest, entry["full"])
copypath(entry["path"], dest_path)
self.prog_update(1)
self.cb(path, dest_path)
break

def rebuild(self, filemap: dict, dest: str):
"""
Rebuild torrent file contents from filemap at dest.
Searches through the contents of the meta file and compares filenames
with those in the filemap dict, and if found checks their contents,
and copies them to the destination path.
Parameters
----------
filemap : dict
filesystem information
dest : str
destiantion path
"""
self.prog_start(len(self.filenames), self.name, unit="files")
if self.meta_version == 2:
self._match_v2(filemap, dest)
else:
self._match_v1(filemap, dest)
self.prog_close()


class Assembler(CbMixin):
"""
Expand Down Expand Up @@ -458,10 +485,7 @@ def rebuild(self, metafile: Metadata) -> None:
the matches to the destination directory respecting folder
structures along the way.
"""
if metafile.meta_version == 2:
metafile.rebuild(self.filemap, self.dest)
else:
metafile.find_matches(self.filemap, self.dest)
metafile.rebuild(self.filemap, self.dest)

def _get_metafiles(self) -> list:
"""
Expand Down

0 comments on commit ff39d99

Please sign in to comment.