home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / btmakemetafile.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.1 KB  |  167 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. from sys import argv
  5. from os.path import getsize, split, join, abspath, isdir
  6. from os import listdir
  7. from sha import sha
  8. from copy import copy
  9. from string import strip
  10. from BitTorrent.bencode import bencode
  11. from BitTorrent.btformats import check_info
  12. from BitTorrent.parseargs import parseargs, formatDefinitions
  13. from threading import Event
  14. from time import time
  15. defaults = [
  16.     ('piece_size_pow2', 18, 'which power of 2 to set the piece size to'),
  17.     ('comment', '', 'optional human-readable comment to put in .torrent'),
  18.     ('target', '', 'optional target file for the torrent')]
  19. ignore = [
  20.     'core',
  21.     'CVS']
  22.  
  23. def dummy(v):
  24.     pass
  25.  
  26.  
  27. def make_meta_file(file, url, piece_len_exp = 18, flag = Event(), progress = dummy, progress_percent = 1, comment = None, target = None):
  28.     if piece_len_exp == None:
  29.         piece_len_exp = 18
  30.     
  31.     piece_length = 2 ** piece_len_exp
  32.     (a, b) = split(file)
  33.     if not target:
  34.         if b == '':
  35.             f = a + '.torrent'
  36.         else:
  37.             f = join(a, b + '.torrent')
  38.     else:
  39.         f = target
  40.     info = makeinfo(file, piece_length, flag, progress, progress_percent)
  41.     if flag.isSet():
  42.         return None
  43.     
  44.     check_info(info)
  45.     h = open(f, 'wb')
  46.     data = {
  47.         'info': info,
  48.         'announce': strip(url),
  49.         'creation date': long(time()) }
  50.     if comment:
  51.         data['comment'] = comment
  52.     
  53.     h.write(bencode(data))
  54.     h.close()
  55.  
  56.  
  57. def calcsize(file):
  58.     if not isdir(file):
  59.         return getsize(file)
  60.     
  61.     total = 0
  62.     for s in subfiles(abspath(file)):
  63.         total += getsize(s[1])
  64.     
  65.     return total
  66.  
  67.  
  68. def makeinfo(file, piece_length, flag, progress, progress_percent = 1):
  69.     file = abspath(file)
  70.     if isdir(file):
  71.         subs = subfiles(file)
  72.         subs.sort()
  73.         pieces = []
  74.         sh = sha()
  75.         done = 0
  76.         fs = []
  77.         totalsize = 0.0
  78.         totalhashed = 0
  79.         for p, f in subs:
  80.             totalsize += getsize(f)
  81.         
  82.         for p, f in subs:
  83.             pos = 0
  84.             size = getsize(f)
  85.             fs.append({
  86.                 'length': size,
  87.                 'path': p })
  88.             h = open(f, 'rb')
  89.             while pos < size:
  90.                 a = min(size - pos, piece_length - done)
  91.                 sh.update(h.read(a))
  92.                 if flag.isSet():
  93.                     return None
  94.                 
  95.                 done += a
  96.                 pos += a
  97.                 totalhashed += a
  98.                 if done == piece_length:
  99.                     pieces.append(sh.digest())
  100.                     done = 0
  101.                     sh = sha()
  102.                 
  103.                 if progress_percent:
  104.                     progress(totalhashed / totalsize)
  105.                     continue
  106.                 progress(a)
  107.             h.close()
  108.         
  109.         if done > 0:
  110.             pieces.append(sh.digest())
  111.         
  112.         return {
  113.             'pieces': ''.join(pieces),
  114.             'piece length': piece_length,
  115.             'files': fs,
  116.             'name': split(file)[1] }
  117.     else:
  118.         size = getsize(file)
  119.         pieces = []
  120.         p = 0
  121.         h = open(file, 'rb')
  122.         while p < size:
  123.             x = h.read(min(piece_length, size - p))
  124.             if flag.isSet():
  125.                 return None
  126.             
  127.             pieces.append(sha(x).digest())
  128.             p += piece_length
  129.             if p > size:
  130.                 p = size
  131.             
  132.             if progress_percent:
  133.                 progress(float(p) / size)
  134.                 continue
  135.             progress(min(piece_length, size - p))
  136.         h.close()
  137.         return {
  138.             'pieces': ''.join(pieces),
  139.             'piece length': piece_length,
  140.             'length': size,
  141.             'name': split(file)[1] }
  142.  
  143.  
  144. def subfiles(d):
  145.     r = []
  146.     stack = [
  147.         ([], d)]
  148.     while len(stack) > 0:
  149.         (p, n) = stack.pop()
  150.         if isdir(n):
  151.             for s in listdir(n):
  152.                 if s not in ignore and s[:1] != '.':
  153.                     stack.append((copy(p) + [
  154.                         s], join(n, s)))
  155.                     continue
  156.             
  157.         r.append((p, n))
  158.     return r
  159.  
  160.  
  161. def prog(amount):
  162.     print '%.1f%% complete\r' % amount * 100,
  163.  
  164. if __name__ == '__main__':
  165.     pass
  166. None if len(argv) < 3 else None<EXCEPTION MATCH>ValueError
  167.