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
|
|
|
try: |
83
|
|
|
os.remove(path) |
84
|
|
|
|
85
|
|
|
log.debug('Deleted %r', cls.to_relative_path(path)) |
86
|
|
|
return True |
87
|
|
|
except Exception, ex: |
88
|
|
|
log.warn('Unable to delete file: %r - %s', cls.to_relative_path(path), ex) |
89
|
|
|
|
90
|
|
|
return False |
91
|
|
|
|
92
|
1 |
|
@classmethod |
93
|
|
|
def delete_tree(cls, path): |
94
|
|
|
"""Delete the directory (at `path`) |
95
|
|
|
|
96
|
|
|
:type path: str |
97
|
|
|
""" |
98
|
|
|
|
99
|
|
|
try: |
100
|
|
|
shutil.rmtree(path) |
101
|
|
|
|
102
|
|
|
log.debug('Deleted %r', cls.to_relative_path(path)) |
103
|
|
|
return True |
104
|
|
|
except Exception, ex: |
105
|
|
|
log.warn('Unable to delete directory: %r - %s', cls.to_relative_path(path), ex) |
106
|
|
|
|
107
|
|
|
return False |
108
|
|
|
|
109
|
1 |
|
@classmethod |
110
|
|
|
def to_relative_path(cls, path): |
111
|
|
|
"""Convert `path` to be relative to `StorageHelper.base_names` |
112
|
|
|
|
113
|
|
|
:type path: str |
114
|
|
|
""" |
115
|
|
|
|
116
|
1 |
|
path_lower = path.lower() |
117
|
|
|
|
118
|
|
|
# Find base path |
119
|
1 |
|
base_path = None |
120
|
|
|
|
121
|
1 |
|
for base in cls.base_names: |
122
|
1 |
|
if base not in path_lower: |
123
|
1 |
|
continue |
124
|
|
|
|
125
|
1 |
|
base_path = path[:path_lower.find(base)] |
126
|
1 |
|
break |
127
|
|
|
|
128
|
|
|
# Check if `base_path` was found |
129
|
1 |
|
if not base_path: |
130
|
|
|
log.warn('Unable to find base path in %r', path) |
131
|
|
|
return path |
132
|
|
|
|
133
|
|
|
# Return relative path |
134
|
1 |
|
return os.path.relpath(path, base_path) |
135
|
|
|
|
136
|
1 |
|
@classmethod |
137
|
|
|
def is_relative_path(cls, path): |
138
|
|
|
"""Check if `path` is relative to `StorageHelper.base_names` |
139
|
|
|
|
140
|
|
|
:type path: str |
141
|
|
|
""" |
142
|
|
|
|
143
|
1 |
|
path_lower = path.lower() |
144
|
|
|
|
145
|
|
|
# Ignore framework paths |
146
|
1 |
|
if 'framework.bundle' in path_lower: |
147
|
|
|
return False |
148
|
|
|
|
149
|
|
|
# Find base path |
150
|
1 |
|
for base in cls.base_names: |
151
|
1 |
|
if base not in path_lower: |
152
|
1 |
|
continue |
153
|
|
|
|
154
|
1 |
|
return True |
155
|
|
|
|
156
|
|
|
return False |
157
|
|
|
|