def _create(metainfo): # TODO: replace with constructor # raises ValueErrors if not good validTorrentFile(metainfo) t = TorrentDef() t.metainfo = metainfo t.metainfo_valid = True # copy stuff into self.input maketorrent.copy_metainfo_to_input(t.metainfo, t.input) # For testing EXISTING LIVE, or EXISTING MERKLE: DISABLE, i.e. keep true infohash if t.get_url_compat(): t.infohash = makeurl.metainfo2swarmid(t.metainfo) else: # Two places where infohash calculated, here and in maketorrent.py # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. t.infohash = sha(bencode(metainfo['info'])).digest() assert isinstance( t.infohash, str), "INFOHASH has invalid type: %s" % type(t.infohash) assert len( t.infohash ) == INFOHASH_LENGTH, "INFOHASH has invalid length: %d" % len( t.infohash) #print >>sys.stderr,"INFOHASH",`t.infohash` return t
def _create(metainfo): # TODO: replace with constructor # raises ValueErrors if not good validTorrentFile(metainfo) t = TorrentDef() t.metainfo = metainfo t.metainfo_valid = True # copy stuff into self.input maketorrent.copy_metainfo_to_input(t.metainfo, t.input) # For testing EXISTING LIVE, or EXISTING MERKLE: DISABLE, i.e. keep true infohash if t.get_url_compat(): t.infohash = makeurl.metainfo2swarmid(t.metainfo) else: # Two places where infohash calculated, here and in maketorrent.py # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. t.infohash = sha(bencode(metainfo["info"])).digest() # print >>sys.stderr,time.asctime(),'-', "INFOHASH",`t.infohash` return t
def _create(metainfo): # TODO: replace with constructor # raises ValueErrors if not good validTorrentFile(metainfo) t = TorrentDef() t.metainfo = metainfo t.metainfo_valid = True # copy stuff into self.input maketorrent.copy_metainfo_to_input(t.metainfo,t.input) # For testing EXISTING LIVE, or EXISTING MERKLE: DISABLE, i.e. keep true infohash if t.get_url_compat(): t.infohash = makeurl.metainfo2swarmid(t.metainfo) else: # Two places where infohash calculated, here and in maketorrent.py # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. t.infohash = sha(bencode(metainfo['info'])).digest() assert isinstance(t.infohash, str), "INFOHASH has invalid type: %s" % type(t.infohash) assert len(t.infohash) == INFOHASH_LENGTH, "INFOHASH has invalid length: %d" % len(t.infohash) #print >>sys.stderr,"INFOHASH",`t.infohash` return t
def finalize(self, userabortflag=None, userprogresscallback=None): """ Create BT torrent file by reading the files added with add_content() and calculate the torrent file's infohash. Creating the torrent file can take a long time and will be carried out by the calling thread. The process can be made interruptable by passing a threading.Event() object via the userabortflag and setting it when the process should be aborted. The also optional userprogresscallback will be called by the calling thread periodically, with a progress percentage as argument. The userprogresscallback function will be called by the calling thread. @param userabortflag threading.Event() object @param userprogresscallback Function accepting a fraction as first argument. """ if self.readonly: raise OperationNotPossibleAtRuntimeException() if self.metainfo_valid: return if "live" in self.input: # Make sure the duration is an integral number of pieces, for # security (live source auth). secs = parse_playtime_to_secs(self.input["playtime"]) pl = float(self.get_piece_length()) length = float(self.input["bps"] * secs) print >>sys.stderr, time.asctime(), "-", "TorrentDef: finalize", length, pl diff = length % pl add = (pl - diff) % pl newlen = int(length + add) # print >>sys.stderr,time.asctime(),'-', "CHECK INFO LENGTH",secs,newlen d = self.input["files"][0] d["length"] = newlen # Note: reading of all files and calc of hashes is done by calling # thread. (infohash, metainfo) = maketorrent.make_torrent_file( self.input, userabortflag=userabortflag, userprogresscallback=userprogresscallback ) if infohash is not None: if self.get_url_compat(): url = makeurl.metainfo2p2purl(metainfo) # Make sure metainfo is preserved, in particular, the url-compat field. swarmid = makeurl.metainfo2swarmid(metainfo) self.infohash = swarmid else: self.infohash = infohash self.metainfo = metainfo self.input["name"] = metainfo["info"]["name"] # May have been 0, meaning auto. self.input["piece length"] = metainfo["info"]["piece length"] self.metainfo_valid = True
def finalize(self, userabortflag=None, userprogresscallback=None): """ Create BT torrent file by reading the files added with add_content() and calculate the torrent file's infohash. Creating the torrent file can take a long time and will be carried out by the calling thread. The process can be made interruptable by passing a threading.Event() object via the userabortflag and setting it when the process should be aborted. The also optional userprogresscallback will be called by the calling thread periodically, with a progress percentage as argument. The userprogresscallback function will be called by the calling thread. @param userabortflag threading.Event() object @param userprogresscallback Function accepting a fraction as first argument. """ if self.readonly: raise OperationNotPossibleAtRuntimeException() if self.metainfo_valid: return if 'live' in self.input: # Make sure the duration is an integral number of pieces, for # security (live source auth). secs = parse_playtime_to_secs(self.input['playtime']) pl = float(self.get_piece_length()) length = float(self.input['bps'] * secs) if DEBUG: print >> sys.stderr, "TorrentDef: finalize: length", length, "piecelen", pl diff = length % pl add = (pl - diff) % pl newlen = int(length + add) #print >>sys.stderr,"CHECK INFO LENGTH",secs,newlen d = self.input['files'][0] d['length'] = newlen # Note: reading of all files and calc of hashes is done by calling # thread. (infohash, metainfo) = maketorrent.make_torrent_file( self.input, userabortflag=userabortflag, userprogresscallback=userprogresscallback) if infohash is not None: if self.get_url_compat(): url = makeurl.metainfo2p2purl(metainfo) # Make sure metainfo is preserved, in particular, the url-compat field. swarmid = makeurl.metainfo2swarmid(metainfo) self.infohash = swarmid else: self.infohash = infohash self.metainfo = metainfo self.input['name'] = metainfo['info']['name'] # May have been 0, meaning auto. self.input['piece length'] = metainfo['info']['piece length'] self.metainfo_valid = True assert self.infohash is None or isinstance( self.infohash, str), "INFOHASH has invalid type: %s" % type(self.infohash) assert self.infohash is None or len( self.infohash ) == INFOHASH_LENGTH, "INFOHASH has invalid length: %d" % len( self.infohash)