- add crawlDirectory helper function
[enigma2.git] / lib / python / Tools / Directories.py
1 # -*- coding: utf-8 -*-
2 from os import path as os_path, mkdir, system, walk
3 from re import compile
4
5 SCOPE_TRANSPONDERDATA = 0
6 SCOPE_SYSETC = 1
7 SCOPE_FONTS = 2
8 SCOPE_SKIN = 3
9 SCOPE_SKIN_IMAGE = 4
10 SCOPE_USERETC = 5
11 SCOPE_CONFIG = 6
12 SCOPE_LANGUAGE = 7
13 SCOPE_HDD = 8
14 SCOPE_PLUGINS = 9
15 SCOPE_MEDIA = 10
16 SCOPE_PLAYLIST = 11
17 SCOPE_CURRENT_SKIN = 12
18 SCOPE_DEFAULTDIR = 13
19 SCOPE_DEFAULTPARTITION = 14
20
21 PATH_CREATE = 0
22 PATH_DONTCREATE = 1
23 PATH_FALLBACK = 2
24 defaultPaths = {
25                 SCOPE_TRANSPONDERDATA: ("/etc/", PATH_DONTCREATE),
26                 SCOPE_SYSETC: ("/etc/", PATH_DONTCREATE),
27                 SCOPE_FONTS: ("/usr/share/fonts/", PATH_DONTCREATE),
28                 SCOPE_CONFIG: ("/etc/enigma2/", PATH_CREATE),
29                 SCOPE_PLUGINS: ("/usr/lib/enigma2/python/Plugins/", PATH_CREATE),
30     
31                 SCOPE_LANGUAGE: ("/usr/share/enigma2/po/", PATH_DONTCREATE),
32
33                 SCOPE_SKIN: ("/usr/share/enigma2/", PATH_DONTCREATE),
34                 SCOPE_SKIN_IMAGE: ("/usr/share/enigma2/", PATH_DONTCREATE),
35                 SCOPE_HDD: ("/hdd/movie/", PATH_DONTCREATE),
36                 SCOPE_MEDIA: ("/media/", PATH_DONTCREATE),
37                 SCOPE_PLAYLIST: ("/etc/enigma2/playlist/", PATH_CREATE),
38                 
39                 SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory
40                 
41                 SCOPE_DEFAULTDIR: ("/usr/share/enigma2/defaults/", PATH_CREATE),
42                 SCOPE_DEFAULTPARTITION: ("/dev/mtdblock/4", PATH_DONTCREATE),
43         }
44
45 FILE_COPY = 0 # copy files from fallback dir to the basedir
46 FILE_MOVE = 1 # move files
47 PATH_COPY = 2 # copy the complete fallback dir to the basedir
48 PATH_MOVE = 3 # move the fallback dir to the basedir (can be used for changes in paths)
49 fallbackPaths = {
50                 SCOPE_CONFIG: [("/home/root/", FILE_MOVE),
51                                            ("/usr/share/enigma2/defaults/", FILE_COPY)],
52                 SCOPE_HDD: [("/hdd/movies", PATH_MOVE)]
53         }
54
55 def resolveFilename(scope, base = "", path_prefix = None):
56         if base[0:2] == "~/":
57                 # you can only use the ~/ if we have a prefix directory
58                 assert path_prefix is not None
59                 base = os_path.join(path_prefix, base[2:])
60
61         # don't resolve absolute paths
62         if base[0:1] == '/':
63                 return base
64
65         if scope == SCOPE_CURRENT_SKIN:
66                 from Components.config import config
67                 tmp = defaultPaths[SCOPE_SKIN]
68                 pos = config.skin.primary_skin.value.rfind('/')
69                 if pos != -1:
70                         path = tmp[0]+config.skin.primary_skin.value[:pos+1]
71                 else:
72                         path = tmp[0]
73         else:
74                 tmp = defaultPaths[scope]
75                 path = tmp[0]
76
77         flags = tmp[1]
78
79         if flags == PATH_CREATE:
80                 if not pathExists(path):
81                         mkdir(path)
82
83         fallbackPath = fallbackPaths.get(scope)
84
85         if fallbackPath and not fileExists(path + base):
86                 for x in fallbackPath:
87                         if x[1] == FILE_COPY:
88                                 if fileExists(x[0] + base):
89                                         system("cp " + x[0] + base + " " + path + base)
90                                         break
91                         elif x[1] == FILE_MOVE:
92                                 if fileExists(x[0] + base):
93                                         system("mv " + x[0] + base + " " + path + base)
94                                         break
95                         elif x[1] == PATH_COPY:
96                                 if pathExists(x[0]):
97                                         if not pathExists(defaultPaths[scope][0]):
98                                                 mkdir(path)
99                                         system("cp -a " + x[0] + "* " + path)
100                                         break
101                         elif x[1] == PATH_MOVE:
102                                 if pathExists(x[0]):
103                                         system("mv " + x[0] + " " + path)
104                                         break
105
106         # FIXME: we also have to handle DATADIR etc. here.
107         return path + base
108         # this is only the BASE - an extension must be added later.
109
110 def pathExists(path):
111         return os_path.exists(path)
112
113 def createDir(path):
114         try:
115                 mkdir(path)
116         except:
117                 ret = 0
118         else:
119                 ret = 1
120         return ret
121
122 def fileExists(f, mode='r'):
123         try:
124                 file = open(f, mode)
125         except IOError:
126                 exists = 0
127         else:
128                 exists = 1
129         return exists
130
131 def getRecordingFilename(basename, dirname = None):
132         # filter out non-allowed characters
133         non_allowed_characters = "/.\\:*?<>|\""
134         filename = ""
135         
136         basename = basename.replace('\xc2\x86', '').replace('\xc2\x87', '')
137         
138         for c in basename:
139                 if c in non_allowed_characters:
140                         c = "_"
141                 filename += c
142
143         if dirname is not None:
144                 filename = ''.join([dirname, filename])
145
146         i = 0
147         while True:
148                 path = resolveFilename(SCOPE_HDD, filename)
149                 if i > 0:
150                         path += "_%03d" % i
151                 try:
152                         open(path + ".ts")
153                         i += 1
154                 except IOError:
155                         return path
156
157 # this is clearly a hack:
158 def InitFallbackFiles():
159         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.tv")
160         resolveFilename(SCOPE_CONFIG, "bouquets.tv")
161         resolveFilename(SCOPE_CONFIG, "userbouquet.favourites.radio")
162         resolveFilename(SCOPE_CONFIG, "bouquets.radio")
163
164 # returns a list of tuples containing pathname and filename matching the given pattern
165 # example-pattern: match all txt-files: ".*\.txt$"
166 def crawlDirectory(directory, pattern):
167         expression = compile(pattern)
168         list = []
169         for root, dirs, files in walk(directory):
170                 for file in files:
171                         if expression.match(file) is not None:
172                                 list.append((root, file))
173         return list