Permalink
Browse files

support reading data from stdin

filename is set to <sha256 of input data>.dat
  • Loading branch information...
1 parent e93d1b5 commit 63f1b3a4c0b755c3267f2194467db196168683e2 @fracai fracai committed Apr 27, 2011
Showing with 20 additions and 5 deletions.
  1. +20 −5 hash_blocks
View
@@ -37,25 +37,40 @@ if __name__ == "__main__":
except IndexError:
err.write("Usage: %s <file>\n" % sys.argv[0])
exit(1)
- f = open(filename, "rb")
+
+ if filename == '-':
+ f = sys.stdin
+ else:
+ f = open(filename, "rb")
barray = []
size = 0
+ hash = hashlib.sha256()
while True:
data = f.read(BLOCKSIZE)
if not data:
break
size += len(data)
+ hash.update(data)
barray.append(hashlib.sha256(data).hexdigest())
- stat = os.stat(filename)
-
outdata = {
"blocks":barray,
"size":size,
- "name":path.basename(filename),
- "mtime":stat.st_mtime,
}
+ if filename == '-':
+ import time;
+ outdata.update({
+ "name":hash.hexdigest()+".dat",
+ "mtime":time.mktime(time.localtime()),
+ })
+ else:
+ stat = os.stat(filename)
+ outdata.update({
+ "name":path.basename(filename),
+ "mtime":stat.st_mtime,
+ })
+
outdata = json.dumps(outdata) + '\n'
out.write(outdata)

0 comments on commit 63f1b3a

Please sign in to comment.