Skip to content

Commit

Permalink
Address comments on the PR NVIDIA#904
Browse files Browse the repository at this point in the history
  • Loading branch information
jmancewicz committed Jul 13, 2016
1 parent f55a2fb commit 01e5086
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
1 change: 0 additions & 1 deletion digits/templates/models/images/generic/show.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. #}

{% extends "job.html" %}
Expand Down
29 changes: 0 additions & 29 deletions digits/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import platform
from random import uniform
from urlparse import urlparse
import re
import hashlib

if not platform.system() == 'Windows':
import fcntl
Expand Down Expand Up @@ -154,33 +152,6 @@ def parse_version(*args):
except AttributeError:
return pkg_resources.parse_version(v)

def dir_hash(dir_name):

def file_hash(file_path):
md5 = hashlib.md5()
blocksize = 64 * 1024
with open(file_path, 'rb') as fp:
while True:
data = fp.read(blocksize)
if not data:
break
md5.update(data)
return md5.hexdigest()

def reduce_hash(hashlist):
md5 = hashlib.md5()
for hashvalue in sorted(hashlist):
md5.update(hashvalue.encode('utf-8'))
return md5.hexdigest()

if not os.path.isdir(dir_name):
raise TypeError('{} is not a directory.'.format(dir_name))
hashvalues = []
for root, dirs, files in os.walk(dir_name, topdown=True):
if not re.search(r'/\.', root):
hashvalues.extend([file_hash(os.path.join(root, f)) for f in files if not
f.startswith('.') and not re.search(r'/\.', f)])
return reduce_hash(hashvalues)

### Import the other utility functions

Expand Down
28 changes: 27 additions & 1 deletion digits/utils/filesystem.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
from __future__ import absolute_import

import hashlib
import os.path
import shutil
import platform
import re
import shutil

def get_tree_size(start_path):
"""
Expand Down Expand Up @@ -57,3 +59,27 @@ def tail(file, n=40):
tailing_lines.popleft()
output = ''.join(tailing_lines)
return output

def dir_hash(dir_name):
"""
Return a hash for the files in a directory tree, excluding hidden
files and directoies. If any files are renamed, added, removed, or
modified the hash will change.
"""
if not os.path.isdir(dir_name):
raise TypeError('{} is not a directory.'.format(dir_name))

md5 = hashlib.md5()
for root, dirs, files in os.walk(dir_name, topdown=True):
# Skip if the root has a hidden directory in its path
if not re.search(r'/\.', root):
for f in files:
# Skip if the file is hidden
if not f.startswith('.') and not re.search(r'/\.', f):
# Change the hash if the file name changes
file_name = os.path.join(root, f)
md5.update(hashlib.md5(file_name).hexdigest())
# Change the hash if the file content changes
data = open(file_name, 'rb').read()
md5.update(hashlib.md5(data).hexdigest())
return md5.hexdigest()

0 comments on commit 01e5086

Please sign in to comment.