Skip to content

Rebuild

rebuild #

Clases and functions for the rebuild or reassemble subcommand.

Re-assemble a torrent into the propper directory structure as indicated by a torrent meta file, and validate the contents of each file allong the way. Displays a progress bar for each torrent.

Assembler(metafiles: list, contents: list, dest: str) #

Bases: CbMixin

Does most of the work in attempting the structure of torrentfiles.

Requires three paths as arguments. - torrent metafile or directory containing multiple meta files - directory containing the contents of meta file - directory where torrents will be re-assembled

Reassemble given torrent file from given cli arguments.

Rebuild metafiles and contents into their original directory structure as much as possible in the destination directory. Takes two paths as parameters, - file or directory containing 1 or more torrent meta files - path to where the contents are belived to be located.

PARAMETER DESCRIPTION
metafiles

path to torrent metafile or directory containing torrent metafiles.

TYPE: str

contents

path to content or directory containing content that belongs to torrentfile.

TYPE: str

dest

path to the directory where rebuild will take place.

TYPE: str

Source code in torrentfile\rebuild.py
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
def __init__(self, metafiles: list, contents: list, dest: str):
    """
    Reassemble given torrent file from given cli arguments.

    Rebuild metafiles and contents into their original directory
    structure as much as possible in the destination directory.
    Takes two paths as parameters,
    - file or directory containing 1 or more torrent meta files
    - path to where the contents are belived to be located.

    Parameters
    ----------
    metafiles : str
        path to torrent metafile or directory containing torrent metafiles.
    contents : str
        path to content or directory containing content that belongs to
        torrentfile.
    dest: str
        path to the directory where rebuild will take place.
    """
    Metadata.set_callback(self._callback)
    self.counter = 0
    self._lastlog = None
    self.contents = contents
    self.dest = dest
    self.meta_paths = metafiles
    self.metafiles = self._get_metafiles()
    filenames = set()
    for meta in self.metafiles:
        filenames |= meta.filenames
    self.filemap = _index_contents(self.contents, filenames)

assemble_torrents() #

Assemble collection of torrent files into original structure.

RETURNS DESCRIPTION
int

number of files copied

Source code in torrentfile\rebuild.py
484
485
486
487
488
489
490
491
492
493
494
495
496
497
def assemble_torrents(self):
    """
    Assemble collection of torrent files into original structure.

    Returns
    -------
    int
        number of files copied
    """
    for metafile in self.metafiles:
        logger.info("#%s Searching contents for %s", self.counter,
                    metafile.name)
        self.rebuild(metafile)
    return self.counter

rebuild(metafile: Metadata) -> None #

Build the torrent file structure from contents of directory.

Traverse contents dir and compare discovered files with files listed in torrent metadata and copy the matches to the destination directory respecting folder structures along the way.

Source code in torrentfile\rebuild.py
499
500
501
502
503
504
505
506
507
508
def rebuild(self, metafile: Metadata) -> None:
    """
    Build the torrent file structure from contents of directory.

    Traverse contents dir and compare discovered files
    with files listed in torrent metadata and copy
    the matches to the destination directory respecting folder
    structures along the way.
    """
    metafile.rebuild(self.filemap, self.dest)

Metadata(path: str) #

Bases: CbMixin, ProgMixin

Class containing the metadata contents of a torrent file.

Construct metadata object for torrent info.

PARAMETER DESCRIPTION
path

path to the .torrent file.

TYPE: str

Source code in torrentfile\rebuild.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def __init__(self, path: str):
    """
    Construct metadata object for torrent info.

    Parameters
    ----------
    path : str
        path to the .torrent file.
    """
    self.path = os.path.abspath(path)
    self.name = None
    self.piece_length = 1
    self.meta_version = 1
    self.pieces = b""
    self.piece_nodes = []
    self.length = 0
    self.files = []
    self.filenames = set()
    self.extract()
    if self.meta_version == 2:
        self.num_pieces = len(self.filenames)
    else:
        self.num_pieces = math.ceil(len(self.pieces) / SHA1)

extract() #

Decode and extract information for the .torrent file.

Source code in torrentfile\rebuild.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
def extract(self):
    """
    Decode and extract information for the .torrent file.
    """
    meta = pyben.load(self.path)
    info = meta["info"]
    self.piece_length = info["piece length"]
    self.name = info["name"]
    self.meta_version = info.get("meta version", 1)
    self.pieces = info.get("pieces", bytes())
    if self.meta_version == 2:
        self._parse_tree(info["file tree"], [self.name])
    elif "length" in info:
        self.length += info["length"]
        self.is_file = True
        self.filenames.add(info["name"])
        self.files.append({
            "path": Path(self.name).parent,
            "filename": self.name,
            "full": self.name,
            "length": self.length,
        })
    elif "files" in info:
        for f in info["files"]:
            path = f["path"]
            full = os.path.join(self.name, *path)
            self.files.append({
                "path": Path(full).parent,
                "filename": path[-1],
                "full": full,
                "length": f["length"],
            })
            self.length += f["length"]
            self.filenames.add(path[-1])

rebuild(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.

PARAMETER DESCRIPTION
filemap

filesystem information

TYPE: dict

dest

destiantion path

TYPE: str

Source code in torrentfile\rebuild.py
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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 = None
    if self.meta_version == 2:
        self._match_v2(filemap, dest)
    else:
        self._match_v1(filemap, dest)
    if self._prog is not None:
        self.progbar.close_out()

PathNode(start: int = None, stop: int = None, full: str = None, filename: str = None, path: str = None, length: int = None) #

Base class representing information regarding a file included in torrent.

Hold file information that contributes to the contents of torrent.

PARAMETER DESCRIPTION
start

where the piece starts, by default None

TYPE: int, optional DEFAULT: None

stop

where the piece ends, by default None

TYPE: int, optional DEFAULT: None

full

full path, by default None

TYPE: str, optional DEFAULT: None

filename

filename, by default None

TYPE: str, optional DEFAULT: None

path

parent path, by default None

TYPE: str, optional DEFAULT: None

length

size, by default None

TYPE: int, optional DEFAULT: None

Source code in torrentfile\rebuild.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def __init__(
    self,
    start: int = None,
    stop: int = None,
    full: str = None,
    filename: str = None,
    path: str = None,
    length: int = None,
):
    """
    Hold file information that contributes to the contents of torrent.

    Parameters
    ----------
    start : int, optional
        where the piece starts, by default None
    stop : int, optional
        where the piece ends, by default None
    full : str, optional
        full path, by default None
    filename : str, optional
        filename, by default None
    path : str, optional
        parent path, by default None
    length : int, optional
        size, by default None
    """
    self.path = path
    self.start = start
    self.stop = stop
    self.length = length
    self.filename = filename
    self.full = full

__len__() -> int #

Return size of the file.

RETURNS DESCRIPTION
int

total size

Source code in torrentfile\rebuild.py
104
105
106
107
108
109
110
111
112
113
def __len__(self) -> int:
    """
    Return size of the file.

    Returns
    -------
    int
        total size
    """
    return self.length

get_part(path: str) -> bytes #

Extract the part of the file needed to complete the hash.

PARAMETER DESCRIPTION
path

filesystem path location of file.

TYPE: str

RETURNS DESCRIPTION
bytes

part of the file’s contents

Source code in torrentfile\rebuild.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def get_part(self, path: str) -> bytes:
    """
    Extract the part of the file needed to complete the hash.

    Parameters
    ----------
    path : str
        filesystem path location of file.

    Returns
    -------
    bytes
        part of the file's contents
    """
    with open(path, "rb") as fd:
        if self.start:
            fd.seek(self.start)
        if self.stop != -1:
            partial = fd.read(self.stop - self.start)
        else:
            partial = fd.read()
    return partial

PieceNode(piece: bytes) #

Base class representing a single SHA1 hash block of data from a torrent.

Store information about an individual SHA1 hash for a torrent file.

extended_summary

PARAMETER DESCRIPTION
piece

SHA1 hash bytes

TYPE: bytes

Source code in torrentfile\rebuild.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def __init__(self, piece: bytes):
    """
    Store information about an individual SHA1 hash for a torrent file.

    _extended_summary_

    Parameters
    ----------
    piece : bytes
        SHA1 hash bytes
    """
    self.piece = piece
    self.paths = []
    self.result = None
    self.dest = None

append(pathnode: PathNode) #

Append the path argument to the paths list attribute.

PARAMETER DESCRIPTION
pathnode

the pathnode

TYPE: PathNode

Source code in torrentfile\rebuild.py
137
138
139
140
141
142
143
144
145
146
def append(self, pathnode: PathNode):
    """
    Append the path argument to the paths list attribute.

    Parameters
    ----------
    pathnode : PathNode
        the pathnode
    """
    self.paths.append(pathnode)

find_matches(filemap: dict, dest: str) -> bool #

Find the matching files for each path in the node.

PARAMETER DESCRIPTION
filemap

filename and details

TYPE: dict

dest

target destination path

TYPE: str

RETURNS DESCRIPTION
bool

success status

Source code in torrentfile\rebuild.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def find_matches(self, filemap: dict, dest: str) -> bool:
    """
    Find the matching files for each path in the node.

    Parameters
    ----------
    filemap : dict
        filename and details
    dest : str
        target destination path

    Returns
    -------
    bool
        success status
    """
    self.dest = dest
    self.result = self._find_matches(filemap, self.paths[:], bytes())
    return self.result