add isMount encapsulation
[enigma2.git] / lib / python / Tools / Directories.py
1 # -*- coding: utf-8 -*-
2
3 from os import path as os_path, mkdir, rmdir, system, walk, stat as os_stat, listdir, readlink, makedirs, error as os_error, symlink, access, F_OK, R_OK, W_OK
4 from stat import S_IMODE
5 from re import compile
6
7 try:
8         from os import chmod
9         have_chmod = True
10 except:
11         have_chmod = False
12
13 try:
14         from os import utime
15         have_utime = True
16 except:
17         have_utime = False
18
19 SCOPE_TRANSPONDERDATA = 0
20 SCOPE_SYSETC = 1
21 SCOPE_FONTS = 2
22 SCOPE_SKIN = 3
23 SCOPE_SKIN_IMAGE = 4
24 SCOPE_USERETC = 5
25 SCOPE_CONFIG = 6
26 SCOPE_LANGUAGE = 7
27 SCOPE_HDD = 8
28 SCOPE_PLUGINS = 9
29 SCOPE_MEDIA = 10
30 SCOPE_PLAYLIST = 11
31 SCOPE_CURRENT_SKIN = 12
32 SCOPE_DEFAULTDIR = 13
33 SCOPE_DEFAULTPARTITION = 14
34 SCOPE_DEFAULTPARTITIONMOUNTDIR = 14
35
36 PATH_CREATE = 0
37 PATH_DONTCREATE = 1
38 PATH_FALLBACK = 2
39 defaultPaths = {
40                 SCOPE_TRANSPONDERDATA: ("/etc/", PATH_DONTCREATE),
41                 SCOPE_SYSETC: ("/etc/", PATH_DONTCREATE),
42                 SCOPE_FONTS: ("/usr/share/fonts/", PATH_DONTCREATE),
43                 SCOPE_CONFIG: ("/etc/enigma2/", PATH_CREATE),
44                 SCOPE_PLUGINS: ("/usr/lib/enigma2/python/Plugins/", PATH_CREATE),
45     
46                 SCOPE_LANGUAGE: ("/usr/share/enigma2/po/", PATH_DONTCREATE),
47
48                 SCOPE_SKIN: ("/usr/share/enigma2/", PATH_DONTCREATE),
49                 SCOPE_SKIN_IMAGE: ("/usr/share/enigma2/", PATH_DONTCREATE),
50                 SCOPE_HDD: ("/hdd/movie/", PATH_DONTCREATE),
51                 SCOPE_MEDIA: ("/media/", PATH_DONTCREATE),
52                 SCOPE_PLAYLIST: ("/etc/enigma2/playlist/", PATH_CREATE),
53                 
54                 SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory
55                 
56                 SCOPE_DEFAULTDIR: ("/usr/share/enigma2/defaults/", PATH_CREATE),
57                 SCOPE_DEFAULTPARTITION: ("/dev/mtdblock/6", PATH_DONTCREATE),
58                 SCOPE_DEFAULTPARTITIONMOUNTDIR: ("/usr/share/enigma2/dealer", PATH_CREATE),
59         }
60
61 FILE_COPY = 0 # copy files from fallback dir to the basedir
62 FILE_MOVE = 1 # move files
63 PATH_COPY = 2 # copy the complete fallback dir to the basedir
64 PATH_MOVE = 3 # move the fallback dir to the basedir (can be used for changes in paths)
65 fallbackPaths = {
66                 SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
67                                            ("/usr/share/enigma2/defaults/", FILE_COPY)],
68                 SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
69         }
70
71 def resolveFilename(scope, base = "", path_prefix = None):
72         if base[0:2] == "~/":
73                 # you can only use the ~/ if we have a prefix directory
74                 assert path_prefix is not None
75                 base = os_path.join(path_prefix, base[2:])
76
77         # don't resolve absolute paths
78         if base[0:1] == '/':
79                 return base
80
81         if scope == SCOPE_CURRENT_SKIN:
82                 from Components.config import config
83                 tmp = defaultPaths[SCOPE_SKIN]
84                 pos = config.skin.primary_skin.value.rfind('/')
85                 if pos != -1:
86                         path = tmp[0]+config.skin.primary_skin.value[:pos+1]
87                 else:
88                         path = tmp[0]
89         else:
90                 tmp = defaultPaths[scope]
91                 path = tmp[0]
92
93         flags = tmp[1]
94
95         if flags == PATH_CREATE:
96                 if not pathExists(path):
97                         mkdir(path)
98
99         fallbackPath = fallbackPaths.get(scope)
100
101         if fallbackPath and not fileExists(path + base):
102                 for x in fallbackPath:
103                         if x[1] == FILE_COPY:
104                                 if fileExists(x[0] + base):
105                                         system("cp " + x[0] + base + " " + path + base)
106                                         break
107                         elif x[1] == FILE_MOVE:
108                                 if fileExists(x[0] + base):
109                                         system("mv " + x[0] + base + " " + path + base)
110                                         break
111                         elif x[1] == PATH_COPY:
112                                 if pathExists(x[0]):
113                                         if not pathExists(defaultPaths[scope][0]):
114                                                 mkdir(path)
115                                         system("cp -a " + x[0] + "* " + path)
116                                         break
117                         elif x[1] == PATH_MOVE:
118                                 if pathExists(x[0]):
119                                         system("mv " + x[0] + " " + path)
120                                         break
121
122         # FIXME: we also have to handle DATADIR etc. here.
123         return path + base
124         # this is only the BASE - an extension must be added later.
125
126 def pathExists(path):
127         return os_path.exists(path)
128
129 def isMount(path):
130         return os_path.ismount(path)
131
132 def createDir(path, makeParents = False):
133         try:
134                 if makeParents:
135                         makedirs(path)
136                 else:
137                         mkdir(path)
138         except:
139                 ret = 0
140         else:
141                 ret = 1
142         return ret
143
144 def removeDir(path):
145         try:
146                 rmdir(path)
147         except:
148                 ret = 0
149         else:
150                 ret = 1
151         return ret
152
153 def fileExists(f, mode='r'):
154         if mode == 'r':
155                 acc_mode = R_OK
156         elif mode == 'w':
157                 acc_mode = W_OK
158         else:
159                 acc_mode = F_OK
160         return access(f, acc_mode)
161
162 def getRecordingFilename(basename, dirname = None):
163         # filter out non-allowed characters
164         non_allowed_characters = "/.\\:*?<>|\""
165         filename = ""
166         
167         basename = basename.replace('\xc2\x86', '').replace('\xc2\x87', '')
168         
169         for c in basename:
170                 if c in non_allowed_characters:
171                         c = "_"
172                 filename += c
173
174         if dirname is not None:
175                 filename = ''.join([dirname, filename])
176
177         i = 0
178         while True:
179                 path = resolveFilename(SCOPE_HDD, filename)
180                 if i > 0:
181                         path += "_%03d" % i
182                 try:
183                         open(path + ".ts")
184                         i += 1
185                 except IOError:
186                         return path
187
188 # this is clearly a hack:
189 def InitFallbackFiles():
190         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
191         resolveFilename(SCOPE_CONFIG, "bouquets.tv")
192         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.radio")
193         resolveFilename(SCOPE_CONFIG, "bouquets.radio")
194
195 # returns a list of tuples containing pathname and filename matching the given pattern
196 # example-pattern: match all txt-files: ".*\.txt$"
197 def crawlDirectory(directory, pattern):
198         expression = compile(pattern)
199         list = []
200         for root, dirs, files in walk(directory):
201                 for file in files:
202                         if expression.match(file) is not None:
203                                 list.append((root, file))
204         return list
205
206 def copyfile(src, dst):
207         try:
208                 f1 = open(src, "rb")
209                 if os_path.isdir(dst):
210                         dst = os_path.join(dst, os_path.basename(src))
211                 f2 = open(dst, "w+b")
212                 while True:
213                         buf = f1.read(16*1024)
214                         if not buf:
215                                 break
216                         f2.write(buf)
217                 st = os_stat(src)
218                 mode = S_IMODE(st.st_mode)
219                 if have_chmod:
220                         chmod(dst, mode)
221                 if have_utime:
222                         utime(dst, (st.st_atime, st.st_mtime))
223         except:
224                 print "copy", src, "to", dst, "failed!"
225                 return -1
226         return 0
227
228 def copytree(src, dst, symlinks=False):
229         names = listdir(src)
230         if os_path.isdir(dst):
231                 dst = os_path.join(dst, os_path.basename(src))
232                 if not os_path.isdir(dst):
233                         mkdir(dst)
234         else:
235                 makedirs(dst)
236         for name in names:
237                 srcname = os_path.join(src, name)
238                 dstname = os_path.join(dst, name)
239                 try:
240                         if symlinks and os_path.islink(srcname):
241                                 linkto = readlink(srcname)
242                                 symlink(linkto, dstname)
243                         elif os_path.isdir(srcname):
244                                 copytree(srcname, dstname, symlinks)
245                         else:
246                                 copyfile(srcname, dstname)
247                 except:
248                         print "dont copy srcname (no file or link or folder)"
249         try:
250                 st = os_stat(src)
251                 mode = S_IMODE(st.st_mode)
252                 if have_chmod:
253                         chmod(dst, mode)
254                 if have_utime:
255                         utime(dst, (st.st_atime, st.st_mtime))
256         except:
257                 print "copy stats for", src, "failed!"