diff options
-rw-r--r-- | combox/file.py | 28 | ||||
-rw-r--r-- | tests/file_test.py | 29 |
2 files changed, 57 insertions, 0 deletions
diff --git a/combox/file.py b/combox/file.py index d42d099..2f18823 100644 --- a/combox/file.py +++ b/combox/file.py @@ -93,6 +93,34 @@ def node_path(cb_path, config, isfile): return file_node_path +def node_paths(cb_path, config, isfile): + """Returns list of abs. path of file (in node dir.) the cb_path. + + If cb_path is a file, it returns a list of abs. path names of + shards of the file in the node directories. + + isfile: True if cb_path is a file + """ + + n_paths = [] + nodes = get_nodedirs(config) + rel_path = relative_path(cb_path, config) + + if isfile: + shard_no = 0 + for node in nodes: + file_shard = '%s.shard%d' % (rel_path, shard_no) + n_path = path.join(node, file_shard) + n_paths.append(n_path) + shard_no += 1 + else: + for node in nodes: + n_path = path.join(node, rel_path) + n_paths.append(n_path) + + return n_paths + + def mk_nodedir(directory, config): """ Creates directory `directory' inside the nodes. diff --git a/tests/file_test.py b/tests/file_test.py index 6517943..bd81fd7 100644 --- a/tests/file_test.py +++ b/tests/file_test.py @@ -157,6 +157,35 @@ class TestFile(object): isfile=False) + def test_nodepaths(self): + """Tests the node_paths function.""" + nodes = get_nodedirs(self.config) + node_iter = iter(nodes) + + # test for file shards + foo = path.join(self.config['combox_dir'], 'foo.txt') + foo_rel_path = relative_path(foo, self.config) + foo_shards = node_paths(foo, self.config, True) + + shard_no = 0 + for foo_shard in foo_shards: + file_shard = '%s.shard%d' % (foo_rel_path, shard_no) + n_path = path.join(node_iter.next(), file_shard) + assert_equal(n_path, foo_shard) + shard_no += 1 + + # test for directory inside node_directories + bar = path.join(self.config['combox_dir'], 'bar') + bar_rel_path = relative_path(bar, self.config) + bar_n_paths = node_paths(bar, self.config, False) + + + node_iter = iter(nodes) + for bar_n_path in bar_n_paths: + b_n_path = path.join(node_iter.next(), bar_rel_path) + assert_equal(b_n_path, bar_n_path) + + def test_rmpath(self): """Tests rm_path function""" new_dir = path.join(self.config['combox_dir'], 'fooius') |