~ubuntu-branches/debian/sid/bittornado/sid

0.1.1 by Michael Vogt
Import upstream version 0.3.8
1
#!/usr/bin/env python
2
3
# Written by Bram Cohen
4
# see LICENSE.txt for license information
5
6
from BitTornado import PSYCO
7
if PSYCO.psyco:
8
    try:
9
        import psyco
10
        assert psyco.__version__ >= 0x010100f0
11
        psyco.full()
12
    except:
13
        pass
14
15
from sys import argv, version
16
assert version >= '2', "Install Python 2.0 or greater"
17
from BitTornado.BT1.makemetafile import completedir
18
from threading import Event, Thread
19
import sys
20
from os import getcwd
21
from os.path import join
22
try:
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
23
    import wx
0.1.1 by Michael Vogt
Import upstream version 0.3.8
24
except:
25
    print 'wxPython is either not installed or has not been installed properly.'
26
    sys.exit(1)
27
28
try:
29
    True
30
except:
31
    True = 1
32
    False = 0
33
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
34
wxEVT_INVOKE = wx.NewEventType()
0.1.1 by Michael Vogt
Import upstream version 0.3.8
35
36
def EVT_INVOKE(win, func):
37
    win.Connect(-1, -1, wxEVT_INVOKE, func)
38
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
39
class InvokeEvent(wx.PyEvent):
0.1.1 by Michael Vogt
Import upstream version 0.3.8
40
    def __init__(self, func, args, kwargs):
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
41
        wx.PyEvent.__init__(self)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
42
        self.SetEventType(wxEVT_INVOKE)
43
        self.func = func
44
        self.args = args
45
        self.kwargs = kwargs
46
47
class DownloadInfo:
48
    def __init__(self):
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
49
        frame = wx.Frame(None, -1, 'BitTorrent complete dir 1.0.1', size = wx.Size(550, 250))
0.1.1 by Michael Vogt
Import upstream version 0.3.8
50
        self.frame = frame
51
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
52
        panel = wx.Panel(frame, -1)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
53
7 by Olly Betts
* Non-maintainer upload.
54
        gridSizer = wx.FlexGridSizer(cols = 2, vgap = 15, hgap = 8)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
55
        
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
56
        gridSizer.Add(wx.StaticText(panel, -1, 'directory to build:'))
57
        self.dirCtl = wx.TextCtrl(panel, -1, '')
58
59
        b = wx.BoxSizer(wx.HORIZONTAL)
60
        b.Add(self.dirCtl, 1, wx.EXPAND)
61
        b.Add((10, 10), 0, wx.EXPAND)
62
        button = wx.Button(panel, -1, 'select')
63
        b.Add(button, 0, wx.EXPAND)
64
        wx.EVT_BUTTON(frame, button.GetId(), self.select)
65
66
        gridSizer.Add(b, 0, wx.EXPAND)
67
68
        gridSizer.Add(wx.StaticText(panel, -1, 'announce url:'))
69
        self.annCtl = wx.TextCtrl(panel, -1, 'http://my.tracker:6969/announce')
70
        gridSizer.Add(self.annCtl, 0, wx.EXPAND)
71
72
        gridSizer.Add(wx.StaticText(panel, -1, 'piece size:'))
73
        self.piece_length = wx.Choice(panel, -1, choices = ['2 ** 21', '2 ** 20', '2 ** 19',
0.1.1 by Michael Vogt
Import upstream version 0.3.8
74
            '2 ** 18', '2 ** 17', '2 ** 16', '2 ** 15'])
75
        self.piece_length.SetSelection(3)
76
        gridSizer.Add(self.piece_length)
77
78
        gridSizer.AddGrowableCol(1)
79
 
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
80
        border = wx.BoxSizer(wx.VERTICAL)
81
        border.Add(gridSizer, 0, wx.EXPAND | wx.NORTH | wx.EAST | wx.WEST, 25)
82
        b2 = wx.Button(panel, -1, 'make')
83
        border.Add((10, 10), 1, wx.EXPAND)
84
        border.Add(b2, 0, wx.ALIGN_CENTER | wx.SOUTH, 20)
85
        wx.EVT_BUTTON(frame, b2.GetId(), self.complete)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
86
        panel.SetSizer(border)
87
        panel.SetAutoLayout(True)
88
89
    def select(self, x):
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
90
        dl = wx.DirDialog(self.frame, style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
91
        if dl.ShowModal() == wx.ID_OK:
0.1.1 by Michael Vogt
Import upstream version 0.3.8
92
            self.dirCtl.SetValue(dl.GetPath())
93
94
    def complete(self, x):
95
        if self.dirCtl.GetValue() == '':
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
96
            dlg = wx.MessageDialog(self.frame, message = 'You must select a directory',
97
                caption = 'Error', style = wx.OK | wx.ICON_ERROR)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
98
            dlg.ShowModal()
99
            dlg.Destroy()
100
            return
101
        try:
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
102
            ps = (21 - self.piece_length.GetSelection())
0.1.1 by Michael Vogt
Import upstream version 0.3.8
103
            CompleteDir(self.dirCtl.GetValue(), self.annCtl.GetValue(), ps)
104
        except:
105
            print_exc()
106
107
from traceback import print_exc
108
109
class CompleteDir:
110
    def __init__(self, d, a, pl):
111
        self.d = d
112
        self.a = a
113
        self.pl = pl
114
        self.flag = Event()
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
115
        frame = wx.Frame(None, -1, 'BitTorrent make directory', size = wx.Size(550, 250))
0.1.1 by Michael Vogt
Import upstream version 0.3.8
116
        self.frame = frame
117
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
118
        panel = wx.Panel(frame, -1)
119
120
        gridSizer = wx.FlexGridSizer(cols = 1, vgap = 15, hgap = 8)
121
122
        self.currentLabel = wx.StaticText(panel, -1, 'checking file sizes')
123
        gridSizer.Add(self.currentLabel, 0, wx.EXPAND)
124
        self.gauge = wx.Gauge(panel, -1, range = 1000, style = wx.GA_SMOOTH)
125
        gridSizer.Add(self.gauge, 0, wx.EXPAND)
126
        gridSizer.Add((10, 10), 1, wx.EXPAND)
127
        self.button = wx.Button(panel, -1, 'cancel')
128
        gridSizer.Add(self.button, 0, wx.ALIGN_CENTER)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
129
        gridSizer.AddGrowableRow(2)
130
        gridSizer.AddGrowableCol(0)
131
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
132
        g2 = wx.FlexGridSizer(cols = 1, vgap = 15, hgap = 8)
133
        g2.Add(gridSizer, 1, wx.EXPAND | wx.ALL, 25)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
134
        g2.AddGrowableRow(0)
135
        g2.AddGrowableCol(0)
136
        panel.SetSizer(g2)
137
        panel.SetAutoLayout(True)
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
138
        wx.EVT_BUTTON(frame, self.button.GetId(), self.done)
139
        wx.EVT_CLOSE(frame, self.done)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
140
        EVT_INVOKE(frame, self.onInvoke)
141
        frame.Show(True)
142
        Thread(target = self.complete).start()
143
144
    def complete(self):
145
        params = {'piece_size_pow2': self.pl}
146
        try:
147
            completedir(self.d, self.a, params, self.flag, self.valcallback, self.filecallback)
148
            if not self.flag.isSet():
149
                self.currentLabel.SetLabel('Done!')
150
                self.gauge.SetValue(1000)
151
                self.button.SetLabel('Close')
152
        except (OSError, IOError), e:
153
            self.currentLabel.SetLabel('Error!')
154
            self.button.SetLabel('Close')
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
155
            dlg = wx.MessageDialog(self.frame, message = 'Error - ' + str(e),
156
                caption = 'Error', style = wx.OK | wx.ICON_ERROR)
0.1.1 by Michael Vogt
Import upstream version 0.3.8
157
            dlg.ShowModal()
158
            dlg.Destroy()
159
160
    def valcallback(self, amount):
161
        self.invokeLater(self.onval, [amount])
162
163
    def onval(self, amount):
164
        self.gauge.SetValue(int(amount * 1000))
165
166
    def filecallback(self, f):
167
        self.invokeLater(self.onfile, [f])
168
169
    def onfile(self, f):
170
        self.currentLabel.SetLabel('building ' + join(self.d, f) + '.torrent')
171
172
    def onInvoke(self, event):
173
        if not self.flag.isSet():
174
            apply(event.func, event.args, event.kwargs)
175
176
    def invokeLater(self, func, args = [], kwargs = {}):
177
        if not self.flag.isSet():
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
178
            wx.PostEvent(self.frame, InvokeEvent(func, args, kwargs))
0.1.1 by Michael Vogt
Import upstream version 0.3.8
179
180
    def done(self, event):
181
        self.flag.set()
182
        self.frame.Destroy()
183
6 by Cameron Dale
* New patch from upstream's CVS to allow torrents that only have an
184
class btWxApp(wx.App):
0.1.1 by Michael Vogt
Import upstream version 0.3.8
185
    def OnInit(self):
186
        d = DownloadInfo()
187
        d.frame.Show(True)
188
        self.SetTopWindow(d.frame)
189
        return True
190
191
if __name__ == '__main__':
192
    btWxApp().MainLoop()