From efb903303f8000d3262e02df405573585214cae7 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 5 Jun 2023 14:33:38 -0700 Subject: [PATCH] aider: Added `diff_files.py` script that reads two file names from the command line, reads the contents of the files, and then uses the `difflib` module to print their differences. # Context: USER: read 2 files named on the cmd line, use difflib to print their differences ASSISTANT: To achieve this, we'll create a new Python script that reads two file names from the command line, reads the contents of the files, and then uses the `difflib` module to print their differences. 1. Create a new Python script called `diff_files.py` in the `aider` directory. 2. Import the necessary modules: `sys` and `difflib`. 3. Read the file names from the command line arguments. 4. Read the contents of the files. 5. Use `difflib.unified_diff()` to generate the differences. 6. Print the differences. Here's the code for the new `diff_files.py` script: ```python aider/diff_files ^C KeyboardInterrupt USER: put it in `aider/diffs.py` --- aider/diffs.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/aider/diffs.py b/aider/diffs.py index e69de29bb..621d60c4c 100644 --- a/aider/diffs.py +++ b/aider/diffs.py @@ -0,0 +1,23 @@ +import sys +import difflib + +def main(): + if len(sys.argv) != 3: + print("Usage: python diffs.py file1 file2") + sys.exit(1) + + file1, file2 = sys.argv[1], sys.argv[2] + + with open(file1, 'r') as f1: + content1 = f1.readlines() + + with open(file2, 'r') as f2: + content2 = f2.readlines() + + differences = list(difflib.unified_diff(content1, content2, fromfile=file1, tofile=file2)) + + for line in differences: + sys.stdout.write(line) + +if __name__ == "__main__": + main()