|
| 1 | +#! /usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import filecmp |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +cwd = os.getcwd() |
| 10 | +print(cwd) |
| 11 | +list_files = os.listdir(cwd) |
| 12 | + |
| 13 | +old_stdout = sys.stdout # save original stdout |
| 14 | + |
| 15 | +# --------------------------- User input |
| 16 | +# dir1 = os.path.abspath(os.path.realpath(os.path.join('C:','Users',...))) |
| 17 | +dir1 = "C:\\Users\\mat\\Documents\\00001" |
| 18 | +dir2 = "C:\\Users\\mat\\Documents\\00007" |
| 19 | + |
| 20 | +directories_to_ignore = ['.svn', '.git', '.idea', 'docs'] |
| 21 | +files_to_ignore = ['.pyc', '.log'] |
| 22 | +write_command_for_compare = True |
| 23 | + |
| 24 | +meld = "C:\\\\Program\ Files\ \(x86\)\\\\Meld\\\\Meld.exe " # tool to compare files |
| 25 | + |
| 26 | +star = "********************************************************************************\n" |
| 27 | +# --------------------------- End User input |
| 28 | + |
| 29 | +# --------------------------- Comparison, raw report |
| 30 | +comparison = filecmp.dircmp(dir1, dir2) |
| 31 | + |
| 32 | +# store differences in a file, in a python format |
| 33 | +f = open('report_differences.txt', 'w') |
| 34 | +sys.stdout = f # changing system output to the file |
| 35 | +comparison.report_full_closure() |
| 36 | +f.close() |
| 37 | + |
| 38 | +sys.stdout = old_stdout # back to original system output |
| 39 | + |
| 40 | +# read the differences to process them later |
| 41 | +with open('report_differences.txt', 'r') as f: |
| 42 | + rep = f.readlines() |
| 43 | +# --------------------------- End of comparison, raw report |
| 44 | +for i in rep: |
| 45 | + print(i) |
| 46 | +# --------------------------- Process differences |
| 47 | +summa = [] |
| 48 | +to_pass = False |
| 49 | +for ll in range(len(rep)): |
| 50 | + # get lines starting by "Only in..." |
| 51 | + if "Only in " in rep[ll]: |
| 52 | + # flag for ignoring directories |
| 53 | + for directo in directories_to_ignore: |
| 54 | + if directo in rep[ll]: |
| 55 | + to_pass = True |
| 56 | + break |
| 57 | + if to_pass: |
| 58 | + to_pass = False |
| 59 | + continue |
| 60 | + summa.append(rep[ll]) |
| 61 | + summa.append('\n') |
| 62 | + |
| 63 | + if "Differing files :" in rep[ll]: |
| 64 | + # get lines starting by "diff" and "Differing" |
| 65 | + ii = ll |
| 66 | + while "diff " not in rep[ii]: |
| 67 | + ii -= 1 |
| 68 | + # flag for ignoring directories |
| 69 | + for directo in directories_to_ignore: |
| 70 | + if directo in rep[ii]: |
| 71 | + to_pass = True |
| 72 | + break |
| 73 | + if to_pass: |
| 74 | + to_pass = False |
| 75 | + continue |
| 76 | + summa.append(rep[ii]) |
| 77 | + summa.append(rep[ll]) |
| 78 | + summa.append('\n') |
| 79 | + |
| 80 | +# process and write only interesting lines in the file |
| 81 | +cdirs = 0 |
| 82 | +cfiles = 0 |
| 83 | +with open('report_differences.txt', 'w') as f: |
| 84 | + f.write(str(time.ctime()) + "\n") # date time |
| 85 | + f.write(dir1 + "\n") |
| 86 | + f.write('vs.\n') |
| 87 | + f.write(dir2 + "\n\n\n") |
| 88 | + |
| 89 | + for i in range(len(summa)): |
| 90 | + if "Only" in summa[i]: |
| 91 | + _, a, b = summa[i].split(":") # a:adress, b:list of files |
| 92 | + f.write(star) |
| 93 | + f.write("Only in C:" + a.replace("\\", "\\\\") + ":\n") # a : "Only in ..." |
| 94 | + cdirs += 1 |
| 95 | + f.write(star) |
| 96 | + for l in b.split("'"): # b : "['file1,'file2'..]" |
| 97 | + if ("[" not in l) and ("]" not in l) and ("," not in l): |
| 98 | + # write only file names |
| 99 | + # flag for ignoring files |
| 100 | + for fti in files_to_ignore: |
| 101 | + if fti in l: |
| 102 | + to_pass = True |
| 103 | + break |
| 104 | + if to_pass: |
| 105 | + to_pass = False |
| 106 | + continue |
| 107 | + f.write(l + "\n") # str(l) |
| 108 | + cfiles += 1 |
| 109 | + f.write('\n') |
| 110 | + |
| 111 | + if "Differing files :" in summa[i]: |
| 112 | + # f.write(star) |
| 113 | + phra = summa[i - 1].split() # 'diff' 'path1' 'path2' |
| 114 | + a, b = summa[i].split(":") # a: adress, b:list of files |
| 115 | + # f.write(summa[i-1]) # diff... |
| 116 | + listfiles = "" |
| 117 | + for l in b.split("'"): |
| 118 | + if ("[" not in l) and ("]" not in l) and ("," not in l): |
| 119 | + # flag for files to ignore |
| 120 | + for fti in files_to_ignore: |
| 121 | + if fti in l: |
| 122 | + to_pass = True |
| 123 | + break |
| 124 | + if to_pass: |
| 125 | + to_pass = False |
| 126 | + continue |
| 127 | + listfiles += l + "\n" |
| 128 | + cfiles += 1 |
| 129 | + |
| 130 | + f.write(star + "Differing files between " + phra[1] + " and " |
| 131 | + + phra[2] + "\n" + star + listfiles[:-1] + "\n" + star) # [-2] to remove ", " |
| 132 | + cdirs += 1 |
| 133 | + |
| 134 | + if write_command_for_compare: |
| 135 | + for l in b.split("'"): |
| 136 | + if ("[" not in l) and ("]" not in l) and ("," not in l): |
| 137 | + # flag for files to ignore |
| 138 | + for fti in files_to_ignore: |
| 139 | + if fti in l: |
| 140 | + to_pass = True |
| 141 | + break |
| 142 | + if to_pass: |
| 143 | + to_pass = False |
| 144 | + continue |
| 145 | + toWrite = ("nohup " + meld + # all the \\\ for windobws |
| 146 | + phra[1].replace("\\", "\\\\") + "\\\\" + l + " " + |
| 147 | + phra[2].replace("\\", "\\\\") + "\\\\" + l + " &" + "\n") |
| 148 | + f.write(toWrite) |
| 149 | + f.write('\n') |
| 150 | + |
| 151 | + summary = 'Total of ' + str(cfiles) + ' differences in ' + str(cdirs) + ' directories.' |
| 152 | + f.write(summary) |
| 153 | + |
| 154 | +# --------------------------- End of Process differences |
| 155 | + |
| 156 | +# --------------------------- Process identical |
| 157 | +cdirs = 0 |
| 158 | +cfiles = 0 |
| 159 | +summa = [] |
| 160 | +to_pass = False |
| 161 | +for ll in range(len(rep)): |
| 162 | + # get lines starting by "Identical files :" |
| 163 | + if "Identical files :" in rep[ll]: |
| 164 | + # flag for ignoring directories |
| 165 | + ii = ll |
| 166 | + while "diff " not in rep[ii]: |
| 167 | + ii -= 1 |
| 168 | + # flag for ignoring directories |
| 169 | + for directo in directories_to_ignore: |
| 170 | + if directo in rep[ii]: |
| 171 | + to_pass = True |
| 172 | + break |
| 173 | + if to_pass: |
| 174 | + to_pass = False |
| 175 | + continue |
| 176 | + summa.append(rep[ii]) |
| 177 | + summa.append(rep[ll]) |
| 178 | + summa.append('\n') |
| 179 | + |
| 180 | +# process and write only interesting lines in the file |
| 181 | +with open('report_identical.txt', 'w') as f: |
| 182 | + f.write(str(time.ctime()) + "\n") # date time |
| 183 | + f.write(dir1 + "\n") |
| 184 | + f.write('vs.\n') |
| 185 | + f.write(dir2 + "\n\n\n") |
| 186 | + |
| 187 | + for i in range(len(summa)): |
| 188 | + if "Identical files :" in summa[i]: |
| 189 | + # f.write(star) |
| 190 | + phra = summa[i - 1].split() # 'diff' 'path1' 'path2' |
| 191 | + a, b = summa[i].split(":") # a: adress, b:list of files |
| 192 | + # f.write(summa[i-1]) # diff... |
| 193 | + listfiles = "" |
| 194 | + for l in b.split("'"): |
| 195 | + if ("[" not in l) and ("]" not in l) and ("," not in l): |
| 196 | + # flag for files to ignore |
| 197 | + for fti in files_to_ignore: |
| 198 | + if fti in l: |
| 199 | + to_pass = True |
| 200 | + break |
| 201 | + if to_pass: |
| 202 | + to_pass = False |
| 203 | + continue |
| 204 | + listfiles += l + "\n" |
| 205 | + cfiles += 1 |
| 206 | + |
| 207 | + f.write(star + "Identical files between " + phra[1] + " and " |
| 208 | + + phra[2] + "\n" + star + listfiles[:-1] + "\n\n") # [-2] to remove ", " |
| 209 | + cdirs += 1 |
| 210 | + summary = 'Total of ' + str(cfiles) + ' identical files in ' + str(cdirs) + ' directories.' |
| 211 | + f.write(summary) |
0 commit comments