1 # -*- coding: utf-8 -*-
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
6 from enigma import eEnv
22 SCOPE_TRANSPONDERDATA = 0
34 SCOPE_CURRENT_SKIN = 12
36 SCOPE_DEFAULTPARTITION = 14
37 SCOPE_DEFAULTPARTITIONMOUNTDIR = 15
39 SCOPE_CURRENT_PLUGIN = 17
45 SCOPE_TRANSPONDERDATA: (eEnv.resolve("${sysconfdir}/"), PATH_DONTCREATE),
46 SCOPE_SYSETC: (eEnv.resolve("${sysconfdir}/"), PATH_DONTCREATE),
47 SCOPE_FONTS: (eEnv.resolve("${datadir}/fonts/"), PATH_DONTCREATE),
48 SCOPE_CONFIG: (eEnv.resolve("${sysconfdir}/enigma2/"), PATH_CREATE),
49 SCOPE_PLUGINS: (eEnv.resolve("${libdir}/enigma2/python/Plugins/"), PATH_CREATE),
51 SCOPE_LANGUAGE: (eEnv.resolve("${datadir}/enigma2/po/"), PATH_DONTCREATE),
53 SCOPE_SKIN: (eEnv.resolve("${datadir}/enigma2/"), PATH_DONTCREATE),
54 SCOPE_SKIN_IMAGE: (eEnv.resolve("${datadir}/enigma2/"), PATH_DONTCREATE),
55 SCOPE_HDD: ("/hdd/movie/", PATH_DONTCREATE),
56 SCOPE_MEDIA: ("/media/", PATH_DONTCREATE),
57 SCOPE_PLAYLIST: (eEnv.resolve("${sysconfdir}/enigma2/playlist/"), PATH_CREATE),
59 SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory
61 SCOPE_DEFAULTDIR: (eEnv.resolve("${datadir}/enigma2/defaults/"), PATH_CREATE),
62 SCOPE_DEFAULTPARTITION: ("/dev/mtdblock6", PATH_DONTCREATE),
63 SCOPE_DEFAULTPARTITIONMOUNTDIR: (eEnv.resolve("${datadir}/enigma2/dealer"), PATH_CREATE),
64 SCOPE_METADIR: (eEnv.resolve("${datadir}/meta"), PATH_CREATE),
67 FILE_COPY = 0 # copy files from fallback dir to the basedir
68 FILE_MOVE = 1 # move files
69 PATH_COPY = 2 # copy the complete fallback dir to the basedir
70 PATH_MOVE = 3 # move the fallback dir to the basedir (can be used for changes in paths)
72 SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
73 (eEnv.resolve("${datadir}/enigma2/defaults/"), FILE_COPY)],
74 SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
77 def resolveFilename(scope, base = "", path_prefix = None):
79 # you can only use the ~/ if we have a prefix directory
80 assert path_prefix is not None
81 base = os_path.join(path_prefix, base[2:])
83 # don't resolve absolute paths
87 if scope == SCOPE_CURRENT_SKIN:
88 from Components.config import config
89 tmp = defaultPaths[SCOPE_SKIN]
90 pos = config.skin.primary_skin.value.rfind('/')
92 #if basefile is not available use default skin path as fallback
93 tmpfile = tmp[0]+config.skin.primary_skin.value[:pos+1] + base
94 if fileExists(tmpfile):
95 path = tmp[0]+config.skin.primary_skin.value[:pos+1]
101 elif scope == SCOPE_CURRENT_PLUGIN:
102 tmp = defaultPaths[SCOPE_PLUGINS]
103 from Components.config import config
104 skintmp = defaultPaths[SCOPE_SKIN]
105 pos = config.skin.primary_skin.value.rfind('/')
107 #if basefile is not available inside current skin path, use the original provided file as fallback
108 skintmpfile = skintmp[0]+config.skin.primary_skin.value[:pos+1] + base
109 if fileExists(skintmpfile):
110 path = skintmp[0]+config.skin.primary_skin.value[:pos+1]
116 tmp = defaultPaths[scope]
121 if flags == PATH_CREATE:
122 if not pathExists(path):
126 print "resolveFilename: Couldn't create %s" % path
129 fallbackPath = fallbackPaths.get(scope)
131 if fallbackPath and not fileExists(path + base):
132 for x in fallbackPath:
133 if x[1] == FILE_COPY:
134 if fileExists(x[0] + base):
135 system("cp " + x[0] + base + " " + path + base)
137 elif x[1] == FILE_MOVE:
138 if fileExists(x[0] + base):
139 system("mv " + x[0] + base + " " + path + base)
141 elif x[1] == PATH_COPY:
143 if not pathExists(defaultPaths[scope][0]):
145 system("cp -a " + x[0] + "* " + path)
147 elif x[1] == PATH_MOVE:
149 system("mv " + x[0] + " " + path)
152 # FIXME: we also have to handle DATADIR etc. here.
154 # this is only the BASE - an extension must be added later.
156 pathExists = os.path.exists
157 isMount = os.path.ismount
159 def bestRecordingLocation(candidates):
162 from Components import Harddisk
163 ata_devices = [candidate for candidate in candidates if Harddisk.getDeviceInterface(candidate[1]) == "ata"]
165 if len(ata_devices) == 1:
166 path = ata_devices[0][1]
168 elif len(ata_devices):
170 for device in ata_devices:
171 dev = os.path.basename(device[0])
172 if not best or (best > dev):
175 else: # Find the largest usb disk
177 for candidate in candidates:
179 stat = os.statvfs(candidate[1])
180 # must have some free space (i.e. not read-only)
182 # Free space counts double
183 size = (stat.f_blocks + stat.f_bavail) * stat.f_bsize
192 def defaultRecordingLocation(candidate=None):
193 if candidate and os.path.exists(candidate):
195 # First, try whatever /hdd points to, or /media/hdd
197 path = os.path.realpath('/hdd')
200 if not os.path.exists(path) or not os.path.ismount(path):
202 from Components import Harddisk
203 mounts = [m for m in Harddisk.getProcMounts() if m[1].startswith('/media/')]
204 path = bestRecordingLocation([m for m in mounts if m[0].startswith('/dev/')])
206 # If there's a movie subdir, we'd probably want to use that.
207 movie = os.path.join(path, 'movie')
208 if os.path.isdir(movie):
210 if not path.endswith('/'):
211 path += '/' # Bad habits die hard, old code relies on this
215 def createDir(path, makeParents = False):
236 def fileExists(f, mode='r'):
243 return access(f, acc_mode)
245 def fileCheck(f, mode='r'):
246 return fileExists(f, mode) and f
248 def getRecordingFilename(basename, dirname = None):
249 if not dirname.endswith('/'):
252 # filter out non-allowed characters
253 non_allowed_characters = "/.\\:*?<>|\""
256 basename = basename.replace('\xc2\x86', '').replace('\xc2\x87', '')
259 if c in non_allowed_characters or ord(c) < 32:
263 if dirname is not None:
264 filename = ''.join((dirname, filename))
266 while len(filename) > 240:
267 filename = filename.decode('UTF-8')
268 filename = filename[:-1]
269 filename = filename.encode('UTF-8')
273 path = resolveFilename(SCOPE_HDD, filename)
282 # this is clearly a hack:
283 def InitFallbackFiles():
284 resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
285 resolveFilename(SCOPE_CONFIG, "bouquets.tv")
286 resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.radio")
287 resolveFilename(SCOPE_CONFIG, "bouquets.radio")
289 # returns a list of tuples containing pathname and filename matching the given pattern
290 # example-pattern: match all txt-files: ".*\.txt$"
291 def crawlDirectory(directory, pattern):
294 expression = compile(pattern)
295 for root, dirs, files in walk(directory):
297 if expression.match(file) is not None:
298 list.append((root, file))
301 def copyfile(src, dst):
304 if os_path.isdir(dst):
305 dst = os_path.join(dst, os_path.basename(src))
306 f2 = open(dst, "w+b")
308 buf = f1.read(16*1024)
313 mode = S_IMODE(st.st_mode)
317 utime(dst, (st.st_atime, st.st_mtime))
319 print "copy", src, "to", dst, "failed!"
323 def copytree(src, dst, symlinks=False):
325 if os_path.isdir(dst):
326 dst = os_path.join(dst, os_path.basename(src))
327 if not os_path.isdir(dst):
332 srcname = os_path.join(src, name)
333 dstname = os_path.join(dst, name)
335 if symlinks and os_path.islink(srcname):
336 linkto = readlink(srcname)
337 symlink(linkto, dstname)
338 elif os_path.isdir(srcname):
339 copytree(srcname, dstname, symlinks)
341 copyfile(srcname, dstname)
343 print "dont copy srcname (no file or link or folder)"
346 mode = S_IMODE(st.st_mode)
350 utime(dst, (st.st_atime, st.st_mtime))
352 print "copy stats for", src, "failed!"
354 def getSize(path, pattern=".*"):
356 if os_path.isdir(path):
357 files = crawlDirectory(path, pattern)
359 filepath = os_path.join(file[0], file[1])
360 path_size += os_path.getsize(filepath)
361 elif os_path.isfile(path):
362 path_size = os_path.getsize(path)