summaryrefslogtreecommitdiffstats
path: root/odl-pipeline/lib/utils/shutil.py
blob: 787820e9ee50307bff3bb2173c07b4079053a427 (plain)
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
#
# Copyright (c) 2017 All rights reserved
# This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
import os
import glob
import processutils as putils


class shutil():
    '''
    classdocs
    '''
    @staticmethod
    def mkdir_if_not_exist(path):
        if not path:
            raise Exception('Path should not be empty.')
        putils.execute(["mkdir", "-p", path])

    @staticmethod
    def copy(direction, src, dst, **kwargs):
        if direction == 'from':
            dst_tmp = dst
            dst = src
            src = dst_tmp
        if src[-1:] == '*':
            files = glob.glob(src)
            for file in files:
                shutil._copy(file, dst, **kwargs)
        else:
            shutil._copy(src, dst, **kwargs)

    @staticmethod
    def _copy(src, dst, **kwargs):
        if os.path.isfile(src):
            if dst[-1:] == '/':
                shutil.mkdir_if_not_exsist(dst)
            putils.execute(['cp', src, dst], **kwargs)
        else:
            putils.execute(['cp', '-R', src, dst], **kwargs)

    @staticmethod
    def rm(path, **kwargs):
        putils.execute(['rm', '-rf', path], **kwargs)

    @staticmethod
    def mv(src, dst):
        putils.execute(["mv", src, dst])

    @staticmethod
    def get_all_files_in_path(path):
        if os.path.exists(path):
            return putils.execute(['l', path])

    @staticmethod
    def replace_string_in_file(file, str, replace):
        with open(file, 'r') as f:
            string = f.read()
        string = string.replace(str, replace)
        with open(file, 'w+') as f:
            f.write(string)