Skip to content

Commit

Permalink
Replace time.clock with time.perf_counter
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarPersson committed Nov 4, 2019
1 parent 883d157 commit 104e2ab
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions fido/fido.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def identify_file(self, filename, extension=True):
self.current_file = filename
self.matchtype = "signature"
try:
t0 = time.clock()
t0 = time.perf_counter()
f = open(filename, 'rb')
size = os.stat(filename)[6]
self.current_filesize = size
Expand All @@ -357,7 +357,7 @@ def identify_file(self, filename, extension=True):
else:
container_matches = self.match_container("OLE2", OlePackage, filename, container_file)
if len(container_matches) > 0:
self.handle_matches(filename, container_matches, time.clock() - t0, "container")
self.handle_matches(filename, container_matches, time.perf_counter() - t0, "container")
return
# from here is also repeated in walk_zip
# we should make this uniform in a next version!
Expand All @@ -366,10 +366,10 @@ def identify_file(self, filename, extension=True):
# are falsely characterised being 'rtf' (due to wacky sig)
# in these cases we try to match the extension instead
if len(matches) > 0 and self.current_filesize > 0:
self.handle_matches(filename, matches, time.clock() - t0, self.matchtype)
self.handle_matches(filename, matches, time.perf_counter() - t0, self.matchtype)
elif extension and (len(matches) == 0 or self.current_filesize == 0):
matches = self.match_extensions(filename)
self.handle_matches(filename, matches, time.clock() - t0, "extension")
self.handle_matches(filename, matches, time.perf_counter() - t0, "extension")
# only recurse into certain containers, like ZIP or TAR
container = self.container_type(matches)
# till here matey!
Expand Down Expand Up @@ -404,7 +404,7 @@ def identify_multi_object_stream(self, stream, extension=True):
"""
offset = 0
while True:
t0 = time.clock()
t0 = time.perf_counter()
content_length = -1
for line in stream:
offset += len(line)
Expand All @@ -425,25 +425,25 @@ def identify_multi_object_stream(self, stream, extension=True):
matches = self.match_formats(bofbuffer, eofbuffer)
# MdR: this needs attention
if len(matches) > 0:
self.handle_matches(self.current_file, matches, time.clock() - t0, "signature")
self.handle_matches(self.current_file, matches, time.perf_counter() - t0, "signature")
elif extension and (len(matches) == 0 or self.current_filesize == 0):
matches = self.match_extensions(self.current_file)
self.handle_matches(self.current_file, matches, time.clock() - t0, "extension")
self.handle_matches(self.current_file, matches, time.perf_counter() - t0, "extension")

def identify_stream(self, stream, filename, extension=True):
"""
Identify the type of @param stream.
Call self.handle_matches instead of returning a value.
Does not close stream.
"""
t0 = time.clock()
t0 = time.perf_counter()
bofbuffer, eofbuffer, bytes_read = self.get_buffers(stream, length=None)
self.current_filesize = bytes_read
self.current_file = 'STDIN'
matches = self.match_formats(bofbuffer, eofbuffer)
# MdR: this needs attention
if len(matches) > 0:
self.handle_matches(self.current_file, matches, time.clock() - t0, "signature")
self.handle_matches(self.current_file, matches, time.perf_counter() - t0, "signature")
elif extension and (len(matches) == 0 or self.current_filesize == 0):
# we can only determine the filename from the STDIN stream
# on Linux, on Windows there is not a (simple) way to do that
Expand All @@ -462,7 +462,7 @@ def identify_stream(self, stream, filename, extension=True):
# we have to reset self.current_file if not on Windows
if (os.name != "nt"):
self.current_file = 'STDIN'
self.handle_matches(self.current_file, matches, time.clock() - t0, "extension")
self.handle_matches(self.current_file, matches, time.perf_counter() - t0, "extension")

def container_type(self, matches):
"""
Expand Down Expand Up @@ -566,7 +566,7 @@ def walk_zip(self, filename, fileobj=None, extension=True):
for item in zipstream.infolist():
if item.file_size == 0:
continue # TODO: Find a better test for isdir
t0 = time.clock()
t0 = time.perf_counter()
with zipstream.open(item) as f:
item_name = filename + '!' + item.filename
self.current_file = item_name
Expand All @@ -576,10 +576,10 @@ def walk_zip(self, filename, fileobj=None, extension=True):
bofbuffer, eofbuffer, _ = self.get_buffers(f, item.file_size)
matches = self.match_formats(bofbuffer, eofbuffer)
if len(matches) > 0 and self.current_filesize > 0:
self.handle_matches(item_name, matches, time.clock() - t0, "signature")
self.handle_matches(item_name, matches, time.perf_counter() - t0, "signature")
elif extension and (len(matches) == 0 or self.current_filesize == 0):
matches = self.match_extensions(item_name)
self.handle_matches(item_name, matches, time.clock() - t0, "extension")
self.handle_matches(item_name, matches, time.perf_counter() - t0, "extension")
if self.container_type(matches):
target = tempfile.SpooledTemporaryFile(prefix='Fido')
with zipstream.open(item) as source:
Expand All @@ -603,14 +603,14 @@ def walk_tar(self, filename, fileobj, extension=True):
for item in tarstream.getmembers():
if not item.isfile():
continue
t0 = time.clock()
t0 = time.perf_counter()
with closing(tarstream.extractfile(item)) as f:
tar_item_name = filename + '!' + item.name
self.current_file = tar_item_name
self.current_filesize = item.size
bofbuffer, eofbuffer, _ = self.get_buffers(f, item.size)
matches = self.match_formats(bofbuffer, eofbuffer)
self.handle_matches(tar_item_name, matches, time.clock() - t0)
self.handle_matches(tar_item_name, matches, time.perf_counter() - t0)
if self.container_type(matches):
f.seek(0)
self.identify_contents(tar_item_name, f, self.container_type(matches), extension=extension)
Expand Down Expand Up @@ -657,7 +657,7 @@ def match_formats(self, bofbuffer, eofbuffer):
The list has inferior matches removed.
"""
self.current_count += 1
# t0 = time.clock()
# t0 = time.perf_counter()
result = []
for format in self.formats:
try:
Expand Down Expand Up @@ -696,7 +696,7 @@ def match_formats(self, bofbuffer, eofbuffer):
# print "Unexpected error:", sys.exc_info()[0], e
# sys.stdout.write('***', self.get_puid(format), regex)

# t1 = time.clock()
# t1 = time.perf_counter()
# if t1 - t0 > 0.02:
# print >> sys.stderr, "FIDO: Slow ID", self.current_file
result = [match for match in result if self.as_good_as_any(match[0], result)]
Expand Down Expand Up @@ -775,7 +775,7 @@ def main(args=None):
sys.exit(1)
args = parser.parse_args(args)

t0 = time.clock()
t0 = time.perf_counter()

versions = get_local_pronom_versions(args.confdir)

Expand Down Expand Up @@ -858,7 +858,7 @@ def main(args=None):

if not args.q:
sys.stdout.flush()
fido.print_summary(time.clock() - t0)
fido.print_summary(time.perf_counter() - t0)
sys.stderr.flush()


Expand Down

0 comments on commit 104e2ab

Please sign in to comment.