class TestUrlOpener(TestCase): """Unit test for cygapt.url_opener """ def setUp(self): TestCase.setUp(self) self.obj = CygAptURLopener(True) def test__init__(self): self.assertTrue(isinstance(self.obj, CygAptURLopener)) def testHttp_error_default(self): f = TemporaryFile() errcode = 404 self.obj.http_error_default("url", f, errcode, "errmsg", "headers") f.close() self.assertEqual(self.obj.getErrorCode(), errcode) def testDlProgress(self): old_stdout = sys.stdout buf = StringIO() sys.stdout = buf self.obj.dlProgress(1, 512, 1024) sys.stdout = old_stdout buf.seek(0) out = buf.readline() buf.close() expect_out = "[====================> ]\r" self.assertEqual(out, expect_out)
def uri_get(directory, uri, verbose=False): up = urlparse.urlparse(uri) scriptname = os.path.basename(sys.argv[0]) if up.scheme == "file": shutil.copy(uri[7:], directory) if verbose: print("cp {0} {1}".format(uri[7:], directory)) elif up.scheme == "http" or up.scheme == "ftp": url_base = os.path.basename(up.path) old_cwd = os.getcwd() os.chdir(directory) if verbose: print("\r{0}: downloading: {1}".format(scriptname, uri)) try: opener = CygAptURLopener(verbose) opener.retrieve(uri, "{0}.tmp".format(url_base), reporthook=opener.dlProgress) except IOError: opener.setErrorCode(1) finally: opener.close() if opener.getErrorCode() == 200: rename(url_base + ".tmp", url_base) else: if os.path.exists(url_base + ".tmp"): os.remove(url_base + ".tmp") os.chdir(old_cwd) raise RequestException("{0} unreached URL {1}" "".format(opener.getErrorCode(), uri)) os.chdir(old_cwd) else: raise InvalidArgumentException("bad URL {0}".format(uri))
def uri_get(directory, uri, verbose=False): up = urlparse.urlparse(uri); scriptname = os.path.basename(sys.argv[0]); if up.scheme == "file": shutil.copy(uri[7:], directory); if verbose: print("cp {0} {1}".format(uri[7:], directory)); elif up.scheme == "http" or up.scheme == "ftp": url_base = os.path.basename(up.path); old_cwd = os.getcwd(); os.chdir(directory); if verbose: print("\r{0}: downloading: {1}".format(scriptname, uri)); try: opener = CygAptURLopener(verbose); opener.retrieve( uri, "{0}.tmp".format(url_base), reporthook=opener.dlProgress ); except IOError: opener.setErrorCode(1); finally: opener.close(); if opener.getErrorCode() == 200: rename(url_base + ".tmp", url_base); else: if os.path.exists(url_base + ".tmp"): os.remove(url_base + ".tmp"); os.chdir(old_cwd); raise RequestException( "{0} unreached URL {1}" "".format(opener.getErrorCode(), uri) ); os.chdir(old_cwd); else: raise InvalidArgumentException("bad URL {0}".format(uri));
def setUp(self): TestCase.setUp(self) self.obj = CygAptURLopener(True)
def setUp(self): unittest.TestCase.setUp(self); self.obj = CygAptURLopener(True);