|
13 | 13 | #
|
14 | 14 | # You should have received a copy of the GNU General Public License
|
15 | 15 | # along with this program; if not, write to the Free Software
|
16 |
| -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 |
| -# |
| 16 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 17 | +# USA. |
| 18 | + |
| 19 | +"""Compare contents of db with output from other version of file(1).""" |
| 20 | + |
| 21 | +from __future__ import print_function |
18 | 22 |
|
19 |
| -import os |
20 | 23 | import sys
|
| 24 | +import mutex |
21 | 25 | from pyfile import *
|
22 |
| -from pyfile.progressbar import ProgressBar |
23 | 26 | from pyfile.threadpool import *
|
24 |
| -import mutex |
25 | 27 |
|
26 |
| -def compare_all_files(file_name = 'file', magdir = 'Magdir', exact = False): |
27 |
| - pool = ThreadPool(4) |
28 |
| - m = mutex.mutex() |
29 | 28 |
|
30 |
| - split_patterns(magdir, file_name) |
31 |
| - compile_patterns(file_name) |
32 |
| - compiled = is_compilation_supported(file_name) |
| 29 | +def compare_all_files(file_name='file', magdir='Magdir', exact=False): |
| 30 | + """ |
| 31 | + Compares all saved file(1) output in db with that of other file(1) version. |
| 32 | +
|
| 33 | + Creates a ThreadPool to do this in parallel. Uses a mutex lock to ensure |
| 34 | + that text output is not garbled. |
| 35 | + """ |
| 36 | + n_threads = 4 |
| 37 | + pool = ThreadPool(n_threads) |
| 38 | + print_lock = mutex.mutex() |
| 39 | + |
| 40 | + split_patterns(magdir, file_name) |
| 41 | + compile_patterns(file_name) |
| 42 | + compiled = is_compilation_supported(file_name) |
| 43 | + |
| 44 | + entries = get_stored_files("db") |
| 45 | + |
| 46 | + def store_mimedata(entry): |
| 47 | + """For a single db entry, calls file(1) and compares it to db data.""" |
| 48 | + metadata = get_full_metadata(entry, file_name, compiled) |
| 49 | + stored_metadata = get_stored_metadata(entry) |
| 50 | + text = "PASS " + entry |
| 51 | + if is_regression(stored_metadata, metadata, exact): |
| 52 | + text = "FAIL " + entry + "\n" + \ |
| 53 | + get_diff(stored_metadata, metadata, exact) |
| 54 | + return text |
| 55 | + |
| 56 | + def data_print(data): |
| 57 | + """Print result for single entry and unlock print lock.""" |
| 58 | + print(data) |
| 59 | + print_lock.unlock() |
| 60 | + |
| 61 | + def data_stored(data): |
| 62 | + """Call data_print as soon as print lock has been acquired.""" |
| 63 | + print_lock.lock(data_print, data) |
| 64 | + |
| 65 | + for entry in entries: |
| 66 | + # Insert tasks into the queue and let them run |
| 67 | + pool.queueTask(store_mimedata, args=(entry, ), |
| 68 | + callback=data_stored) |
| 69 | + |
| 70 | + # When all tasks are finished, allow the threads to terminate |
| 71 | + pool.joinAll() |
| 72 | + print('') |
| 73 | + |
33 | 74 |
|
34 |
| - entries = get_stored_files("db") |
| 75 | +def main(): |
| 76 | + """Parse arguments, call :py:func:`compare_all_files`.""" |
| 77 | + file_name = 'file' |
| 78 | + magdir = "Magdir" |
| 79 | + exact = False |
35 | 80 |
|
36 |
| - def store_mimedata(data): |
37 |
| - metadata = get_full_metadata(data[0], file_name, compiled) |
38 |
| - stored_metadata = get_stored_metadata(data[0]) |
39 |
| - text = "PASS " + data[0] |
40 |
| - if is_regression(stored_metadata, metadata, exact): |
41 |
| - text = "FAIL " + data[0] + "\n" + get_diff(stored_metadata, metadata, exact) |
42 |
| - return text |
| 81 | + if len(sys.argv) >= 3: |
| 82 | + file_name = sys.argv[1] |
| 83 | + magdir = sys.argv[2] |
| 84 | + elif (len(sys.argv) == 2 and sys.argv[1] == "-h") or len(sys.argv) == 1: |
| 85 | + print("Compares files in database with output of current file binary.") |
| 86 | + print(sys.argv[0] + " [path_to_magdir_directory] [file_name]") |
| 87 | + print(" Default path_to_magdir_directory='Magdir'") |
| 88 | + print(" Default file_name='file'") |
| 89 | + print("Examples:") |
| 90 | + print(" " + sys.argv[0] + " file-5.07;") |
| 91 | + print(" " + sys.argv[0] + " file-5.07 file-5.04/magic/Magdir;") |
| 92 | + sys.exit(0) |
43 | 93 |
|
44 |
| - def data_print(data): |
45 |
| - print data |
46 |
| - m.unlock() |
| 94 | + if magdir == "exact": |
| 95 | + exact = True |
| 96 | + magdir = "Magdir" |
47 | 97 |
|
48 |
| - def data_stored(data): |
49 |
| - m.lock(data_print, data) |
| 98 | + if len(sys.argv) == 4 and sys.argv[3] == "exact": |
| 99 | + exact = True |
50 | 100 |
|
51 |
| - for i,entry in enumerate(entries): |
52 |
| - # Insert tasks into the queue and let them run |
53 |
| - pool.queueTask(store_mimedata, (entry, i % 2), data_stored) |
| 101 | + file_name = sys.argv[1] |
| 102 | + compare_all_files(file_name, magdir, exact) |
54 | 103 |
|
55 |
| - # When all tasks are finished, allow the threads to terminate |
56 |
| - pool.joinAll() |
57 |
| - print '' |
58 | 104 |
|
59 | 105 | # run this only if started as script from command line
|
60 | 106 | if __name__ == '__main__':
|
61 |
| - file_name = 'file' |
62 |
| - magdir = "Magdir" |
63 |
| - exact = False |
64 |
| - |
65 |
| - if len(sys.argv) >= 3: |
66 |
| - file_name = sys.argv[1] |
67 |
| - magdir = sys.argv[2] |
68 |
| - elif (len(sys.argv) == 2 and sys.argv[1] == "-h") or len(sys.argv) == 1: |
69 |
| - print "Compares files in database with output of current file binary." |
70 |
| - print sys.argv[0] + " [path_to_magdir_directory] [file_name]" |
71 |
| - print " Default path_to_magdir_directory='Magdir'" |
72 |
| - print " Default file_name='file'" |
73 |
| - print "Examples:" |
74 |
| - print " " + sys.argv[0] + " file-5.07;" |
75 |
| - print " " + sys.argv[0] + " file-5.07 file-5.04/magic/Magdir;" |
76 |
| - sys.exit(0) |
77 |
| - |
78 |
| - if magdir == "exact": |
79 |
| - exact = True |
80 |
| - magdir = "Magdir" |
81 |
| - |
82 |
| - if len(sys.argv) == 4 and sys.argv[3] == "exact": |
83 |
| - exact = True |
84 |
| - |
85 |
| - file_name = sys.argv[1] |
86 |
| - compare_all_files(file_name, magdir, exact) |
| 107 | + main() |
0 commit comments