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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python

# Written by Bram Cohen
# see LICENSE.txt for license information

from BitTornado import PSYCO
if PSYCO.psyco:
    try:
        import psyco
        assert psyco.__version__ >= 0x010100f0
        psyco.full()
    except:
        pass

from sys import argv, version
assert version >= '2', "Install Python 2.0 or greater"
from BitTornado.BT1.makemetafile import completedir
from threading import Event, Thread
import sys
from os import getcwd
from os.path import join
try:
    import wx
except:
    print 'wxPython is either not installed or has not been installed properly.'
    sys.exit(1)

try:
    True
except:
    True = 1
    False = 0

wxEVT_INVOKE = wx.NewEventType()

def EVT_INVOKE(win, func):
    win.Connect(-1, -1, wxEVT_INVOKE, func)

class InvokeEvent(wx.PyEvent):
    def __init__(self, func, args, kwargs):
        wx.PyEvent.__init__(self)
        self.SetEventType(wxEVT_INVOKE)
        self.func = func
        self.args = args
        self.kwargs = kwargs

class DownloadInfo:
    def __init__(self):
        frame = wx.Frame(None, -1, 'BitTorrent complete dir 1.0.1', size = wx.Size(550, 250))
        self.frame = frame

        panel = wx.Panel(frame, -1)

        gridSizer = wx.FlexGridSizer(cols = 2, vgap = 15, hgap = 8)
        
        gridSizer.Add(wx.StaticText(panel, -1, 'directory to build:'))
        self.dirCtl = wx.TextCtrl(panel, -1, '')

        b = wx.BoxSizer(wx.HORIZONTAL)
        b.Add(self.dirCtl, 1, wx.EXPAND)
        b.Add((10, 10), 0, wx.EXPAND)
        button = wx.Button(panel, -1, 'select')
        b.Add(button, 0, wx.EXPAND)
        wx.EVT_BUTTON(frame, button.GetId(), self.select)

        gridSizer.Add(b, 0, wx.EXPAND)

        gridSizer.Add(wx.StaticText(panel, -1, 'announce url:'))
        self.annCtl = wx.TextCtrl(panel, -1, 'http://my.tracker:6969/announce')
        gridSizer.Add(self.annCtl, 0, wx.EXPAND)

        gridSizer.Add(wx.StaticText(panel, -1, 'piece size:'))
        self.piece_length = wx.Choice(panel, -1, choices = ['2 ** 21', '2 ** 20', '2 ** 19',
            '2 ** 18', '2 ** 17', '2 ** 16', '2 ** 15'])
        self.piece_length.SetSelection(3)
        gridSizer.Add(self.piece_length)

        gridSizer.AddGrowableCol(1)
 
        border = wx.BoxSizer(wx.VERTICAL)
        border.Add(gridSizer, 0, wx.EXPAND | wx.NORTH | wx.EAST | wx.WEST, 25)
        b2 = wx.Button(panel, -1, 'make')
        border.Add((10, 10), 1, wx.EXPAND)
        border.Add(b2, 0, wx.ALIGN_CENTER | wx.SOUTH, 20)
        wx.EVT_BUTTON(frame, b2.GetId(), self.complete)
        panel.SetSizer(border)
        panel.SetAutoLayout(True)

    def select(self, x):
        dl = wx.DirDialog(self.frame, style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dl.ShowModal() == wx.ID_OK:
            self.dirCtl.SetValue(dl.GetPath())

    def complete(self, x):
        if self.dirCtl.GetValue() == '':
            dlg = wx.MessageDialog(self.frame, message = 'You must select a directory',
                caption = 'Error', style = wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()
            return
        try:
            ps = (21 - self.piece_length.GetSelection())
            CompleteDir(self.dirCtl.GetValue(), self.annCtl.GetValue(), ps)
        except:
            print_exc()

from traceback import print_exc

class CompleteDir:
    def __init__(self, d, a, pl):
        self.d = d
        self.a = a
        self.pl = pl
        self.flag = Event()
        frame = wx.Frame(None, -1, 'BitTorrent make directory', size = wx.Size(550, 250))
        self.frame = frame

        panel = wx.Panel(frame, -1)

        gridSizer = wx.FlexGridSizer(cols = 1, vgap = 15, hgap = 8)

        self.currentLabel = wx.StaticText(panel, -1, 'checking file sizes')
        gridSizer.Add(self.currentLabel, 0, wx.EXPAND)
        self.gauge = wx.Gauge(panel, -1, range = 1000, style = wx.GA_SMOOTH)
        gridSizer.Add(self.gauge, 0, wx.EXPAND)
        gridSizer.Add((10, 10), 1, wx.EXPAND)
        self.button = wx.Button(panel, -1, 'cancel')
        gridSizer.Add(self.button, 0, wx.ALIGN_CENTER)
        gridSizer.AddGrowableRow(2)
        gridSizer.AddGrowableCol(0)

        g2 = wx.FlexGridSizer(cols = 1, vgap = 15, hgap = 8)
        g2.Add(gridSizer, 1, wx.EXPAND | wx.ALL, 25)
        g2.AddGrowableRow(0)
        g2.AddGrowableCol(0)
        panel.SetSizer(g2)
        panel.SetAutoLayout(True)
        wx.EVT_BUTTON(frame, self.button.GetId(), self.done)
        wx.EVT_CLOSE(frame, self.done)
        EVT_INVOKE(frame, self.onInvoke)
        frame.Show(True)
        Thread(target = self.complete).start()

    def complete(self):
        params = {'piece_size_pow2': self.pl}
        try:
            completedir(self.d, self.a, params, self.flag, self.valcallback, self.filecallback)
            if not self.flag.isSet():
                self.currentLabel.SetLabel('Done!')
                self.gauge.SetValue(1000)
                self.button.SetLabel('Close')
        except (OSError, IOError), e:
            self.currentLabel.SetLabel('Error!')
            self.button.SetLabel('Close')
            dlg = wx.MessageDialog(self.frame, message = 'Error - ' + str(e),
                caption = 'Error', style = wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()

    def valcallback(self, amount):
        self.invokeLater(self.onval, [amount])

    def onval(self, amount):
        self.gauge.SetValue(int(amount * 1000))

    def filecallback(self, f):
        self.invokeLater(self.onfile, [f])

    def onfile(self, f):
        self.currentLabel.SetLabel('building ' + join(self.d, f) + '.torrent')

    def onInvoke(self, event):
        if not self.flag.isSet():
            apply(event.func, event.args, event.kwargs)

    def invokeLater(self, func, args = [], kwargs = {}):
        if not self.flag.isSet():
            wx.PostEvent(self.frame, InvokeEvent(func, args, kwargs))

    def done(self, event):
        self.flag.set()
        self.frame.Destroy()

class btWxApp(wx.App):
    def OnInit(self):
        d = DownloadInfo()
        d.frame.Show(True)
        self.SetTopWindow(d.frame)
        return True

if __name__ == '__main__':
    btWxApp().MainLoop()