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
19 SCOPE_TRANSPONDERDATA = 0
31 SCOPE_CURRENT_SKIN = 12
33 SCOPE_DEFAULTPARTITION = 14
34 SCOPE_DEFAULTPARTITIONMOUNTDIR = 14
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),
46 SCOPE_LANGUAGE: ("/usr/share/enigma2/po/", PATH_DONTCREATE),
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),
54 SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory
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),
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)
66 SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
67 ("/usr/share/enigma2/defaults/", FILE_COPY)],
68 SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
71 def resolveFilename(scope, base = "", path_prefix = None):
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:])
77 # don't resolve absolute paths
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('/')
86 path = tmp[0]+config.skin.primary_skin.value[:pos+1]
90 tmp = defaultPaths[scope]
95 if flags == PATH_CREATE:
96 if not pathExists(path):
99 fallbackPath = fallbackPaths.get(scope)
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)
107 elif x[1] == FILE_MOVE:
108 if fileExists(x[0] + base):
109 system("mv " + x[0] + base + " " + path + base)
111 elif x[1] == PATH_COPY:
113 if not pathExists(defaultPaths[scope][0]):
115 system("cp -a " + x[0] + "* " + path)
117 elif x[1] == PATH_MOVE:
119 system("mv " + x[0] + " " + path)
122 # FIXME: we also have to handle DATADIR etc. here.
124 # this is only the BASE - an extension must be added later.
126 def pathExists(path):
127 return os_path.exists(path)
129 def createDir(path, makeParents = False):
150 def fileExists(f, mode='r'):
157 return access(f, acc_mode)
159 def getRecordingFilename(basename, dirname = None):
160 # filter out non-allowed characters
161 non_allowed_characters = "/.\\:*?<>|\""
164 basename = basename.replace('\xc2\x86', '').replace('\xc2\x87', '')
167 if c in non_allowed_characters:
171 if dirname is not None:
172 filename = ''.join([dirname, filename])
176 path = resolveFilename(SCOPE_HDD, filename)
185 # this is clearly a hack:
186 def InitFallbackFiles():
187 resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
188 resolveFilename(SCOPE_CONFIG, "bouquets.tv")
189 resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.radio")
190 resolveFilename(SCOPE_CONFIG, "bouquets.radio")
192 # returns a list of tuples containing pathname and filename matching the given pattern
193 # example-pattern: match all txt-files: ".*\.txt$"
194 def crawlDirectory(directory, pattern):
195 expression = compile(pattern)
197 for root, dirs, files in walk(directory):
199 if expression.match(file) is not None:
200 list.append((root, file))
203 def copyfile(src, dst):
206 if os_path.isdir(dst):
207 dst = os_path.join(dst, os_path.basename(src))
208 f2 = open(dst, "w+b")
210 buf = f1.read(16*1024)
215 mode = S_IMODE(st.st_mode)
219 utime(dst, (st.st_atime, st.st_mtime))
221 print "copy", src, "to", dst, "failed!"
225 def copytree(src, dst, symlinks=False):
227 if os_path.isdir(dst):
228 dst = os_path.join(dst, os_path.basename(src))
229 if not os_path.isdir(dst):
234 srcname = os_path.join(src, name)
235 dstname = os_path.join(dst, name)
237 if symlinks and os_path.islink(srcname):
238 linkto = readlink(srcname)
239 symlink(linkto, dstname)
240 elif os_path.isdir(srcname):
241 copytree(srcname, dstname, symlinks)
243 copyfile(srcname, dstname)
245 print "dont copy srcname (no file or link or folder)"
248 mode = S_IMODE(st.st_mode)
252 utime(dst, (st.st_atime, st.st_mtime))
254 print "copy stats for", src, "failed!"