def test_remotefile1(self): """get a simple file1""" f = RORemoteFile('/source/project/package/fname') self.assertEqual(f.read(5), 'This ') self.assertEqual( f.read(), 'is a simple file\nwith some newlines\n\n' 'and\ntext.\n')
def test_remotefile3(self): """store file""" f = RORemoteFile('/source/project/package/fname2') sio = StringIO() f.write_to(sio, size=12) self.assertEqual(sio.getvalue(), 'yet another\n') sio = StringIO() f.write_to(sio) self.assertEqual(sio.getvalue(), 'simple\nfile\n')
def test_remotefile5(self): """store file""" f = RORemoteFile('/source/project/package/fname2', mtime=1311512569) path = self.fixture_file('write_me') f.write_to(path) self.assertEqualFile('yet another\nsimple\nfile\n', 'remotefile2') st = os.stat(path) self.assertEqual(st.st_mtime, 1311512569) # default mode is 0644 self.assertEqual(stat.S_IMODE(st.st_mode), 420)
def test_remotefile2(self): """iterate over the file""" f = RORemoteFile('/source/project/package/fname') i = iter(f) self.assertEqualFile(i.next(), 'remotefile1') f = RORemoteFile('/source/project/package/fname2', stream_bufsize=6) i = iter(f) self.assertEqual(i.next(), 'yet an') self.assertEqual(i.next(), 'other\n') self.assertEqual(i.next(), 'simple') self.assertEqual(i.next(), '\nfile\n')
def test_rwremotefile9(self): """iterate over the file (but read only 8 bytes)""" f = RORemoteFile('/source/project/package/fname') i = iter(f) self.assertEqualFile(next(i), 'remotefile1') f = RORemoteFile('/source/project/package/fname2', stream_bufsize=6) i = f.__iter__(size=8) self.assertEqual(next(i), 'yet an') self.assertEqual(next(i), 'ot') f = RWRemoteFile('/source/project/package/fname2', stream_bufsize=6) i = f.__iter__(size=8) self.assertEqual(next(i), 'yet an') self.assertEqual(next(i), 'ot')
def urlopen(self, host, path, **kwargs): """Returns a RORemoteFile instance. Invoked by the CustomMirrorGroup object. host is the host and path is the path. Keyword arguments: kwargs -- optional arguments for the RORemoteFile instance (like query parameters) """ return RORemoteFile(path, apiurl=host, lazy_open=False, **kwargs)
def urlopen(self, url, **kwargs): """Returns a RORemoteFile instance. Invoked by the CustomMirrorGroup object. url is a list with 3 elements: url[0] the host, url[1] the path and url[2] optional query parameters. Keyword arguments: **kwargs -- kwargs are ignored """ host, path, query = url f = None try: f = RORemoteFile(path, apiurl=host, lazy_open=False, **query) except HTTPError as e: if f is not None: f.close() exc = grabber.URLGrabError(14, str(e)) exc.orig_exc = e raise exc return f
def test_rwremotefile9(self): """iterate over the file (but read only 8 bytes)""" f = RORemoteFile('/source/project/package/fname') i = iter(f) self.assertEqualFile(i.next(), 'remotefile1') f = RORemoteFile('/source/project/package/fname2', stream_bufsize=6) i = f.__iter__(size=8) self.assertEqual(i.next(), 'yet an') self.assertEqual(i.next(), 'ot') f = RWRemoteFile('/source/project/package/fname2', stream_bufsize=6) i = f.__iter__(size=8) self.assertEqual(i.next(), 'yet an') self.assertEqual(i.next(), 'ot')
def file(self, **kwargs): """Returns a RORemoteFile object. This can be used to read/save the binary file. Keyword arguments: **kwargs -- optional parameters for the http request """ path = "/build/%(project)s/%(repository)s/%(arch)s/%(package)s/" \ "%(fname)s" parent = self.getparent() data = {'project': parent.get('project'), 'package': parent.get('package'), 'repository': parent.get('repository'), 'arch': parent.get('arch'), 'fname': self.get('filename')} path = path % data return RORemoteFile(path, **kwargs)
def file(self, **kwargs): """Returns a RORemoteFile object. This can be used to read/save the source file. Keyword arguments: **kwargs -- optional parameters for the http request """ path = "/source/%(project)s/%(package)s/%(file)s" parent = self.getparent() data = { 'project': parent.get('project'), 'package': parent.get('name'), 'file': self.get('name') } # specifying a different rev probably makes no sense... if 'rev' not in kwargs: kwargs['rev'] = self.getparent().get('srcmd5') path = path % data mtime = int(self.get('mtime')) return RORemoteFile(path, mtime=mtime, **kwargs)
def test_remotefile7(self): """test lazy_open=False""" # directly open the url (even if no read request is issued) f = RORemoteFile('/path/to/file', lazy_open=False) f.close()
def test_remotefile6(self): """test lazy_open=True (default)""" # the url is opened lazily (that is when a read request is issued) f = RORemoteFile('/path/to/file', lazy_open=True) f.close()
def test_remotefile4(self): """test exception (we don't try to overwrite existing files)""" f = RORemoteFile('/foo/bar') # do not run this as privileged user assert not os.access('/', os.W_OK) self.assertRaises(ValueError, f.write_to, '/foo')
def test_remotefile1(self): """get a simple file1""" f = RORemoteFile('/source/project/package/fname') self.assertEqual(f.read(5), 'This ') self.assertEqual(f.read(), 'is a simple file\nwith some newlines\n\n' 'and\ntext.\n')