use builtin python functions instead of use os.system
authorAndreas Monzner <andreas.monzner@multimedia-labs.de>
Mon, 21 Jul 2008 14:06:10 +0000 (14:06 +0000)
committerAndreas Monzner <andreas.monzner@multimedia-labs.de>
Mon, 21 Jul 2008 14:06:10 +0000 (14:06 +0000)
lib/python/Components/DreamInfoHandler.py
lib/python/Plugins/SystemPlugins/DefaultServicesScanner/plugin.py
lib/python/Screens/DefaultWizard.py
lib/python/Tools/Directories.py

index 8e34632..900a218 100644 (file)
@@ -1,5 +1,5 @@
 import xml.sax
-from Tools.Directories import crawlDirectory, resolveFilename, SCOPE_CONFIG, SCOPE_SKIN
+from Tools.Directories import crawlDirectory, resolveFilename, SCOPE_CONFIG, SCOPE_SKIN, copyfile, copytree
 from Components.NimManager import nimmanager
 from Components.Ipkg import IpkgComponent
 from Components.config import config, configfile
@@ -305,7 +305,7 @@ class DreamInfoHandler:
                print "installing skin:", directory, " - ", name
                print "cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))
                if self.blocking:
-                       os.system("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN)))
+                       copytree(directory, resolveFilename(SCOPE_SKIN))
                        self.installNext()
                else:
                        if self.console.execute("cp -a %s %s" % (directory, resolveFilename(SCOPE_SKIN))):
@@ -324,9 +324,9 @@ class DreamInfoHandler:
        def installFavourites(self, directory, name):
                print "installing favourites:", directory, " - ", name
                self.reloadFavourites = True
-               
+
                if self.blocking:
-                       os.system("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG)))
+                       copyfile(directory + name, resolveFilename(SCOPE_CONFIG))
                        self.installNext()
                else:
                        if self.console.execute("cp %s %s" % ((directory + name), resolveFilename(SCOPE_CONFIG))):
index 96ff43a..2c29cba 100644 (file)
@@ -9,10 +9,10 @@ from Plugins.Plugin import PluginDescriptor
 from Screens.ScanSetup import ScanSetup
 from Screens.ServiceScan import ServiceScan
 from Screens.MessageBox import MessageBox
-from Tools.Directories import resolveFilename, SCOPE_CONFIG
+from Tools.Directories import resolveFilename, SCOPE_CONFIG, copyfile
 #from Screens.Screen import Screen
+from file import unlink
 from enigma import eTimer, eDVBDB
-import os
 
 class DefaultServiceScan(ServiceScan):
        skin = """
@@ -39,9 +39,12 @@ class DefaultServiceScan(ServiceScan):
                <widget name="scan_progress" position="10,105" size="400,15" pixmap="skin_default/progress_big.png" borderWidth="2" borderColor="#cccccc" />
                <widget name="servicelist" position="10,135" size="400,265" selectionDisabled="1" />
        </screen>"""
-               
+
        def __init__(self, session, scanList):
-               os.system("rm " + resolveFilename(SCOPE_CONFIG) + "/lamedb")
+               try:
+                       unlink("rm " + resolveFilename(SCOPE_CONFIG) + "/lamedb");
+               except OSError:
+                       pass
                db = eDVBDB.getInstance()
                db.reloadServicelist()
                ServiceScan.__init__(self, session, scanList)
@@ -59,7 +62,8 @@ class DefaultServicesScannerPlugin(ScanSetup):
        def __init__(self, session, args = None):
                ScanSetup.__init__(self, session)
                # backup lamedb
-               os.system("cp " + resolveFilename(SCOPE_CONFIG) + "/lamedb " + resolveFilename(SCOPE_CONFIG) + "/lamedb.backup")
+               confdir = resolveFilename(SCOPE_CONFIG)
+               copyfile(confdir + "/lamedb", confdir + "/lamedb.backup)
                self.scan_type.value = "multisat"
                self.createSetup()
                self.scanIndex = 0
@@ -115,7 +119,8 @@ class DefaultServicesScannerPlugin(ScanSetup):
                self.scanIndex += 1
                if self.scanIndex + 1 >= len(self.multiscanlist):
                        print "no more sats to scan"
-                       os.system("cp " + resolveFilename(SCOPE_CONFIG) + "/lamedb.backup " + resolveFilename(SCOPE_CONFIG) + "/lamedb")
+                       confdir = resolveFilename(SCOPE_CONFIG)
+                       copyfile(confdir + "/lamedb.backup", confdir + "/lamedb)
                        db.reloadServicelist()
                        self.close()
                else:
index ec45a28..9883dc8 100644 (file)
@@ -1,13 +1,13 @@
 from Wizard import wizardManager
 from Screens.WizardLanguage import WizardLanguage
-from Tools.Directories import resolveFilename, SCOPE_DEFAULTDIR, SCOPE_DEFAULTPARTITIONMOUNTDIR, SCOPE_DEFAULTPARTITION
+from Tools.Directories import pathExists, resolveFilename, SCOPE_DEFAULTDIR, SCOPE_DEFAULTPARTITIONMOUNTDIR, SCOPE_DEFAULTPARTITION
 
 from Components.Pixmap import Pixmap, MovingPixmap
 from Components.config import config, ConfigBoolean, configfile, ConfigYesNo, getConfigListEntry
 from Components.DreamInfoHandler import DreamInfoHandler
 from Components.PluginComponent import plugins
 from Plugins.Plugin import PluginDescriptor
-from os import system as os_system, path as os_path
+from os import system as os_system, path as os_path, mkdir
 
 config.misc.defaultchosen = ConfigBoolean(default = True)
 
@@ -59,10 +59,11 @@ class DefaultWizard(WizardLanguage, DreamInfoHandler):
                for x in range(len(self.packagesConfig)):
                        if self.packagesConfig[x].value:
                                self.indexList.append(x)
-               
+
 class DreamPackageWizard(DefaultWizard):
        def __init__(self, session, packagefile, silent = False):
-               os_system("mkdir /tmp/package")
+               if not pathExists("/tmp/package"):
+                       mkdir("/tmp/package")
                os_system("tar xpzf %s -C /tmp/package" % packagefile)
                self.packagefile = packagefile
                DefaultWizard.__init__(self, session, silent)
index 5e5b03a..576d277 100644 (file)
@@ -1,7 +1,21 @@
 # -*- coding: utf-8 -*-
-from os import path as os_path, mkdir, rmdir, system, walk
+
+from os import path as os_path, mkdir, rmdir, system, walk, stat as os_stat, listdir, readlink, makedirs, error as os_error, symlink
+from stat import S_IMODE
 from re import compile
 
+try:
+       from os import chmod
+       have_chmod = True
+except:
+       have_chmod = False
+
+try:
+       from os import utime
+       have_utime = True
+except:
+       have_utime = False
+
 SCOPE_TRANSPONDERDATA = 0
 SCOPE_SYSETC = 1
 SCOPE_FONTS = 2
@@ -182,3 +196,56 @@ def crawlDirectory(directory, pattern):
                        if expression.match(file) is not None:
                                list.append((root, file))
        return list
+
+def copyfile(src, dst):
+       try:
+               f1 = open(src, "rb")
+               if os_path.isdir(dst):
+                       dst = os_path.join(dst, os_path.basename(src))
+               f2 = open(dst, "w+b")
+               while True:
+                       buf = f1.read(16*1024)
+                       if not buf:
+                               break
+                       f2.write(buf)
+               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))
+       except:
+               print "copy", src, "to", dst, "failed!"
+               return -1
+       return 0
+
+def copytree(src, dst, symlinks=False):
+       names = listdir(src)
+       if os_path.isdir(dst):
+               dst = os_path.join(dst, os_path.basename(src))
+               if not os_path.isdir(dst):
+                       mkdir(dst)
+       else:
+               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)
+                       elif os_path.isdir(srcname):
+                               copytree(srcname, dstname, symlinks)
+                       else:
+                               copyfile(srcname, dstname)
+               except:
+                       print "dont copy srcname (no file or link or folder)"
+       try:
+               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))
+       except:
+               print "copy stats for", src, "failed!"