Permalink
Browse files

Merge pull request #3 from fracai/master

Support for reading from STDIN
  • Loading branch information...
2 parents 53be910 + 63f1b3a commit 508f16417babc10e4205ec675707f6b561990cac @driverdan committed Jun 23, 2011
Showing with 26 additions and 7 deletions.
  1. +4 −1 dropship
  2. +22 −6 hash_blocks
View
@@ -50,7 +50,10 @@ if __name__ == '__main__':
else:
path = None
- f = open(infile, 'r')
+ if infile == '-':
+ f = sys.stdin
+ else:
+ f = open(infile, 'r')
try:
indata = json.load(f)
except ValueError,e:
View
@@ -37,24 +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())
- size = f.tell()
- 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 508f164

Please sign in to comment.