Commit c01e9c0b authored by Tomas Touceda's avatar Tomas Touceda
Browse files

Add a multiplatform ScriptWrapper

parent c3aeaece
Loading
Loading
Loading
Loading
+33 −3
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import shutil
import subprocess
import sys
import errno
import traceback

from lockfile import LockFile, AlreadyLocked, LockFailed

@@ -335,7 +336,7 @@ class ThpPackage(object):
                    env['THP_PURGE'] = "0"
                    env['THP_TEMP_DIR'] = self._tmp_path
                    
                    sw = ScriptWrapper(os.path.join(self._tmp_path, "meta", 
                    sw = ScriptBundleWrapper(os.path.join(self._tmp_path, "meta", 
                                                          "scripts", script[0]), env)

                    for type in script[1]:
@@ -397,3 +398,32 @@ class ScriptWrapper(object):
                                         env=self._env)
        self._process.wait()
        return self._process.returncode

class ScriptBundleWrapper(object):
    """ Wrapper for the scripts that runs the code without spawning
        another python process. This is mostly for handling
        multiplatform code. """
    def __init__(self, path = None, env = None):
        super(ScriptBundleWrapper, self).__init__()
        self._path = path
        self._env = env

    def run(self):
        """ Runs the script using the code class. """
        loc = {}
        glob = {}
        old_environ = os.environ.copy()
        os.environ.clear()
        os.environ.update(self._env)
        try:
            execfile(self._path, loc, glob)
        except SystemExit, e:
            return 1
        except:
            traceback.print_exc()
            return 1
        finally:
            os.environ.clear()
            os.environ.update(old_environ)

        return 0