Updates ODL Pipeline scripts for CSIT
[sdnvpn.git] / odl-pipeline / lib / utils / shutil.py
1 #
2 # Copyright (c) 2015 All rights reserved
3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #
10 import os
11 import glob
12 import processutils as putils
13
14
15 class shutil():
16     '''
17     classdocs
18     '''
19     @staticmethod
20     def mkdir_if_not_exist(path):
21         if not path:
22             raise Exception('Path should not be empty.')
23         putils.execute(["mkdir", "-p", path])
24
25     @staticmethod
26     def copy(direction, src, dst, **kwargs):
27         if direction == 'from':
28             dst_tmp = dst
29             dst = src
30             src = dst_tmp
31         if src[-1:] == '*':
32             files = glob.glob(src)
33             for file in files:
34                 shutil._copy(file, dst, **kwargs)
35         else:
36             shutil._copy(src, dst, **kwargs)
37
38     @staticmethod
39     def _copy(src, dst, **kwargs):
40         if os.path.isfile(src):
41             if dst[-1:] == '/':
42                 shutil.mkdir_if_not_exsist(dst)
43             putils.execute(['cp', src, dst], **kwargs)
44         else:
45             putils.execute(['cp', '-R', src, dst], **kwargs)
46
47     @staticmethod
48     def rm(path, **kwargs):
49         putils.execute(['rm', '-rf', path], **kwargs)
50
51     @staticmethod
52     def mv(src, dst):
53         putils.execute(["mv", src, dst])
54
55     @staticmethod
56     def get_all_files_in_path(path):
57         if os.path.exists(path):
58             return putils.execute(['l', path])
59
60     @staticmethod
61     def replace_string_in_file(file, str, replace):
62         with open(file, 'r') as f:
63             string = f.read()
64         string = string.replace(str, replace)
65         with open(file, 'w+') as f:
66             f.write(string)