def validate(taskKwargs, xlog=KodiLogger.log):
     tmp = translatepath(taskKwargs['pythonfile'])
     fse = sys.getfilesystemencoding()
     if fse is None:
         fse = 'utf-8'
     if sys.platform.lower().startswith('win'):
         if tmp.encode('utf-8') != tmp.encode(fse):
             tmp = fsencode(tmp)
     if xbmcvfs.exists(tmp):
         ext = os.path.splitext(tmp)[1]
         if ext.lower() == '.py':
             return True
         else:
             xlog(msg=_('Error - not a python script: %s') % tmp)
             return False
     else:
         xlog(msg=_('Error - File not found: %s') % tmp)
         return False
예제 #2
0
 def validate(taskKwargs, xlog=KodiLogger.log):
     tmp = translatepath(taskKwargs['pythonfile'])
     fse = sys.getfilesystemencoding()
     if fse is None:
         fse = 'utf-8'
     if sys.platform.lower().startswith('win'):
         if tmp.encode('utf-8') != tmp.encode(fse):
             tmp = fsencode(tmp)
     if xbmcvfs.exists(tmp):
         ext = os.path.splitext(tmp)[1]
         if ext.lower() == '.py':
             return True
         else:
             xlog(msg=_('Error - not a python script: %s') % tmp)
             return False
     else:
         xlog(msg=_('Error - File not found: %s') % tmp)
         return False
    def run(self):
        if self.taskKwargs['notify'] is True:
            notify(_('Task %s launching for event: %s') % (self.taskId, str(self.topic)))
        err = False
        msg = ''
        args = self.runtimeargs
        try:
            useImport = self.taskKwargs['import']
        except KeyError:
            useImport = False
        fn = translatepath(self.taskKwargs['pythonfile'])
        fse = sys.getfilesystemencoding()
        if fse is None:
            fse = 'utf-8'
        if sys.platform.lower().startswith('win'):
            if fn.encode('utf-8') != fn.encode(fse):
                fn = fsencode(fn)
        else:
            fn = fn.encode(fse)
        try:
            if len(self.runtimeargs) > 0:
                if useImport is False:
                    args = u' %s' % ' '.join(args)
                    try:
                        args = args.encode(fse)
                    except UnicodeEncodeError:
                        msg += 'Unicode Encode Error for "%s" Encoder: %s' % (args, fse)
                    result = xbmc.executebuiltin('XBMC.RunScript(%s, %s)' % (fn, args))
                else:
                    directory, module_name = os.path.split(self.taskKwargs['pythonfile'])
                    module_name = os.path.splitext(module_name)[0]
                    path = list(sys.path)
                    sys.path.insert(0, directory)
                    try:
                        module = __import__(module_name.encode('utf-8'))
                        result = module.run(args)
                    finally:
                        sys.path[:] = path
            else:
                if useImport is False:
                    result = xbmc.executebuiltin(u'XBMC.RunScript(%s)' % fn)
                else:

                    directory, module_name = os.path.split(fn)
                    module_name = os.path.splitext(module_name)[0]

                    path = list(sys.path)
                    sys.path.insert(0, directory)
                    try:
                        module = __import__(module_name.encode('utf-8'))
                        result = module.run(None)
                    finally:
                        sys.path[:] = path
            if result is not None:
                msg = result
                if result != '':
                    err = True
        except Exception:
            e = sys.exc_info()[0]
            err = True
            msg = u''
            if hasattr(e, 'message'):
                msg += unicode(e.message) + u'\n'
            msg +=  unicode(e) + u'\n'
            tb = traceback.format_exc()
            msg += tb.decode('utf-8')
        self.threadReturn(err, msg)
예제 #4
0
    def run(self):
        if self.taskKwargs['notify'] is True:
            notify(
                _('Task %s launching for event: %s') %
                (self.taskId, str(self.topic)))
        err = False
        msg = ''
        args = self.runtimeargs
        try:
            useImport = self.taskKwargs['import']
        except KeyError:
            useImport = False
        fn = translatepath(self.taskKwargs['pythonfile'])
        fse = sys.getfilesystemencoding()
        if fse is None:
            fse = 'utf-8'
        if sys.platform.lower().startswith('win'):
            if fn.encode('utf-8') != fn.encode(fse):
                fn = fsencode(fn)
        else:
            fn = fn.encode(fse)
        try:
            if len(self.runtimeargs) > 0:
                if useImport is False:
                    args = u' %s' % ' '.join(args)
                    try:
                        args = args.encode(fse)
                    except UnicodeEncodeError:
                        msg += 'Unicode Encode Error for "%s" Encoder: %s' % (
                            args, fse)
                    result = xbmc.executebuiltin('XBMC.RunScript(%s, %s)' %
                                                 (fn, args))
                else:
                    directory, module_name = os.path.split(
                        self.taskKwargs['pythonfile'])
                    module_name = os.path.splitext(module_name)[0]
                    path = list(sys.path)
                    sys.path.insert(0, directory)
                    try:
                        module = __import__(module_name.encode('utf-8'))
                        result = module.run(args)
                    finally:
                        sys.path[:] = path
            else:
                if useImport is False:
                    result = xbmc.executebuiltin(u'XBMC.RunScript(%s)' % fn)
                else:

                    directory, module_name = os.path.split(fn)
                    module_name = os.path.splitext(module_name)[0]

                    path = list(sys.path)
                    sys.path.insert(0, directory)
                    try:
                        module = __import__(module_name.encode('utf-8'))
                        result = module.run(None)
                    finally:
                        sys.path[:] = path
            if result is not None:
                msg = result
                if result != '':
                    err = True
        except Exception:
            e = sys.exc_info()[0]
            err = True
            msg = u''
            if hasattr(e, 'message'):
                msg += unicode(e.message) + u'\n'
            msg += unicode(e) + u'\n'
            tb = traceback.format_exc()
            msg += tb.decode('utf-8')
        self.threadReturn(err, msg)
예제 #5
0
    def run(self):
        msg = u''
        if self.taskKwargs['notify'] is True:
            notify(_('Task %s launching for event: %s') % (self.taskId, str(self.topic)))
        try:
            needs_shell = self.taskKwargs['use_shell']
        except KeyError:
            needs_shell = False
        try:
            wait = self.taskKwargs['waitForCompletion']
        except KeyError:
            wait = True
        fse = sys.getfilesystemencoding()
        if fse is None:
            fse = 'utf-8'
        cmd = self.taskKwargs['scriptfile']
        if sysplat.startswith('win'):
            if cmd.encode('utf-8') != cmd.encode(fse):
                cmd = fsencode(self.taskKwargs['scriptfile'])
        tmpl = process_cmdline(cmd)
        filefound = False
        basedir = None
        sysexecutable = None
        for i, tmp in enumerate(tmpl):
            tmp = unicode(xbmc.translatePath(tmp), encoding='utf-8')
            if os.path.exists(tmp) and filefound is False:
                basedir, fn = os.path.split(tmp)
                basedir = os.path.realpath(basedir)
                tmpl[i] = fn
                filefound = True
                if i == 0:
                    if os.path.splitext(fn)[1] == u'.sh':
                        if isAndroid:
                            sysexecutable = u'/system/bin/sh'
                        elif not sysplat.startswith('win'):
                            sysexecutable = u'/bin/bash'
            else:
                tmpl[i] = tmp
        if sysexecutable == u'/system/bin/sh':
            tmpl.insert(0, u'sh')
        elif sysexecutable == u'/bin/bash':
            tmpl.insert(0, u'bash')

        cwd = os.getcwd()
        argsu = tmpl + self.runtimeargs

        args = []
        for arg in argsu:
            try:
                args.append(arg.encode(fse))
            except UnicodeEncodeError:
                msg += u'Unicode Encode Error for: "%s" Encoder: %s' % (arg, fse)
        if needs_shell:
            args = ' '.join(args)
        err = False
        msg += u'taskScript ARGS = %s\n    SYSEXEC = %s\n BASEDIR = %s\n' % (args, sysexecutable, basedir)
        sys.exc_clear()

        try:
            if basedir is not None:
                os.chdir(basedir)
            if sysexecutable is not None:
                if isAndroid or sysplat.startswith('darwin'):
                    p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=needs_shell, stderr=subprocess.STDOUT, executable=sysexecutable)
                else:
                    p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=needs_shell, stderr=subprocess.STDOUT, executable=sysexecutable, cwd=basedir)
            else:
                if isAndroid or sysplat.startswith('darwin'):
                    p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=needs_shell, stderr=subprocess.STDOUT)
                else:
                    p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=needs_shell, stderr=subprocess.STDOUT, cwd=basedir)
            if wait:
                stdoutdata, stderrdata = p.communicate()
                if stdoutdata is not None:
                    stdoutdata = stdoutdata.decode(fse, 'ignore').strip()
                    if stdoutdata != '':
                        msg += _(u'Process returned data: [%s]\n') % stdoutdata
                    else:
                        msg += _(u'Process returned no data\n')
                else:
                    msg += _(u'Process returned no data\n')
                if stderrdata is not None:
                    stderrdata = stderrdata.decode(fse, 'ignore').strip()
                    if stderrdata != '':
                        msg += _(u'Process returned error: %s') % stderrdata
        except ValueError, e:
            err = True
            msg = unicode(e)