Script for recursive directory comparison

    In this post I want to share with you a simple but very useful python script that I wrote to compare the directories of the test and working Django projects.


    Problem


    At work, I use Django to solve internal problems. As a result, software was written, with which most of the employees — those. support, operators, technical service of our company (field of activity is telecom).

    Quite often, you need to make changes, but you can’t do this right away in a working draft, because downtime due to possible errors is very undesirable. Therefore, a copy of the project was created, in which all changes are made first, and after testing, the changed files are copied to the working project.

    The problem is that it’s hard to remember which files have been changed or added, and it’s ineffective to write every time to notepad, as I did before.

    Decision


    It was decided to write a script that compares the directories of the test and working projects and displays a list of modified or added files on the console screen. For example, we have a test directory called myproject , and the working project is in the intranet directory . We launch our script and see on the screen: Modified files - [*], new (which are not in the working draft) - [-]. Very comfortably :)

    /var/django_projects/myproject$ ./cmp.py

    [*] /var/django_projects/myproject/templates/base.html
    [-] /var/django_projects/myproject/templates/calls/call_add.html
    [*] /var/django_projects/myproject/templates/calls/call_edit.html
    [*] /var/django_projects/myproject/site_media/main.css




    The source code of the cmp.py script

    1. #!/usr/bin/env python
    2. #------------------------------------------------------------
    3. # Compare directories 'myproject' and 'intranet' recursively
    4. #------------------------------------------------------------
    5.  
    6. import os, filecmp
    7.  
    8. # путь к тестовому проекту - с этой директории будем начинать обход
    9. dir_src = '/var/django_projects/myproject'
    10.  
    11. # будем сравнивать только файлы и директории из этого списка
    12. check_list = (
    13.   '/var/django_projects/myproject/apps/',
    14.   '/var/django_projects/myproject/templates/',
    15.   '/var/django_projects/myproject/scripts/',
    16.   '/var/django_projects/myproject/site_media/main.css',
    17. )
    18.  
    19. for root, dirs, files in os.walk(dir_src):
    20.   for name in files:
    21.     f_src = os.path.join(root, name)
    22.     need_check = f_src.startswith(check_list)
    23.     if need_check and not f_src.endswith('.pyc'):
    24.       f_dst = f_src.replace("myproject", "intranet")
    25.       if not os.path.exists(f_dst):
    26.         print "[-] ", f_src
    27.       elif not filecmp.cmp(f_src, f_dst):
    28.         print "[*] ", f_src


    As you can see from the code, the main work is done by the os.walk method , and os.path.exists and filecmp.cmp are used to check for existence and compare files, respectively .

    That's all, I hope someone comes in handy :)

    UPD: Before writing another comment about version control systems, please pay attention to the name of the blog :) Nevertheless, thanks to all those who have already touched on this topic, I think to complete the picture will be useful.

    Also popular now: