Ejemplo n.º 1
0
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_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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
def test_ogr_pds4_create_table_delimited_with_srs_no_vrt():

    options = [
        'VAR_LOGICAL_IDENTIFIER=logical_identifier', 'VAR_TITLE=title',
        'VAR_INVESTIGATION_AREA_NAME=ian',
        'VAR_INVESTIGATION_AREA_LID_REFERENCE=INVESTIGATION_AREA_LID_REFERENCE',
        'VAR_OBSERVING_SYSTEM_NAME=osn', 'VAR_TARGET=target',
        'VAR_TARGET_TYPE=target'
    ]

    ds = ogr.GetDriverByName('PDS4').CreateDataSource('/vsimem/test.xml',
                                                      options=options)
    srs = osr.SpatialReference()
    srs.SetFromUserInput('+proj=tmerc +datum=WGS84')
    lyr = ds.CreateLayer('foo', srs=srs, options=['CREATE_VRT=NO'])
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    f = ogr.Feature(lyr.GetLayerDefn())
    lyr.CreateFeature(f)
    ds = None

    assert validate_xml('/vsimem/test.xml')

    ds = ogr.Open('/vsimem/test.xml')
    lyr = ds.GetLayerByName('foo')
    wkt = lyr.GetSpatialRef().ExportToWkt()
    assert wkt.replace(
        'D_WGS_1984', 'WGS_1984'
    ) == 'PROJCS["Transverse Mercator target",GEOGCS["GCS_target",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,0]],PRIMEM["Reference_Meridian",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Metre",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'.replace(
        'D_WGS_1984', 'WGS_1984'), wkt

    ds = None

    ogr.GetDriverByName('PDS4').DeleteDataSource('/vsimem/test.xml')
    gdal.Rmdir('/vsimem/test')
Ejemplo n.º 5
0
def gdal_rm_recurse(filename, simulate=False):

    delete_self = True
    if filename.endswith('/*'):
        delete_self = False
        filename = filename[0:-2]

    dir_contents = gdal.ReadDir(filename)
    if dir_contents:
        for f in dir_contents:
            if f not in ('.', '..'):
                ret = gdal_rm_recurse(filename + '/' + f, simulate=simulate)
                if ret != 0:
                    return ret
        if not delete_self:
            return 0
        elif simulate:
            print('Rmdir(%s)' % filename)
            return 0
        else:
            ret = gdal.Rmdir(filename)
            # Some filesystems, like /vsiaz/ don't have a real directory
            # implementation. As soon as you remove the last file in the dir,
            # the dir "disappears".
            if ret < 0:
                if gdal.VSIStatL(filename) is None:
                    ret = 0
            return ret
    else:
        if simulate:
            print('Unlink(%s)' % filename)
            return 0
        return gdal.Unlink(filename)
Ejemplo n.º 6
0
def vsifile_9():

    lst = gdal.ReadDir('.')
    if len(lst) < 4:
        gdaltest.post_reason('fail')
        return 'fail'
    # Test truncation
    lst_truncated = gdal.ReadDir('.', int(len(lst) / 2))
    if len(lst_truncated) <= int(len(lst) / 2):
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.Mkdir('/vsimem/mydir', 438)
    for i in range(10):
        fp = gdal.VSIFOpenL('/vsimem/mydir/%d' % i, 'wb')
        gdal.VSIFCloseL(fp)

    lst = gdal.ReadDir('/vsimem/mydir')
    if len(lst) < 4:
        gdaltest.post_reason('fail')
        return 'fail'
    # Test truncation
    lst_truncated = gdal.ReadDir('/vsimem/mydir', int(len(lst) / 2))
    if len(lst_truncated) <= int(len(lst) / 2):
        gdaltest.post_reason('fail')
        return 'fail'

    for i in range(10):
        gdal.Unlink('/vsimem/mydir/%d' % i)
    gdal.Rmdir('/vsimem/mydir')

    return 'success'
Ejemplo n.º 7
0
def ogr_libkml_check_write_dir():
    if not ogrtest.have_read_libkml:
        return 'skip'

    ret = ogr_libkml_check_write('/vsimem/libkmldir')
    files = gdal.ReadDir('/vsimem/libkmldir')
    for filename in files:
        gdal.Unlink('/vsimem/libkmldir/' + filename)
    gdal.Rmdir('/vsimem/libkmldir')
    return ret
Ejemplo n.º 8
0
def test_ogr_vdv_cleanup():

    gdal.Unlink('tmp/test.x10')
    gdal.Unlink('/vsimem/vdv/ogr_vdv_2.x10')
    gdal.Unlink('/vsimem/vdv/ogr_vdv_7.x10')
    gdal.Unlink('/vsimem/vdv/ogr_vdv_8.x10')
    files = gdal.ReadDir('tmp/test_x10')
    if files is not None:
        for f in files:
            gdal.Unlink('tmp/test_x10/' + f)
    gdal.Rmdir('tmp/test_x10')
Ejemplo n.º 9
0
def test_vsifile_8():

    # octal 0666 = decimal 438
    gdal.Mkdir('/vsimem/mydir', 438)
    fp = gdal.VSIFOpenL('/vsimem/mydir/a', 'wb')
    gdal.VSIFCloseL(fp)
    gdal.Rename('/vsimem/mydir', '/vsimem/newdir'.encode('ascii').decode('ascii'))
    assert gdal.VSIStatL('/vsimem/newdir') is not None
    assert gdal.VSIStatL('/vsimem/newdir/a') is not None
    gdal.Unlink('/vsimem/newdir/a')
    gdal.Rmdir('/vsimem/newdir')
Ejemplo n.º 10
0
def vsifile_8():

    # octal 0666 = decimal 438
    gdal.Mkdir('/vsimem/mydir', 438)
    fp = gdal.VSIFOpenL('/vsimem/mydir/a', 'wb')
    gdal.VSIFCloseL(fp)
    gdal.Rename('/vsimem/mydir', '/vsimem/newdir'.encode('ascii').decode('ascii'))
    if gdal.VSIStatL('/vsimem/newdir') is None:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIStatL('/vsimem/newdir/a') is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.Unlink('/vsimem/newdir/a')
    gdal.Rmdir('/vsimem/newdir')

    return 'success'
Ejemplo n.º 11
0
def test_vsifile_9():

    lst = gdal.ReadDir('.')
    assert len(lst) >= 4
    # Test truncation
    lst_truncated = gdal.ReadDir('.', int(len(lst) / 2))
    assert len(lst_truncated) > int(len(lst) / 2)

    gdal.Mkdir('/vsimem/mydir', 438)
    for i in range(10):
        fp = gdal.VSIFOpenL('/vsimem/mydir/%d' % i, 'wb')
        gdal.VSIFCloseL(fp)

    lst = gdal.ReadDir('/vsimem/mydir')
    assert len(lst) >= 4
    # Test truncation
    lst_truncated = gdal.ReadDir('/vsimem/mydir', int(len(lst) / 2))
    assert len(lst_truncated) > int(len(lst) / 2)

    for i in range(10):
        gdal.Unlink('/vsimem/mydir/%d' % i)
    gdal.Rmdir('/vsimem/mydir')
Ejemplo n.º 12
0
def gdal_rm(argv, progress=None):
    filename = None

    argv = gdal.GeneralCmdLineProcessor(argv)
    if argv is None:
        return -1

    for i in range(1, len(argv)):
        if 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()

    ret = gdal.Rmdir(filename)
    if ret != 0:
        print('Deletion failed')
    return ret
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
def vsiaz_extra_1():

    if not gdaltest.built_against_curl():
        return 'skip'

    az_resource = gdal.GetConfigOption('/azure/blob/myaccount/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'

    # 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'

    return 'success'
Ejemplo n.º 16
0
def vsiaz_fake_mkdir_rmdir():

    if gdaltest.webserver_port == 0:
        return 'skip'

    # Invalid name
    ret = gdal.Mkdir('/vsiaz', 0)
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    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)
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # 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)
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid name
    ret = gdal.Rmdir('/vsiaz')
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # 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')
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # 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')
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # 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')
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # 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')
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Ejemplo n.º 17
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

        md = gdal.GetFileMetadata(subpath + '/test.txt', 'METADATA')
        assert 'ETag' in md
        assert 'x-ms-blob-type' not in 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['Content-Type'] == 'foo'

        # 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
        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
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
0
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'
Ejemplo n.º 20
0
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'
Ejemplo n.º 21
0
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>az_bucket_test_mkdir/</Prefix>
                        <Blobs>
                          <Blob>
                            <Name>az_bucket_test_mkdir/it_is_a_file</Name>
                          </Blob>
                        </Blobs>
                    </EnumerationResults>
                """)
    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)
    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
    # --> do not consider this as an error because Azure directories are removed
    # as soon as the last object in it is removed. So when directories are created
    # without .gdal_marker_for_dir they will disappear without explicit removal
    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
Ejemplo n.º 22
0
def test_ogr_pds4_create_table_character(line_ending):

    options = ['VAR_LOGICAL_IDENTIFIER=urn:foo:bar:baz:logical_identifier',
               'VAR_TITLE=title',
               'VAR_INVESTIGATION_AREA_NAME=ian',
               'VAR_INVESTIGATION_AREA_LID_REFERENCE=urn:foo:bar:baz:ialr',
               'VAR_OBSERVING_SYSTEM_NAME=osn',
               'VAR_TARGET=target',
               'VAR_TARGET_TYPE=target']

    ds = ogr.GetDriverByName('PDS4').CreateDataSource('/vsimem/test.xml',
                                                      options=options)

    layer_creation_options = ['TABLE_TYPE=CHARACTER']
    if line_ending:
        layer_creation_options.append('LINE_ENDING=' + line_ending)
    if line_ending == 'error':
        with gdaltest.error_handler():
            lyr = ds.CreateLayer('0f:oo', options=layer_creation_options)
    else:
        lyr = ds.CreateLayer('0f:oo', options=layer_creation_options)
    fld = ogr.FieldDefn('bool', ogr.OFTInteger)
    fld.SetSubType(ogr.OFSTBoolean)
    lyr.CreateField(fld)
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('int64', ogr.OFTInteger64))
    lyr.CreateField(ogr.FieldDefn('real', ogr.OFTReal))
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('datetime', ogr.OFTDateTime))
    lyr.CreateField(ogr.FieldDefn('date', ogr.OFTDate))
    lyr.CreateField(ogr.FieldDefn('time', ogr.OFTTime))
    f = ogr.Feature(lyr.GetLayerDefn())
    f['bool'] = 1
    f['int'] = -123456789
    f['int64'] = -1234567890123
    f['real'] = 1.25
    f['str'] = 'foo'
    f['datetime'] = '2019/01/24 12:34:56.789+00'
    f['date'] = '2019-01-24'
    f['time'] = '12:34:56.789'
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/test.xml', 'rb')
    data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert '_Character' in data
    assert '_Binary' not in data
    if line_ending == 'LF':
        assert '<record_delimiter>Line-Feed</record_delimiter>' in data
    else:
        assert '<record_delimiter>Carriage-Return Line-Feed</record_delimiter>' in data
    assert 'LSB' not in data
    assert 'MSB' not in data
    assert '<local_identifier>_0f_oo</local_identifier>' in data

    if line_ending is None:
        # Only do that check in that configuration for faster test execution
        assert validate_xml('/vsimem/test.xml')

    assert gdal.VSIStatL('/vsimem/test/0f_oo.dat')

    f = gdal.VSIFOpenL('/vsimem/test/0f_oo.dat', 'rb')
    data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
    gdal.VSIFCloseL(f)
    if line_ending == 'LF':
        assert '\n' in data
        assert '\r\n' not in data
    else:
        assert '\r\n' in data

    ds = ogr.Open('/vsimem/test.xml')
    lyr = ds.GetLayer(0)
    assert lyr.GetLayerDefn().GetFieldCount() == 8
    f = lyr.GetNextFeature()
    assert f['bool']
    assert f['int'] == -123456789
    assert f['int64'] == -1234567890123
    assert f['real'] == 1.25
    assert f['str'] == 'foo'
    assert f['datetime'] == '2019/01/24 12:34:56.789+00'
    assert f['date'] == '2019/01/24'
    assert f['time'] == '12:34:56.789'
    ds = None

    if line_ending is None:
        # Only do that part in that configuration for faster test execution

        # Add new layer
        ds = ogr.Open('/vsimem/test.xml', update = 1)
        lyr = ds.CreateLayer('bar', options=['TABLE_TYPE=CHARACTER'])
        lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
        f = ogr.Feature(lyr.GetLayerDefn())
        f['int'] = 123
        lyr.CreateFeature(f)
        ds = None

        assert validate_xml('/vsimem/test.xml')

        ds = ogr.Open('/vsimem/test.xml')
        lyr = ds.GetLayerByName('bar')
        f = lyr.GetNextFeature()
        assert f['int'] == 123

        lyr = ds.GetLayer(0)
        f = lyr.GetNextFeature()
        assert f['int'] == -123456789

        ds = None

    ogr.GetDriverByName('PDS4').DeleteDataSource('/vsimem/test.xml')
    gdal.Rmdir('/vsimem/test')
Ejemplo n.º 23
0
def test_ogr_pds4_create_table_character():

    options = [
        'VAR_LOGICAL_IDENTIFIER=logical_identifier', 'VAR_TITLE=title',
        'VAR_INVESTIGATION_AREA_NAME=ian',
        'VAR_INVESTIGATION_AREA_LID_REFERENCE=INVESTIGATION_AREA_LID_REFERENCE',
        'VAR_OBSERVING_SYSTEM_NAME=osn', 'VAR_TARGET=target',
        'VAR_TARGET_TYPE=target'
    ]

    ds = ogr.GetDriverByName('PDS4').CreateDataSource('/vsimem/test.xml',
                                                      options=options)
    lyr = ds.CreateLayer('foo', options=['TABLE_TYPE=CHARACTER'])
    fld = ogr.FieldDefn('bool', ogr.OFTInteger)
    fld.SetSubType(ogr.OFSTBoolean)
    lyr.CreateField(fld)
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('int64', ogr.OFTInteger64))
    lyr.CreateField(ogr.FieldDefn('real', ogr.OFTReal))
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('datetime', ogr.OFTDateTime))
    lyr.CreateField(ogr.FieldDefn('date', ogr.OFTDate))
    lyr.CreateField(ogr.FieldDefn('time', ogr.OFTTime))
    f = ogr.Feature(lyr.GetLayerDefn())
    f['bool'] = 1
    f['int'] = -123456789
    f['int64'] = -1234567890123
    f['real'] = 1.25
    f['str'] = 'foo'
    f['datetime'] = '2019/01/24 12:34:56.789+00'
    f['date'] = '2019-01-24'
    f['time'] = '12:34:56.789'
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/test.xml', 'rb')
    data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert '_Character' in data
    assert '_Binary' not in data
    assert 'LSB' not in data
    assert 'MSB' not in data

    assert validate_xml('/vsimem/test.xml')

    assert gdal.VSIStatL('/vsimem/test/foo.dat')

    ds = ogr.Open('/vsimem/test.xml')
    lyr = ds.GetLayer(0)
    assert lyr.GetLayerDefn().GetFieldCount() == 8
    f = lyr.GetNextFeature()
    assert f['bool']
    assert f['int'] == -123456789
    assert f['int64'] == -1234567890123
    assert f['real'] == 1.25
    assert f['str'] == 'foo'
    assert f['datetime'] == '2019/01/24 12:34:56.789+00'
    assert f['date'] == '2019/01/24'
    assert f['time'] == '12:34:56.789'
    ds = None

    # Add new layer
    ds = ogr.Open('/vsimem/test.xml', update=1)
    lyr = ds.CreateLayer('bar', options=['TABLE_TYPE=CHARACTER'])
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    f = ogr.Feature(lyr.GetLayerDefn())
    f['int'] = 123
    lyr.CreateFeature(f)
    ds = None

    assert validate_xml('/vsimem/test.xml')

    ds = ogr.Open('/vsimem/test.xml')
    lyr = ds.GetLayerByName('bar')
    f = lyr.GetNextFeature()
    assert f['int'] == 123

    lyr = ds.GetLayer(0)
    f = lyr.GetNextFeature()
    assert f['int'] == -123456789

    ds = None

    ogr.GetDriverByName('PDS4').DeleteDataSource('/vsimem/test.xml')
    gdal.Rmdir('/vsimem/test')
Ejemplo n.º 24
0
def test_visoss_extra_1():

    if not gdaltest.built_against_curl():
        pytest.skip()

    # Either a bucket name or bucket/filename
    OSS_RESOURCE = gdal.GetConfigOption('OSS_RESOURCE')

    if gdal.GetConfigOption('OSS_SECRET_ACCESS_KEY') is None:
        pytest.skip('Missing OSS_SECRET_ACCESS_KEY')
    elif gdal.GetConfigOption('OSS_ACCESS_KEY_ID') is None:
        pytest.skip('Missing OSS_ACCESS_KEY_ID')
    elif OSS_RESOURCE is None:
        pytest.skip('Missing OSS_RESOURCE')

    if '/' not in OSS_RESOURCE:
        path = '/vsioss/' + OSS_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 = 'visoss_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('/vsioss/' + OSS_RESOURCE)
    assert f is not None, ('cannot open %s' % ('/vsioss/' + OSS_RESOURCE))
    ret = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)

    assert len(ret) == 1

    # Same with /vsioss_streaming/
    f = open_for_read('/vsioss_streaming/' + OSS_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('/vsioss/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('/vsioss_streaming/' + OSS_RESOURCE +
                      '/invalid_resource.baz')
    assert f is None, gdal.VSIGetLastErrorMsg()

    # Test GetSignedURL()
    signed_url = gdal.GetSignedURL('/vsioss/' + OSS_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
Ejemplo n.º 25
0
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
Ejemplo n.º 26
0
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 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
Ejemplo n.º 28
0
def test_ogr_pds4_create_table_delimited(line_ending):

    options = ['VAR_LOGICAL_IDENTIFIER=urn:foo:bar:baz:logical_identifier',
               'VAR_TITLE=title',
               'VAR_INVESTIGATION_AREA_NAME=ian',
               'VAR_INVESTIGATION_AREA_LID_REFERENCE=urn:foo:bar:baz:ialr',
               'VAR_OBSERVING_SYSTEM_NAME=osn',
               'VAR_TARGET=target',
               'VAR_TARGET_TYPE=target']

    ds = ogr.GetDriverByName('PDS4').CreateDataSource('/vsimem/test.xml',
                                                      options=options)

    layer_creation_options = []
    if line_ending:
        layer_creation_options.append('LINE_ENDING=' + line_ending)
    if line_ending == 'error':
        with gdaltest.error_handler():
            lyr = ds.CreateLayer('foo', options=layer_creation_options)
    else:
        lyr = ds.CreateLayer('foo', options=layer_creation_options)

    fld = ogr.FieldDefn('bool', ogr.OFTInteger)
    fld.SetSubType(ogr.OFSTBoolean)
    lyr.CreateField(fld)
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('int64', ogr.OFTInteger64))
    lyr.CreateField(ogr.FieldDefn('real', ogr.OFTReal))
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('datetime', ogr.OFTDateTime))
    lyr.CreateField(ogr.FieldDefn('date', ogr.OFTDate))
    lyr.CreateField(ogr.FieldDefn('time', ogr.OFTTime))
    f = ogr.Feature(lyr.GetLayerDefn())
    f['bool'] = 1
    f['int'] = -123456789
    f['int64'] = -1234567890123
    f['real'] = 1.25
    f['str'] = 'foo'
    f['datetime'] = '2019/01/24 12:34:56.789+00'
    f['date'] = '2019-01-24'
    f['time'] = '12:34:56.789'
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('LINESTRING (1 2,3 4)'))
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/test.xml', 'rb')
    data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert '_Character' not in data
    assert '_Binary' not in data
    if line_ending == 'LF':
        assert '<record_delimiter>Line-Feed</record_delimiter>' in data
    else:
        assert '<record_delimiter>Carriage-Return Line-Feed</record_delimiter>' in data
    assert 'LSB' not in data
    assert 'MSB' not in data

    if line_ending is None:
        # Only do that check in that configuration for faster test execution
        assert validate_xml('/vsimem/test.xml')

    ds = gdal.OpenEx('/vsimem/test.xml')
    assert ds
    assert ds.GetLayerCount() == 1
    fl = ds.GetFileList()
    assert len(fl) == 3, fl
    assert 'test.xml' in fl[0]
    assert 'foo.csv' in fl[1]
    assert 'foo.vrt' in fl[2]
    ds= None

    f = gdal.VSIFOpenL('/vsimem/test/foo.csv', 'rb')
    data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
    gdal.VSIFCloseL(f)
    if line_ending == 'LF':
        assert '\n' in data
        assert '\r\n' not in data
    else:
        assert '\r\n' in data

    for filename in [ '/vsimem/test.xml', '/vsimem/test/foo.vrt' ]:
        ds = ogr.Open(filename)
        lyr = ds.GetLayer(0)
        assert lyr.GetLayerDefn().GetFieldCount() == 8, filename
        f = lyr.GetNextFeature()
        assert f['bool']
        assert f['int'] == -123456789
        assert f['int64'] == -1234567890123
        assert f['real'] == 1.25
        assert f['str'] == 'foo'
        assert f['datetime'] == '2019/01/24 12:34:56.789+00'
        assert f['date'] == '2019/01/24'
        assert f['time'] == '12:34:56.789'
        assert f.GetGeometryRef().ExportToIsoWkt() == 'LINESTRING (1 2,3 4)'
        ds = None

    if line_ending is None:
        # Only do that part in that configuration for faster test execution

        # Add new layer
        ds = ogr.Open('/vsimem/test.xml', update = 1)
        lyr = ds.CreateLayer('no_geom', geom_type = ogr.wkbNone, options=['TABLE_TYPE=DELIMITED'])
        lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
        f = ogr.Feature(lyr.GetLayerDefn())
        f['int'] = 123
        lyr.CreateFeature(f)
        ds = None

        assert validate_xml('/vsimem/test.xml')

        ds = ogr.Open('/vsimem/test.xml')
        lyr = ds.GetLayerByName('no_geom')
        f = lyr.GetNextFeature()
        assert f['int'] == 123

        lyr = ds.GetLayer(0)
        f = lyr.GetNextFeature()
        assert f['int'] == -123456789

        ds = None

    ogr.GetDriverByName('PDS4').DeleteDataSource('/vsimem/test.xml')
    gdal.Rmdir('/vsimem/test')
Ejemplo n.º 29
0
def test_ogr_pds4_create_table_binary():

    options = [
        'VAR_LOGICAL_IDENTIFIER=logical_identifier', 'VAR_TITLE=title',
        'VAR_INVESTIGATION_AREA_NAME=ian',
        'VAR_INVESTIGATION_AREA_LID_REFERENCE=INVESTIGATION_AREA_LID_REFERENCE',
        'VAR_OBSERVING_SYSTEM_NAME=osn', 'VAR_TARGET=target',
        'VAR_TARGET_TYPE=target'
    ]

    for signedness in ['Signed', 'Unsigned']:
        for endianness in ['LSB', 'MSB']:

            ds = ogr.GetDriverByName('PDS4').CreateDataSource(
                '/vsimem/test.xml', options=options)

            layername = endianness
            with gdaltest.config_options({
                    'PDS4_ENDIANNESS': endianness,
                    'PDS4_SIGNEDNESS': signedness
            }):
                lyr = ds.CreateLayer(layername, options=['TABLE_TYPE=BINARY'])
                fld = ogr.FieldDefn('bool', ogr.OFTInteger)
                fld.SetSubType(ogr.OFSTBoolean)
                lyr.CreateField(fld)

                fld = ogr.FieldDefn('byte', ogr.OFTInteger)
                fld.SetWidth(2)
                lyr.CreateField(fld)

                fld = ogr.FieldDefn('int16', ogr.OFTInteger)
                fld.SetSubType(ogr.OFSTInt16)
                lyr.CreateField(fld)

                lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
                lyr.CreateField(ogr.FieldDefn('int64', ogr.OFTInteger64))

                fld = ogr.FieldDefn('float', ogr.OFTReal)
                fld.SetSubType(ogr.OFSTFloat32)
                lyr.CreateField(fld)

                lyr.CreateField(ogr.FieldDefn('real', ogr.OFTReal))
                lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
                lyr.CreateField(ogr.FieldDefn('datetime', ogr.OFTDateTime))
                lyr.CreateField(ogr.FieldDefn('date', ogr.OFTDate))
                lyr.CreateField(ogr.FieldDefn('time', ogr.OFTTime))

            sign = -1 if signedness == 'Signed' else 1

            f = ogr.Feature(lyr.GetLayerDefn())
            f['bool'] = 1
            f['byte'] = sign * 9
            f['int16'] = sign * 12345
            f['int'] = sign * 123456789
            f['int64'] = sign * 1234567890123
            f['float'] = 1.25
            f['real'] = 1.2567
            f['str'] = 'foo'
            f['datetime'] = '2019/01/24 12:34:56.789+00'
            f['date'] = '2019-01-24'
            f['time'] = '12:34:56.789'
            lyr.CreateFeature(f)

            ds = None

            f = gdal.VSIFOpenL('/vsimem/test.xml', 'rb')
            data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
            gdal.VSIFCloseL(f)

            assert '_Binary' in data
            assert '_Character' not in data
            if endianness == 'LSB':
                assert 'LSB' in data, data
                assert 'MSB' not in data, data
            else:
                assert 'MSB' in data, data
                assert 'LSB' not in data, data

            if signedness == 'Signed':
                assert 'Signed' in data, data
                assert 'Unsigned' not in data, data
            else:
                assert 'Unsigned' in data, data
                assert 'Signed' not in data, data

            assert validate_xml('/vsimem/test.xml')

            ds = ogr.Open('/vsimem/test.xml')
            layername = endianness
            lyr = ds.GetLayerByName(layername)
            assert lyr.GetLayerDefn().GetFieldCount() == 11
            f = lyr.GetNextFeature()
            assert f['bool']
            assert f['byte'] == sign * 9
            assert f['int16'] == sign * 12345
            assert f['int'] == sign * 123456789
            assert f['int64'] == sign * 1234567890123
            assert f['float'] == 1.25
            assert f['real'] == 1.2567
            assert f['str'] == 'foo'
            assert f['datetime'] == '2019/01/24 12:34:56.789+00'
            assert f['date'] == '2019/01/24'
            assert f['time'] == '12:34:56.789'

    ds = None

    # Add new layer
    ds = ogr.Open('/vsimem/test.xml', update=1)
    sr = osr.SpatialReference()
    sr.SetFromUserInput('WGS84')
    lyr = ds.CreateLayer('bar',
                         geom_type=ogr.wkbPoint25D,
                         srs=sr,
                         options=['TABLE_TYPE=BINARY'])
    f = ogr.Feature(lyr.GetLayerDefn())
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT Z (1 2 3)'))
    lyr.CreateFeature(f)
    ds = None

    assert validate_xml('/vsimem/test.xml')

    ds = ogr.Open('/vsimem/test.xml')
    lyr = ds.GetLayerByName('bar')
    f = lyr.GetNextFeature()
    assert f.GetGeometryRef().ExportToIsoWkt() == 'POINT Z (1 2 3)'
    ds = None

    ogr.GetDriverByName('PDS4').DeleteDataSource('/vsimem/test.xml')
    gdal.Rmdir('/vsimem/test')
Ejemplo n.º 30
0
def test_ogr_pds4_create_table_delimited():

    options = [
        'VAR_LOGICAL_IDENTIFIER=logical_identifier', 'VAR_TITLE=title',
        'VAR_INVESTIGATION_AREA_NAME=ian',
        'VAR_INVESTIGATION_AREA_LID_REFERENCE=INVESTIGATION_AREA_LID_REFERENCE',
        'VAR_OBSERVING_SYSTEM_NAME=osn', 'VAR_TARGET=target',
        'VAR_TARGET_TYPE=target'
    ]

    ds = ogr.GetDriverByName('PDS4').CreateDataSource('/vsimem/test.xml',
                                                      options=options)
    lyr = ds.CreateLayer('foo')
    fld = ogr.FieldDefn('bool', ogr.OFTInteger)
    fld.SetSubType(ogr.OFSTBoolean)
    lyr.CreateField(fld)
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('int64', ogr.OFTInteger64))
    lyr.CreateField(ogr.FieldDefn('real', ogr.OFTReal))
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('datetime', ogr.OFTDateTime))
    lyr.CreateField(ogr.FieldDefn('date', ogr.OFTDate))
    lyr.CreateField(ogr.FieldDefn('time', ogr.OFTTime))
    f = ogr.Feature(lyr.GetLayerDefn())
    f['bool'] = 1
    f['int'] = -123456789
    f['int64'] = -1234567890123
    f['real'] = 1.25
    f['str'] = 'foo'
    f['datetime'] = '2019/01/24 12:34:56.789+00'
    f['date'] = '2019-01-24'
    f['time'] = '12:34:56.789'
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('LINESTRING (1 2,3 4)'))
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/test.xml', 'rb')
    data = gdal.VSIFReadL(1, 100000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert '_Character' not in data
    assert '_Binary' not in data
    assert 'LSB' not in data
    assert 'MSB' not in data

    assert validate_xml('/vsimem/test.xml')

    ds = gdal.OpenEx('/vsimem/test.xml')
    assert ds
    assert ds.GetLayerCount() == 1
    fl = ds.GetFileList()
    assert len(fl) == 3, fl
    assert 'test.xml' in fl[0]
    assert 'foo.csv' in fl[1]
    assert 'foo.vrt' in fl[2]
    ds = None

    for filename in ['/vsimem/test.xml', '/vsimem/test/foo.vrt']:
        ds = ogr.Open(filename)
        lyr = ds.GetLayer(0)
        assert lyr.GetLayerDefn().GetFieldCount() == 8, filename
        f = lyr.GetNextFeature()
        assert f['bool']
        assert f['int'] == -123456789
        assert f['int64'] == -1234567890123
        assert f['real'] == 1.25
        assert f['str'] == 'foo'
        assert f['datetime'] == '2019/01/24 12:34:56.789+00'
        assert f['date'] == '2019/01/24'
        assert f['time'] == '12:34:56.789'
        assert f.GetGeometryRef().ExportToIsoWkt() == 'LINESTRING (1 2,3 4)'
        ds = None

    # Add new layer
    ds = ogr.Open('/vsimem/test.xml', update=1)
    lyr = ds.CreateLayer('no_geom',
                         geom_type=ogr.wkbNone,
                         options=['TABLE_TYPE=DELIMITED'])
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    f = ogr.Feature(lyr.GetLayerDefn())
    f['int'] = 123
    lyr.CreateFeature(f)
    ds = None

    assert validate_xml('/vsimem/test.xml')

    ds = ogr.Open('/vsimem/test.xml')
    lyr = ds.GetLayerByName('no_geom')
    f = lyr.GetNextFeature()
    assert f['int'] == 123

    lyr = ds.GetLayer(0)
    f = lyr.GetNextFeature()
    assert f['int'] == -123456789

    ds = None

    ogr.GetDriverByName('PDS4').DeleteDataSource('/vsimem/test.xml')
    gdal.Rmdir('/vsimem/test')