Compare Python files (.py) and replace values of variables if the same variables present in the same section

49 Views Asked by At

I have two python files: clean_file.py and settings_QA.py.

I want to do the following:

  1. Read the files clean_file.py and settings_QA.py
  2. Create a new file called settings_QA_clean.py
  3. Write the contents of clean_file.py to settings_QA_clean.py
  4. Replace the value of the variables in settings_QA_clean.py with the values from settings_QA.py if the variable name is the same in both files
  5. The variables are in the format: variable = value. The value of the variables are enclosed in " " and can contain any kind of data/data structure, and can be multi line also.

For example:

clean_file.py

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": f"redis://{REDIS_HOST}:6379",  # int(REDIS_PORT)),
        "OPTIONS": {
            "PICKLE_VERSION": 2,
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 25, "retry_on_timeout": True}
        },
    }
}

CAMS_PASSWORD = ""

settings_QA.py

CACHES = {
    "default": {
        'BACKEND': 'django_redis.cache.RedisCache',
        "LOCATION": "redis://%s:%s" % (LOCATION, REDIS_PORT),
        'OPTIONS': {
            'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster',
            'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool',
            'CONNECTION_POOL_KWARGS': {'skip_full_coverage_check': True,
                                       "max_connections": 25,
                                       "retry_on_timeout": True,
                                       }
        }
    }
}

TEMP_PASSWORD = ""

new_file.py

CACHES = {
    "default": {
        'BACKEND': 'django_redis.cache.RedisCache',
        "LOCATION": "redis://%s:%s" % (LOCATION, REDIS_PORT),
        'OPTIONS': {
            'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster',
            'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool',
            'CONNECTION_POOL_KWARGS': {'skip_full_coverage_check': True,
                                       "max_connections": 25,
                                       "retry_on_timeout": True,
                                       }
        }
    }
}

CAMS_PASSWORD = ""

I have tried using regex and ast module. This is the code I wrote:

import re
import ast

def update_variables(clean_file_path, settings_qa_file_path):
    qa_variables = {}

    with open(settings_qa_file_path, 'r') as f:
        lines = f.readlines()

    for line in lines:
        match = re.match(r'^(\w+)\s*=\s*(.*)$', line)
        if match:
            name, value = match.group(1), match.group(2)
            try:
                qa_variables[name] = ast.literal_eval(value)
            except (ValueError, SyntaxError):
                qa_variables[name] = value

    with open(clean_file_path, 'r') as f:
        clean_file_lines = f.readlines()

    # Update the variables in the clean_file with the values from settings_QA
    updated_lines = []
    for line in clean_file_lines:
        match = re.match(r'^(\w+)\s*=\s*(.*)$', line)
        if match and match.group(1) in qa_variables:
            name = match.group(1)
            value = repr(qa_variables[name])
            updated_lines.append(f'{name} = {value}')
        else:
            updated_lines.append(line)

    # Write the updated clean_file
    with open('settings_clean.py', 'w') as f:
        f.writelines(updated_lines)

update_variables('clean_file.py', 'settings_QA.py')

The problem is that all the content gets written in one line, some of it is half written and the values are not really replaced for the same variable.

0

There are 0 best solutions below