def upload_to_database(variables): import os import ast import pathlib import subprocess host = variables['connection']['ip'] port = variables['connection']['port'] user = variables['connection']['user'] password = variables['connection']['password'] database = variables['connection']['database'] schema = variables['connection']['schema'] shapes = variables['shapes'] if isinstance(shapes, str): if os.path.isdir(shapes): shapes = list((pathlib.Path(shapes)).iterdir()) else: shapes = ast.literal_eval(shapes) if isinstance(shapes, str): shapes = [shapes] for item in shapes: item = str(item) archivo = pathlib.Path(item).suffix if archivo == '.shp': print(item) command = f""" ogr2ogr -f "PostgreSQL" PG:"host={host} port={port} user={user} dbname={database} password={password} schemas={schema}" {item} """ process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) output = process.stdout errors = process.stderr if output: print('Output: ', output) else: print('No hay output') if errors: print('Errores: ', errors) print(type(errors)) if errors.find('MultiPolygon') != -1: print('Este ha fallado por ser multialgo, lo lanzo otra vez') command = f'{command} -nlt PROMOTE_TO_MULTI' process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) output = process.stdout errors = process.stderr if output: print('Output: ', output) else: print('No hay output') if errors: print('Errores: ', errors) else: print('No hay errores') else: print('No hay errores') print('-------------------------------------------------------') object_to_return = {'successful_files': [], 'failed_files': []} return object_to_return variables = {'connection': {"ip": "146.59.69.180", "port": 30038, "user": "postgres", "password": "pg", "database": "lidarand", "schema": "victoriano"}, 'shapes': ['/home/data3/master_folder/demo/fcc/GIS/PKS_TEXTO_proposta_asg.shp', '/home/data3/master_folder/demo/fcc/GIS/Arcos.shp', '/home/data3/master_folder/demo/fcc/GIS/Explotaciones.shp']} upload_to_database(variables)