X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=blobdiff_plain;f=lib%2Fpython%2FTools%2FDirectories.py;h=6aa527110b43bd252e18905ffd4bf54fc96f60de;hp=518db49d31501b80d92930ceda42bde23d236be6;hb=HEAD;hpb=d6b8ff865af5c59b109c7e6ede3868db11e46f7d diff --git a/lib/python/Tools/Directories.py b/lib/python/Tools/Directories.py index 518db49..6aa5271 100755 --- a/lib/python/Tools/Directories.py +++ b/lib/python/Tools/Directories.py @@ -17,6 +17,8 @@ try: except: have_utime = False +import os + SCOPE_TRANSPONDERDATA = 0 SCOPE_SYSETC = 1 SCOPE_FONTS = 2 @@ -57,7 +59,7 @@ defaultPaths = { SCOPE_USERETC: ("", PATH_DONTCREATE), # user home directory SCOPE_DEFAULTDIR: (eEnv.resolve("${datadir}/enigma2/defaults/"), PATH_CREATE), - SCOPE_DEFAULTPARTITION: ("/dev/mtdblock/6", PATH_DONTCREATE), + SCOPE_DEFAULTPARTITION: ("/dev/mtdblock6", PATH_DONTCREATE), SCOPE_DEFAULTPARTITIONMOUNTDIR: (eEnv.resolve("${datadir}/enigma2/dealer"), PATH_CREATE), SCOPE_METADIR: (eEnv.resolve("${datadir}/meta"), PATH_CREATE), } @@ -118,7 +120,11 @@ def resolveFilename(scope, base = "", path_prefix = None): if flags == PATH_CREATE: if not pathExists(path): - mkdir(path) + try: + mkdir(path) + except OSError: + print "resolveFilename: Couldn't create %s" % path + return None fallbackPath = fallbackPaths.get(scope) @@ -147,11 +153,64 @@ def resolveFilename(scope, base = "", path_prefix = None): return path + base # this is only the BASE - an extension must be added later. -def pathExists(path): - return os_path.exists(path) +pathExists = os.path.exists +isMount = os.path.ismount + +def bestRecordingLocation(candidates): + path = '' + + from Components import Harddisk + ata_devices = [candidate for candidate in candidates if Harddisk.getDeviceInterface(candidate[1]) == "ata"] -def isMount(path): - return os_path.ismount(path) + if len(ata_devices) == 1: + path = ata_devices[0][1] + + elif len(ata_devices): + best = "" + for device in ata_devices: + dev = os.path.basename(device[0]) + if not best or (best > dev): + best = dev + path = device[1] + else: # Find the largest usb disk + biggest = 0 + for candidate in candidates: + try: + stat = os.statvfs(candidate[1]) + # must have some free space (i.e. not read-only) + if stat.f_bavail: + # Free space counts double + size = (stat.f_blocks + stat.f_bavail) * stat.f_bsize + if size > biggest: + path = candidate[1] + biggest = size + except Exception, e: + print "[DRL]", e + + return path + +def defaultRecordingLocation(candidate=None): + if candidate and os.path.exists(candidate): + return candidate + # First, try whatever /hdd points to, or /media/hdd + try: + path = os.path.realpath('/hdd') + except: + path = '/media/hdd' + if not os.path.exists(path) or not os.path.ismount(path): + path = '' + from Components import Harddisk + mounts = [m for m in Harddisk.getProcMounts() if m[1].startswith('/media/')] + path = bestRecordingLocation([m for m in mounts if m[0].startswith('/dev/')]) + if path: + # If there's a movie subdir, we'd probably want to use that. + movie = os.path.join(path, 'movie') + if os.path.isdir(movie): + path = movie + if not path.endswith('/'): + path += '/' # Bad habits die hard, old code relies on this + + return path def createDir(path, makeParents = False): try: @@ -183,7 +242,13 @@ def fileExists(f, mode='r'): acc_mode = F_OK return access(f, acc_mode) +def fileCheck(f, mode='r'): + return fileExists(f, mode) and f + def getRecordingFilename(basename, dirname = None): + if not dirname.endswith('/'): + dirname += '/' + # filter out non-allowed characters non_allowed_characters = "/.\\:*?<>|\"" filename = "" @@ -224,12 +289,13 @@ def InitFallbackFiles(): # returns a list of tuples containing pathname and filename matching the given pattern # example-pattern: match all txt-files: ".*\.txt$" def crawlDirectory(directory, pattern): - expression = compile(pattern) list = [] - for root, dirs, files in walk(directory): - for file in files: - if expression.match(file) is not None: - list.append((root, file)) + if directory: + expression = compile(pattern) + for root, dirs, files in walk(directory): + for file in files: + if expression.match(file) is not None: + list.append((root, file)) return list def copyfile(src, dst):