def test_visoss_7(): if gdaltest.webserver_port == 0: pytest.skip() handler = webserver.SequentialHandler() handler.add('GET', '/oss_bucket_test_mkdir/dir/', 404, {'Connection': 'close'}) handler.add( 'GET', '/oss_bucket_test_mkdir/?delimiter=%2F&max-keys=100&prefix=dir%2F', 404, {'Connection': 'close'}) handler.add('PUT', '/oss_bucket_test_mkdir/dir/', 200) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsioss/oss_bucket_test_mkdir/dir', 0) assert ret == 0 # Try creating already existing directory handler = webserver.SequentialHandler() handler.add('GET', '/oss_bucket_test_mkdir/dir/', 416) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsioss/oss_bucket_test_mkdir/dir', 0) assert ret != 0 handler = webserver.SequentialHandler() handler.add('DELETE', '/oss_bucket_test_mkdir/dir/', 204) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsioss/oss_bucket_test_mkdir/dir') assert ret == 0 # Try deleting already deleted directory handler = webserver.SequentialHandler() handler.add('GET', '/oss_bucket_test_mkdir/dir/', 404) handler.add( 'GET', '/oss_bucket_test_mkdir/?delimiter=%2F&max-keys=100&prefix=dir%2F', 404, {'Connection': 'close'}) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsioss/oss_bucket_test_mkdir/dir') assert ret != 0 # Try deleting non-empty directory handler = webserver.SequentialHandler() handler.add('GET', '/oss_bucket_test_mkdir/dir_nonempty/', 416) handler.add( 'GET', '/oss_bucket_test_mkdir/?delimiter=%2F&max-keys=100&prefix=dir_nonempty%2F', 200, {'Content-type': 'application/xml'}, """<?xml version="1.0" encoding="UTF-8"?> <ListBucketResult> <Prefix>dir_nonempty/</Prefix> <Contents> <Key>dir_nonempty/test.txt</Key> <LastModified>1970-01-01T00:00:01.000Z</LastModified> <Size>40</Size> </Contents> </ListBucketResult> """) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsioss/oss_bucket_test_mkdir/dir_nonempty') assert ret != 0
def gdal_cp(argv, progress=None): srcfile = None targetfile = None recurse = False skip_failure = False argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: return -1 for i in range(1, len(argv)): if argv[i] == '-progress': progress = TermProgress() elif argv[i] == '-r': version_num = int(gdal.VersionInfo('VERSION_NUM')) if version_num < 1900: print('ERROR: Python bindings of GDAL 1.9.0 or later required for -r option') return -1 recurse = True elif len(argv[i]) >= 5 and argv[i][0:5] == '-skip': skip_failure = True elif argv[i][0] == '-': print('Unrecognized option : %s' % argv[i]) return Usage() elif srcfile is None: srcfile = argv[i] elif targetfile is None: targetfile = argv[i] else: print('Unexpected option : %s' % argv[i]) return Usage() if srcfile is None or targetfile is None: return Usage() if needsVSICurl(srcfile): srcfile = '/vsicurl/' + srcfile if recurse: # Make sure that 'gdal_cp.py -r [srcdir/]lastsubdir targetdir' creates # targetdir/lastsubdir if targetdir already exists (like cp -r does). if srcfile[-1] == '/': srcfile = srcfile[0:len(srcfile) - 1] statBufSrc = gdal.VSIStatL(srcfile, gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG) if statBufSrc is None: statBufSrc = gdal.VSIStatL(srcfile + '/', gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG) statBufDst = gdal.VSIStatL(targetfile, gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG) if statBufSrc is not None and statBufSrc.IsDirectory() and statBufDst is not None and statBufDst.IsDirectory(): if targetfile[-1] == '/': targetfile = targetfile[0:-1] if srcfile.rfind('/') != -1: targetfile = targetfile + srcfile[srcfile.rfind('/'):] else: targetfile = targetfile + '/' + srcfile if gdal.VSIStatL(targetfile) is None: gdal.Mkdir(targetfile, int('0755', 8)) return gdal_cp_recurse(srcfile, targetfile, progress, skip_failure) (srcdir, pattern) = os.path.split(srcfile) if not srcdir.startswith('/vsi') and ('*' in pattern or '?' in pattern): return gdal_cp_pattern_match(srcdir, pattern, targetfile, progress, skip_failure) return gdal_cp_single(srcfile, targetfile, progress)
def vsizip_1(): # We can keep the handle open during all the ZIP writing hZIP = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "wb") if hZIP is None: gdaltest.post_reason('fail 1') return 'fail' # One way to create a directory f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir2/", "wb") if f is None: gdaltest.post_reason('fail 2') return 'fail' gdal.VSIFCloseL(f) # A more natural one gdal.Mkdir("/vsizip/vsimem/test.zip/subdir1", 0) # Create 1st file f2 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "wb") if f2 is None: gdaltest.post_reason('fail 3') return 'fail' gdal.VSIFWriteL("abcd", 1, 4, f2) gdal.VSIFCloseL(f2) # Test that we cannot read a zip file being written gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") gdal.PopErrorHandler() if gdal.GetLastErrorMsg() != 'Cannot read a zip file being written': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if f is not None: gdaltest.post_reason('should not have been successful 1') return 'fail' # Create 2nd file f3 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/efghi", "wb") if f3 is None: gdaltest.post_reason('fail 4') return 'fail' gdal.VSIFWriteL("efghi", 1, 5, f3) # Try creating a 3d file gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f4 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/that_wont_work", "wb") gdal.PopErrorHandler() if gdal.GetLastErrorMsg( ) != 'Cannot create that_wont_work while another file is being written in the .zip': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if f4 is not None: gdaltest.post_reason('should not have been successful 2') return 'fail' gdal.VSIFCloseL(f3) # Now we can close the main handle gdal.VSIFCloseL(hZIP) f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") if f is None: gdaltest.post_reason('fail 5') return 'fail' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) if data.decode('ASCII') != 'abcd': gdaltest.post_reason('fail') print(data) return 'fail' # Test alternate uri syntax gdal.Rename("/vsimem/test.zip", "/vsimem/test.xxx") f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd", "rb") if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) if data.decode('ASCII') != 'abcd': gdaltest.post_reason('fail') print(data) return 'fail' # With a trailing slash f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd/", "rb") if f is None: gdaltest.post_reason('fail') return 'fail' gdal.VSIFCloseL(f) # Test ReadDir() if len(gdal.ReadDir("/vsizip/{/vsimem/test.xxx}")) != 3: gdaltest.post_reason('fail') print(gdal.ReadDir("/vsizip/{/vsimem/test.xxx}")) return 'fail' # Unbalanced curls f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx", "rb") if f is not None: gdaltest.post_reason('fail') return 'fail' # Non existing mainfile f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/bla", "rb") if f is not None: gdaltest.post_reason('fail') return 'fail' # Non existing subfile f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.zzz}/bla", "rb") if f is not None: gdaltest.post_reason('fail') return 'fail' # Wrong syntax f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}.aux.xml", "rb") if f is not None: gdaltest.post_reason('fail') return 'fail' # Test nested { { } } hZIP = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}", "wb") if hZIP is None: gdaltest.post_reason('fail 1') return 'fail' f = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}/test.xxx", "wb") f_src = gdal.VSIFOpenL("/vsimem/test.xxx", "rb") data = gdal.VSIFReadL(1, 10000, f_src) gdal.VSIFCloseL(f_src) gdal.VSIFWriteL(data, 1, len(data), f) gdal.VSIFCloseL(f) gdal.VSIFCloseL(hZIP) f = gdal.VSIFOpenL( "/vsizip/{/vsizip/{/vsimem/zipinzip.yyy}/test.xxx}/subdir3/abcd/", "rb") if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) if data.decode('ASCII') != 'abcd': gdaltest.post_reason('fail') print(data) return 'fail' gdal.Unlink("/vsimem/test.xxx") gdal.Unlink("/vsimem/zipinzip.yyy") return 'success'
def test_vsiswift_extra_1(): if not gdaltest.built_against_curl(): pytest.skip() swift_resource = gdal.GetConfigOption('SWIFT_RESOURCE') if swift_resource is None: pytest.skip('Missing SWIFT_RESOURCE') if '/' not in swift_resource: path = '/vsiswift/' + swift_resource statres = gdal.VSIStatL(path) assert statres is not None and stat.S_ISDIR(statres.mode), \ ('%s is not a valid bucket' % path) readdir = gdal.ReadDir(path) assert readdir is not None, 'ReadDir() should not return empty list' for filename in readdir: if filename != '.': subpath = path + '/' + filename assert gdal.VSIStatL(subpath) is not None, \ ('Stat(%s) should not return an error' % subpath) unique_id = 'vsiswift_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id in readdir, \ ('ReadDir(%s) should contain %s' % (path, unique_id)) ret = gdal.Mkdir(subpath, 0) assert ret != 0, ('Mkdir(%s) repeated should return an error' % subpath) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id not in readdir, \ ('ReadDir(%s) should not contain %s' % (path, unique_id)) ret = gdal.Rmdir(subpath) assert ret != 0, ('Rmdir(%s) repeated should return an error' % subpath) ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') assert f is not None gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) assert ret != 0, \ ('Rmdir(%s) on non empty directory should return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test.txt') assert ret >= 0, \ ('Unlink(%s) should not return an error' % (subpath + '/test.txt')) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) return f = open_for_read('/vsiswift/' + swift_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 # Same with /vsiswift_streaming/ f = open_for_read('/vsiswift_streaming/' + swift_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 # Invalid resource gdal.ErrorReset() f = open_for_read('/vsiswift_streaming/' + swift_resource + '/invalid_resource.baz') assert f is None, gdal.VSIGetLastErrorMsg()
def test_vsiaz_extra_1(): if not gdaltest.built_against_curl(): pytest.skip() az_resource = gdal.GetConfigOption('AZ_RESOURCE') if az_resource is None: pytest.skip('Missing AZ_RESOURCE') if '/' not in az_resource: path = '/vsiaz/' + az_resource statres = gdal.VSIStatL(path) assert statres is not None and stat.S_ISDIR(statres.mode), \ ('%s is not a valid bucket' % path) readdir = gdal.ReadDir(path) assert readdir is not None, 'ReadDir() should not return empty list' for filename in readdir: if filename != '.': subpath = path + '/' + filename assert gdal.VSIStatL(subpath) is not None, \ ('Stat(%s) should not return an error' % subpath) unique_id = 'vsiaz_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id in readdir, \ ('ReadDir(%s) should contain %s' % (path, unique_id)) ret = gdal.Mkdir(subpath, 0) assert ret != 0, ('Mkdir(%s) repeated should return an error' % subpath) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id not in readdir, \ ('ReadDir(%s) should not contain %s' % (path, unique_id)) ret = gdal.Rmdir(subpath) assert ret != 0, ('Rmdir(%s) repeated should return an error' % subpath) ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') assert f is not None gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) assert ret != 0, \ ('Rmdir(%s) on non empty directory should return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test.txt') assert ret >= 0, \ ('Unlink(%s) should not return an error' % (subpath + '/test.txt')) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) return f = open_for_read('/vsiaz/' + az_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 # Same with /vsiaz_streaming/ f = open_for_read('/vsiaz_streaming/' + az_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 if False: # pylint: disable=using-constant-test # we actually try to read at read() time and bSetError = false # Invalid bucket : "The specified bucket does not exist" gdal.ErrorReset() f = open_for_read('/vsiaz/not_existing_bucket/foo') with gdaltest.error_handler(): gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert gdal.VSIGetLastErrorMsg() != '' # Invalid resource gdal.ErrorReset() f = open_for_read('/vsiaz_streaming/' + az_resource + '/invalid_resource.baz') assert f is None, gdal.VSIGetLastErrorMsg() # Test GetSignedURL() signed_url = gdal.GetSignedURL('/vsiaz/' + az_resource) f = open_for_read('/vsicurl_streaming/' + signed_url) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1
def vsiswift_fake_mkdir_rmdir(): if gdaltest.webserver_port == 0: return 'skip' gdal.VSICurlClearCache() # Invalid name ret = gdal.Mkdir('/vsiswift', 0) if ret == 0: gdaltest.post_reason('fail') return 'fail' handler = webserver.SequentialHandler() handler.add('GET', '/v1/AUTH_something/foo/dir/', 404, {'Connection':'close'}) handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=10000', 200, {'Connection':'close'}, "[]") handler.add('PUT', '/v1/AUTH_something/foo/dir/', 201) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsiswift/foo/dir', 0) if ret != 0: gdaltest.post_reason('fail') return 'fail' # Try creating already existing directory handler = webserver.SequentialHandler() handler.add('GET', '/v1/AUTH_something/foo/dir/', 404, {'Connection':'close'}) handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=10000', 200, {'Connection':'close', 'Content-type': 'application/json' }, """[ { "subdir": "dir/" } ]""") with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsiswift/foo/dir', 0) if ret == 0: gdaltest.post_reason('fail') return 'fail' # Invalid name ret = gdal.Rmdir('/vsiswift') if ret == 0: gdaltest.post_reason('fail') return 'fail' gdal.VSICurlClearCache() # Not a directory handler = webserver.SequentialHandler() handler.add('GET', '/v1/AUTH_something/foo/it_is_a_file/', 404) handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=10000', 200, {'Connection':'close', 'Content-type': 'application/json' }, """[ { "name": "it_is_a_file/", "bytes": 0, "last_modified": "1970-01-01T00:00:01" } ]""") with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiswift/foo/it_is_a_file') if ret == 0: gdaltest.post_reason('fail') return 'fail' # Valid handler = webserver.SequentialHandler() handler.add('GET', '/v1/AUTH_something/foo/dir/', 200) handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=2&prefix=dir%2F', 200, {'Connection':'close', 'Content-type': 'application/json' }, """[] """) handler.add('DELETE', '/v1/AUTH_something/foo/dir/', 204) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiswift/foo/dir') if ret != 0: gdaltest.post_reason('fail') return 'fail' # Try deleting already deleted directory handler = webserver.SequentialHandler() handler.add('GET', '/v1/AUTH_something/foo/dir/', 404) handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=10000', 200) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiswift/foo/dir') if ret == 0: gdaltest.post_reason('fail') return 'fail' gdal.VSICurlClearCache() # Try deleting non-empty directory handler = webserver.SequentialHandler() handler.add('GET', '/v1/AUTH_something/foo/dir_nonempty/', 404) handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=10000', 200, {'Connection':'close', 'Content-type': 'application/json' }, """[ { "subdir": "dir_nonempty/" } ]""") handler.add('GET', '/v1/AUTH_something/foo?delimiter=%2F&limit=2&prefix=dir_nonempty%2F', 200, {'Connection':'close', 'Content-type': 'application/json' }, """[ { "name": "dir_nonempty/some_file", "bytes": 0, "last_modified": "1970-01-01T00:00:01" } ]""") with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiswift/foo/dir_nonempty') if ret == 0: gdaltest.post_reason('fail') return 'fail' return 'success'
def test_vsiadls_fake_mkdir_rmdir(): if gdaltest.webserver_port == 0: pytest.skip() gdal.VSICurlClearCache() # Invalid name ret = gdal.Mkdir('/vsiadls', 0) assert ret != 0 handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir', 404, {'Connection': 'close'}) handler.add( 'PUT', '/azure/blob/myaccount/az_bucket_test_mkdir/dir?resource=directory', 201) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsiadls/az_bucket_test_mkdir/dir', 0) assert ret == 0 # Try creating already existing directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir', 200, { 'x-ms-permissions': 'rwxrwxrwx', 'x-ms-resource-type': 'directory' }) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsiadls/az_bucket_test_mkdir/dir', 0) assert ret != 0 # Invalid name ret = gdal.Rmdir('/vsiadls') assert ret != 0 # Not a directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/it_is_a_file', 200, { 'x-ms-permissions': 'rwxrwxrwx', 'x-ms-resource-type': 'file' }) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiadls/az_bucket_test_mkdir/it_is_a_file') assert ret != 0 # Valid handler = webserver.SequentialHandler() handler.add( 'DELETE', '/azure/blob/myaccount/az_bucket_test_mkdir/dir?recursive=false', 200) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiadls/az_bucket_test_mkdir/dir') assert ret == 0 # Try deleting already deleted directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir', 404) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiadls/az_bucket_test_mkdir/dir') assert ret != 0 # RmdirRecursive handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir_rec', 200, { 'x-ms-permissions': 'rwxrwxrwx', 'x-ms-resource-type': 'directory' }) handler.add( 'DELETE', '/azure/blob/myaccount/az_bucket_test_mkdir/dir_rec?recursive=true', 200) with webserver.install_http_handler(handler): ret = gdal.RmdirRecursive('/vsiadls/az_bucket_test_mkdir/dir_rec') assert ret == 0
def test_vsifile_opendir(): # Non existing dir d = gdal.OpenDir('/vsimem/i_dont_exist') assert not d gdal.Mkdir('/vsimem/vsifile_opendir', 0o755) # Empty dir d = gdal.OpenDir('/vsimem/vsifile_opendir') assert d entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) gdal.FileFromMemBuffer('/vsimem/vsifile_opendir/test', 'foo') gdal.Mkdir('/vsimem/vsifile_opendir/subdir', 0o755) gdal.Mkdir('/vsimem/vsifile_opendir/subdir/subdir2', 0o755) gdal.FileFromMemBuffer('/vsimem/vsifile_opendir/subdir/subdir2/test2', 'bar') # Unlimited depth d = gdal.OpenDir('/vsimem/vsifile_opendir') entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir' assert entry.mode == 16384 entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir/subdir2' assert entry.mode == 16384 entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir/subdir2/test2' assert entry.mode == 32768 entry = gdal.GetNextDirEntry(d) assert entry.name == 'test' assert entry.mode == 32768 assert entry.modeKnown assert entry.size == 3 assert entry.sizeKnown assert entry.mtime != 0 assert entry.mtimeKnown assert not entry.extra entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) # Only top level d = gdal.OpenDir('/vsimem/vsifile_opendir', 0) entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir' entry = gdal.GetNextDirEntry(d) assert entry.name == 'test' entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) # Depth 1 files = [l_entry.name for l_entry in gdal.listdir('/vsimem/vsifile_opendir', 1)] assert files == ['subdir', 'subdir/subdir2', 'test'] gdal.RmdirRecursive('/vsimem/vsifile_opendir')
def test_vsiwebhdfs_extra_1(): if not gdaltest.built_against_curl(): pytest.skip() webhdfs_url = gdal.GetConfigOption('WEBHDFS_URL') if webhdfs_url is None: pytest.skip('Missing WEBHDFS_URL') if webhdfs_url.endswith('/webhdfs/v1') or webhdfs_url.endswith( '/webhdfs/v1/'): path = '/vsiwebhdfs/' + webhdfs_url statres = gdal.VSIStatL(path) assert statres is not None and stat.S_ISDIR(statres.mode), \ ('%s is not a valid bucket' % path) readdir = gdal.ReadDir(path) assert readdir is not None, 'ReadDir() should not return empty list' for filename in readdir: if filename != '.': subpath = path + '/' + filename assert gdal.VSIStatL(subpath) is not None, \ ('Stat(%s) should not return an error' % subpath) unique_id = 'vsiwebhdfs_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id in readdir, \ ('ReadDir(%s) should contain %s' % (path, unique_id)) #ret = gdal.Mkdir(subpath, 0) # if ret == 0: # gdaltest.post_reason('fail') # print('Mkdir(%s) repeated should return an error' % subpath) # return 'fail' ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id not in readdir, \ ('ReadDir(%s) should not contain %s' % (path, unique_id)) ret = gdal.Rmdir(subpath) assert ret != 0, ('Rmdir(%s) repeated should return an error' % subpath) ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') assert f is not None gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) assert ret != 0, \ ('Rmdir(%s) on non empty directory should return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test.txt') assert ret >= 0, ('Unlink(%s) should not return an error' % (subpath + '/test.txt')) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) return f = open_for_read('/vsiwebhdfs/' + webhdfs_url) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1
def ogr_mvt_errors(): if ogr.Open('MVT:/i_do_not/exist') is not None: gdaltest.post_reason('fail') return 'fail' # Cannot detect Z in directory name if ogr.Open('MVT:data') is not None: gdaltest.post_reason('fail') return 'fail' # Invalid Z gdal.Mkdir('/vsimem/33', 0) if ogr.Open('MVT:/vsimem/33') is not None: gdaltest.post_reason('fail') return 'fail' gdal.Rmdir('/vsimem/33') # Inexisting metadata with gdaltest.error_handler(): if gdal.OpenEx('data/mvt/linestring/0/0/0.pbf', open_options=['METADATA_FILE=/i_do_not/exist']) is None: gdaltest.post_reason('fail') return 'fail' # Invalid metadata with gdaltest.error_handler(): if gdal.OpenEx('data/mvt/linestring/0/0/0.pbf', open_options=['METADATA_FILE=ogr_mvt.py']) is None: gdaltest.post_reason('fail') return 'fail' # Invalid metadata gdal.FileFromMemBuffer('/vsimem/my.json', '{}') with gdaltest.error_handler(): if gdal.OpenEx('data/mvt/linestring/0/0/0.pbf', open_options=['METADATA_FILE=/vsimem/my.json']) is None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/my.json') # Invalid metadata gdal.FileFromMemBuffer('/vsimem/my.json', '{ "json": "x y" }') with gdaltest.error_handler(): if gdal.OpenEx('data/mvt/linestring/0/0/0.pbf', open_options=['METADATA_FILE=/vsimem/my.json']) is None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/my.json') # Too big file tmpfilename = '/vsimem/foo.pbf' gdal.FileFromMemBuffer( tmpfilename, open('data/mvt/polygon_larger_than_header.pbf', 'rb').read()) f = gdal.VSIFOpenL(tmpfilename, 'rb+') gdal.VSIFSeekL(f, 20 * 1024 * 1024, 0) gdal.VSIFWriteL(' ', 1, 1, f) gdal.VSIFCloseL(f) ds = ogr.Open(tmpfilename) gdal.Unlink(tmpfilename) if ds is not None: gdaltest.post_reason('fail') return 'fail' return 'success'
def test_vsiwebhdfs_mkdir_rmdir(): if gdaltest.webserver_port == 0: pytest.skip() gdal.VSICurlClearCache() # Invalid name ret = gdal.Mkdir('/vsiwebhdfs', 0) assert ret != 0 # Valid handler = webserver.SequentialHandler() handler.add('PUT', '/webhdfs/v1/foo/dir?op=MKDIRS', 200, {}, '{"boolean":true}') with webserver.install_http_handler(handler): ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + '/foo/dir', 0) assert ret == 0 # Valid with all options handler = webserver.SequentialHandler() handler.add( 'PUT', '/webhdfs/v1/foo/dir?op=MKDIRS&user.name=root&delegation=token&permission=755', 200, {}, '{"boolean":true}') with gdaltest.config_options({ 'WEBHDFS_USERNAME': '******', 'WEBHDFS_DELEGATION': 'token' }): with webserver.install_http_handler(handler): ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + '/foo/dir/', 493) # 0755 assert ret == 0 # Error handler = webserver.SequentialHandler() handler.add('PUT', '/webhdfs/v1/foo/dir_error?op=MKDIRS', 404) with webserver.install_http_handler(handler): ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + '/foo/dir_error', 0) assert ret != 0 # Root name is invalid ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + '/', 0) assert ret != 0 # Invalid name ret = gdal.Rmdir('/vsiwebhdfs') assert ret != 0 gdal.VSICurlClearCache() # Valid handler = webserver.SequentialHandler() handler.add('DELETE', '/webhdfs/v1/foo/dir?op=DELETE', 200, {}, '{"boolean":true}') with webserver.install_http_handler(handler): ret = gdal.Rmdir(gdaltest.webhdfs_base_connection + '/foo/dir') assert ret == 0 # Error handler = webserver.SequentialHandler() handler.add('DELETE', '/webhdfs/v1/foo/dir_error?op=DELETE', 404) with webserver.install_http_handler(handler): ret = gdal.Rmdir(gdaltest.webhdfs_base_connection + '/foo/dir_error') assert ret != 0
def test_vsiaz_extra_1(): if not gdaltest.built_against_curl(): pytest.skip() az_resource = gdal.GetConfigOption('AZ_RESOURCE') if az_resource is None: pytest.skip('Missing AZ_RESOURCE') if '/' not in az_resource: path = '/vsiaz/' + az_resource statres = gdal.VSIStatL(path) assert statres is not None and stat.S_ISDIR(statres.mode), \ ('%s is not a valid bucket' % path) readdir = gdal.ReadDir(path) assert readdir is not None, 'ReadDir() should not return empty list' for filename in readdir: if filename != '.': subpath = path + '/' + filename assert gdal.VSIStatL(subpath) is not None, \ ('Stat(%s) should not return an error' % subpath) unique_id = 'vsiaz_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id in readdir, \ ('ReadDir(%s) should contain %s' % (path, unique_id)) ret = gdal.Mkdir(subpath, 0) assert ret != 0, ('Mkdir(%s) repeated should return an error' % subpath) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id not in readdir, \ ('ReadDir(%s) should not contain %s' % (path, unique_id)) ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') assert f is not None gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) assert ret != 0, \ ('Rmdir(%s) on non empty directory should return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS') assert 'x-ms-blob-type' in md metadata_md = gdal.GetFileMetadata(subpath + '/test.txt', 'METADATA') assert 'ETag' in metadata_md or 'etag' in metadata_md assert metadata_md != md md = gdal.GetFileMetadata(subpath + '/test.txt', 'TAGS') assert md == {} # Change properties assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-blob-content-type': 'foo'}, 'PROPERTIES') md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS') assert md.get('Content-Type', '') == 'foo' or md.get( 'content-type', '') == 'foo', md # Change metadata assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-meta-FOO': 'BAR'}, 'METADATA') md = gdal.GetFileMetadata(subpath + '/test.txt', 'METADATA') assert md['x-ms-meta-FOO'] == 'BAR' # Change tags (doesn't seem to work with Azurite) if ':10000/devstoreaccount1' not in gdal.GetConfigOption( 'AZURE_STORAGE_CONNECTION_STRING', ''): assert gdal.SetFileMetadata(subpath + '/test.txt', {'BAR': 'BAZ'}, 'TAGS') md = gdal.GetFileMetadata(subpath + '/test.txt', 'TAGS') assert md['BAR'] == 'BAZ' assert gdal.Rename(subpath + '/test.txt', subpath + '/test2.txt') == 0 f = gdal.VSIFOpenL(subpath + '/test2.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test2.txt') assert ret >= 0, \ ('Unlink(%s) should not return an error' % (subpath + '/test2.txt')) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) return f = open_for_read('/vsiaz/' + az_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 # Same with /vsiaz_streaming/ f = open_for_read('/vsiaz_streaming/' + az_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 if False: # pylint: disable=using-constant-test # we actually try to read at read() time and bSetError = false # Invalid bucket : "The specified bucket does not exist" gdal.ErrorReset() f = open_for_read('/vsiaz/not_existing_bucket/foo') with gdaltest.error_handler(): gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert gdal.VSIGetLastErrorMsg() != '' # Invalid resource gdal.ErrorReset() f = open_for_read('/vsiaz_streaming/' + az_resource + '/invalid_resource.baz') assert f is None, gdal.VSIGetLastErrorMsg() # Test GetSignedURL() signed_url = gdal.GetSignedURL('/vsiaz/' + az_resource) f = open_for_read('/vsicurl_streaming/' + signed_url) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1
def vsiwebhdfs_extra_1(): if not gdaltest.built_against_curl(): return 'skip' webhdfs_url = gdal.GetConfigOption('WEBHDFS_URL') if webhdfs_url is None: print('Missing WEBHDFS_URL for running gdaltest_list_extra') return 'skip' if webhdfs_url.endswith('/webhdfs/v1') or webhdfs_url.endswith('/webhdfs/v1/'): path = '/vsiwebhdfs/' + webhdfs_url statres = gdal.VSIStatL(path) if statres is None or not stat.S_ISDIR(statres.mode): gdaltest.post_reason('fail') print('%s is not a valid bucket' % path) return 'fail' readdir = gdal.ReadDir(path) if readdir is None: gdaltest.post_reason('fail') print('ReadDir() should not return empty list') return 'fail' for filename in readdir: if filename != '.': subpath = path + '/' + filename if gdal.VSIStatL(subpath) is None: gdaltest.post_reason('fail') print('Stat(%s) should not return an error' % subpath) return 'fail' unique_id = 'vsiwebhdfs_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) if ret < 0: gdaltest.post_reason('fail') print('Mkdir(%s) should not return an error' % subpath) return 'fail' readdir = gdal.ReadDir(path) if unique_id not in readdir: gdaltest.post_reason('fail') print('ReadDir(%s) should contain %s' % (path, unique_id)) print(readdir) return 'fail' #ret = gdal.Mkdir(subpath, 0) # if ret == 0: # gdaltest.post_reason('fail') # print('Mkdir(%s) repeated should return an error' % subpath) # return 'fail' ret = gdal.Rmdir(subpath) if ret < 0: gdaltest.post_reason('fail') print('Rmdir(%s) should not return an error' % subpath) return 'fail' readdir = gdal.ReadDir(path) if unique_id in readdir: gdaltest.post_reason('fail') print('ReadDir(%s) should not contain %s' % (path, unique_id)) print(readdir) return 'fail' ret = gdal.Rmdir(subpath) if ret == 0: gdaltest.post_reason('fail') print('Rmdir(%s) repeated should return an error' % subpath) return 'fail' ret = gdal.Mkdir(subpath, 0) if ret < 0: gdaltest.post_reason('fail') print('Mkdir(%s) should not return an error' % subpath) return 'fail' f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') if f is None: gdaltest.post_reason('fail') return 'fail' gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) if ret == 0: gdaltest.post_reason('fail') print('Rmdir(%s) on non empty directory should return an error' % subpath) return 'fail' f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 5, f).decode('utf-8') if data != 'hello': gdaltest.post_reason('fail') print(data) return 'fail' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test.txt') if ret < 0: gdaltest.post_reason('fail') print('Unlink(%s) should not return an error' % (subpath + '/test.txt')) return 'fail' ret = gdal.Rmdir(subpath) if ret < 0: gdaltest.post_reason('fail') print('Rmdir(%s) should not return an error' % subpath) return 'fail' return 'success' f = open_for_read('/vsiwebhdfs/' + webhdfs_url) if f is None: gdaltest.post_reason('fail') return 'fail' ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(ret) != 1: gdaltest.post_reason('fail') print(ret) return 'fail' return 'success'
def vsizip_1(): # We can keep the handle open during all the ZIP writing hZIP = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "wb") if hZIP is None: gdaltest.post_reason('fail 1') return 'fail' # One way to create a directory f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir2/", "wb") if f is None: gdaltest.post_reason('fail 2') return 'fail' gdal.VSIFCloseL(f) # A more natural one gdal.Mkdir("/vsizip/vsimem/test.zip/subdir1", 0) # Create 1st file f2 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "wb") if f2 is None: gdaltest.post_reason('fail 3') return 'fail' gdal.VSIFWriteL("abcd", 1, 4, f2) gdal.VSIFCloseL(f2) # Test that we cannot read a zip file being written gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") gdal.PopErrorHandler() if gdal.GetLastErrorMsg() != 'Cannot read a zip file being written': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if f is not None: gdaltest.post_reason('should not have been successful 1') return 'fail' # Create 2nd file f3 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/efghi", "wb") if f3 is None: gdaltest.post_reason('fail 4') return 'fail' gdal.VSIFWriteL("efghi", 1, 5, f3) # Try creating a 3d file gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f4 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/that_wont_work", "wb") gdal.PopErrorHandler() if gdal.GetLastErrorMsg( ) != 'Cannot create that_wont_work while another file is being written in the .zip': gdaltest.post_reason('expected error') print(gdal.GetLastErrorMsg()) return 'fail' if f4 is not None: gdaltest.post_reason('should not have been successful 2') return 'fail' gdal.VSIFCloseL(f3) # Now we can close the main handle gdal.VSIFCloseL(hZIP) f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") if f is None: gdaltest.post_reason('fail 5') return 'fail' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) gdal.Unlink("/vsimem/test.zip") if data.decode('ASCII') != 'abcd': print(data) return 'fail' return 'success'
def test_vsifile_opendir(basepath): # Non existing dir d = gdal.OpenDir(basepath + '/i_dont_exist') assert not d gdal.RmdirRecursive(basepath + '/vsifile_opendir') gdal.Mkdir(basepath + '/vsifile_opendir', 0o755) # Empty dir d = gdal.OpenDir(basepath + '/vsifile_opendir') assert d entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) f = gdal.VSIFOpenL(basepath + '/vsifile_opendir/test', 'wb') assert f gdal.VSIFWriteL('foo', 1, 3, f) gdal.VSIFCloseL(f) gdal.Mkdir(basepath + '/vsifile_opendir/subdir', 0o755) gdal.Mkdir(basepath + '/vsifile_opendir/subdir/subdir2', 0o755) f = gdal.VSIFOpenL(basepath + '/vsifile_opendir/subdir/subdir2/test2', 'wb') assert f gdal.VSIFWriteL('bar', 1, 3, f) gdal.VSIFCloseL(f) # Unlimited depth d = gdal.OpenDir(basepath + '/vsifile_opendir') entries_found = [] for i in range(4): entry = gdal.GetNextDirEntry(d) assert entry if entry.name == 'test': entries_found.append(entry.name) assert (entry.mode & 32768) != 0 assert entry.modeKnown assert entry.size == 3 assert entry.sizeKnown assert entry.mtime != 0 assert entry.mtimeKnown assert not entry.extra elif entry.name == 'subdir': entries_found.append(entry.name) assert (entry.mode & 16384) != 0 elif entry.name == 'subdir/subdir2': entries_found.append(entry.name) assert (entry.mode & 16384) != 0 elif entry.name == 'subdir/subdir2/test2': entries_found.append(entry.name) assert (entry.mode & 32768) != 0 else: assert False, entry.name assert len(entries_found) == 4, entries_found entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) # Unlimited depth, do not require stating (only honoured on Unix) d = gdal.OpenDir(basepath + '/vsifile_opendir', -1, ['NAME_AND_TYPE_ONLY=YES']) entries_found = [] for i in range(4): entry = gdal.GetNextDirEntry(d) assert entry if entry.name == 'test': entries_found.append(entry.name) assert (entry.mode & 32768) != 0 if os.name == 'posix' and basepath == 'tmp/': assert entry.size == 0 elif entry.name == 'subdir': entries_found.append(entry.name) assert (entry.mode & 16384) != 0 elif entry.name == 'subdir/subdir2': entries_found.append(entry.name) assert (entry.mode & 16384) != 0 elif entry.name == 'subdir/subdir2/test2': entries_found.append(entry.name) assert (entry.mode & 32768) != 0 if os.name == 'posix' and basepath == 'tmp/': assert entry.size == 0 else: assert False, entry.name assert len(entries_found) == 4, entries_found entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) # Only top level d = gdal.OpenDir(basepath + '/vsifile_opendir', 0) entries_found = set() for i in range(2): entry = gdal.GetNextDirEntry(d) assert entry entries_found.add(entry.name) assert entries_found == set(['test', 'subdir']) entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) # Depth 1 files = set([l_entry.name for l_entry in gdal.listdir(basepath + '/vsifile_opendir', 1)]) assert files == set(['test', 'subdir', 'subdir/subdir2']) # Prefix filtering d = gdal.OpenDir(basepath + '/vsifile_opendir', -1, ['PREFIX=t']) entry = gdal.GetNextDirEntry(d) assert entry.name == 'test' entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) d = gdal.OpenDir(basepath + '/vsifile_opendir', -1, ['PREFIX=testtoolong']) entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) d = gdal.OpenDir(basepath + '/vsifile_opendir', -1, ['PREFIX=subd']) entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir' entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir/subdir2' entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir/subdir2/test2' entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) d = gdal.OpenDir(basepath + '/vsifile_opendir', -1, ['PREFIX=subdir/sub']) entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir/subdir2' entry = gdal.GetNextDirEntry(d) assert entry.name == 'subdir/subdir2/test2' entry = gdal.GetNextDirEntry(d) assert not entry gdal.CloseDir(d) # Cleanup gdal.RmdirRecursive(basepath + '/vsifile_opendir')
def test_vsizip_1(): # We can keep the handle open during all the ZIP writing hZIP = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "wb") assert hZIP is not None, 'fail 1' # One way to create a directory f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir2/", "wb") assert f is not None, 'fail 2' gdal.VSIFCloseL(f) # A more natural one gdal.Mkdir("/vsizip/vsimem/test.zip/subdir1", 0) # Create 1st file f2 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "wb") assert f2 is not None, 'fail 3' gdal.VSIFWriteL("abcd", 1, 4, f2) gdal.VSIFCloseL(f2) # Test that we cannot read a zip file being written gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() == 'Cannot read a zip file being written', \ 'expected error' assert f is None, 'should not have been successful 1' # Create 2nd file f3 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/efghi", "wb") assert f3 is not None, 'fail 4' gdal.VSIFWriteL("efghi", 1, 5, f3) # Try creating a 3d file gdal.ErrorReset() gdal.PushErrorHandler('CPLQuietErrorHandler') f4 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/that_wont_work", "wb") gdal.PopErrorHandler() assert gdal.GetLastErrorMsg() == 'Cannot create that_wont_work while another file is being written in the .zip', \ 'expected error' assert f4 is None, 'should not have been successful 2' gdal.VSIFCloseL(f3) # Now we can close the main handle gdal.VSIFCloseL(hZIP) # ERROR 6: Support only 1 file in archive file /vsimem/test.zip when no explicit in-archive filename is specified gdal.ErrorReset() with gdaltest.error_handler(): f = gdal.VSIFOpenL('/vsizip/vsimem/test.zip', 'rb') if f is not None: gdal.VSIFCloseL(f) assert gdal.GetLastErrorMsg() != '', 'expected error' f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb") assert f is not None, 'fail 5' data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) assert data.decode('ASCII') == 'abcd' # Test alternate uri syntax gdal.Rename("/vsimem/test.zip", "/vsimem/test.xxx") f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd", "rb") assert f is not None data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) assert data.decode('ASCII') == 'abcd' # With a trailing slash f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd/", "rb") assert f is not None gdal.VSIFCloseL(f) # Test ReadDir() assert len(gdal.ReadDir("/vsizip/{/vsimem/test.xxx}")) == 3 # Unbalanced curls f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx", "rb") assert f is None # Non existing mainfile f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/bla", "rb") assert f is None # Non existing subfile f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.zzz}/bla", "rb") assert f is None # Wrong syntax f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}.aux.xml", "rb") assert f is None # Test nested { { } } hZIP = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}", "wb") assert hZIP is not None, 'fail 1' f = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}/test.xxx", "wb") f_src = gdal.VSIFOpenL("/vsimem/test.xxx", "rb") data = gdal.VSIFReadL(1, 10000, f_src) gdal.VSIFCloseL(f_src) gdal.VSIFWriteL(data, 1, len(data), f) gdal.VSIFCloseL(f) gdal.VSIFCloseL(hZIP) f = gdal.VSIFOpenL( "/vsizip/{/vsizip/{/vsimem/zipinzip.yyy}/test.xxx}/subdir3/abcd/", "rb") assert f is not None data = gdal.VSIFReadL(1, 4, f) gdal.VSIFCloseL(f) assert data.decode('ASCII') == 'abcd' gdal.Unlink("/vsimem/test.xxx") gdal.Unlink("/vsimem/zipinzip.yyy") # Test VSIStatL on a non existing file assert gdal.VSIStatL('/vsizip//vsimem/foo.zip') is None # Test ReadDir on a non existing file assert gdal.ReadDir('/vsizip//vsimem/foo.zip') is None
def vsiaz_extra_1(): if not gdaltest.built_against_curl(): return 'skip' az_resource = gdal.GetConfigOption('AZ_RESOURCE') if az_resource is None: print('Missing AZ_RESOURCE for running gdaltest_list_extra') return 'skip' if az_resource.find('/') < 0: path = '/vsiaz/' + az_resource statres = gdal.VSIStatL(path) if statres is None or not stat.S_ISDIR(statres.mode): gdaltest.post_reason('fail') print('%s is not a valid bucket' % path) return 'fail' readdir = gdal.ReadDir(path) if readdir is None: gdaltest.post_reason('fail') print('ReadDir() should not return empty list') return 'fail' for filename in readdir: if filename != '.': subpath = path + '/' + filename if gdal.VSIStatL(subpath) is None: gdaltest.post_reason('fail') print('Stat(%s) should not return an error' % subpath) return 'fail' unique_id = 'vsiaz_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) if ret < 0: gdaltest.post_reason('fail') print('Mkdir(%s) should not return an error' % subpath) return 'fail' readdir = gdal.ReadDir(path) if unique_id not in readdir: gdaltest.post_reason('fail') print('ReadDir(%s) should contain %s' % (path, unique_id)) print(readdir) return 'fail' ret = gdal.Mkdir(subpath, 0) if ret == 0: gdaltest.post_reason('fail') print('Mkdir(%s) repeated should return an error' % subpath) return 'fail' ret = gdal.Rmdir(subpath) if ret < 0: gdaltest.post_reason('fail') print('Rmdir(%s) should not return an error' % subpath) return 'fail' readdir = gdal.ReadDir(path) if unique_id in readdir: gdaltest.post_reason('fail') print('ReadDir(%s) should not contain %s' % (path, unique_id)) print(readdir) return 'fail' ret = gdal.Rmdir(subpath) if ret == 0: gdaltest.post_reason('fail') print('Rmdir(%s) repeated should return an error' % subpath) return 'fail' ret = gdal.Mkdir(subpath, 0) if ret < 0: gdaltest.post_reason('fail') print('Mkdir(%s) should not return an error' % subpath) return 'fail' f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') if f is None: gdaltest.post_reason('fail') return 'fail' gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) if ret == 0: gdaltest.post_reason('fail') print('Rmdir(%s) on non empty directory should return an error' % subpath) return 'fail' f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 5, f).decode('utf-8') if data != 'hello': gdaltest.post_reason('fail') print(data) return 'fail' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test.txt') if ret < 0: gdaltest.post_reason('fail') print('Unlink(%s) should not return an error' % (subpath + '/test.txt')) return 'fail' ret = gdal.Rmdir(subpath) if ret < 0: gdaltest.post_reason('fail') print('Rmdir(%s) should not return an error' % subpath) return 'fail' return 'success' f = open_for_read('/vsiaz/' + az_resource) if f is None: gdaltest.post_reason('fail') return 'fail' ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(ret) != 1: gdaltest.post_reason('fail') print(ret) return 'fail' # Same with /vsiaz_streaming/ f = open_for_read('/vsiaz_streaming/' + az_resource) if f is None: gdaltest.post_reason('fail') return 'fail' ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(ret) != 1: gdaltest.post_reason('fail') print(ret) return 'fail' if False: # we actually try to read at read() time and bSetError = false # Invalid bucket : "The specified bucket does not exist" gdal.ErrorReset() f = open_for_read('/vsiaz/not_existing_bucket/foo') with gdaltest.error_handler(): gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if gdal.VSIGetLastErrorMsg() == '': gdaltest.post_reason('fail') print(gdal.VSIGetLastErrorMsg()) return 'fail' # Invalid resource gdal.ErrorReset() f = open_for_read('/vsiaz_streaming/' + az_resource + '/invalid_resource.baz') if f is not None: gdaltest.post_reason('fail') print(gdal.VSIGetLastErrorMsg()) return 'fail' # Test GetSignedURL() signed_url = gdal.GetSignedURL('/vsiaz/' + az_resource) f = open_for_read('/vsicurl_streaming/' + signed_url) if f is None: gdaltest.post_reason('fail') return 'fail' ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(ret) != 1: gdaltest.post_reason('fail') print(ret) return 'fail' return 'success'
def test_vsiaz_fake_mkdir_rmdir(): if gdaltest.webserver_port == 0: pytest.skip() # Invalid name ret = gdal.Mkdir('/vsiaz', 0) assert ret != 0 handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir/', 404, {'Connection': 'close'}) handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=dir%2F&restype=container', 200, {'Connection': 'close'}) handler.add( 'PUT', '/azure/blob/myaccount/az_bucket_test_mkdir/dir/.gdal_marker_for_dir', 201) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsiaz/az_bucket_test_mkdir/dir', 0) assert ret == 0 # Try creating already existing directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir/', 404) handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=dir%2F&restype=container', 200, { 'Connection': 'close', 'Content-type': 'application/xml' }, """<?xml version="1.0" encoding="UTF-8"?> <EnumerationResults> <Prefix>dir/</Prefix> <Blobs> <Blob> <Name>dir/.gdal_marker_for_dir</Name> </Blob> </Blobs> </EnumerationResults> """) with webserver.install_http_handler(handler): ret = gdal.Mkdir('/vsiaz/az_bucket_test_mkdir/dir', 0) assert ret != 0 # Invalid name ret = gdal.Rmdir('/vsiaz') assert ret != 0 # Not a directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/it_is_a_file/', 404) handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=it_is_a_file%2F&restype=container', 200, { 'Connection': 'close', 'Content-type': 'application/xml' }, """<?xml version="1.0" encoding="UTF-8"?> <EnumerationResults> <Prefix>it_is_a_file/</Prefix> </EnumerationResults> """) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiaz/az_bucket_test_mkdir/it_is_a_file') assert ret != 0 # Valid handler = webserver.SequentialHandler() handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=dir%2F&restype=container', 200, { 'Connection': 'close', 'Content-type': 'application/xml' }, """<?xml version="1.0" encoding="UTF-8"?> <EnumerationResults> <Prefix>dir/</Prefix> <Blobs> <Blob> <Name>dir/.gdal_marker_for_dir</Name> </Blob> </Blobs> </EnumerationResults> """) handler.add( 'DELETE', '/azure/blob/myaccount/az_bucket_test_mkdir/dir/.gdal_marker_for_dir', 202) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiaz/az_bucket_test_mkdir/dir') assert ret == 0 # Try deleting already deleted directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir/', 404) handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=dir%2F&restype=container', 200) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiaz/az_bucket_test_mkdir/dir') assert ret != 0 # Try deleting non-empty directory handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/az_bucket_test_mkdir/dir_nonempty/', 404) handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=dir_nonempty%2F&restype=container', 200, { 'Connection': 'close', 'Content-type': 'application/xml' }, """<?xml version="1.0" encoding="UTF-8"?> <EnumerationResults> <Prefix>dir_nonempty/</Prefix> <Blobs> <Blob> <Name>dir_nonempty/foo</Name> </Blob> </Blobs> </EnumerationResults> """) handler.add( 'GET', '/azure/blob/myaccount/az_bucket_test_mkdir?comp=list&delimiter=%2F&maxresults=1&prefix=dir_nonempty%2F&restype=container', 200, { 'Connection': 'close', 'Content-type': 'application/xml' }, """<?xml version="1.0" encoding="UTF-8"?> <EnumerationResults> <Prefix>dir_nonempty/</Prefix> <Blobs> <Blob> <Name>dir_nonempty/foo</Name> </Blob> </Blobs> </EnumerationResults> """) with webserver.install_http_handler(handler): ret = gdal.Rmdir('/vsiaz/az_bucket_test_mkdir/dir_nonempty') assert ret != 0
def vsifile_opendir(): # Non existing dir d = gdal.OpenDir('/vsimem/i_dont_exist') if d: gdaltest.post_reason('fail') return 'fail' gdal.Mkdir('/vsimem/vsifile_opendir', 0o755) # Empty dir d = gdal.OpenDir('/vsimem/vsifile_opendir') if not d: gdaltest.post_reason('fail') return 'fail' entry = gdal.GetNextDirEntry(d) if entry: gdaltest.post_reason('fail') return 'fail' gdal.CloseDir(d) gdal.FileFromMemBuffer('/vsimem/vsifile_opendir/test', 'foo') gdal.Mkdir('/vsimem/vsifile_opendir/subdir', 0o755) gdal.Mkdir('/vsimem/vsifile_opendir/subdir/subdir2', 0o755) gdal.FileFromMemBuffer('/vsimem/vsifile_opendir/subdir/subdir2/test2', 'bar') # Unlimited depth d = gdal.OpenDir('/vsimem/vsifile_opendir') entry = gdal.GetNextDirEntry(d) if entry.name != 'subdir': gdaltest.post_reason('fail') print(entry.name) return 'fail' if entry.mode != 16384: gdaltest.post_reason('fail') print(entry.mode) return 'fail' entry = gdal.GetNextDirEntry(d) if entry.name != 'subdir/subdir2': gdaltest.post_reason('fail') print(entry.name) return 'fail' if entry.mode != 16384: gdaltest.post_reason('fail') print(entry.mode) return 'fail' entry = gdal.GetNextDirEntry(d) if entry.name != 'subdir/subdir2/test2': gdaltest.post_reason('fail') print(entry.name) return 'fail' if entry.mode != 32768: gdaltest.post_reason('fail') print(entry.mode) return 'fail' entry = gdal.GetNextDirEntry(d) if entry.name != 'test': gdaltest.post_reason('fail') print(entry.name) return 'fail' if entry.mode != 32768: gdaltest.post_reason('fail') print(entry.mode) return 'fail' if not entry.modeKnown: gdaltest.post_reason('fail') return 'fail' if entry.size != 3: gdaltest.post_reason('fail') return 'fail' if not entry.sizeKnown: gdaltest.post_reason('fail') return 'fail' if entry.mtime == 0: gdaltest.post_reason('fail') return 'fail' if not entry.mtimeKnown: gdaltest.post_reason('fail') return 'fail' if entry.extra: gdaltest.post_reason('fail') return 'fail' entry = gdal.GetNextDirEntry(d) if entry: gdaltest.post_reason('fail') return 'fail' gdal.CloseDir(d) # Only top level d = gdal.OpenDir('/vsimem/vsifile_opendir', 0) entry = gdal.GetNextDirEntry(d) if entry.name != 'subdir': gdaltest.post_reason('fail') print(entry.name) return 'fail' entry = gdal.GetNextDirEntry(d) if entry.name != 'test': gdaltest.post_reason('fail') print(entry.name) return 'fail' entry = gdal.GetNextDirEntry(d) if entry: gdaltest.post_reason('fail') return 'fail' gdal.CloseDir(d) # Depth 1 files = [l_entry.name for l_entry in gdal.listdir('/vsimem/vsifile_opendir', 1)] if files != ['subdir', 'subdir/subdir2', 'test']: gdaltest.post_reason('fail') print(files) return 'fail' gdal.RmdirRecursive('/vsimem/vsifile_opendir') return 'success'
def test_cog_byte_to_web_mercator(): tab = [0] def my_cbk(pct, _, arg): assert pct >= tab[0] tab[0] = pct return 1 directory = '/vsimem/test_cog_byte_to_web_mercator' gdal.Mkdir(directory, 0o755) filename = directory + '/cog.tif' src_ds = gdal.Open('data/byte.tif') ds = gdal.GetDriverByName('COG').CreateCopy( filename, src_ds, options=['TILING_SCHEME=GoogleMapsCompatible', 'ALIGNED_LEVELS=3'], callback=my_cbk, callback_data=tab) assert tab[0] == 1.0 assert ds assert len( gdal.ReadDir(directory)) == 1 # check that the temp file has gone away ds = None ds = gdal.Open(filename) assert ds.RasterCount == 2 assert ds.RasterXSize == 1024 assert ds.RasterYSize == 1024 assert ds.GetRasterBand( 1).GetMaskFlags() == gdal.GMF_ALPHA + gdal.GMF_PER_DATASET assert ds.GetRasterBand(1).GetBlockSize() == [256, 256] gt = ds.GetGeoTransform() expected_gt = [ -13149614.849955443, 76.43702828517598, 0.0, 4070118.8821290657, 0.0, -76.43702828517598 ] for i in range(6): if gt[i] != pytest.approx(expected_gt[i], abs=1e-10 * abs(expected_gt[i])): assert False, gt assert ds.GetRasterBand(1).Checksum() in ( 4363, 4264, 4362) # 4264 on Mac , 4362 on Mac / Conda assert ds.GetRasterBand(1).GetMaskBand().Checksum() == 4356 assert ds.GetRasterBand(1).GetOverviewCount() == 2 ds = None _check_cog(filename) # Use our generated COG as the input of the same COG generation: reprojection # should be skipped filename2 = directory + '/cog2.tif' src_ds = gdal.Open(filename) class my_error_handler(object): def __init__(self): self.debug_msg_list = [] self.other_msg_list = [] def handler(self, eErrClass, err_no, msg): if eErrClass == gdal.CE_Debug: self.debug_msg_list.append(msg) else: self.other_msg_list.append(msg) handler = my_error_handler() try: gdal.PushErrorHandler(handler.handler) gdal.SetCurrentErrorHandlerCatchDebug(True) with gdaltest.config_option('CPL_DEBUG', 'COG'): ds = gdal.GetDriverByName('COG').CreateCopy( filename2, src_ds, options=[ 'TILING_SCHEME=GoogleMapsCompatible', 'ALIGNED_LEVELS=3' ]) finally: gdal.PopErrorHandler() assert ds assert 'COG: Skipping reprojection step: source dataset matches reprojection specifications' in handler.debug_msg_list assert handler.other_msg_list == [] src_ds = None ds = None # Cleanup gdal.GetDriverByName('GTiff').Delete(filename) gdal.GetDriverByName('GTiff').Delete(filename2) gdal.Unlink(directory)
def vsisync(): with gdaltest.error_handler(): if gdal.Sync('/i_do/not/exist', '/vsimem/'): gdaltest.post_reason('fail') return 'fail' with gdaltest.error_handler(): if gdal.Sync('vsifile.py', '/i_do/not/exist'): gdaltest.post_reason('fail') return 'fail' # Test copying a file for i in range(2): if not gdal.Sync('vsifile.py', '/vsimem/'): gdaltest.post_reason('fail') return 'fail' if gdal.VSIStatL('/vsimem/vsifile.py').size != gdal.VSIStatL('vsifile.py').size: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/vsifile.py') # Test copying the content of a directory gdal.Mkdir('/vsimem/test_sync', 0) gdal.FileFromMemBuffer('/vsimem/test_sync/foo.txt', 'bar') gdal.Mkdir('/vsimem/test_sync/subdir', 0) gdal.FileFromMemBuffer('/vsimem/test_sync/subdir/bar.txt', 'baz') if sys.platform != 'win32': with gdaltest.error_handler(): if gdal.Sync('/vsimem/test_sync/', '/i_do_not/exist'): gdaltest.post_reason('fail') return 'fail' if not gdal.Sync('/vsimem/test_sync/', '/vsimem/out'): gdaltest.post_reason('fail') return 'fail' if gdal.ReadDir('/vsimem/out') != [ 'foo.txt', 'subdir' ]: gdaltest.post_reason('fail') print(gdal.ReadDir('/vsimem/out')) return 'fail' if gdal.ReadDir('/vsimem/out/subdir') != [ 'bar.txt' ]: gdaltest.post_reason('fail') print(gdal.ReadDir('/vsimem/out/subdir')) return 'fail' # Again if not gdal.Sync('/vsimem/test_sync/', '/vsimem/out'): gdaltest.post_reason('fail') return 'fail' gdal.RmdirRecursive('/vsimem/out') # Test copying a directory pct_values = [] def my_progress(pct, message, user_data): pct_values.append(pct) if not gdal.Sync('/vsimem/test_sync', '/vsimem/out', callback = my_progress): gdaltest.post_reason('fail') return 'fail' if pct_values != [0.5, 1.0]: gdaltest.post_reason('fail') print(pct_values) return 'fail' if gdal.ReadDir('/vsimem/out') != [ 'test_sync' ]: gdaltest.post_reason('fail') print(gdal.ReadDir('/vsimem/out')) return 'fail' if gdal.ReadDir('/vsimem/out/test_sync') != [ 'foo.txt', 'subdir' ]: gdaltest.post_reason('fail') print(gdal.ReadDir('/vsimem/out/test_sync')) return 'fail' gdal.RmdirRecursive('/vsimem/test_sync') gdal.RmdirRecursive('/vsimem/out') return 'success'
def vsiswift_extra_1(): if not gdaltest.built_against_curl(): return 'skip' swift_resource = gdal.GetConfigOption('SWIFT_RESOURCE') if swift_resource is None: print('Missing SWIFT_RESOURCE for running gdaltest_list_extra') return 'skip' if swift_resource.find('/') < 0: path = '/vsiswift/' + swift_resource statres = gdal.VSIStatL(path) if statres is None or not stat.S_ISDIR(statres.mode): gdaltest.post_reason('fail') print('%s is not a valid bucket' % path) return 'fail' readdir = gdal.ReadDir(path) if readdir is None: gdaltest.post_reason('fail') print('ReadDir() should not return empty list') return 'fail' for filename in readdir: if filename != '.': subpath = path + '/' + filename if gdal.VSIStatL(subpath) is None: gdaltest.post_reason('fail') print('Stat(%s) should not return an error' % subpath) return 'fail' unique_id = 'vsiswift_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) if ret < 0: gdaltest.post_reason('fail') print('Mkdir(%s) should not return an error' % subpath) return 'fail' readdir = gdal.ReadDir(path) if unique_id not in readdir: gdaltest.post_reason('fail') print('ReadDir(%s) should contain %s' % (path, unique_id)) print(readdir) return 'fail' ret = gdal.Mkdir(subpath, 0) if ret == 0: gdaltest.post_reason('fail') print('Mkdir(%s) repeated should return an error' % subpath) return 'fail' ret = gdal.Rmdir(subpath) if ret < 0: gdaltest.post_reason('fail') print('Rmdir(%s) should not return an error' % subpath) return 'fail' readdir = gdal.ReadDir(path) if unique_id in readdir: gdaltest.post_reason('fail') print('ReadDir(%s) should not contain %s' % (path, unique_id)) print(readdir) return 'fail' ret = gdal.Rmdir(subpath) if ret == 0: gdaltest.post_reason('fail') print('Rmdir(%s) repeated should return an error' % subpath) return 'fail' ret = gdal.Mkdir(subpath, 0) if ret < 0: gdaltest.post_reason('fail') print('Mkdir(%s) should not return an error' % subpath) return 'fail' f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') if f is None: gdaltest.post_reason('fail') return 'fail' gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) if ret == 0: gdaltest.post_reason('fail') print('Rmdir(%s) on non empty directory should return an error' % subpath) return 'fail' f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') if f is None: gdaltest.post_reason('fail') return 'fail' data = gdal.VSIFReadL(1, 5, f).decode('utf-8') if data != 'hello': gdaltest.post_reason('fail') print(data) return 'fail' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test.txt') if ret < 0: gdaltest.post_reason('fail') print('Unlink(%s) should not return an error' % (subpath + '/test.txt')) return 'fail' ret = gdal.Rmdir(subpath) if ret < 0: gdaltest.post_reason('fail') print('Rmdir(%s) should not return an error' % subpath) return 'fail' return 'success' f = open_for_read('/vsiswift/' + swift_resource) if f is None: gdaltest.post_reason('fail') return 'fail' ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(ret) != 1: gdaltest.post_reason('fail') print(ret) return 'fail' # Same with /vsiswift_streaming/ f = open_for_read('/vsiswift_streaming/' + swift_resource) if f is None: gdaltest.post_reason('fail') return 'fail' ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) if len(ret) != 1: gdaltest.post_reason('fail') print(ret) return 'fail' # Invalid resource gdal.ErrorReset() f = open_for_read('/vsiswift_streaming/' + swift_resource + '/invalid_resource.baz') if f is not None: gdaltest.post_reason('fail') print(gdal.VSIGetLastErrorMsg()) return 'fail' return 'success'
def test_vsiadls_real_instance_tests(): adls_resource = gdal.GetConfigOption('ADLS_RESOURCE') if adls_resource is None: pytest.skip('Missing ADLS_RESOURCE') if '/' not in adls_resource: path = '/vsiadls/' + adls_resource try: statres = gdal.VSIStatL(path) assert statres is not None and stat.S_ISDIR(statres.mode), \ ('%s is not a valid bucket' % path) readdir = gdal.ReadDir(path) assert readdir is not None, 'ReadDir() should not return empty list' for filename in readdir: if filename != '.': subpath = path + '/' + filename assert gdal.VSIStatL(subpath) is not None, \ ('Stat(%s) should not return an error' % subpath) unique_id = 'vsiadls_test' subpath = path + '/' + unique_id ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id in readdir, \ ('ReadDir(%s) should contain %s' % (path, unique_id)) ret = gdal.Mkdir(subpath, 0) assert ret != 0, ('Mkdir(%s) repeated should return an error' % subpath) ret = gdal.Rmdir(subpath) assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath) readdir = gdal.ReadDir(path) assert unique_id not in readdir, \ ('ReadDir(%s) should not contain %s' % (path, unique_id)) ret = gdal.Rmdir(subpath) assert ret != 0, ('Rmdir(%s) repeated should return an error' % subpath) ret = gdal.Mkdir(subpath, 0) assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb') assert f is not None gdal.VSIFWriteL('hello', 1, 5, f) gdal.VSIFCloseL(f) ret = gdal.Rmdir(subpath) assert ret != 0, \ ('Rmdir(%s) on non empty directory should return an error' % subpath) f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) assert gdal.VSIStatL(subpath + '/test.txt') is not None md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS') assert 'x-ms-properties' in md md = gdal.GetFileMetadata(subpath + '/test.txt', 'STATUS') assert 'x-ms-resource-type' in md assert 'x-ms-properties' not in md md = gdal.GetFileMetadata(subpath + '/test.txt', 'ACL') assert 'x-ms-acl' in md assert 'x-ms-permissions' in md # Change properties properties_foo_bar = 'foo=' + base64.b64encode('bar') assert gdal.SetFileMetadata( subpath + '/test.txt', {'x-ms-properties': properties_foo_bar}, 'PROPERTIES') md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS') assert 'x-ms-properties' in md assert md['x-ms-properties'] == properties_foo_bar # Change ACL assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-permissions': '0777'}, 'ACL') md = gdal.GetFileMetadata(subpath + '/test.txt', 'ACL') assert 'x-ms-permissions' in md assert md['x-ms-permissions'] == 'rwxrwxrwx' # Change ACL recursively md = gdal.GetFileMetadata(subpath, 'ACL') assert 'x-ms-acl' in md assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-acl': md['x-ms-acl']}, 'ACL', ['RECURSIVE=YES', 'MODE=set']) assert gdal.Rename(subpath + '/test.txt', subpath + '/test2.txt') == 0 assert gdal.VSIStatL(subpath + '/test.txt') is None assert gdal.VSIStatL(subpath + '/test2.txt') is not None f = gdal.VSIFOpenL(subpath + '/test2.txt', 'rb') assert f is not None data = gdal.VSIFReadL(1, 5, f).decode('utf-8') assert data == 'hello' gdal.VSIFCloseL(f) ret = gdal.Unlink(subpath + '/test2.txt') assert ret >= 0, \ ('Unlink(%s) should not return an error' % (subpath + '/test2.txt')) assert gdal.VSIStatL(subpath + '/test2.txt') is None assert gdal.Unlink( subpath + '/test2.txt' ) != 0, "Unlink on a deleted file should return an error" f = gdal.VSIFOpenL(subpath + '/test2.txt', 'wb') assert f is not None gdal.VSIFCloseL(f) assert gdal.VSIStatL(subpath + '/test2.txt') is not None finally: assert gdal.RmdirRecursive(subpath) == 0 return f = open_for_read('/vsiadls/' + adls_resource) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1 # Test GetSignedURL() signed_url = gdal.GetSignedURL('/vsiadls/' + adls_resource) f = open_for_read('/vsicurl_streaming/' + signed_url) assert f is not None ret = gdal.VSIFReadL(1, 1, f) gdal.VSIFCloseL(f) assert len(ret) == 1