]> git.lizzy.rs Git - torbrowser-launcher.git/commitdiff
working on #7, checking for updates by loading RecommendedTBBVersions
authorMicah Lee <micahflee@riseup.net>
Fri, 22 Feb 2013 17:51:43 +0000 (09:51 -0800)
committerMicah Lee <micahflee@riseup.net>
Fri, 22 Feb 2013 17:51:43 +0000 (09:51 -0800)
torbrowser-launcher

index 616975a2e84fd9dcf200b7369335ad335802ca6a..00b15e981089e429534a42699ba722f09c24e348 100755 (executable)
@@ -1,52 +1,74 @@
 #!/usr/bin/env python
 
-import os, sys, subprocess, locale, urllib2, gobject, time
+import os, sys, subprocess, locale, urllib2, gobject, time, pickle, json
 
 import pygtk
 pygtk.require('2.0')
 import gtk
 
 class TorBrowserLauncher:
-  def __init__(self, current_tbb_version):
+  def __init__(self):
     # initialize the app
-    self.current_tbb_version = current_tbb_version
     self.discover_arch_lang()
     self.build_paths()
     self.mkdirs()
+    self.clear_ui()
+
+    # allow buttons to have icons
+    try:
+      settings = gtk.settings_get_default()
+      settings.props.gtk_button_images = True
+    except:
+      pass
 
     launch_gui = True
 
+    # load settings
+    if self.load_settings():
+      self.build_paths(self.settings['latest_version'])
+
+      # how long was it since the last update check?
+      # 86400 seconds = 24 hours
+      current_timestamp = int(time.time())
+      if current_timestamp - self.settings['last_update_check_timestamp'] >= 86400:
+        # check for update
+        self.set_gui('task', "Checking for Tor Browser update.", 
+          ['download_update_check', 
+           'attempt_update'])
+
+      else:
+        # no need to check for update
+        self.start_launcher()
+
+    else:
+      self.set_gui('error', "Error loading settings. Delete ~/.torbrowser and try again.", [])
+
+    if launch_gui:
+      self.build_ui()
+      gtk.main()
+
+  # download or run TBB
+  def start_launcher(self):
     # is TBB already installed?
     if os.path.isfile(self.paths['file']['start']) and os.access(self.paths['file']['start'], os.X_OK):
-      # does the version file exist?
-      if os.path.isfile(self.paths['file']['version']):
-        installed_tbb_version = open(self.paths['file']['version']).read().strip()
-
-        if installed_tbb_version == current_tbb_version:
-          # current version is tbb is installed, launch it
-          self.run(False)
-          launch_gui = False
-        elif installed_tbb_version < self.current_tbb_version:
-          # there is a tbb upgrade available
-          self.set_gui('task', "Your Tor Browser is out of date.", 
-            ['download_tarball', 
-             'download_tarball_sig', 
-             'verify', 
-             'extract', 
-             'run'])
-        else:
-          # for some reason the installed tbb is newer than the current version?
-          self.set_gui('error', "Something is wrong. The version of Tor Browser Bundle you have installed is newer than the current version?", [])
-
+      if self.settings.installed_version == self.settings.latest_version:
+        # current version of tbb is installed, launch it
+        self.run(False)
+        launch_gui = False
+      elif self.settings.installed_version < self.settings.latest_version:
+        # there is a tbb upgrade available
+        self.set_gui('task', "Your Tor Browser is out of date.", 
+          ['download_tarball', 
+           'download_tarball_sig', 
+           'verify', 
+           'extract', 
+           'run'])
       else:
-        # if tbb is installed but the version file doesn't exist, something is wrong
-        self.set_gui('error', "Something is wrong. You have the Tor Browser Bundle installed, but the version file is missing.", [])
+        # for some reason the installed tbb is newer than the current version?
+        self.set_gui('error', "Something is wrong. The version of Tor Browser Bundle you have installed is newer than the current version?", [])
 
     # not installed
     else:
-      # save the current version to the file
-      open(self.paths['file']['version'], 'w').write(self.current_tbb_version)
-
       # are the tarball and sig already downloaded?
       if os.path.isfile(self.paths['file']['tarball']) and os.path.isfile(self.paths['file']['tarball_sig']):
         # start the gui with verify
@@ -64,10 +86,6 @@ class TorBrowserLauncher:
            'extract', 
            'run'])
 
-    if launch_gui:
-      self.build_ui()
-      gtk.main()
-  
   # discover the architecture and language
   def discover_arch_lang(self):
     # figure out the architecture
@@ -92,33 +110,38 @@ class TorBrowserLauncher:
         self.language = 'en-US'
 
   # build all relevant paths
-  def build_paths(self):
+  def build_paths(self, tbb_version = None):
     tbb_data = os.getenv('HOME')+'/.torbrowser'
-    tarball_filename = 'tor-browser-gnu-linux-'+self.architecture+'-'+self.current_tbb_version+'-dev-'+self.language+'.tar.gz'
-
-    self.paths = {
-      'dir': {
-        'data': tbb_data,
-        'download': tbb_data+'/download',
-        'tbb': tbb_data+'/tbb/'+self.architecture,
-        'gpg': tbb_data+'/gpgtmp'
-      },
-      'file': {
-        'version': tbb_data+'/version',
-        'start': tbb_data+'/tbb/'+self.architecture+'/tor-browser_'+self.language+'/start-tor-browser',
-        'tarball': tbb_data+'/download/'+tarball_filename,
-        'tarball_sig': tbb_data+'/download/'+tarball_filename+'.asc',
-        'verify': '/usr/share/torbrowser-launcher/verify.sh'
-      },
-      'url': {
-        'tarball': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename,
-        'tarball_sig': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename+'.asc'
-      },
-      'filename': {
-        'tarball': tarball_filename,
-        'tarball_sig': tarball_filename+'.asc'
+
+    if tbb_version:
+      tarball_filename = 'tor-browser-gnu-linux-'+self.architecture+'-'+tbb_version+'-dev-'+self.language+'.tar.gz'
+      self.paths['file']['tarball'] = tbb_data+'/download/'+tarball_filename
+      self.paths['file']['tarball_sig'] = tbb_data+'/download/'+tarball_filename+'.asc'
+      self.paths['url']['tarball'] = 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename
+      self.paths['url']['tarball_sig'] = 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename+'.asc'
+      self.paths['filename']['tarball'] = tarball_filename
+      self.paths['filename']['tarball_sig'] = tarball_filename+'.asc'
+
+    else:
+      self.paths = {
+        'dir': {
+          'data': tbb_data,
+          'download': tbb_data+'/download',
+          'tbb': tbb_data+'/tbb/'+self.architecture,
+          'gpg': tbb_data+'/gpgtmp'
+        },
+        'file': {
+          'settings': tbb_data+'/settings',
+          'version': tbb_data+'/version',
+          'start': tbb_data+'/tbb/'+self.architecture+'/tor-browser_'+self.language+'/start-tor-browser',
+          'update_check': tbb_data+'/download/RecommendedTBBVersions',
+          'verify': '/usr/share/torbrowser-launcher/verify.sh'
+        },
+        'url': {
+          'update_check': 'https://check.torproject.org/RecommendedTBBVersions'
+        },
+        'filename': {}
       }
-    }
 
   # create directories that don't exist
   def mkdirs(self):
@@ -134,17 +157,21 @@ class TorBrowserLauncher:
     self.gui_tasks = tasks
     self.gui_autostart = autostart
 
-  # build the application's UI
-  def build_ui(self):
+  # set all gtk variables to False
+  def clear_ui(self):
     self.timer = False
+    self.window = False
+    self.box = False
+    self.label1 = False
+    self.label2 = False
+    self.label = False
+    self.progressbar = False
+    self.button_box = False
+    self.start_button = False
+    self.exit_button = False
 
-    # allow buttons to have icons
-    try:
-      settings = gtk.settings_get_default()
-      settings.props.gtk_button_images = True
-    except:
-      pass
-
+  # build the application's UI
+  def build_ui(self):
     # set up the window
     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
     self.window.set_title("Tor Browser")
@@ -243,7 +270,15 @@ class TorBrowserLauncher:
     # get ready for the next task
     self.gui_task_i += 1
 
-    if task == 'download_tarball':
+    if task == 'download_update_check':
+      print 'Downloading '+self.paths['url']['update_check']
+      self.download('update check', self.paths['url']['update_check'], self.paths['file']['update_check'])
+    
+    if task == 'attempt_update':
+      print 'Attempting to update'
+      self.attempt_update()
+
+    elif task == 'download_tarball':
       print 'Downloading '+self.paths['url']['tarball']
       self.download('tarball', self.paths['url']['tarball'], self.paths['file']['tarball'])
 
@@ -312,6 +347,37 @@ class TorBrowserLauncher:
 
     return True
 
+  def attempt_update(self):
+    # load the update check file
+    try:
+      versions = json.load(open(self.paths['file']['update_check']))
+      latest_version = None
+
+      end = '-Linux-%s' % self.architecture
+      for version in versions:
+        if str(version).find(end) != -1:
+          latest_version = version
+
+      if latest_version:
+        self.settings.latest_version = latest_version[:-len(end)]
+        self.settings.last_update_check_timestamp = int(time.time())
+        self.save_settings()
+        self.build_paths(self.settings['latest_version'])
+        self.start_launcher()
+
+      else:
+        # failed to find the latest version
+        self.set_gui('error', "Error checking for updates.", [])
+    
+    except:
+      # not a valid JSON object
+      self.set_gui('error', "Error checking for updates.", [])
+
+    # now start over
+    self.window.destroy()
+    self.clear_ui()
+    self.build_ui()
+
   def verify(self):
     # initialize the progress bar
     self.progressbar.set_fraction(0) 
@@ -362,6 +428,31 @@ class TorBrowserLauncher:
     self.gui_tasks = ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run']
     self.gui_task_i = 0
     self.start(None)
+
+  # load settings
+  def load_settings(self):
+    if os.path.isfile(self.paths['file']['settings']):
+      self.settings = pickle.load(open(self.paths['file']['settings']))
+      # sanity checks
+      if not 'installed_version' in self.settings:
+        return False
+      if not 'latest_version' in self.settings:
+        return False
+      if not 'last_update_check_timestamp' in self.settings:
+        return False
+    else:
+      self.settings = {
+        'installed_version': False,
+        'latest_version': '0',
+        'last_update_check_timestamp': 0
+      }
+      self.save_settings()
+    return True
+
+  # save settings
+  def save_settings(self):
+    pickle.dump(self.settings, open(self.paths['file']['settings'], 'w'))
+    return True
   
   # refresh gtk
   def refresh_gtk(self):
@@ -379,12 +470,11 @@ class TorBrowserLauncher:
     gtk.main_quit()
 
 if __name__ == "__main__":
-  current_tbl_version = '0.1'
-  current_tbb_version = '2.3.25-2'
+  tor_browser_launcher_version = '0.1'
 
   print 'Tor Browser Launcher'
-  print 'version %s' % (current_tbl_version)
+  print 'version %s' % (tor_browser_launcher_version)
   print 'https://github.com/micahflee/torbrowser-launcher'
 
-  app = TorBrowserLauncher(current_tbb_version)
+  app = TorBrowserLauncher()