예제 #1
0
파일: vsiaz.py 프로젝트: zhn2019/gdal
def test_vsiaz_opendir():

    if gdaltest.webserver_port == 0:
        pytest.skip()

    # Unlimited depth
    handler = webserver.SequentialHandler()
    handler.add(
        'GET', '/azure/blob/myaccount/opendir?comp=list&restype=container',
        200, {'Content-type': 'application/xml'},
        """<?xml version="1.0" encoding="UTF-8"?>
                    <EnumerationResults>
                        <Prefix></Prefix>
                        <Blobs>
                          <Blob>
                            <Name>test.txt</Name>
                            <Properties>
                              <Last-Modified>01 Jan 1970 00:00:01</Last-Modified>
                              <Content-Length>40</Content-Length>
                            </Properties>
                          </Blob>
                          <Blob>
                            <Name>subdir/.gdal_marker_for_dir</Name>
                          </Blob>
                          <Blob>
                            <Name>subdir/test.txt</Name>
                            <Properties>
                              <Last-Modified>01 Jan 1970 00:00:01</Last-Modified>
                              <Content-Length>4</Content-Length>
                            </Properties>
                          </Blob>
                        </Blobs>
                    </EnumerationResults>""")
    with webserver.install_http_handler(handler):
        d = gdal.OpenDir('/vsiaz/opendir')
    assert d is not None

    entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'test.txt'
    assert entry.size == 40
    assert entry.mode == 32768
    assert entry.mtime == 1

    entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'subdir/'
    assert entry.mode == 16384

    entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'subdir/test.txt'
    assert entry.size == 4
    assert entry.mode == 32768

    entry = gdal.GetNextDirEntry(d)
    assert entry is None

    gdal.CloseDir(d)
예제 #2
0
파일: vsiadls.py 프로젝트: rsbivand/gdal
def test_vsiadls_opendir():

    if gdaltest.webserver_port == 0:
        pytest.skip()

    gdal.VSICurlClearCache()

    # Unlimited depth from root
    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/?resource=account', 200, {'Content-type': 'application/json', 'x-ms-continuation': 'contmarker_root'},
                """{ "filesystems": [{ "name": "fs1"}, { "name": "fs2"} ]}""")
    with webserver.install_http_handler(handler):
        d = gdal.OpenDir('/vsiadls/')
    assert d is not None

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs1?recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': 'contmarker_within_fs'},
                """
                {"paths":[{"name":"foo.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs1'
    assert entry.mode == 16384

    handler = webserver.SequentialHandler()
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs1/foo.txt'

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs1?continuation=contmarker_within_fs&recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': ''},
                """
                {"paths":[{"name":"bar.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs1/bar.txt'

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs2?recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': ''},
                """
                {"paths":[]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs2'

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/?continuation=contmarker_root&resource=account', 200, {'Content-type': 'application/json'},
                """{ "filesystems": [{ "name": "fs3"}] }""")
    handler.add('GET', '/azure/blob/myaccount/fs3?recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': ''},
                """
                {"paths":[{"name":"baz.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs3'

    handler = webserver.SequentialHandler()
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs3/baz.txt'

    entry = gdal.GetNextDirEntry(d)
    assert entry is None

    gdal.CloseDir(d)

    # Prefix filtering on subdir
    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs1?directory=sub_dir&recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8'},
                """
                {"paths":[{"name":"sub_dir/foo.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"},
                          {"name":"sub_dir/my_prefix_test.txt","contentLength":"40","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        d = gdal.OpenDir('/vsiadls/fs1/sub_dir', -1, ['PREFIX=my_prefix'])
    assert d is not None

    handler = webserver.SequentialHandler()
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'my_prefix_test.txt'
    assert entry.size == 40
    assert entry.mode == 32768
    assert entry.mtime == 1

    entry = gdal.GetNextDirEntry(d)
    assert entry is None

    gdal.CloseDir(d)

    # No network access done
    s = gdal.VSIStatL('/vsiadls/fs1/sub_dir/my_prefix_test.txt',
                      gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG |gdal.VSI_STAT_CACHE_ONLY)
    assert s
    assert (s.mode & 32768) != 0
    assert s.size == 40
    assert s.mtime == 1

    # No network access done
    assert gdal.VSIStatL('/vsiadls/fs1/sub_dir/i_do_not_exist.txt',
                         gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG | gdal.VSI_STAT_CACHE_ONLY) is None
예제 #3
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')
예제 #4
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'
예제 #5
0
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')
예제 #6
0
def test_vsiadls_opendir():

    if gdaltest.webserver_port == 0:
        pytest.skip()

    gdal.VSICurlClearCache()

    # Unlimited depth from root
    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/?resource=account', 200, {'Content-type': 'application/json', 'x-ms-continuation': 'contmarker_root'},
                """{ "filesystems": [{ "name": "fs1"}, { "name": "fs2"} ]}""")
    with webserver.install_http_handler(handler):
        d = gdal.OpenDir('/vsiadls/')
    assert d is not None

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs1?recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': 'contmarker_within_fs'},
                """
                {"paths":[{"name":"foo.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs1'
    assert entry.mode == 16384

    handler = webserver.SequentialHandler()
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs1/foo.txt'

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs1?continuation=contmarker_within_fs&recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': ''},
                """
                {"paths":[{"name":"bar.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs1/bar.txt'

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/fs2?recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': ''},
                """
                {"paths":[]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs2'

    handler = webserver.SequentialHandler()
    handler.add('GET', '/azure/blob/myaccount/?continuation=contmarker_root&resource=account', 200, {'Content-type': 'application/json'},
                """{ "filesystems": [{ "name": "fs3"}] }""")
    handler.add('GET', '/azure/blob/myaccount/fs3?recursive=true&resource=filesystem', 200,
                {'Content-type': 'application/json;charset=utf-8', 'x-ms-continuation': ''},
                """
                {"paths":[{"name":"baz.txt","contentLength":"123456","lastModified": "Mon, 01 Jan 1970 00:00:01"}]}
                """)
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs3'

    handler = webserver.SequentialHandler()
    with webserver.install_http_handler(handler):
        entry = gdal.GetNextDirEntry(d)
    assert entry.name == 'fs3/baz.txt'

    entry = gdal.GetNextDirEntry(d)
    assert entry is None

    gdal.CloseDir(d)