summaryrefslogtreecommitdiffstats
path: root/combox/file.py
blob: 05c893e2789e311c260cf2983e4008a579e60b9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# -*- coding: utf-8 -*-
#
#    Copyright (C) 2016 Dr. Robert C. Green II.
#
#    This file is part of Combox.
#
#   Combox is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Combox is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Combox (see COPYING).  If not, see
#   <http://www.gnu.org/licenses/>.

import os

from hashlib import sha512
from os import path
from sys import exit
from glob import glob

from combox.config import get_nodedirs
from combox.log import log_e


def relative_path(p, config, comboxd=True):
    """Returns the relative path to the `p` w. r. t combox or node directory.

    :param str p:
        Path to a directory or file.
    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :param bool comboxd:
        True if `p` is a path relative to combox directory.
    :returns:
        Path relative to combox directory if `comboxd` is `True`,
        otherwise returns path relative to the node directory.
    :rtype: str
    :raises ValueError:
        If path `p` is not under the combox directory or the node
        directory.

    """
    directory = None
    if comboxd:
        directory = '%s/' % config['combox_dir']
    else:
        for node in get_nodedirs(config):
            if p.startswith(node):
                directory = '%s/' % node

    if directory is None:
        err_msg = "invalid path %s" % p
        raise ValueError, err_msg

    return p.partition(directory)[2]


def cb_path(node_path, config):
    """Returns absolute path of file/directory under the combox directory.

    :param str node_path:
        Path to a file/directory under a node directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :returns:
        Returns the corresponding path to the file/directory under the
        combox directory.
    :rtype: str

    """
    if node_path[:-1].endswith('shard'):
        # partition function is used to remove the `.shard.N' from the
        # file name.
        rel_file_path = relative_path(node_path,
                                      config,
                                      False).partition('.shard')[0]
        file_cb_path = path.join(config['combox_dir'],
                                 rel_file_path)
    else:
        file_cb_path = path.join(config['combox_dir'],
                                 relative_path(node_path, config, False))

    return file_cb_path


def node_path(cb_path, config, isfile):
    """Returns abs. path of file/directory in node directory.

    :param str cb_path:
        Path to a file/directory under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :param bool isfile:
        True if `cb_path` is not a directory.
    :returns:
        If `cb_path` is a file, it returns the path to its shard under
        the first node directory.

        If `cb_path` is a directory, it returns the corresponding path
        to this directory under the first node directory.
    :rtype: str

    """
    if isfile:
        # partition function is used to remove the `.shard.N' from the
        # file name.
        rel_file_path = relative_path(cb_path, config)
        file_node_path = path.join(get_nodedirs(config)[0],
                                 rel_file_path)
        file_node_path = "%s.shard0" % file_node_path
    else:
        file_node_path = path.join(get_nodedirs(config)[0],
                                   relative_path(cb_path, config))

    return file_node_path


def node_paths(cb_path, config, isfile):
    """Returns a list of abs. paths of a file/directory in node directories.

    :param str cb_path:
        Path to a file/directory under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :param bool isfile:
        True if `cb_path` is not a directory.
    :returns:
        If `cb_path` is a file, it returns a list of paths to its
        shards under the node directories.

        If `cb_path` is a directory, it returns a list its
        corresponding paths under the node directories.
    :rtype: list

    """

    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` under all the node directories.

    :param str directory:
        Path to a directory under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.

    """
    nodes = get_nodedirs(config)

    rel_path = relative_path(directory, config)

    for node in nodes:
        dir_path = path.join(node, rel_path)
        mk_dir(dir_path)


def mk_dir(directory):
    """Creates a directory.

    :param str directory:
        Path to a directory to create.

    """
    try:
        os.mkdir(directory)
    except OSError, e:
        log_e("Error when trying to make directory %s" % directory)


def rm_nodedir(directory, config):
    """Removes directory `directory` under all the node directories.

    :param str directory:
        Path to a directory under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.

    """
    nodes = get_nodedirs(config)

    rel_path = relative_path(directory, config)

    for node in nodes:
        dir_path = path.join(node, rel_path)
        rm_path(dir_path)


def rm_path(fpath):
    """Removes path `fpath`.

    :param str fpath:
        It can be a file or an empty directory.
    """
    try:
        if path.isfile(fpath):
            os.remove(fpath)
        elif path.isdir(fpath):
            purge_dir(fpath)
            os.rmdir(fpath)
    except OSError, e:
        log_e("Error when trying to remove path %s" % fpath)


def move_nodedir(src, dest, config):
    """Moves directory `src` to `dest`.

    :param str src:
         Old path to the directory.
    :param str dest:
         New path to the directory
    :param dict config:
        A dictionary that contains configuration information about
        combox.

    """
    nodes = get_nodedirs(config)

    src_rel_path = relative_path(src, config)
    dest_rel_path = relative_path(dest, config)

    for node in nodes:
        src_dir_path = path.join(node, src_rel_path)
        dest_dir_path = path.join(node, dest_rel_path)
        try:
            os.renames(src_dir_path, dest_dir_path)
        except OSError, e:
            log_e("Error when trying to rename %s -> %s" % (src_dir_path, dest_dir_path))


def rm_shards(fpath, config):
    """Removes the file shards of `fpath` under the node directories.

    :param str fpath:
         Path to file under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.

    """
    nodes = get_nodedirs(config)

    rel_path = relative_path(fpath, config)

    for node in nodes:
        shard_glob = "%s.shard*" % path.join(node, rel_path)

        shard_glob = glob(shard_glob)
        if not len(shard_glob):
            # shard was already deleted.
            continue

        # there's always only one shard in each node directory. So,
        # the glob() will alawys return a list of size 1.
        shard = shard_glob[0]
        try:
            os.remove(shard)
        except OSError, e:
            log_e("Error when trying to remove shard %s" % shard)


def move_shards(src, dest, config):
    """Move the shards in node directories.

    This function is used when a file is moved to different location
    inside the combox directory. It moves the shards of the respective
    file to the new location under the node directories.

    :param str src:
        Old path to the file that was moved under the combox directory.
    :param str dest:
        New path to the file that was moved under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.

    """
    nodes = get_nodedirs(config)

    src_rel_path = relative_path(src, config)
    dest_rel_path = relative_path(dest, config)

    for node in nodes:
        src_shard_glob = "%s.shard*" % path.join(node, src_rel_path)
        # there's always only one shard in each node directory.
        glob_list = glob(src_shard_glob)
        if glob_list:
            src_shard = glob_list[0]
        else:
            # shards are not there!, so we return.
            return

        # get shard number
        shard_no = src_shard.partition('.shard')[2]
        dest_shard = "%s.shard%s" % (path.join(node, dest_rel_path),
                                     shard_no)
        try:
            os.renames(src_shard, dest_shard)
        except OSError, e:
            log_e("Error when trying to rename shard %s -> %s" % (src_shard, dest_shard))


def purge_dir(p):
    """
    Purge everything under the given directory `p`.

    Directory `p` itself is not deleted.

    :param str p:
        Path to directory that needs to be purged.
    """

    p = path.abspath(p)

    if path.isfile(p):
        return os.remove(p)

    for f in os.listdir(p):
        f_path = path.join(p, f)

        if path.isfile(f_path):
            os.remove(f_path)
        else:
            purge_dir(f_path)
            os.rmdir(f_path)


def split_data(data, n):
    """Split `data` into `n` parts and return them as a list.

    :param str data:
        Stream of bytes or string.
    :param int n:
        Number of parts the `data` has to be split.
    :returns:
        List of strings -- the `data` divided into `n` parts.
    :rtype: list

    """
    d_parts = []
    # File size in bytes.
    data_size = len(data)
    # No. of bytes for each data part.
    part_size =  data_size / n
    # Take note of remaining bytes; this is non-zero when data_size is
    # not divisible by `n'.
    rem_bytes = data_size % n

    start = 0
    end = part_size
    while end <= data_size:
        d_parts.append(data[start:end])
        start = end
        end = end + part_size

    # read the remaining bytes into the last data part.
    end += start + rem_bytes
    d_parts[n-1] += data[start:end]

    return d_parts


def glue_data(d_parts):
    """Glue different parts of the `data` into a single whole.

    :param list d_parts:
        List containing different parts of the `data`. Each part is a
        sequence of bytes.
    :returns:
        The `data` glued into a single whole.
    :rtype: str

    """
    data = ''
    for part in d_parts:
        data += part

    return data


def read_file(filename):
    """Read file `filename` and return it as a string.

    :param str filename:
        Absolute pathname of the file.
    :returns:
        Content of file `filename` as a string.
    :rtype: str

    """
    file_ = None
    content = ''

    with open(filename, 'rb') as f:
        for line in f:
            content = content + line

    return content


def hash_file(filename, file_content=None):
    """Does a SHA512 hash on the contents of file.

    Returns the hexdigest of the file content's hash.

    :param str filename:
        Absolute pathname of the file.
    :param str file_content:
        If not ``None``, SHA512 hash of `file_content` is returned.
    :returns:
        If `file_content` is ``None``, returns the SHA512 hash of
        contents of file `filename`.

        If `file_content` is not ``None``, returns the SHA512 hash of
        `file_content`.
    :rtype: str
    """

    if not file_content:
        file_content = read_file(filename)

    return sha512(file_content).hexdigest()


def write_file(filename, filecontent):
    """Write `filecontent` to `filename`.

    :param str filename:
        Absolute pathname of the file.
    :param str filecontent:
        Data to write to `filename`.

    """
    file_ = None
    try:
      file_   = open(filename, 'wb')
      file_.write(filecontent)
      file_.close()
    except IOError:
        log_e("Error creating and writing content to %s" % filename)
        exit(1)


def write_shards(shards, directories, shard_basename):
    """Write shards to node directories.

    :param list shards:
        List of strings (data).
    :param list directories:
        List of paths of node directories.
    :param str shard_basename:
        Base name for the shards.

    """
    shard_no = 0
    for directory in directories:
        # partial filename of the shard
        p_filename =  path.join(directory, shard_basename)
        shard_name =  "%s.shard%s" % (p_filename, shard_no)
        write_file(shard_name, shards[shard_no])
        shard_no += 1


def read_shards(directories, shard_basename):
    """Read the shards of a file from node directories and return it as a list.

    :param list directories:
        List of paths of node directories from which to read the
        shards.
    :param str shard_basename:
        Base name for the shards; the canonical file name.
    :returns:
        List of contents of the shards of the file with basename
        `shard_basename`.

    """
    # get the names of the file shards
    file_shards = []
    for directory in directories:
        filename_glob = "%s.shard*" % path.join(directory, shard_basename)
        file_shard = glob(filename_glob)[0]
        file_shards.append(file_shard)

    shards = []
    for file_shard in sorted(file_shards):
        shard_content = read_file(file_shard)
        shards.append(shard_content)

    return shards


def no_of_shards(cb_path, config):
    """Returns the no. of shards that exists for `cb_path` under the node directories.

    :param str cb_path:
        Path to a file under the combox directory.
    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :returns:
        The number of shards that exists for the file `cb_path`.
    :rtype: int

    """
    no_shards_there = 0
    shard_paths = node_paths(cb_path, config, isfile=True)

    for shard in shard_paths:
        if path.isfile(shard):
            no_shards_there += 1

    return no_shards_there