# Path: skema/utils.py
# def log_line(line="", tabs=0):
#     if (line == ""):
#         lansi = "[skema] "
#         l = ""
#     else:
#         l = ('\t' * tabs) + "[" + line + "]"
#         lansi = "[skema] " + bcolors.WARNING + l + bcolors.ENDC
#     logging.info(l)
#     printit (lansi)
# 
# Path: skema/utils.py
# def log_result(tag, err):
#     log_line ()
#     if (err == "OMX_ErrorNone"):
#         l = "[" + tag + " -> '" + err + "']"
#         lansi = "[skema] " + bcolors.OKGREEN + l + bcolors.ENDC
#         logging.info(l)
#     else:
#         l = "[" + tag + " -> '" + err + "']"
#         lansi = "[skema] " + bcolors.FAIL + l + bcolors.ENDC
#         logging.error(l)
# 
#     printit (lansi)
# 
# Path: skema/utils.py
# def log_api(line):
#     log_line ()
#     logging.info("")
#     log_line ()
#     logging.info("")
#     l = "[" + line  +"]"
#     logging.info(l)
#     lansi = "[skema] " + bcolors.BOLD + l + bcolors.ENDC
#     printit (lansi)


import os
import re
import pwd
import shlex
import skema.tag
from subprocess import Popen
from skema.utils import log_line
from skema.utils import log_result
from skema.utils import log_api
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



class tag_OMX_StartSubProcess(skema.tag.SkemaTag):
    """

    """
    def run(self, element, context):
        alias = element.get('alias')
        cmdlinestr = element.get('cmdline')

        # Replace '$USER' and '$HOME' strings wih the actual representations
        cmdlinestr = re.sub("\$USER", pwd.getpwuid(os.getuid())[0], cmdlinestr, 0)
        cmdlinestr = re.sub("\$HOME", pwd.getpwuid(os.getuid())[5], cmdlinestr, 0)

        log_api ("%s '%s' '%s'" \
            % (element.tag, alias, cmdlinestr))

        args = shlex.split(cmdlinestr)
        process = Popen(args)
        context.subprocesses[alias] = process


# Path: skema/utils.py
# def log_line(line="", tabs=0):
#     if (line == ""):
#         lansi = "[skema] "
#         l = ""
#     else:
#         l = ('\t' * tabs) + "[" + line + "]"
#         lansi = "[skema] " + bcolors.WARNING + l + bcolors.ENDC
#     logging.info(l)
#     printit (lansi)
# 
# Path: skema/utils.py
# def log_result(tag, err):
#     log_line ()
#     if (err == "OMX_ErrorNone"):
#         l = "[" + tag + " -> '" + err + "']"
#         lansi = "[skema] " + bcolors.OKGREEN + l + bcolors.ENDC
#         logging.info(l)
#     else:
#         l = "[" + tag + " -> '" + err + "']"
#         lansi = "[skema] " + bcolors.FAIL + l + bcolors.ENDC
#         logging.error(l)
# 
#     printit (lansi)
# 
# Path: skema/utils.py
# def log_api(line):
#     log_line ()
#     logging.info("")
#     log_line ()
#     logging.info("")
#     l = "[" + line  +"]"
#     logging.info(l)
#     lansi = "[skema] " + bcolors.BOLD + l + bcolors.ENDC
#     printit (lansi)


# Path: skema/tags/OMX_StartSubProcess.py
import os
import re
import pwd
import shlex
import skema.tag
from subprocess import Popen
from skema.utils import log_line
from skema.utils import log_result
from skema.utils import log_api

#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



class tag_OMX_StartSubProcess(skema.tag.SkemaTag):
    """

    """
    def run(self, element, context):
        alias = element.get('alias')
        cmdlinestr = element.get('cmdline')

        # Replace '$USER' and '$HOME' strings wih the actual representations
        cmdlinestr = re.sub("\$USER", pwd.getpwuid(os.getuid())[0], cmdlinestr, 0)
        cmdlinestr = re.sub("\$HOME", pwd.getpwuid(os.getuid())[5], cmdlinestr, 0)

        log_api ("%s '%s' '%s'" \
            % (element.tag, alias, cmdlinestr))

        args = shlex.split(cmdlinestr)
        process = Popen(args)
        context.subprocesses[alias] = process


        log_line ()