Tools/Directories.py: Refactor evil imports
authorAthanasios Oikonomou <athoik@gmail.com>
Thu, 31 Dec 2015 06:41:49 +0000 (08:41 +0200)
committerMike Looijmans <milo@openpli.org>
Thu, 31 Dec 2015 09:14:03 +0000 (10:14 +0100)
Get rid of "from os import path as os_path" and similar badness.

Don't check for chmod and utime existance, they exist..

>>> import os
>>> os.utime
<built-in function utime>
>>> os.chmod
<built-in function chmod>

Finnaly avoid os.system and use resource friendly eConsoleAppContainer

Signed-off-by: Mike Looijmans <milo@openpli.org>

lib/python/Tools/Directories.py

index 2e84926..2b57dc8 100755 (executable)
@@ -1,21 +1,9 @@
 # -*- coding: utf-8 -*-
 import os
-from os import mkdir, rmdir, system, walk, stat as os_stat, listdir, readlink, makedirs, error as os_error, symlink, access, F_OK, R_OK, W_OK, rename as os_rename
+import re
 from stat import S_IMODE
-from re import compile
-from enigma import eEnv
 
-try:
-       from os import chmod
-       have_chmod = True
-except:
-       have_chmod = False
-
-try:
-       from os import utime
-       have_utime = True
-except:
-       have_utime = False
+from enigma import eEnv, eConsoleAppContainer
 
 SCOPE_TRANSPONDERDATA = 0
 SCOPE_SYSETC = 1
@@ -118,7 +106,7 @@ def resolveFilename(scope, base = "", path_prefix = None):
        if flags == PATH_CREATE:
                if not pathExists(path):
                        try:
-                               mkdir(path)
+                               os.mkdir(path)
                        except OSError:
                                print "resolveFilename: Couldn't create %s" % path
                                return None
@@ -133,7 +121,7 @@ def resolveFilename(scope, base = "", path_prefix = None):
                                                try:
                                                        os.link(x[0] + base, path + base)
                                                except:
-                                                       system("cp " + x[0] + base + " " + path + base)
+                                                       eConsoleAppContainer().execute("cp " + x[0] + base + " " + path + base)
                                                break
                                elif x[1] == FILE_MOVE:
                                        if fileExists(x[0] + base):
@@ -142,8 +130,8 @@ def resolveFilename(scope, base = "", path_prefix = None):
                                elif x[1] == PATH_COPY:
                                        if pathExists(x[0]):
                                                if not pathExists(defaultPaths[scope][0]):
-                                                       mkdir(path)
-                                               system("cp -a " + x[0] + "* " + path)
+                                                       os.mkdir(path)
+                                               eConsoleAppContainer().execute("cp -a " + x[0] + "* " + path)
                                                break
                                elif x[1] == PATH_MOVE:
                                        if pathExists(x[0]):
@@ -199,9 +187,9 @@ def defaultRecordingLocation(candidate=None):
 def createDir(path, makeParents = False):
        try:
                if makeParents:
-                       makedirs(path)
+                       os.makedirs(path)
                else:
-                       mkdir(path)
+                       os.mkdir(path)
        except:
                return 0
        else:
@@ -209,7 +197,7 @@ def createDir(path, makeParents = False):
 
 def removeDir(path):
        try:
-               rmdir(path)
+               os.rmdir(path)
        except:
                return 0
        else:
@@ -217,12 +205,12 @@ def removeDir(path):
 
 def fileExists(f, mode='r'):
        if mode == 'r':
-               acc_mode = R_OK
+               acc_mode = os.R_OK
        elif mode == 'w':
-               acc_mode = W_OK
+               acc_mode = os.W_OK
        else:
-               acc_mode = F_OK
-       return access(f, acc_mode)
+               acc_mode = os.F_OK
+       return os.access(f, acc_mode)
 
 def fileCheck(f, mode='r'):
        return fileExists(f, mode) and f
@@ -272,8 +260,8 @@ def InitFallbackFiles():
 def crawlDirectory(directory, pattern):
        list = []
        if directory:
-               expression = compile(pattern)
-               for root, dirs, files in walk(directory):
+               expression = re.compile(pattern)
+               for root, dirs, files in os.walk(directory):
                        for file in files:
                                if expression.match(file) is not None:
                                        list.append((root, file))
@@ -290,32 +278,30 @@ def copyfile(src, dst):
                        if not buf:
                                break
                        f2.write(buf)
-               st = os_stat(src)
+               st = os.stat(src)
                mode = S_IMODE(st.st_mode)
-               if have_chmod:
-                       chmod(dst, mode)
-               if have_utime:
-                       utime(dst, (st.st_atime, st.st_mtime))
+               os.chmod(dst, mode)
+               os.utime(dst, (st.st_atime, st.st_mtime))
        except:
                print "copy", src, "to", dst, "failed!"
                return -1
        return 0
 
 def copytree(src, dst, symlinks=False):
-       names = listdir(src)
+       names = os.listdir(src)
        if os.path.isdir(dst):
                dst = os.path.join(dst, os.path.basename(src))
                if not os.path.isdir(dst):
-                       mkdir(dst)
+                       os.mkdir(dst)
        else:
-               makedirs(dst)
+               os.makedirs(dst)
        for name in names:
                srcname = os.path.join(src, name)
                dstname = os.path.join(dst, name)
                try:
                        if symlinks and os.path.islink(srcname):
-                               linkto = readlink(srcname)
-                               symlink(linkto, dstname)
+                               linkto = os.readlink(srcname)
+                               os.symlink(linkto, dstname)
                        elif os.path.isdir(srcname):
                                copytree(srcname, dstname, symlinks)
                        else:
@@ -323,12 +309,10 @@ def copytree(src, dst, symlinks=False):
                except:
                        print "dont copy srcname (no file or link or folder)"
        try:
-               st = os_stat(src)
+               st = os.stat(src)
                mode = S_IMODE(st.st_mode)
-               if have_chmod:
-                       chmod(dst, mode)
-               if have_utime:
-                       utime(dst, (st.st_atime, st.st_mtime))
+               os.chmod(dst, mode)
+               os.utime(dst, (st.st_atime, st.st_mtime))
        except:
                print "copy stats for", src, "failed!"
 
@@ -339,7 +323,7 @@ def moveFiles(fileList):
        try:
                try:
                        for item in fileList:
-                               os_rename(item[0], item[1])
+                               os.rename(item[0], item[1])
                                movedList.append(item)
                except OSError, e:
                        if e.errno == 18:
@@ -353,7 +337,7 @@ def moveFiles(fileList):
                print "[Directories] Failed move:", e
                for item in movedList:
                        try:
-                               os_rename(item[1], item[0])
+                               os.rename(item[1], item[0])
                        except:
                                print "[Directories] Failed to undo move:", item
                                raise