3 import os, sys, subprocess, locale, urllib2, gobject
9 def download_chunk(base):
10 # download 10kb a time
11 chunk = base.dl_response.read(10240)
12 base.dl_bytes_so_far += len(chunk)
13 base.tarball_file.write(chunk)
16 base.tarball_file.close()
19 percent = float(base.dl_bytes_so_far) / base.dl_total_size
20 base.progressbar.set_fraction(percent)
21 percent = round(percent*100, 2)
22 base.progressbar.set_text("Downloaded %d" % (percent) + '%')
24 sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" % (base.dl_bytes_so_far, base.dl_total_size, percent))
26 if base.dl_bytes_so_far >= base.dl_total_size:
27 sys.stdout.write('\n')
31 class TorBrowserLauncher:
32 def __init__(self, current_tbb_version):
33 # figure out the architecture
34 architecture = subprocess.check_output(['arch']).strip('\n')
36 # figure out the language
37 available_languages = ['en-US', 'ar', 'de', 'es-ES', 'fa', 'fr', 'it', 'ko', 'nl', 'pl', 'pt-PT', 'ru', 'vi', 'zh-CN']
38 language = locale.getdefaultlocale()[0].replace('_', '-')
39 if language not in available_languages:
40 language = language.split('-')[0]
41 if language not in available_languages:
42 for l in available_languages:
43 if l[0:2] == language:
45 # if language isn't available, default to english
46 if language not in available_languages:
49 # make sure local directory structure is setup
50 data_dir = os.getenv('HOME')+'/.torbrowser'
51 download_dir = data_dir+'/download'
52 tbb_dir = data_dir+'/tbb/'+architecture+'/'+language
53 if os.path.exists(download_dir) == False:
54 print 'making '+download_dir
55 os.makedirs(download_dir)
56 if os.path.exists(tbb_dir) == False:
57 print 'making '+tbb_dir
60 # is TBB already installed?
61 tbb_start = tbb_dir+'/start-tor-browser'
62 if os.path.isfile(tbb_start):
63 print 'Launching '+tbb_start
64 subprocess.call([tbb_start])
67 tarball_filename = 'tor-browser-gnu-linux-'+architecture+'-'+current_tbb_version+'-dev-'+language+'.tar.gz'
68 tarball_path = download_dir+'/'+tarball_filename
69 if os.path.exists(tarball_path):
71 print 'Already downloaded'
74 #tarball_url = 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename
75 tarball_url = 'http://127.0.0.1/'+tarball_filename
82 self.current_tbb_version = current_tbb_version
84 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
85 self.window.set_title("Tor Browser Launcher - First Run")
86 self.window.set_border_width(10)
88 self.window.connect("delete_event", self.delete_event)
89 self.window.connect("destroy", self.destroy)
91 self.box = gtk.VBox(False, 20)
92 self.window.add(self.box)
94 self.label = gtk.Label("The first time you run the Tor Browser Launcher you need to download the Tor Browser Bundle. Would you like to download it from the following URL now?")
95 self.label.set_line_wrap(True)
96 self.box.pack_start(self.label, True, True, 0)
99 self.progressbar = gtk.ProgressBar(adjustment=None)
100 self.progressbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
101 self.box.pack_start(self.progressbar, True, True, 0)
103 self.button_box = gtk.HButtonBox()
104 self.button_box.set_layout(gtk.BUTTONBOX_SPREAD)
105 self.box.pack_start(self.button_box, True, True, 0)
106 self.button_box.show()
108 self.download = gtk.Button("Download")
109 self.download.connect("clicked", self.start_download, None)
110 self.button_box.add(self.download)
113 self.exit = gtk.Button("Exit")
114 self.exit.connect("clicked", self.destroy, None)
115 self.button_box.add(self.exit)
121 def delete_event(self, widget, event, data=None):
124 def destroy(self, widget, data=None):
126 gobject.source_remove(self.timer)
131 def start_download(self, widget, data=None):
132 print 'Starting to download '+self.tarball_url
134 # disable the download button
135 self.download.set_sensitive(False)
137 # initialize the progress bar
138 self.progressbar.set_fraction(0)
139 self.progressbar.set_text('Downloaded 0%')
140 self.progressbar.show()
143 self.dl_response = urllib2.urlopen(self.tarball_url);
145 self.dl_total_size = self.dl_response.info().getheader('Content-Length').strip()
146 self.dl_total_size = int(self.dl_total_size)
147 self.dl_bytes_so_far = 0
149 # set a timer to download more chunks
150 self.timer = gobject.timeout_add(1, download_chunk, self)
152 # open a file to write to
153 self.tarball_file = open(self.tarball_path, 'w')
159 if __name__ == "__main__":
160 print 'Tor Browser Launcher'
161 print 'https://github.com/micahflee/torbrowser-launcher'
163 current_tbb_version = '2.3.25-2'
164 app = TorBrowserLauncher(current_tbb_version)