def test_vsisync(): with gdaltest.error_handler(): assert not gdal.Sync('/i_do/not/exist', '/vsimem/') with gdaltest.error_handler(): assert not gdal.Sync('vsifile.py', '/i_do/not/exist') # Test copying a file for i in range(2): assert gdal.Sync('vsifile.py', '/vsimem/') assert gdal.VSIStatL('/vsimem/vsifile.py').size == gdal.VSIStatL( 'vsifile.py').size 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(): # even root cannot write into /proc assert not gdal.Sync('/vsimem/test_sync/', '/proc/i_do_not/exist') assert gdal.Sync('/vsimem/test_sync/', '/vsimem/out') assert gdal.ReadDir('/vsimem/out') == ['foo.txt', 'subdir'] assert gdal.ReadDir('/vsimem/out/subdir') == ['bar.txt'] # Again assert gdal.Sync('/vsimem/test_sync/', '/vsimem/out') gdal.RmdirRecursive('/vsimem/out') # Test copying a directory pct_values = [] def my_progress(pct, message, user_data): pct_values.append(pct) assert gdal.Sync('/vsimem/test_sync', '/vsimem/out', callback=my_progress) assert pct_values == [0.5, 1.0] assert gdal.ReadDir('/vsimem/out') == ['test_sync'] assert gdal.ReadDir('/vsimem/out/test_sync') == ['foo.txt', 'subdir'] gdal.RmdirRecursive('/vsimem/test_sync') gdal.RmdirRecursive('/vsimem/out')
def test_vsiadls_fake_sync_multithreaded_upload_single_file(): if gdaltest.webserver_port == 0: pytest.skip() gdal.VSICurlClearCache() gdal.Mkdir('/vsimem/test', 0) gdal.FileFromMemBuffer('/vsimem/test/foo', 'foo\n') handler = webserver.SequentialHandler() handler.add('HEAD', '/azure/blob/myaccount/test_bucket?resource=filesystem', 200) handler.add('HEAD', '/azure/blob/myaccount/test_bucket/foo', 404) handler.add('PUT', '/azure/blob/myaccount/test_bucket/foo?resource=file', 201) handler.add('PATCH', '/azure/blob/myaccount/test_bucket/foo?action=append&position=0', 202, expected_body = b'foo') handler.add('PATCH', '/azure/blob/myaccount/test_bucket/foo?action=append&position=3', 202, expected_body = b'\n') handler.add('PATCH', '/azure/blob/myaccount/test_bucket/foo?action=flush&close=true&position=4', 200) with gdaltest.config_option('VSIS3_SIMULATE_THREADING', 'YES'): with webserver.install_http_handler(handler): assert gdal.Sync('/vsimem/test/foo', '/vsiadls/test_bucket', options=['NUM_THREADS=1', 'CHUNK_SIZE=3']) gdal.RmdirRecursive('/vsimem/test')
def gdal_rm(argv, progress=None): # pylint: disable=unused-argument filename = None recursive = False argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: return -1 for i in range(1, len(argv)): if argv[i] == '-r': recursive = True elif filename is None: filename = argv[i] elif argv[i][0] == '-': print('Unexpected option : %s' % argv[i]) return Usage() else: print('Unexpected option : %s' % argv[i]) return Usage() if filename is None: return Usage() if recursive: ret = gdal.RmdirRecursive(filename) else: ret = gdal.Rmdir(filename) if ret != 0: print('Deletion failed') return ret
def sync_to_dir(src_dir: str, dst_dir_uri: str, delete: bool = False): """Syncs a local source directory to a destination directory. If the FileSystem is remote, this involves uploading. Args: src_dir: local source directory to sync from dst_dir_uri: A destination directory that can be synced to by this FileSystem delete: True if the destination should be deleted first. """ def work(src, vsi_dest): gdal.Mkdir(vsi_dest, 0o777) for item in src.iterdir(): item_vsi_dest = os.path.join(vsi_dest, item.name) if item.is_dir(): work(item, item_vsi_dest) else: VsiFileSystem.copy_to(str(item), item_vsi_dest) stats = gdal.VSIStatL(dst_dir_uri) if stats: assert delete, 'Cannot overwrite existing files if delete=False' if stats.IsDirectory(): gdal.RmdirRecursive(dst_dir_uri) else: gdal.Unlink(dst_dir_uri) src = Path(src_dir) assert src.exists() and src.is_dir(), \ 'Local source ({}) must be a directory'.format(src_dir) work(src, dst_dir_uri)
def test_vsiadls_real_instance_filesystem_tests(): if gdal.GetConfigOption('ADLS_ALLOW_FILESYSTEM_TESTS') is None: pytest.skip('Missing ADLS_ALLOW_FILESYSTEM_TESTS') fspath = '/vsiadls/test-vsiadls-filesystem-tests' try: assert gdal.VSIStatL(fspath) is None assert gdal.Mkdir(fspath, 0) == 0 statres = gdal.VSIStatL(fspath) assert statres is not None and stat.S_ISDIR(statres.mode) assert gdal.ReadDir(fspath) == ["."] assert gdal.Mkdir(fspath, 0) != 0 assert gdal.Mkdir(fspath + '/subdir', 0) == 0 statres = gdal.VSIStatL(fspath + '/subdir') assert statres is not None and stat.S_ISDIR(statres.mode) assert gdal.Rmdir(fspath) != 0 finally: assert gdal.RmdirRecursive(fspath) == 0 assert gdal.VSIStatL(fspath) is None
def test_vsifile_rmdirrecursive(): gdal.Mkdir('tmp/rmdirrecursive', 493) gdal.Mkdir('tmp/rmdirrecursive/subdir', 493) open('tmp/rmdirrecursive/foo.bin', 'wb').close() open('tmp/rmdirrecursive/subdir/bar.bin', 'wb').close() assert gdal.RmdirRecursive('tmp/rmdirrecursive') == 0 assert not os.path.exists('tmp/rmdirrecursive')
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_gdal2tiles_vsimem(): gdal2tiles.main( argv=['-q', '../../gcore/data/byte.tif', '/vsimem/gdal2tiles']) assert set(gdal.ReadDirRecursive('/vsimem/gdal2tiles')) == set([ '14/', '14/2837/', '14/2837/9833.png', '14/2838/', '14/2838/9833.png', 'googlemaps.html', 'leaflet.html', 'openlayers.html', 'tilemapresource.xml' ]) gdal.RmdirRecursive('/vsimem/gdal2tiles')
def test_eedai_cleanup(): if gdaltest.eedai_drv is None: pytest.skip() gdal.SetConfigOption('CPL_CURL_ENABLE_VSIMEM', None) gdal.SetConfigOption('EEDA_BEARER', gdaltest.EEDA_BEARER) gdal.SetConfigOption('EEDA_URL', gdaltest.EEDA_URL) gdal.SetConfigOption('EEDA_PRIVATE_KEY', gdaltest.EEDA_PRIVATE_KEY) gdal.SetConfigOption('EEDA_CLIENT_EMAIL', gdaltest.EEDA_CLIENT_EMAIL) gdal.SetConfigOption('GO2A_AUD', None) gdal.SetConfigOption('GOA2_NOW', None) gdal.SetConfigOption('GOOGLE_APPLICATION_CREDENTIALS', gdaltest.GOOGLE_APPLICATION_CREDENTIALS) gdal.Unlink('/vsimem/ee/projects/earthengine-public/assets/image') gdal.RmdirRecursive('/vsimem/ee/')
def eedai_cleanup(): if gdaltest.eedai_drv is None: return 'skip' gdal.SetConfigOption('CPL_CURL_ENABLE_VSIMEM', None) gdal.SetConfigOption('EEDA_BEARER', gdaltest.EEDA_BEARER) gdal.SetConfigOption('EEDA_URL', gdaltest.EEDA_URL) gdal.SetConfigOption('EEDA_PRIVATE_KEY', gdaltest.EEDA_PRIVATE_KEY) gdal.SetConfigOption('EEDA_CLIENT_EMAIL', gdaltest.EEDA_CLIENT_EMAIL) gdal.SetConfigOption('GO2A_AUD', None) gdal.SetConfigOption('GOA2_NOW', None) gdal.Unlink('/vsimem/ee/assets/image') gdal.RmdirRecursive('/vsimem/ee/') return 'success'
def test_vsiaz_rmdirrecursive(): if gdaltest.webserver_port == 0: pytest.skip() handler = webserver.SequentialHandler() handler.add( 'GET', '/azure/blob/myaccount/rmdirrec?comp=list&prefix=subdir%2F&restype=container', 200, {'Content-type': 'application/xml'}, """<?xml version="1.0" encoding="UTF-8"?> <EnumerationResults> <Prefix>subdir/</Prefix> <Blobs> <Blob> <Name>subdir/test.txt</Name> <Properties> <Last-Modified>01 Jan 1970 00:00:01</Last-Modified> <Content-Length>40</Content-Length> </Properties> </Blob> <Blob> <Name>subdir/subdir2/.gdal_marker_for_dir</Name> </Blob> <Blob> <Name>subdir/subdir2/test.txt</Name> <Properties> <Last-Modified>01 Jan 1970 00:00:01</Last-Modified> <Content-Length>4</Content-Length> </Properties> </Blob> </Blobs> </EnumerationResults>""") handler.add('DELETE', '/azure/blob/myaccount/rmdirrec/subdir/test.txt', 202) handler.add('DELETE', '/azure/blob/myaccount/rmdirrec/subdir/subdir2/test.txt', 202) with webserver.install_http_handler(handler): assert gdal.RmdirRecursive('/vsiaz/rmdirrec/subdir') == 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 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 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_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_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