Last active
August 29, 2015 14:22
-
-
Save jandrusk/6c1085d007e77c84cd5b to your computer and use it in GitHub Desktop.
compute_hashes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Name: compute_hashes.py | |
# Author: Justin Andrusk | |
# Date: 08/23/12 | |
# Purpose: Walk a directory tree recursivly in order to computer SHA512 checksums. | |
import sys, os | |
global filectr | |
filectr = 0 | |
flist = [] | |
def mylister(currdir): | |
global filectr | |
import subprocess, time | |
Files = [] | |
try: | |
localtime = time.localtime(time.time()) | |
stamp = str(localtime.tm_year) + "_" + str(localtime.tm_mon) + "_" + str(localtime.tm_mday) + "_" + str(localtime.tm_hour) + "_" + "_" + str(localtime.tm_min) + "_" + str(localtime.tm_sec) | |
for (root, dirs, files) in os.walk(currdir): #List files from here. | |
#Process dirs first then files | |
localtime = time.localtime(time.time()) | |
stamp = str(localtime.tm_year) + "_" + str(localtime.tm_mon) + "_" + str(localtime.tm_mday) + "_" + str(localtime.tm_hour) + "_" + "_" + str(localtime.tm_min) + "_" + str(localtime.tm_sec) | |
flist.extend(os.path.join(root, f) for f in files) | |
for fpath in flist: | |
if os.path.isfile(fpath): | |
# print "%s is a file" % (fpath) | |
Files.append(fpath) | |
except WindowsError: | |
pass | |
return Files | |
if __name__ == '__main__': | |
from libhash import GetSHA512 | |
from time import gmtime, strftime | |
import optparse | |
files = [] | |
parser = optparse.OptionParser("usage: compute_hashes.py "+\ | |
"-d <Root Directory> -o <output file>") | |
parser.add_option('-d', dest='line', type='string',\ | |
help='specify root directory') | |
parser.add_option('-o', dest='hashlog', type='string',\ | |
help='specify output file') | |
(options, args) = parser.parse_args() | |
if (options.line == None): | |
print parser.usage | |
exit(0) | |
hashlog = options.hashlog | |
line = options.line | |
files = mylister(line) # Call function | |
files = list(set(files)) | |
files = sorted(files) | |
for item in files: | |
filectr = filectr + 1 | |
tstamp = strftime("%Y-%m-%d %H:%M:%S") | |
print "[%d] Processing %s" % (filectr, item) | |
fhash = GetSHA512(item) | |
# print tstamp, item, fhash | |
log_file = open(hashlog, 'a') | |
log_file.write("%s, %s, %s\n" % (tstamp, item, fhash)) | |
log_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment