Skip to content

Commit

Permalink
lets ship it
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp committed Oct 25, 2024
1 parent 316f3b5 commit c4fc616
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 89 deletions.
46 changes: 33 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
# sdkconfig-differ
sdkconfig differ for esp-idf config files
# esp-config-diff

`esp-config-diff` is a Python command-line tool for comparing two configuration
files and displaying differences. It highlights added, removed, and modified
configuration values, making it easy to track changes between versions.

## Features

- Compare two configuration files and output the differences.
- Color-coded output to distinguish between added, removed, and modified entries.
- Option to disable colors for plain-text output.
- Can be used as a standalone command-line tool or within a Python script.

---

## Installation

## Install requirements
```
python3 -m pip install -r requirements.txt
python3 -m pip install esp-config-diff
```

## How to use eg
## Usage

### Basic Usage
```
./sdkconfig-differ.py --conf sdkconfig --old-conf sdkconfig.old
esp-config-diff --conf sdkconfig --old-conf sdkconfig.old
```

## Example Output
### Disabling Color Output
```
esp-config-diff --conf sdkconfig --old-conf sdkconfig.old --no-color
```

### Example output
```
CONFIG Old Value New Value
===================================================
CONFIG_FEATURE_ENABLED ABSENT true
CONFIG_MAX_CONNECTIONS 10 20
CONFIG_TIMEOUT 300 ABSENT
```
CONFIG Old Value New Value
CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT ABSENT n
CONFIG_LWIP_IPV6_NUM_ADDRESSES ABSENT 6
CONFIG_BSP_LED_RGB_GPIO 8 48
CONFIG_IDF_TARGET "esp32c6" ABSENT
``
Empty file added diff/__init__.py
Empty file.
92 changes: 92 additions & 0 deletions diff/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3

import json
import click

# ANSI color codes for styling
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RESET = '\033[0m'
BOLD = '\033[1m'

# array to json
def to_json(config):
json_data = {}
for line in config:
parts = line.strip().split('=')
if len(parts) == 2:
json_data[parts[0]] = parts[1]
return json_data

# file to json
def make_config(sdkconfig):
with open(sdkconfig, 'r') as f:
conf_lines = f.readlines()
config = [line.strip() for line in conf_lines if not line.startswith('#')]
return config

@click.command()
@click.option('--conf', help='Current config file')
@click.option('--old-conf', help='Older config file')
@click.option('--no-color', is_flag=True, help='Disable colored output')
def main(conf, old_conf, no_color):
# Determine if colors should be used
use_color = not no_color

# Read the config into lists
new_conf = make_config(conf)
old_conf = make_config(old_conf)

# Remove common config
for item in new_conf[:]: # Copy to modify in-place
if item in old_conf:
new_conf.remove(item)
old_conf.remove(item)

new_conf_json = to_json(new_conf)
old_conf_json = to_json(old_conf)

# Generate diff list
diff_list = []

# Process new configuration items
for key, val in new_conf_json.items():
j = {key: {'old': 'ABSENT', 'new': val}}
if key in old_conf_json:
j[key]['old'] = old_conf_json[key]
old_conf_json.pop(key)
diff_list.append(j)

# Process remaining old configuration items
for key, val in old_conf_json.items():
j = {key: {'old': val, 'new': 'ABSENT'}}
diff_list.append(j)

# Display formatted output
header = f"{BOLD}{'CONFIG':<60}{'Old Value':<60}{'New Value'}{RESET}" if use_color else f"{'CONFIG':<60}{'Old Value':<60}{'New Value'}"
print(header)
print(f"{'='*140}")

for item in diff_list:
key = list(item.keys())[0]
old_val = item[key]['old']
new_val = item[key]['new']

# Set color based on value status
if use_color:
if old_val == 'ABSENT':
color = GREEN # New addition
elif new_val == 'ABSENT':
color = RED # Removed
else:
color = YELLOW # Modified
reset_color = RESET
else:
color = ""
reset_color = ""

print(f"{color}{key:<60}{old_val:<60}{new_val}{reset_color}")

if __name__ == '__main__':
main()
76 changes: 0 additions & 76 deletions sdkconfig-differ.py

This file was deleted.

26 changes: 26 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# setup.py

from setuptools import setup, find_packages

setup(
name='esp_config_diff',
version='0.1.0',
packages=find_packages(),
install_requires=[
'Click',
],
entry_points={
'console_scripts': [
'esp-config-diff=diff.cli:main',
],
},
description='A tool for comparing esp idf sdkconfigs',
author='shubhamdp',
url='https://github.com/shubhamdp/esp-config-diff',
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
python_requires='>=3.6',
)

0 comments on commit c4fc616

Please sign in to comment.