Skip to content

Pep8 ification #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3da6242
Pep8-ify and pylint-ify update-db.py
christian-intra2net Jan 10, 2019
c38a7c6
pep8-ify, pylint-ify compare-db.py: reindent
christian-intra2net Jan 10, 2019
6f4e1c2
pep8-ify, pylint-ify compare-db.py: break lines, fix spaces
christian-intra2net Jan 10, 2019
f699898
pep8-ify, pylint-ify compare-db.py: use print() function
christian-intra2net Jan 10, 2019
deedb9e
pep8-ify, pylint-ify compare-db.py: create main() function
christian-intra2net Jan 10, 2019
603e16a
pep8-ify, pylint-ify compare-db.py: add doc strings
christian-intra2net Jan 10, 2019
8951ed3
pep8-ify, pylint-ify compare-db.py: replace single-letter vars
christian-intra2net Jan 10, 2019
4da2010
pep8-ify, pylint-ify compare-db.py: remove unused
christian-intra2net Jan 10, 2019
cb983d2
pep8-ify, pylint-ify compare-db.py: Miscellaneous
christian-intra2net Jan 11, 2019
0b00157
pep8-ify, pylint-ify fast-regression-test: re-indent
christian-intra2net Jan 11, 2019
b024d35
pep8-ify, pylint-ify fast-regression-test: Wrap long lines
christian-intra2net Jan 11, 2019
bad6b91
pep8-ify, pylint-ify fast-regression-test: Spaces
christian-intra2net Jan 11, 2019
d8b930c
pep8-ify, pylint-ify fast-regression-test: main function
christian-intra2net Jan 11, 2019
d0934e1
pep8-ify, pylint-ify fast-regression-test: Rename single-char var
christian-intra2net Jan 11, 2019
1421493
pep8-ify, pylint-ify fast-regression-test: print function
christian-intra2net Jan 11, 2019
13ad9e6
pep8-ify, pylint-ify fast-regression-test: Fix imports
christian-intra2net Jan 11, 2019
aab4bc0
pep8-ify, pylint-ify fast-regression-test: Create doc
christian-intra2net Jan 11, 2019
5bffaa2
pep8-ify, pylint-ify db.py: Re-indent
christian-intra2net Jan 10, 2019
6a842d7
pep8-ify, pylint-ify db.py: wrap long lines
christian-intra2net Jan 11, 2019
45c7a84
pep8-ify, pylint-ify db.py: whitespace
christian-intra2net Jan 11, 2019
9618284
pep8-ify, pylint-ify db.py: use with open()
christian-intra2net Jan 14, 2019
61c7d16
pep8-ify, pylint-ify db.py: replace single-letter var
christian-intra2net Jan 14, 2019
117426f
pep8-ify, pylint-ify db.py: Fix error from re-indent
christian-intra2net Jan 14, 2019
b592f3b
pep8-ify, pylint-ify db.py: doc strings
christian-intra2net Jan 14, 2019
b2eadc0
pep8-ify, pylint-ify db.py: add todos
christian-intra2net Jan 14, 2019
913dc9f
pep8-ify, pylint-ify db.py: replace deprecated has_key
christian-intra2net Jan 14, 2019
c3a86a9
pep8-ify, pylint-ify db.py: remove unused imports
christian-intra2net Jan 14, 2019
b14defc
pep8-ify, pylint-ify db.py: miscellaneous
christian-intra2net Jan 14, 2019
2bb9c65
pep8-ify, pylint-ify db.py: invalid var names
christian-intra2net Jan 14, 2019
ce3965c
pep8-ify, pylint-ify file.py: Re-indent
christian-intra2net Jan 10, 2019
4ff8f41
pep8-ify, pylint-ify file.py: wrap lines
christian-intra2net Jan 14, 2019
a10d724
pep8-ify, pylint-ify file.py: whitespace
christian-intra2net Jan 14, 2019
5ea1246
pep8-ify, pylint-ify file.py: import order
christian-intra2net Jan 14, 2019
652c1e9
pep8-ify, pylint-ify file.py: unused variables
christian-intra2net Jan 14, 2019
feebd42
pep8-ify, pylint-ify file.py: print function
christian-intra2net Jan 14, 2019
4afbf46
pep8-ify, pylint-ify file.py: short variable names
christian-intra2net Jan 14, 2019
eb4520b
pep8-ify, pylint-ify file.py: use with open(...)
christian-intra2net Jan 14, 2019
159e7b7
pep8-ify, pylint-ify file.py: doc strings
christian-intra2net Jan 14, 2019
ec853ed
pep8-ify, pylint-ify file.py: todos
christian-intra2net Jan 14, 2019
291d249
pep8-ify, pylint-ify file.py: miscellaneous
christian-intra2net Jan 16, 2019
7b99417
Bring back old flush()
christian-intra2net Jan 16, 2019
a6bda95
Fix wrong argument name
christian-intra2net Jan 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 77 additions & 56 deletions compare-db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,95 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

"""Compare contents of db with output from other version of file(1)."""

from __future__ import print_function

import os
import sys
import mutex
from pyfile import *
from pyfile.progressbar import ProgressBar
from pyfile.threadpool import *
import mutex

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

split_patterns(magdir, file_name)
compile_patterns(file_name)
compiled = is_compilation_supported(file_name)
def compare_all_files(file_name='file', magdir='Magdir', exact=False):
"""
Compares all saved file(1) output in db with that of other file(1) version.

Creates a ThreadPool to do this in parallel. Uses a mutex lock to ensure
that text output is not garbled.
"""
n_threads = 4
pool = ThreadPool(n_threads)
print_lock = mutex.mutex()

split_patterns(magdir, file_name)
compile_patterns(file_name)
compiled = is_compilation_supported(file_name)

entries = get_stored_files("db")

def store_mimedata(entry):
"""For a single db entry, calls file(1) and compares it to db data."""
metadata = get_full_metadata(entry, file_name, compiled)
stored_metadata = get_stored_metadata(entry)
text = "PASS " + entry
if is_regression(stored_metadata, metadata, exact):
text = "FAIL " + entry + "\n" + \
get_diff(stored_metadata, metadata, exact)
return text

def data_print(data):
"""Print result for single entry and unlock print lock."""
print(data)
print_lock.unlock()

def data_stored(data):
"""Call data_print as soon as print lock has been acquired."""
print_lock.lock(data_print, data)

for entry in entries:
# Insert tasks into the queue and let them run
pool.queueTask(store_mimedata, args=(entry, ),
callback=data_stored)

# When all tasks are finished, allow the threads to terminate
pool.joinAll()
print('')


entries = get_stored_files("db")
def main():
"""Parse arguments, call :py:func:`compare_all_files`."""
file_name = 'file'
magdir = "Magdir"
exact = False

def store_mimedata(data):
metadata = get_full_metadata(data[0], file_name, compiled)
stored_metadata = get_stored_metadata(data[0])
text = "PASS " + data[0]
if is_regression(stored_metadata, metadata, exact):
text = "FAIL " + data[0] + "\n" + get_diff(stored_metadata, metadata, exact)
return text
if len(sys.argv) >= 3:
file_name = sys.argv[1]
magdir = sys.argv[2]
elif (len(sys.argv) == 2 and sys.argv[1] == "-h") or len(sys.argv) == 1:
print("Compares files in database with output of current file binary.")
print(sys.argv[0] + " [path_to_magdir_directory] [file_name]")
print(" Default path_to_magdir_directory='Magdir'")
print(" Default file_name='file'")
print("Examples:")
print(" " + sys.argv[0] + " file-5.07;")
print(" " + sys.argv[0] + " file-5.07 file-5.04/magic/Magdir;")
sys.exit(0)

def data_print(data):
print data
m.unlock()
if magdir == "exact":
exact = True
magdir = "Magdir"

def data_stored(data):
m.lock(data_print, data)
if len(sys.argv) == 4 and sys.argv[3] == "exact":
exact = True

for i,entry in enumerate(entries):
# Insert tasks into the queue and let them run
pool.queueTask(store_mimedata, (entry, i % 2), data_stored)
file_name = sys.argv[1]
compare_all_files(file_name, magdir, exact)

# When all tasks are finished, allow the threads to terminate
pool.joinAll()
print ''

# run this only if started as script from command line
if __name__ == '__main__':
file_name = 'file'
magdir = "Magdir"
exact = False

if len(sys.argv) >= 3:
file_name = sys.argv[1]
magdir = sys.argv[2]
elif (len(sys.argv) == 2 and sys.argv[1] == "-h") or len(sys.argv) == 1:
print "Compares files in database with output of current file binary."
print sys.argv[0] + " [path_to_magdir_directory] [file_name]"
print " Default path_to_magdir_directory='Magdir'"
print " Default file_name='file'"
print "Examples:"
print " " + sys.argv[0] + " file-5.07;"
print " " + sys.argv[0] + " file-5.07 file-5.04/magic/Magdir;"
sys.exit(0)

if magdir == "exact":
exact = True
magdir = "Magdir"

if len(sys.argv) == 4 and sys.argv[3] == "exact":
exact = True

file_name = sys.argv[1]
compare_all_files(file_name, magdir, exact)
main()
151 changes: 87 additions & 64 deletions fast-regression-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,87 +14,110 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

"""Do a quick comparison of output of file(1) with that saved in db."""

from __future__ import print_function

import os
import sys
import getopt
import mutex
from pyfile import *
from pyfile.threadpool import *
import mutex

ret = 0

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

global ret
ret = 0
#: return value from test_all_files
#: TODO: make this a nonlocal in py3
ret = 0

print_file_info(binary)

m = mutex.mutex()
def test_all_files(exact=False, binary="file"):
"""Compare output of given file(1) binary with db for all entries."""
global ret
ret = 0

print_file_info(binary)

print_lock = mutex.mutex()

entries = sorted(get_stored_files("db"))

def store_mimedata(filename):
"""Compare file(1) output with db for single entry."""
metadata = get_simple_metadata(filename, binary)
try:
stored_metadata = get_stored_metadata(filename)
except IOError:
# file not found or corrupt
text = "FAIL " + filename + "\n" + \
"FAIL could not find stored metadata!\n" + \
"This can mean that the File failed to generate " + \
"any output for this file."
else:
text = "PASS " + filename
if is_regression(stored_metadata, metadata, exact):
text = "FAIL " + filename + "\n" + \
get_diff(stored_metadata, metadata, exact)
return text

def data_print(data):
"""Print given text, set global return value, unlock print lock."""
print(data)
if data[0] == "F":
global ret
ret = 1
print_lock.unlock()

def data_stored(data):
"""Acquire print lock and call :py:function:`data_print`."""
print_lock.lock(data_print, data)

# create here so program exits if error occurs earlier
n_threads = 1
pool = ThreadPool(n_threads)

for entry in entries:
# Insert tasks into the queue and let them run
pool.queueTask(store_mimedata, entry, data_stored)

# When all tasks are finished, allow the threads to terminate
pool.joinAll()
print('')
return ret

entries = sorted(get_stored_files("db"))

def store_mimedata(filename):
metadata = get_simple_metadata(filename, binary)
try:
stored_metadata = get_stored_metadata(filename)
except IOError:
# file not found or corrupt
text = "FAIL " + filename + "\n" + "FAIL could not find stored metadata!\n\
This can mean that the File failed to generate any output for this file."
else:
text = "PASS " + filename
if is_regression(stored_metadata, metadata, exact):
text = "FAIL " + filename + "\n" + get_diff(stored_metadata, metadata, exact)
return text
def usage(ecode):
"""Print info on how to use this program. Return given code."""
print("Runs regressions.")
print(sys.argv[0] + " [-e] [-b <file-binary>]")
print(" Default file_binary='file'")
print("Examples:")
print(" " + sys.argv[0] + " -e -b '../file -m ../../magic/magic.mgc'")
print(" " + sys.argv[0] + " -e")
sys.exit(ecode)

def data_print(data):
print data
if data[0] == "F":
global ret
ret = 1
m.unlock()

def data_stored(data):
m.lock(data_print, data)
def main():
"""Called when running this as script. Parse args, call test_all_files."""
exact = False
file_binary = "file"
args = sys.argv[1:]

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

for entry in entries:
# Insert tasks into the queue and let them run
pool.queueTask(store_mimedata, entry, data_stored)
for option, arg in optlist:
if option == '-b':
file_binary = arg
elif option == '-e':
exact = True
else:
usage(1)

# When all tasks are finished, allow the threads to terminate
pool.joinAll()
print ''
return ret
sys.exit(test_all_files(exact, file_binary))

def usage(ecode):
print "Runs regressions."
print sys.argv[0] + " [-e] [-b <file-binary>]"
print " Default file_binary='file'"
print "Examples:"
print " " + sys.argv[0] + " -e -b '../file -m ../../magic/magic.mgc'"
print " " + sys.argv[0] + " -e"
sys.exit(ecode)

# run this only if started as script from command line
if __name__ == '__main__':
exact = False
file_binary = "file"
args = sys.argv[1:]

optlist, args = getopt.getopt(args, 'b:e')

for o, a in optlist:
if o == '-b':
file_binary = a
elif o == '-e':
exact = True
else:
usage(1)

sys.exit(test_all_files(exact, file_binary))
main()
Loading