Untitled

 avatar
unknown
plain_text
2 years ago
4.5 kB
3
Indexable
###########################################################################################################################
"""
AUTHOR - M.Veerendra Kumar
EMP-ID - 2009927
SCRIPT - PPM delete script
DESCRIPTION - The below script deletes ppm table records dynamically upon passing the values
"""
############################################################################################################################


import pandas as pd
import mysql.connector
import json
from base64 import b64decode as base_b64decode
from base64 import b64encode as base_b64encode
import logging
import datetime
import os
from sqlalchemy import create_engine as sqlalchemy_create_engine

releaseId = '275.2'
releaseType = 'Deployed'
replicationTarget = 'SOURCE'
catalogId = ''
opId = 'HOB'
buId = 'DEFAULT'
replicationJobId = 'REP_990_234'
json_file_path = "/app/scripts/PPM_Release_Management/Product_Catalog_ETL/config/ppm_reply.json"
sql_log_file = f"/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.sql"
log_file = f'/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.log'
# sql_log_file = os.get_path("PPM_PC_LOG") + "/" + replicationJobId + "_ppm_pc_replication_delete.sql"
# json_file_path = os.get_path("PPM_PC_CONFIG") + "/ppm_pc_replication.json"
# log_file = os.get_path("PPM_PC_LOG") + "/" + replicationJobId + "_ppm_pc_replication_delete.log"
logging.basicConfig(
    filename=log_file,
    level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
query_count = 0
sql_log_file=open(sql_log_file, "w") 

def write_sql(query_info):
    sql_log_file.write(query_info)
    sql_log_file.write('\n')


try:
    def connect_to_database(json_data, replicationTarget):
        encrypt = json_data.get(replicationTarget, {}).get('ENCRYPT')
        host = json_data.get(replicationTarget, {}).get('DB_HOST')
        port = json_data.get(replicationTarget, {}).get('DB_PORT')
        user = json_data.get(replicationTarget, {}).get('DB_USER')
        db_type = json_data.get(replicationTarget, {}).get('DB_TYPE')
        schema = json_data.get(replicationTarget, {}).get('DB_SCHEMA')
        if encrypt == 'Y':
            password = base_b64decode(json_data.get(replicationTarget, {}).get('DB_PASSWORD')).decode('utf-8')
        else:
            password = json_data.get(replicationTarget, {}).get('DB_PASSWORD')
        if db_type == 'MYSQL':
            cnx = mysql.connector.connect(user=user, password=password, host=host, port=port)
            cursor = cnx.cursor()
            logging.info(f"connected to database server PPM_PC: {host}:{port}")
        elif db_type == 'ORACLE':
            import oracledb
            oracle_mode = oracledb.is_thin_mode()
            print("Oracle mode: %s" % oracle_mode)
            if oracle_mode:
                oracledb.init_oracle_client()
                print("Enabled python-oracledb Thick mode")
            else:
                print("Default python-oracledb Thick mode")

            cnx_text = ('oracle://%s:%s@%s:%s/?service_name=%s' % (user, password, host, port, schema))
            cnx = sqlalchemy_create_engine(cnx_text, encoding="utf8")
            cnx = connection.raw_connection()
            cursor = cnx.cursor()
        return cnx, cursor, schema


    with open(json_file_path) as json_file:
        json_data = json.load(json_file)
    
    conn_ppm, cursor_ppm, schema_ppm = connect_to_database(json_data, 'PPM_PC')
    ##cursor_ppm = conn_ppm.cursor()
    primary_query = f"SELECT * FROM {schema_ppm}.etl_ppm_replication_master"
    cursor_ppm.execute(primary_query)
    logging.info(f"executed primary query - {primary_query}")
    query_info = f"-- ++++++++++++++++++++|PRIMARY_QUERY\n"
    query_info += f"-- #Query:\n{primary_query};\n"
    write_sql(query_info)

    rows = cursor_ppm.fetchall()
    columns = [desc[0] for desc in cursor_ppm.description]
    df = pd.DataFrame(rows, columns=columns)




in the above code iam getting like
 File "ppm_pc_replication_delete.py", line 81, in <module>
    conn_ppm, cursor_ppm, schema_ppm = connect_to_database(json_data, 'PPM_PC')
  File "ppm_pc_replication_delete.py", line 54, in connect_to_database
    password = base_b64decode(json_data.get(replicationTarget, {}).get('DB_PASSWORD')).decode('utf-8')
  File "/app/software/binaries/python3.7.3/lib/python3.7/base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

because i have enabled encrypt as Y
Editor is loading...