don't use devfs names: /dev/i2c/X -> /dev/i2c-X
[enigma2.git] / lib / python / Tools / Directories.py
index 1858152b1247073549a1aee59c328388f73d4298..8e4d0044fc6043d4ef00f17e6ba15828cabd21f4 100755 (executable)
@@ -3,6 +3,7 @@
 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
 from stat import S_IMODE
 from re import compile
+from enigma import eEnv
 
 try:
        from os import chmod
@@ -33,31 +34,32 @@ SCOPE_DEFAULTDIR = 13
 SCOPE_DEFAULTPARTITION = 14
 SCOPE_DEFAULTPARTITIONMOUNTDIR = 15
 SCOPE_METADIR = 16
+SCOPE_CURRENT_PLUGIN = 17
 
 PATH_CREATE = 0
 PATH_DONTCREATE = 1
 PATH_FALLBACK = 2
 defaultPaths = {
-               SCOPE_TRANSPONDERDATA: ("/etc/", PATH_DONTCREATE),
-               SCOPE_SYSETC: ("/etc/", PATH_DONTCREATE),
-               SCOPE_FONTS: ("/usr/share/fonts/", PATH_DONTCREATE),
-               SCOPE_CONFIG: ("/etc/enigma2/", PATH_CREATE),
-               SCOPE_PLUGINS: ("/usr/lib/enigma2/python/Plugins/", PATH_CREATE),
+               SCOPE_TRANSPONDERDATA: (eEnv.resolve("${sysconfdir}/"), PATH_DONTCREATE),
+               SCOPE_SYSETC: (eEnv.resolve("${sysconfdir}/"), PATH_DONTCREATE),
+               SCOPE_FONTS: (eEnv.resolve("${datadir}/fonts/"), PATH_DONTCREATE),
+               SCOPE_CONFIG: (eEnv.resolve("${sysconfdir}/enigma2/"), PATH_CREATE),
+               SCOPE_PLUGINS: (eEnv.resolve("${libdir}/enigma2/python/Plugins/"), PATH_CREATE),
 
-               SCOPE_LANGUAGE: ("/usr/share/enigma2/po/", PATH_DONTCREATE),
+               SCOPE_LANGUAGE: (eEnv.resolve("${datadir}/enigma2/po/"), PATH_DONTCREATE),
 
-               SCOPE_SKIN: ("/usr/share/enigma2/", PATH_DONTCREATE),
-               SCOPE_SKIN_IMAGE: ("/usr/share/enigma2/", PATH_DONTCREATE),
+               SCOPE_SKIN: (eEnv.resolve("${datadir}/enigma2/"), PATH_DONTCREATE),
+               SCOPE_SKIN_IMAGE: (eEnv.resolve("${datadir}/enigma2/"), PATH_DONTCREATE),
                SCOPE_HDD: ("/hdd/movie/", PATH_DONTCREATE),
                SCOPE_MEDIA: ("/media/", PATH_DONTCREATE),
-               SCOPE_PLAYLIST: ("/etc/enigma2/playlist/", PATH_CREATE),
+               SCOPE_PLAYLIST: (eEnv.resolve("${sysconfdir}/enigma2/playlist/"), PATH_CREATE),
                
                SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory
                
-               SCOPE_DEFAULTDIR: ("/usr/share/enigma2/defaults/", PATH_CREATE),
-               SCOPE_DEFAULTPARTITION: ("/dev/mtdblock/6", PATH_DONTCREATE),
-               SCOPE_DEFAULTPARTITIONMOUNTDIR: ("/usr/share/enigma2/dealer", PATH_CREATE),
-               SCOPE_METADIR: ("/usr/lib/enigma2/python/Plugins/meta", PATH_CREATE),
+               SCOPE_DEFAULTDIR: (eEnv.resolve("${datadir}/enigma2/defaults/"), PATH_CREATE),
+               SCOPE_DEFAULTPARTITION: ("/dev/mtdblock6", PATH_DONTCREATE),
+               SCOPE_DEFAULTPARTITIONMOUNTDIR: (eEnv.resolve("${datadir}/enigma2/dealer"), PATH_CREATE),
+               SCOPE_METADIR: (eEnv.resolve("${datadir}/meta"), PATH_CREATE),
        }
 
 FILE_COPY = 0 # copy files from fallback dir to the basedir
@@ -66,7 +68,7 @@ PATH_COPY = 2 # copy the complete fallback dir to the basedir
 PATH_MOVE = 3 # move the fallback dir to the basedir (can be used for changes in paths)
 fallbackPaths = {
                SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
-                                          ("/usr/share/enigma2/defaults/", FILE_COPY)],
+                                          (eEnv.resolve("${datadir}/enigma2/defaults/"), FILE_COPY)],
                SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
        }
 
@@ -85,7 +87,27 @@ def resolveFilename(scope, base = "", path_prefix = None):
                tmp = defaultPaths[SCOPE_SKIN]
                pos = config.skin.primary_skin.value.rfind('/')
                if pos != -1:
-                       path = tmp[0]+config.skin.primary_skin.value[:pos+1]
+                       #if basefile is not available use default skin path as fallback
+                       tmpfile = tmp[0]+config.skin.primary_skin.value[:pos+1] + base
+                       if fileExists(tmpfile):
+                               path = tmp[0]+config.skin.primary_skin.value[:pos+1]
+                       else:
+                               path = tmp[0]
+               else:
+                       path = tmp[0]
+
+       elif scope == SCOPE_CURRENT_PLUGIN:
+               tmp = defaultPaths[SCOPE_PLUGINS]
+               from Components.config import config
+               skintmp = defaultPaths[SCOPE_SKIN]
+               pos = config.skin.primary_skin.value.rfind('/')
+               if pos != -1:
+                       #if basefile is not available inside current skin path, use the original provided file as fallback
+                       skintmpfile = skintmp[0]+config.skin.primary_skin.value[:pos+1] + base
+                       if fileExists(skintmpfile):
+                               path = skintmp[0]+config.skin.primary_skin.value[:pos+1]
+                       else:
+                               path = tmp[0]
                else:
                        path = tmp[0]
        else:
@@ -176,6 +198,11 @@ def getRecordingFilename(basename, dirname = None):
        if dirname is not None:
                filename = ''.join((dirname, filename))
 
+       while len(filename) > 240:
+               filename = filename.decode('UTF-8')
+               filename = filename[:-1]
+               filename = filename.encode('UTF-8')
+
        i = 0
        while True:
                path = resolveFilename(SCOPE_HDD, filename)
@@ -257,3 +284,14 @@ def copytree(src, dst, symlinks=False):
                        utime(dst, (st.st_atime, st.st_mtime))
        except:
                print "copy stats for", src, "failed!"
+
+def getSize(path, pattern=".*"):
+       path_size = 0
+       if os_path.isdir(path):
+               files = crawlDirectory(path, pattern)
+               for file in files:
+                       filepath = os_path.join(file[0], file[1])
+                       path_size += os_path.getsize(filepath)
+       elif os_path.isfile(path):
+               path_size = os_path.getsize(path)
+       return path_size