Пример #1
0
def decode_dict(x, f, params=None):
    if params != None and 'use_ordered_dict' in params:
        r = odict()
    else:
        r = {}
    f = f + 1
    lastkey = None
    while x[f] != 'e':
        k, f = decode_string(x, f)
        lastkey = k
        r[k], f = decode_func[x[f]](x, f, params)

    return (r, f + 1)
Пример #2
0
def decode_dict(x, f, params = None):
    if params != None and 'use_ordered_dict' in params:
        r = odict()
    else:
        r = {}
    f = f + 1
    lastkey = None
    while x[f] != 'e':
        k, f = decode_string(x, f)
        lastkey = k
        r[k], f = decode_func[x[f]](x, f, params)

    return (r, f + 1)
Пример #3
0
    def change_playback_pos(self, oldpos, newpos):
        if oldpos is None:
            valid = self.is_interesting
            for d in self.peer_connections.values():
                interesting = odict()
                has = d['connection'].download.have
                for i in self.get_valid_range_iterator():
                    if has[i] and valid(i):
                        if DEBUGPP:
                            print >> sys.stderr, 'pps::change_playback_pos: add interesting: i', i
                        interesting[i] = 1

                if DEBUGPP:
                    print >> sys.stderr, 'pps::change_playback_pos: oldpos', oldpos, 'newpos', newpos, 'interesting', interesting
                d['interesting'] = interesting

        else:
            for d in self.peer_connections.values():
                if DEBUGPP:
                    print >> sys.stderr, 'pps::change_playback_pos: pop oldpos', oldpos
                d['interesting'].pop(oldpos, 0)
Пример #4
0
    def change_playback_pos(self, oldpos, newpos):
        if oldpos is None:
            valid = self.is_interesting
            for d in self.peer_connections.values():
                interesting = odict()
                has = d['connection'].download.have
                for i in self.get_valid_range_iterator():
                    if has[i] and valid(i):
                        if DEBUGPP:
                            print >> sys.stderr, 'pps::change_playback_pos: add interesting: i', i
                        interesting[i] = 1

                if DEBUGPP:
                    print >> sys.stderr, 'pps::change_playback_pos: oldpos', oldpos, 'newpos', newpos, 'interesting', interesting
                d['interesting'] = interesting

        else:
            for d in self.peer_connections.values():
                if DEBUGPP:
                    print >> sys.stderr, 'pps::change_playback_pos: pop oldpos', oldpos
                d['interesting'].pop(oldpos, 0)
Пример #5
0
def makeinfo(input, userabortflag, userprogresscallback):
    encoding = input['encoding']
    pieces = []
    sh = sha()
    done = 0L
    fs = []
    totalsize = 0L
    totalhashed = 0L
    subs = []
    for file in input['files']:
        inpath = file['inpath']
        outpath = file['outpath']
        if DEBUG:
            print >> sys.stderr, 'makeinfo: inpath', inpath, 'outpath', outpath
        if os.path.isdir(inpath):
            dirsubs = subfiles(inpath)
            subs.extend(dirsubs)
        elif outpath is None:
            subs.append(([os.path.basename(inpath)], inpath))
        else:
            subs.append((filename2pathlist(outpath, skipfirst=True), inpath))

    subs.sort()
    newsubs = []
    for p, f in subs:
        if 'live' in input:
            size = input['files'][0]['length']
        else:
            size = os.path.getsize(f)
        totalsize += size
        newsubs.append((p, f, size))

    subs = newsubs
    if input['piece length'] == 0:
        if input['createmerkletorrent']:
            piece_len_exp = 18
        elif totalsize > 8589934592L:
            piece_len_exp = 21
        elif totalsize > 2147483648L:
            piece_len_exp = 20
        elif totalsize > 536870912:
            piece_len_exp = 19
        elif totalsize > 67108864:
            piece_len_exp = 18
        elif totalsize > 16777216:
            piece_len_exp = 17
        elif totalsize > 4194304:
            piece_len_exp = 16
        else:
            piece_len_exp = 15
        piece_length = 2 ** piece_len_exp
    else:
        piece_length = input['piece length']
    if 'live' not in input:
        for p, f, size in subs:
            pos = 0L
            h = open(f, 'rb')
            if input['makehash_md5']:
                hash_md5 = md5.new()
            if input['makehash_sha1']:
                hash_sha1 = sha()
            if input['makehash_crc32']:
                hash_crc32 = zlib.crc32('')
            while pos < size:
                a = min(size - pos, piece_length - done)
                if userabortflag is not None and userabortflag.isSet():
                    return (None, None)
                readpiece = h.read(a)
                if userabortflag is not None and userabortflag.isSet():
                    return (None, None)
                sh.update(readpiece)
                if input['makehash_md5']:
                    hash_md5.update(readpiece)
                if input['makehash_crc32']:
                    hash_crc32 = zlib.crc32(readpiece, hash_crc32)
                if input['makehash_sha1']:
                    hash_sha1.update(readpiece)
                done += a
                pos += a
                totalhashed += a
                if done == piece_length:
                    pieces.append(sh.digest())
                    done = 0
                    sh = sha()
                if userprogresscallback is not None:
                    userprogresscallback(float(totalhashed) / float(totalsize))

            newdict = odict()
            newdict['length'] = num2num(size)
            newdict['path'] = uniconvertl(p, encoding)
            newdict['path.utf-8'] = uniconvertl(p, 'utf-8')
            for file in input['files']:
                if file['inpath'] == f:
                    if file['playtime'] is not None:
                        newdict['playtime'] = file['playtime']
                    break

            if input['makehash_md5']:
                newdict['md5sum'] = hash_md5.hexdigest()
            if input['makehash_crc32']:
                newdict['crc32'] = '%08X' % hash_crc32
            if input['makehash_sha1']:
                newdict['sha1'] = hash_sha1.digest()
            fs.append(newdict)
            h.close()

        if done > 0:
            pieces.append(sh.digest())
    if len(subs) == 1:
        flkey = 'length'
        flval = num2num(totalsize)
        name = subs[0][0][0]
    else:
        flkey = 'files'
        flval = fs
        outpath = input['files'][0]['outpath']
        l = filename2pathlist(outpath)
        name = l[0]
    infodict = odict()
    infodict['piece length'] = num2num(piece_length)
    infodict[flkey] = flval
    infodict['name'] = uniconvert(name, encoding)
    infodict['name.utf-8'] = uniconvert(name, 'utf-8')
    if 'live' not in input:
        if input['createmerkletorrent']:
            merkletree = MerkleTree(piece_length, totalsize, None, pieces)
            root_hash = merkletree.get_root_hash()
            infodict['root hash'] = root_hash
        else:
            infodict['pieces'] = ''.join(pieces)
    else:
        infodict['live'] = input['live']
    if input.has_key('provider'):
        infodict['provider'] = input['provider']
    if input.has_key('content_id'):
        infodict['content_id'] = input['content_id']
    if input.has_key('premium'):
        infodict['premium'] = input['premium']
    if input.has_key('license'):
        infodict['license'] = input['license']
    if input.has_key('tns'):
        infodict['tns'] = input['tns']
    if 'cs_keys' in input:
        infodict['cs_keys'] = input['cs_keys']
    if 'private' in input:
        infodict['private'] = input['private']
    if 'sharing' in input:
        infodict['sharing'] = input['sharing']
    if 'ns-metadata' in input:
        infodict['ns-metadata'] = input['ns-metadata']
    if len(subs) == 1:
        for file in input['files']:
            if file['inpath'] == f:
                if file['playtime'] is not None:
                    infodict['playtime'] = file['playtime']

    infodict.sort()
    return (infodict, piece_length)
Пример #6
0
def makeinfo(input, userabortflag, userprogresscallback):
    encoding = input['encoding']
    pieces = []
    sh = sha()
    done = 0L
    fs = []
    totalsize = 0L
    totalhashed = 0L
    subs = []
    for file in input['files']:
        inpath = file['inpath']
        outpath = file['outpath']
        if DEBUG:
            print >> sys.stderr, 'makeinfo: inpath', inpath, 'outpath', outpath
        if os.path.isdir(inpath):
            dirsubs = subfiles(inpath)
            subs.extend(dirsubs)
        elif outpath is None:
            subs.append(([os.path.basename(inpath)], inpath))
        else:
            subs.append((filename2pathlist(outpath, skipfirst=True), inpath))

    subs.sort()
    newsubs = []
    for p, f in subs:
        if 'live' in input:
            size = input['files'][0]['length']
        else:
            size = os.path.getsize(f)
        totalsize += size
        newsubs.append((p, f, size))

    subs = newsubs
    if input['piece length'] == 0:
        if input['createmerkletorrent']:
            piece_len_exp = 18
        elif totalsize > 8589934592L:
            piece_len_exp = 21
        elif totalsize > 2147483648L:
            piece_len_exp = 20
        elif totalsize > 536870912:
            piece_len_exp = 19
        elif totalsize > 67108864:
            piece_len_exp = 18
        elif totalsize > 16777216:
            piece_len_exp = 17
        elif totalsize > 4194304:
            piece_len_exp = 16
        else:
            piece_len_exp = 15
        piece_length = 2**piece_len_exp
    else:
        piece_length = input['piece length']
    if 'live' not in input:
        for p, f, size in subs:
            pos = 0L
            h = open(f, 'rb')
            if input['makehash_md5']:
                hash_md5 = md5.new()
            if input['makehash_sha1']:
                hash_sha1 = sha()
            if input['makehash_crc32']:
                hash_crc32 = zlib.crc32('')
            while pos < size:
                a = min(size - pos, piece_length - done)
                if userabortflag is not None and userabortflag.isSet():
                    return (None, None)
                readpiece = h.read(a)
                if userabortflag is not None and userabortflag.isSet():
                    return (None, None)
                sh.update(readpiece)
                if input['makehash_md5']:
                    hash_md5.update(readpiece)
                if input['makehash_crc32']:
                    hash_crc32 = zlib.crc32(readpiece, hash_crc32)
                if input['makehash_sha1']:
                    hash_sha1.update(readpiece)
                done += a
                pos += a
                totalhashed += a
                if done == piece_length:
                    pieces.append(sh.digest())
                    done = 0
                    sh = sha()
                if userprogresscallback is not None:
                    userprogresscallback(float(totalhashed) / float(totalsize))

            newdict = odict()
            newdict['length'] = num2num(size)
            newdict['path'] = uniconvertl(p, encoding)
            newdict['path.utf-8'] = uniconvertl(p, 'utf-8')
            for file in input['files']:
                if file['inpath'] == f:
                    if file['playtime'] is not None:
                        newdict['playtime'] = file['playtime']
                    break

            if input['makehash_md5']:
                newdict['md5sum'] = hash_md5.hexdigest()
            if input['makehash_crc32']:
                newdict['crc32'] = '%08X' % hash_crc32
            if input['makehash_sha1']:
                newdict['sha1'] = hash_sha1.digest()
            fs.append(newdict)
            h.close()

        if done > 0:
            pieces.append(sh.digest())
    if len(subs) == 1:
        flkey = 'length'
        flval = num2num(totalsize)
        name = subs[0][0][0]
    else:
        flkey = 'files'
        flval = fs
        outpath = input['files'][0]['outpath']
        l = filename2pathlist(outpath)
        name = l[0]
    infodict = odict()
    infodict['piece length'] = num2num(piece_length)
    infodict[flkey] = flval
    infodict['name'] = uniconvert(name, encoding)
    infodict['name.utf-8'] = uniconvert(name, 'utf-8')
    if 'live' not in input:
        if input['createmerkletorrent']:
            merkletree = MerkleTree(piece_length, totalsize, None, pieces)
            root_hash = merkletree.get_root_hash()
            infodict['root hash'] = root_hash
        else:
            infodict['pieces'] = ''.join(pieces)
    else:
        infodict['live'] = input['live']
    if input.has_key('provider'):
        infodict['provider'] = input['provider']
    if input.has_key('content_id'):
        infodict['content_id'] = input['content_id']
    if input.has_key('premium'):
        infodict['premium'] = input['premium']
    if input.has_key('license'):
        infodict['license'] = input['license']
    if input.has_key('tns'):
        infodict['tns'] = input['tns']
    if 'cs_keys' in input:
        infodict['cs_keys'] = input['cs_keys']
    if 'private' in input:
        infodict['private'] = input['private']
    if 'sharing' in input:
        infodict['sharing'] = input['sharing']
    if 'ns-metadata' in input:
        infodict['ns-metadata'] = input['ns-metadata']
    if len(subs) == 1:
        for file in input['files']:
            if file['inpath'] == f:
                if file['playtime'] is not None:
                    infodict['playtime'] = file['playtime']

    infodict.sort()
    return (infodict, piece_length)
Пример #7
0
 def got_peer(self, connection):
     PiecePicker.got_peer(self, connection)
     self.peer_connections[connection]['interesting'] = odict()
Пример #8
0
 def got_peer(self, connection):
     PiecePicker.got_peer(self, connection)
     self.peer_connections[connection]['interesting'] = odict()