Esempio n. 1
0
    def rerequest(self, url, set):
        # url is s from method announce.
        try:
            if self.ip:
                # Include our IP address in case we are communicating through a proxy.
                url += '&ip=' + gethostbyname(self.ip)
            # Read a reply.
            h = urlopen(url)
            r = h.read()
            h.close()
            if set():
                # Only get here if checkfail did not run and call set() first.
                def add(self=self, r=r):
                    # This call succeeded.
                    self.last_failed = False
                    # Process the reply.
                    self.postrequest(r)

                self.externalsched(add, 0)
        except (IOError, error), e:
            if set():
                # Only get here if checkfail did not run and call set() first.
                def fail(self=self,
                         r='Problem connecting to tracker - ' + str(e)):
                    if self.last_failed:
                        self.errorfunc(r)
                    self.last_failed = True

                self.externalsched(fail, 0)
Esempio n. 2
0
def get_url(url):
    data = None
    errors = []
    err_str = _("Could not download or open \n%s\n"
                "Try using a web browser to download the torrent file.") % url
    u = None

    # pending protocol changes, convert:
    #   torrent://http://path.to/file
    # and:
    #   bittorrent://http://path.to/file
    # to:
    #   http://path.to/file
    url = urlpat_torrent.sub('', url)
    url = urlpat_bittorrent.sub('', url)

    try:
        u = zurllib.urlopen(url)
        data = u.read()
        u.close()
        b = bdecode(data)
    except Exception, e:
        if u is not None:
            u.close()
        errors.append(err_str + "\n(%s)" % e)
        data = None
Esempio n. 3
0
def get_url(url):
    data = None
    errors = []
    err_str = _("Could not download or open \n%s\n"
                "Try using a web browser to download the torrent file.") % url
    u = None

    # pending protocol changes, convert:
    #   torrent://http://path.to/file
    # and:
    #   bittorrent://http://path.to/file
    # to:
    #   http://path.to/file
    url = urlpat_torrent.sub('', url)
    url = urlpat_bittorrent.sub('', url)
    
    try:
        u = zurllib.urlopen(url)
        data = u.read()
        u.close()
        b = bdecode(data)
    except Exception, e:
        if u is not None:
            u.close()
        errors.append(err_str + "\n(%s)" % e)
        data = None
Esempio n. 4
0
 def rerequest(self, url, set):
     # url is s from method announce.
     try:
         if self.ip:
             # Include our IP address in case we are communicating through a proxy.
             url += '&ip=' + gethostbyname(self.ip)
         # Read a reply.
         h = urlopen(url)
         r = h.read()
         h.close()
         if set():
             # Only get here if checkfail did not run and call set() first.
             def add(self = self, r = r):
                 # This call succeeded.
                 self.last_failed = False
                 # Process the reply.
                 self.postrequest(r)
             self.externalsched(add, 0)
     except (IOError, error), e:
         if set():
             # Only get here if checkfail did not run and call set() first.
             def fail(self = self, r = 'Problem connecting to tracker - ' + str(e)):
                 if self.last_failed:
                     self.errorfunc(r)
                 self.last_failed = True
             self.externalsched(fail, 0)
Esempio n. 5
0
def get_language(name):
    url = LOCALE_URL + name + ".tar.gz"
    r = urllib.urlopen(url)
    # urllib seems to ungzip for us
    tarname = os.path.join(locale_root, name + ".tar")
    f = file(tarname, 'wb')
    f.write(r.read())
    f.close()
    tar = tarfile.open(tarname, "r")
    for tarinfo in tar:
        tar.extract(tarinfo, path=locale_root)
    tar.close()
Esempio n. 6
0
def get_response(file, url, errorfunc):
    try:
        if file:
            h = open(file, 'rb')
            try:
                # quick test to see if responsefile contains a dict
                line = h.read(10)
                front = line.split(':', 1)[0]
                assert front[0] == 'd'
                int(front[1:])
            except:
                errorfunc(file + ' is not a valid responsefile')
                return None
            try:
                h.seek(0)
            except:
                try:
                    h.close()
                except:
                    pass
                h = open(file, 'rb')
        else:
            try:
                h = urlopen(url)
            except:
                errorfunc(url + ' bad url')
                return None
        response = h.read()

    except IOError as e:
        errorfunc('problem getting response info - ' + str(e))
        return None
    try:
        h.close()
    except:
        pass
    try:
        try:
            response = bdecode(response)
        except:
            errorfunc("warning: bad data in responsefile")
            response = bdecode(response, sloppy=1)
        check_type(response, dict)
        check_info(response.get('info'))
        check_type(response.get('announce'), str)
    except ValueError as e:
        errorfunc("got bad file info - " + str(e))
        return None

    return response
Esempio n. 7
0
 def _get_available(self, url):
     self.debug('Updater.get_available() hitting url %s' % url)
     try:
         u = zurllib.urlopen(url)
         s = u.read()
         s = s.strip()
     except:
         raise BTFailure(_("Could not get latest version from %s")%url)
     try:
         assert len(s) == 5
         availableversion = Version.from_str(s)
     except:
         raise BTFailure(_("Could not parse new version string from %s")%url)        
     return availableversion
Esempio n. 8
0
def get_response(file, url, errorfunc):
    try:
        if file:
            h = open(file, 'rb')
            try:
                # quick test to see if responsefile contains a dict
                line = h.read(10)
                front = line.split(':', 1)[0]
                assert front[0] == 'd'
                int(front[1:])
            except:
                errorfunc(file + ' is not a valid responsefile')
                return None
            try:
                h.seek(0)
            except:
                try:
                    h.close()
                except:
                    pass
                h = open(file, 'rb')
        else:
            try:
                h = urlopen(url)
            except:
                errorfunc(url + ' bad url')
                return None
        response = h.read()

    except IOError as e:
        errorfunc('problem getting response info - ' + str(e))
        return None
    try:
        h.close()
    except:
        pass
    try:
        try:
            response = bdecode(response)
        except:
            errorfunc("warning: bad data in responsefile")
            response = bdecode(response, sloppy=1)
        check_type(response, dict)
        check_info(response.get('info'))
        check_type(response.get('announce'), str)
    except ValueError as e:
        errorfunc("got bad file info - " + str(e))
        return None

    return response
Esempio n. 9
0
 def _get_available(self, url):
     self.debug('Updater.get_available() hitting url %s' % url)
     try:
         u = zurllib.urlopen(url)
         s = u.read()
         s = s.strip()
     except:
         raise BTFailure(_("Could not get latest version from %s") % url)
     try:
         assert len(s) == 5
         availableversion = Version.from_str(s)
     except:
         raise BTFailure(
             _("Could not parse new version string from %s") % url)
     return availableversion
Esempio n. 10
0
def get_url(url):
    data = None
    errors = []
    err_str = _("Could not download or open \n%s\n" "Try using a web browser to download the torrent file.") % url
    u = None
    try:
        u = zurllib.urlopen(url)
        data = u.read()
        u.close()
        b = bdecode(data)
    except Exception, e:
        if u is not None:
            u.close()
        errors.append(err_str + "\n(%s)" % e)
        data = None
Esempio n. 11
0
 def rerequest(self, url, set):
     try:
         h = urlopen(url)
         r = h.read()
         h.close()
         if set():
             def add(self = self, r = r):
                 self.last_failed = False
                 self.postrequest(r)
             self.externalsched(add, 0)
     except (IOError, error), e:
         if set():
             def fail(self = self, r = 'Problem connecting to tracker - ' + str(e)):
                 if self.last_failed:
                     self.errorfunc(r)
                 self.last_failed = True
             self.externalsched(fail, 0)
Esempio n. 12
0
 def get_available(self):
     url = self.version_site + currentversion.name()
     self.debug('Updater.get_available() hitting url %s' % url)
     try:
         u = zurllib.urlopen(url)
         s = u.read()
         s = s.strip()
     except:
         raise BTFailure(_("Could not get latest version from %s")%url)
     try:
         assert len(s) == 5
         availableversion = Version.from_str(s)
     except:
         raise BTFailure(_("Could not parse new version string from %s")%url)
     self.version = availableversion
     self.debug('Updater.get_available() got %s' % str(self.version))
     return self.version
Esempio n. 13
0
 def rerequest(self, url, set):
     try:
         if self.ip:
             url += '&ip=' + gethostbyname(self.ip)
         h = urlopen(url)
         r = h.read()
         h.close()
         if set():
             def add(self = self, r = r):
                 self.last_failed = False
                 self.postrequest(r)
             self.externalsched(add, 0)
     except (IOError, error), e:
         if set():
             def fail(self = self, r = 'Problem connecting to tracker - ' + str(e)):
                 if self.last_failed:
                     self.errorfunc(r)
                 self.last_failed = True
             self.externalsched(fail, 0)
Esempio n. 14
0
 def ParseResponseFile(self, responsefile, url=None, errorfunc=None):
     try:
         if responsefile and responsefile != '':
             h = open(responsefile, 'rb')
         elif url != None:
             h = urlopen(url)
         else:
             return None
         response = h.read()
         h.close()
     except IOError, e:
         if responsefile != '' and responsefile.find('Temporary Internet Files') != -1:
             if errorfunc != None:
                 errorfunc('BitTorrent was passed a filename that doesn\'t exist.  ' +
                     'Either clear your Temporary Internet Files or right-click the link ' + 
                     'and save the .torrent to disk first.')
         else:
             if errorfunc != None:
                 errorfunc('problem getting response info - ' + str(e))
         return None
Esempio n. 15
0
 def rerequest(self, url, set):
     try:
         h = urlopen(url)
         r = h.read()
         h.close()
         if set():
             def add(self = self, r = r):
                 self.last_failed = False
                 self.postrequest(r)
             self.externalsched(add)
     except (IOError, error), e:
         if set():
             def fail_404(self = self, r = 'Problem connecting to tracker - ' + str(e)):
                 if self.last_failed:
                     self.errorfunc(r, 'tracker_404')
                 self.last_failed = True
             def fail_bcool(self = self, r = 'Problem connecting to tracker - ' + str(e)):
                 if self.last_failed:
                     self.errorfunc(r, 'tracker_bcool')
                 self.last_failed = True
             if self.upratefunc() < 100 and self.downratefunc() < 100:
                 self.externalsched(fail_404)
             else:
                 self.externalsched(fail_bcool)
Esempio n. 16
0
                raise ValueError, 'must have responsefile as arg or parameter, not both'
            if path.isfile(args[0]):
                config['responsefile'] = args[0]
            else: 
                config['url'] = args[0]
        if (config['responsefile'] == '') == (config['url'] == ''):
            raise ValueError, 'need responsefile or url'
    except ValueError, e:
        errorfunc('error: ' + str(e) + '\nrun with no args for parameter explanations')
        return
    
    try:
        if config['responsefile'] != '':
            h = open(config['responsefile'], 'rb')
        else:
            h = urlopen(config['url'])
        response = h.read()
        h.close()
    except IOError, e:
        if config['responsefile'] != '' and config['responsefile'].find('Temporary Internet Files') != -1:
            errorfunc('BitTorrent was passed a filename that doesn\'t exist.  ' +
                'Either clear your Temporary Internet Files or right-click the link ' + 
                'and save the .torrent to disk first.')
        else:
            errorfunc('problem getting response info - ' + str(e))
        return

    try:
        response = bdecode(response)
        check_message(response)
    except ValueError, e:
Esempio n. 17
0
class Updater(object):
    def __init__(self,
                 threadwrap,
                 newversionfunc,
                 startfunc,
                 installfunc,
                 errorfunc,
                 test_new_version='',
                 test_current_version=''):
        self.threadwrap = threadwrap  # for calling back to UI from thread
        self.newversionfunc = newversionfunc  # alert to new version UI function
        self.startfunc = startfunc  # start torrent UI function
        self.installfunc = installfunc  # install torrent UI function
        self.errorfunc = errorfunc  # report error UI function
        self.infohash = None
        self.version = currentversion
        self.currentversion = currentversion
        self.asked_for_install = False
        self.version_site = version_host
        if os.name == 'nt':
            self.version_site += 'win32/'
            if os_version not in ('XP', '2000', '2003'):
                self.version_site += 'legacy/'
        elif osx:
            self.version_site += 'osx/'
        self.debug_mode = DEBUG
        if test_new_version:
            test_new_version = Version.from_str(test_new_version)
            self.debug_mode = True

            def _hack_get_available(url):
                return test_new_version

            self._get_available = _hack_get_available
        if test_current_version:
            self.debug_mode = True
            self.currentversion = Version.from_str(test_current_version)

    def debug(self, message):
        if self.debug_mode:
            self.threadwrap(self.errorfunc, WARNING, message)

    def _get_available(self, url):
        self.debug('Updater.get_available() hitting url %s' % url)
        try:
            u = zurllib.urlopen(url)
            s = u.read()
            s = s.strip()
        except:
            raise BTFailure(_("Could not get latest version from %s") % url)
        try:
            assert len(s) == 5
            availableversion = Version.from_str(s)
        except:
            raise BTFailure(
                _("Could not parse new version string from %s") % url)
        return availableversion

    def get_available(self):
        url = self.version_site + self.currentversion.name()
        availableversion = self._get_available(url)
        if availableversion.is_beta():
            if availableversion[1] != self.currentversion[1]:
                availableversion = self.currentversion
        if self.currentversion.is_beta():
            stable_url = self.version_site + 'stable'
            available_stable_version = self._get_available(stable_url)
            if available_stable_version > availableversion:
                availableversion = available_stable_version
        self.version = availableversion
        self.debug('Updater.get_available() got %s' % str(self.version))
        return self.version

    def get(self):
        try:
            self.get_available()
        except BTFailure, e:
            self.threadwrap(self.errorfunc, WARNING, e)
            return

        if self.version <= self.currentversion:
            self.debug('Updater.get() not updating old version %s' %
                       str(self.version))
            return

        if not self.can_install():
            self.debug('Updater.get() cannot install on this os')
            return

        self.installer_name = self.calc_installer_name()
        self.installer_url = self.version_site + self.installer_name + '.torrent'
        self.installer_dir = self.calc_installer_dir()

        self.torrentfile = None
        torrentfile, terrors = GetTorrent.get_url(self.installer_url)
        signature = None
        try:
            signfile = zurllib.urlopen(self.installer_url + '.sign')
        except:
            self.debug('Updater.get() failed to get signfile %s.sign' %
                       self.installer_url)
        else:
            try:
                signature = pickle.load(signfile)
            except:
                self.debug('Updater.get() failed to load signfile %s' %
                           signfile)

        if terrors:
            self.threadwrap(self.errorfunc, WARNING, '\n'.join(terrors))

        if torrentfile and signature:
            public_key_file = open(os.path.join(doc_root, 'public.key'), 'rb')
            public_key = pickle.load(public_key_file)
            h = sha(torrentfile).digest()
            if public_key.verify(h, signature):
                self.torrentfile = torrentfile
                b = bdecode(torrentfile)
                self.infohash = sha(bencode(b['info'])).digest()
                self.total_size = b['info']['length']
                self.debug('Updater.get() got torrent file and signature')
            else:
                self.debug(
                    'Updater.get() torrent file signature failed to verify.')
                pass
        else:
            self.debug(
                'Updater.get() doesn\'t have torrentfile %s and signature %s' %
                (str(type(torrentfile)), str(type(signature))))
Esempio n. 18
0
            self.debug('Updater.get() cannot install on this os')
            return

        self.installer_name = self.calc_installer_name()
        self.installer_url  = self.version_site + self.installer_name + '.torrent'
        self.installer_dir  = self.calc_installer_dir()

        self.torrentfile = None
        try:
            self.torrentfile = GetTorrent.get_url(self.installer_url)
        except GetTorrent.GetTorrentException, e:
            terrors = [unicode(e.args[0])]

        signature = None
        try:
            signfile = zurllib.urlopen(self.installer_url + '.sign')
        except:
            self.debug('Updater.get() failed to get signfile %s.sign' % self.installer_url)
        else:
            try:
                signature = pickle.load(signfile)
            except:
                self.debug('Updater.get() failed to load signfile %s' % signfile)
        
        if terrors:
            self.threadwrap(self.errorfunc, logging.WARNING, '\n'.join(terrors))

        if torrentfile and signature:
            public_key_file = open(os.path.join(doc_root, 'public.key'), 'rb')
            public_key = pickle.load(public_key_file)
            h = sha(torrentfile).digest()