summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--combox/file.py33
-rw-r--r--combox/tests/test_file.py19
2 files changed, 51 insertions, 1 deletions
diff --git a/combox/file.py b/combox/file.py
index ab76315..ec0372e 100644
--- a/combox/file.py
+++ b/combox/file.py
@@ -18,6 +18,7 @@
from os import path
from sys import exit
+from glob import glob
def split_data(data, n):
@@ -92,3 +93,35 @@ def write_file(filename, filecontent):
except IOError:
print "ERROR: creating and writing content to %s" % (filename)
exit(1)
+
+def write_shards(shards, directory, shard_basename):
+ """Write shards to respective files respective files.
+
+ shard: list of strings (ciphers or data).
+ directory: absolute path of directory to which it shards must be written to.
+ shard_basename: base name of the shard.
+ """
+
+ # partial filename of the shard
+ p_filename = path.join(directory, shard_basename)
+ shard_no = 0
+ for shard in shards:
+ shard_name = "%s.shard%s" % (p_filename, shard_no)
+ write_file(shard_name, shard)
+ shard_no += 1
+
+def read_shards(directory, shard_basename):
+ """Read the shards from directory and return it as a list.
+
+ directory: absolute path of directory from which to read the shards.
+ shard_basename: base name of the shard.
+ """
+
+ shards = []
+ # partial filename of the shard
+ p_filename = "%s.shard*" % path.join(directory, shard_basename)
+ for shard_file in sorted(glob(p_filename)):
+ shard_content = read_file(shard_file)
+ shards.append(shard_content)
+
+ return shards
diff --git a/combox/tests/test_file.py b/combox/tests/test_file.py
index bfd1d9c..590e36f 100644
--- a/combox/tests/test_file.py
+++ b/combox/tests/test_file.py
@@ -18,7 +18,8 @@
from os import path
-from file import split_data, glue_data, write_file, read_file
+from file import (split_data, glue_data, write_file,
+ read_file, write_shards, read_shards)
### Test to split, glue and create a copy of the image file from the
### glued image file.
@@ -28,3 +29,19 @@ f_parts = split_data(f_content, 5)
f_content = glue_data(f_parts)
f_copy = path.abspath('tests/files/the-red-star-copy.jpg')
write_file(f_copy, f_content)
+
+
+## read file and split it into N shards and write the shards to disk.
+SHARDS = 5
+f = path.abspath('tests/files/the-red-star.jpg')
+f_content = read_file(f)
+f_shards = split_data(f_content, SHARDS)
+f_path = path.dirname(f)
+f_basename = path.basename(f)
+write_shards(f_shards, f_path, f_basename)
+
+## read file shards, glue them together and write to disk.
+f_shards = read_shards(f_path, f_basename)
+f_content = glue_data(f_shards)
+f_copy = path.abspath('tests/files/the-red-star-glued.jpg')
+write_file(f_copy, f_content)