Skip to content

Commit ca2841c

Browse files
Merge pull request #7 from christian-intra2net/pep8-ification
Pep8 ification
2 parents eb34047 + a6bda95 commit ca2841c

File tree

5 files changed

+773
-552
lines changed

5 files changed

+773
-552
lines changed

compare-db.py

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,95 @@
1313
#
1414
# You should have received a copy of the GNU General Public License
1515
# 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
1822

19-
import os
2023
import sys
24+
import mutex
2125
from pyfile import *
22-
from pyfile.progressbar import ProgressBar
2326
from pyfile.threadpool import *
24-
import mutex
2527

26-
def compare_all_files(file_name = 'file', magdir = 'Magdir', exact = False):
27-
pool = ThreadPool(4)
28-
m = mutex.mutex()
2928

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+
3374

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
3580

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)
4393

44-
def data_print(data):
45-
print data
46-
m.unlock()
94+
if magdir == "exact":
95+
exact = True
96+
magdir = "Magdir"
4797

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
50100

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)
54103

55-
# When all tasks are finished, allow the threads to terminate
56-
pool.joinAll()
57-
print ''
58104

59105
# run this only if started as script from command line
60106
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()

fast-regression-test.py

Lines changed: 87 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,87 +14,110 @@
1414
#
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program; if not, write to the Free Software
17-
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18-
#
17+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18+
# USA.
19+
20+
"""Do a quick comparison of output of file(1) with that saved in db."""
21+
22+
from __future__ import print_function
1923

20-
import os
2124
import sys
2225
import getopt
26+
import mutex
2327
from pyfile import *
2428
from pyfile.threadpool import *
25-
import mutex
26-
27-
ret = 0
2829

29-
def test_all_files(exact = False, binary = "file"):
3030

31-
global ret
32-
ret = 0
31+
#: return value from test_all_files
32+
#: TODO: make this a nonlocal in py3
33+
ret = 0
3334

34-
print_file_info(binary)
3535

36-
m = mutex.mutex()
36+
def test_all_files(exact=False, binary="file"):
37+
"""Compare output of given file(1) binary with db for all entries."""
38+
global ret
39+
ret = 0
40+
41+
print_file_info(binary)
42+
43+
print_lock = mutex.mutex()
44+
45+
entries = sorted(get_stored_files("db"))
46+
47+
def store_mimedata(filename):
48+
"""Compare file(1) output with db for single entry."""
49+
metadata = get_simple_metadata(filename, binary)
50+
try:
51+
stored_metadata = get_stored_metadata(filename)
52+
except IOError:
53+
# file not found or corrupt
54+
text = "FAIL " + filename + "\n" + \
55+
"FAIL could not find stored metadata!\n" + \
56+
"This can mean that the File failed to generate " + \
57+
"any output for this file."
58+
else:
59+
text = "PASS " + filename
60+
if is_regression(stored_metadata, metadata, exact):
61+
text = "FAIL " + filename + "\n" + \
62+
get_diff(stored_metadata, metadata, exact)
63+
return text
64+
65+
def data_print(data):
66+
"""Print given text, set global return value, unlock print lock."""
67+
print(data)
68+
if data[0] == "F":
69+
global ret
70+
ret = 1
71+
print_lock.unlock()
72+
73+
def data_stored(data):
74+
"""Acquire print lock and call :py:function:`data_print`."""
75+
print_lock.lock(data_print, data)
76+
77+
# create here so program exits if error occurs earlier
78+
n_threads = 1
79+
pool = ThreadPool(n_threads)
80+
81+
for entry in entries:
82+
# Insert tasks into the queue and let them run
83+
pool.queueTask(store_mimedata, entry, data_stored)
84+
85+
# When all tasks are finished, allow the threads to terminate
86+
pool.joinAll()
87+
print('')
88+
return ret
3789

38-
entries = sorted(get_stored_files("db"))
3990

40-
def store_mimedata(filename):
41-
metadata = get_simple_metadata(filename, binary)
42-
try:
43-
stored_metadata = get_stored_metadata(filename)
44-
except IOError:
45-
# file not found or corrupt
46-
text = "FAIL " + filename + "\n" + "FAIL could not find stored metadata!\n\
47-
This can mean that the File failed to generate any output for this file."
48-
else:
49-
text = "PASS " + filename
50-
if is_regression(stored_metadata, metadata, exact):
51-
text = "FAIL " + filename + "\n" + get_diff(stored_metadata, metadata, exact)
52-
return text
91+
def usage(ecode):
92+
"""Print info on how to use this program. Return given code."""
93+
print("Runs regressions.")
94+
print(sys.argv[0] + " [-e] [-b <file-binary>]")
95+
print(" Default file_binary='file'")
96+
print("Examples:")
97+
print(" " + sys.argv[0] + " -e -b '../file -m ../../magic/magic.mgc'")
98+
print(" " + sys.argv[0] + " -e")
99+
sys.exit(ecode)
53100

54-
def data_print(data):
55-
print data
56-
if data[0] == "F":
57-
global ret
58-
ret = 1
59-
m.unlock()
60101

61-
def data_stored(data):
62-
m.lock(data_print, data)
102+
def main():
103+
"""Called when running this as script. Parse args, call test_all_files."""
104+
exact = False
105+
file_binary = "file"
106+
args = sys.argv[1:]
63107

64-
pool = ThreadPool(4) # create here so program exits if error occurs earlier
108+
optlist, args = getopt.getopt(args, 'b:e')
65109

66-
for entry in entries:
67-
# Insert tasks into the queue and let them run
68-
pool.queueTask(store_mimedata, entry, data_stored)
110+
for option, arg in optlist:
111+
if option == '-b':
112+
file_binary = arg
113+
elif option == '-e':
114+
exact = True
115+
else:
116+
usage(1)
69117

70-
# When all tasks are finished, allow the threads to terminate
71-
pool.joinAll()
72-
print ''
73-
return ret
118+
sys.exit(test_all_files(exact, file_binary))
74119

75-
def usage(ecode):
76-
print "Runs regressions."
77-
print sys.argv[0] + " [-e] [-b <file-binary>]"
78-
print " Default file_binary='file'"
79-
print "Examples:"
80-
print " " + sys.argv[0] + " -e -b '../file -m ../../magic/magic.mgc'"
81-
print " " + sys.argv[0] + " -e"
82-
sys.exit(ecode)
83120

84121
# run this only if started as script from command line
85122
if __name__ == '__main__':
86-
exact = False
87-
file_binary = "file"
88-
args = sys.argv[1:]
89-
90-
optlist, args = getopt.getopt(args, 'b:e')
91-
92-
for o, a in optlist:
93-
if o == '-b':
94-
file_binary = a
95-
elif o == '-e':
96-
exact = True
97-
else:
98-
usage(1)
99-
100-
sys.exit(test_all_files(exact, file_binary))
123+
main()

0 commit comments

Comments
 (0)