In this post I want to share with you a simple but very useful python script, which I wrote to compare the Django test and desktop projects directories.
Problem
At work, I use Django to solve my internal problems. As a result, the software was written, with which most of the employees - those working through the browser. support, operators, technical service of our company (field of activity - telecom).
Quite often, it is necessary to make changes, but you cannot do it right away in a working draft, since 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 modified files are copied to the working draft.
')
The problem is that it is difficult to remember which files were changed or added, and writing them to a notebook each time, as I used to do, is inefficient.
Decision
It was decided to write a script that compares the directories of the test and working projects and displays on the console screen a list of modified or added files. For example, we have a test directory called
myproject , and the working project is in the
intranet directory. Run our script and see on the screen:
/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
Modified files - [*], new (which are not in the working project) - [-]. Very comfortably :)
Cmp.py script source code
- #! / usr / bin / env python
- # ------------------------------------------------- -----------
- # Compare directories 'myproject' and 'intranet' recursively
- # ------------------------------------------------- -----------
- import os, filecmp
- # path to the test project - we will start the crawl from this directory
- dir_src = '/ var / django_projects / myproject'
- # we will only compare files and directories from this list
- check_list = (
- '/ var / django_projects / myproject / apps /' ,
- '/ var / django_projects / myproject / templates /' ,
- '/ var / django_projects / myproject / scripts /' ,
- '/var/django_projects/myproject/site_media/main.css' ,
- )
- for root, dirs, files in os.walk (dir_src):
- for name in files:
- f_src = os.path.join (root, name)
- need_check = f_src.startswith (check_list)
- if need_check and not f_src.endswith ( '.pyc' ):
- f_dst = f_src.replace ( "myproject" , "intranet" )
- if not os.path.exists (f_dst):
- print "[-]" , f_src
- elif not filecmp.cmp (f_src, f_dst):
- print "[*]" , f_src
As can be seen from the code, the main work is done by the
os.walk method, and for checking for existence and comparing files,
os.path.exists and
filecmp.cmp are used respectively.
That's all, I hope someone will come in handy :)
UPD: Before writing another comment about version control systems, please pay attention to the blog title :) Nevertheless, thanks to all those who have already touched on this topic, I think it will be useful to complete the picture.