--- fetcher.py.old Fri Nov 30 00:47:35 2007 +++ fetcher.py Fri Nov 30 02:35:05 2007 @@ -99,13 +99,13 @@ class UIHandler: class Fetcher: """Fetcher can fetch a file from various sources using various protocols.""" - def __init__(self, url, destdir): + def __init__(self, url, destdir,wait=False): if not isinstance(url, pisi.uri.URI): url = pisi.uri.URI(url) if ctx.config.get_option("authinfo"): url.set_auth_info(ctx.config.get_option("authinfo")) - + self.waitUntilConnection = wait self.url = url self.destdir = destdir self.archive_file = os.path.join(self.destdir, self.url.filename()) @@ -144,7 +144,15 @@ class Fetcher: reget = self._test_range_support(), user_agent = 'PiSi Fetcher/' + pisi.__version__) except urlgrabber.grabber.URLGrabError, e: - raise FetchError(_('Could not fetch destination file "%s": %s') % (self.archive_file, e)) + if self.checkInternetConnection(): + raise FetchError(_('Could not fetch destination file "%s": %s') % (self.archive_file, e)) + else: + if self.waitUntilConnection: + print "D: 5 sn sonra tekrar denenecek" + time.sleep(5) + self.fetch() + else: + raise FetchError(_('Check whether got a stable internet connection or not')) if os.stat(self.partial_file).st_size == 0: os.remove(self.partial_file) @@ -192,25 +200,43 @@ class Fetcher: return 1024 * int(bandwidth_limit) else: return 0 + + def checkInternetConnection(self): + import urllib2 + try: + file_obj = urllib2.urlopen("http://www.pardus.org.tr/resim/tubitak-uekae.png") + file_obj.close() + return True + except: + return False def _test_range_support(self): if not os.path.exists(self.partial_file): return None import urllib2 - file_obj = urllib2.urlopen(urllib2.Request(self.url.get_uri())) - headers = file_obj.info() - file_obj.close() - if headers.has_key('Content-Length'): - return 'check_timestamp' + if self.checkInternetConnection(): + file_obj = urllib2.urlopen(urllib2.Request(self.url.get_uri())) + headers = file_obj.info() + file_obj.close() + if headers.has_key('Content-Length'): + return 'check_timestamp' + else: + ctx.ui.debug(_("Server doesn't support partial downloads. Previously downloaded part of the file will be over-written.")) + os.remove(self.partial_file) + return None else: - ctx.ui.debug(_("Server doesn't support partial downloads. Previously downloaded part of the file will be over-written.")) - os.remove(self.partial_file) - return None - + if self.waitUntilConnection: + time.sleep(5) + self.fetch() + else: + raise FetchError(_('Check whether got a stable internet connection or not')) # helper function -def fetch_url(url, destdir, progress=None): - fetch = Fetcher(url, destdir) - fetch.progress = progress - fetch.fetch() +def fetch_url(url, destdir, progress=None,wait=False): + fetch = Fetcher(url, destdir,wait) + if fetch.checkInternetConnection(): + fetch.progress = progress + fetch.fetch() + else: + raise FetchError(_('Check whether got a stable internet connection or not'))