apb_cx_oracle_spatial

Package apb_cx_oracle_spatial

Modules to connect to Oracle database and treat his objects in a sqlalchemy style but dealing with spatial data as one more type. Added the functionality of common used libraries in this kind of projects from the apb_extra_osgeo_utils and apb_spatial_utils packages to convert the Oracle data on different open source spatial data types.

Requires instant client Oracle and for all the functionality available, GDAL library version 3.6<=3.9.

To install:

pip install apb_cx_oracle_spatial

Documentation here apb_cx_oracle_spatial

 1#  coding=utf-8
 2#
 3#  Author: Ernesto Arredondo Martinez (ernestone@gmail.com)
 4#  Created: 
 5#  Copyright (c)
 6"""
 7.. include:: ../README.md
 8"""
 9
10import os
11import shutil
12import tempfile
13from pathlib import Path
14from platform import system
15from zipfile import ZipFile
16
17from apb_extra_utils.misc import download_and_unzip
18
19
20def set_instantclient_oracle():
21    """
22    Set the instant client oracle 64bits to use gestor oracle
23
24    Returns:
25        setted (bool)
26    """
27    instant_client = os.getenv('INSTANT_CLIENT_NAME', 'instantclient_oracle')
28    local_path_instant_client = os.getenv('PATH_INSTANT_CLIENT_ORACLE', os.path.join(Path.home(), instant_client))
29    if not os.path.exists(local_path_instant_client):
30        tempdir = tempfile.gettempdir()
31        path_extract = os.path.join(tempdir, instant_client)
32
33        path_instant_client_zip = os.getenv('PATH_INSTANT_CLIENT_ORACLE_ZIP', '')
34        # Path zip
35        if os.path.exists(path_instant_client_zip):
36            zip_name = f'{instant_client}.zip'
37            temp_zip = os.path.join(tempdir, zip_name)
38            if not os.path.exists(temp_zip):
39                shutil.copy(path_instant_client_zip, tempdir)
40            zipfile = ZipFile(temp_zip)
41            zipfile.extractall(path=path_extract)
42        else:
43            # Decide wich system
44            sys_name = system().lower()
45            url_instant_client = None
46            if sys_name == 'windows':
47                url_instant_client = os.getenv(
48                    'URL_INSTANT_CLIENT_ORACLE_WINDOWS',
49                    'https://download.oracle.com/otn_software/nt/instantclient/instantclient-basiclite-windows.zip')
50            elif sys_name == 'linux':
51                url_instant_client = os.getenv(
52                    'URL_INSTANT_CLIENT_ORACLE_LINUX',
53                    'https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip')
54
55            if url_instant_client:
56                download_and_unzip(url_instant_client, path_extract)
57
58        if os.path.exists(path_extract):
59            instant_temp_path = os.path.join(path_extract, next(iter(os.listdir(path_extract)), ''))
60            if os.path.exists(instant_temp_path):
61                shutil.move(instant_temp_path, local_path_instant_client)
62
63    if os.path.exists(local_path_instant_client) and \
64            not any(os.path.samefile(local_path_instant_client, p) for p in os.get_exec_path() if os.path.exists(p)):
65        prev_path = os.getenv('PATH')
66        os.environ['PATH'] = f'{local_path_instant_client};{prev_path}'
67        print(f'Set PATH with instant_client "{local_path_instant_client}"')
68
69
70set_instantclient_oracle()
def set_instantclient_oracle():
21def set_instantclient_oracle():
22    """
23    Set the instant client oracle 64bits to use gestor oracle
24
25    Returns:
26        setted (bool)
27    """
28    instant_client = os.getenv('INSTANT_CLIENT_NAME', 'instantclient_oracle')
29    local_path_instant_client = os.getenv('PATH_INSTANT_CLIENT_ORACLE', os.path.join(Path.home(), instant_client))
30    if not os.path.exists(local_path_instant_client):
31        tempdir = tempfile.gettempdir()
32        path_extract = os.path.join(tempdir, instant_client)
33
34        path_instant_client_zip = os.getenv('PATH_INSTANT_CLIENT_ORACLE_ZIP', '')
35        # Path zip
36        if os.path.exists(path_instant_client_zip):
37            zip_name = f'{instant_client}.zip'
38            temp_zip = os.path.join(tempdir, zip_name)
39            if not os.path.exists(temp_zip):
40                shutil.copy(path_instant_client_zip, tempdir)
41            zipfile = ZipFile(temp_zip)
42            zipfile.extractall(path=path_extract)
43        else:
44            # Decide wich system
45            sys_name = system().lower()
46            url_instant_client = None
47            if sys_name == 'windows':
48                url_instant_client = os.getenv(
49                    'URL_INSTANT_CLIENT_ORACLE_WINDOWS',
50                    'https://download.oracle.com/otn_software/nt/instantclient/instantclient-basiclite-windows.zip')
51            elif sys_name == 'linux':
52                url_instant_client = os.getenv(
53                    'URL_INSTANT_CLIENT_ORACLE_LINUX',
54                    'https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip')
55
56            if url_instant_client:
57                download_and_unzip(url_instant_client, path_extract)
58
59        if os.path.exists(path_extract):
60            instant_temp_path = os.path.join(path_extract, next(iter(os.listdir(path_extract)), ''))
61            if os.path.exists(instant_temp_path):
62                shutil.move(instant_temp_path, local_path_instant_client)
63
64    if os.path.exists(local_path_instant_client) and \
65            not any(os.path.samefile(local_path_instant_client, p) for p in os.get_exec_path() if os.path.exists(p)):
66        prev_path = os.getenv('PATH')
67        os.environ['PATH'] = f'{local_path_instant_client};{prev_path}'
68        print(f'Set PATH with instant_client "{local_path_instant_client}"')

Set the instant client oracle 64bits to use gestor oracle

Returns:

setted (bool)