Passed
Push — develop ( fc562f...b9b638 )
by Dean
02:34
created

StorageHelper.copy()   A

Complexity

Conditions 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.916
Metric Value
cc 2
dl 0
loc 20
ccs 1
cts 10
cp 0.1
crap 4.916
rs 9.4285
1 1
import logging
2 1
import os
3 1
import shutil
4
5 1
log = logging.getLogger(__name__)
6
7
8 1
class StorageHelper(object):
9 1
    base_names = [
10
        'plug-ins',
11
        'plug-in support',
12
        'trakttv.bundle'
13
    ]
14
15 1
    @classmethod
16
    def create_directories(cls, path, *args, **kwargs):
17
        """Create directory at `path` include any parent directories
18
19
        :type path: str
20
        """
21
22
        try:
23
            os.makedirs(path, *args, **kwargs)
24
            return True
25
        except OSError, ex:
26
            if ex.errno == 17:
27
                # Directory already exists
28
                return True
29
30
            log.warn('Unable to create directories: %r - (%s) %s', cls.to_relative_path(path), ex.errno, ex)
31
        except Exception, ex:
32
            log.warn('Unable to create directories: %r - (%s) %s', cls.to_relative_path(path), type(ex), ex)
33
34
        return False
35
36 1
    @classmethod
37
    def copy(cls, source, destination):
38
        """Copy the file at `source` to `destination`
39
40
        :type source: str
41
        :type destination: str
42
        """
43
44
        if os.path.isdir(source):
45
            return cls.copy_tree(source, destination)
46
47
        try:
48
            shutil.copy2(source, destination)
49
50
            log.debug('Copied %r to %r', cls.to_relative_path(source), cls.to_relative_path(destination))
51
            return True
52
        except Exception, ex:
53
            log.warn('Unable to copy %r to %r - %s', cls.to_relative_path(source), cls.to_relative_path(destination), ex)
54
55
        return False
56
57 1
    @classmethod
58
    def copy_tree(cls, source, destination):
59
        """Copy the directory at `source` to `destination`
60
61
        :type source: str
62
        :type destination: str
63
        """
64
65
        try:
66
            shutil.copytree(source, destination)
67
68
            log.debug('Copied %r to %r', cls.to_relative_path(source), cls.to_relative_path(destination))
69
            return True
70
        except Exception, ex:
71
            log.warn('Unable to copy %r to %r - %s', cls.to_relative_path(source), cls.to_relative_path(destination), ex)
72
73
        return False
74
75 1
    @classmethod
76
    def delete(cls, path):
77
        """Delete the file (at `path`)
78
79
        :type path: str
80
        """
81
82
        if os.path.isdir(path):
83
            return cls.delete_tree(path)
84
85
        try:
86
            os.remove(path)
87
88
            log.debug('Deleted %r', cls.to_relative_path(path))
89
            return True
90
        except Exception, ex:
91
            log.warn('Unable to delete file: %r - %s', cls.to_relative_path(path), ex)
92
93
        return False
94
95 1
    @classmethod
96
    def delete_tree(cls, path):
97
        """Delete the directory (at `path`)
98
99
        :type path: str
100
        """
101
102
        try:
103
            shutil.rmtree(path)
104
105
            log.debug('Deleted %r', cls.to_relative_path(path))
106
            return True
107
        except Exception, ex:
108
            log.warn('Unable to delete directory: %r - %s', cls.to_relative_path(path), ex)
109
110
        return False
111
112 1
    @classmethod
113
    def to_relative_path(cls, path):
114
        """Convert `path` to be relative to `StorageHelper.base_names`
115
116
        :type path: str
117
        """
118
119 1
        path_lower = path.lower()
120
121
        # Find base path
122 1
        base_path = None
123
124 1
        for base in cls.base_names:
125 1
            if base not in path_lower:
126 1
                continue
127
128 1
            base_path = path[:path_lower.find(base)]
129 1
            break
130
131
        # Check if `base_path` was found
132 1
        if not base_path:
133
            log.warn('Unable to find base path in %r', path)
134
            return path
135
136
        # Return relative path
137 1
        return os.path.relpath(path, base_path)
138
139 1
    @classmethod
140
    def is_relative_path(cls, path):
141
        """Check if `path` is relative to `StorageHelper.base_names`
142
143
        :type path: str
144
        """
145
146 1
        path_lower = path.lower()
147
148
        # Ignore framework paths
149 1
        if 'framework.bundle' in path_lower:
150
            return False
151
152
        # Find base path
153 1
        for base in cls.base_names:
154 1
            if base not in path_lower:
155 1
                continue
156
157 1
            return True
158
159
        return False
160