def validate(taskKwargs, xlog=KodiLogger.log): tmpl = process_cmdline(taskKwargs['scriptfile']) found = False for tmp in tmpl: tmp = xbmc.translatePath(tmp) if xbmcvfs.exists(tmp) or os.path.exists(tmp) and found is False: try: mode = os.stat(tmp).st_mode mode |= stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH os.chmod(tmp, mode) except OSError: if sysplat.startswith('win') is False: xlog(msg=_('Failed to set execute bit on script: %s') % tmp) finally: found = True return True
def run(self): msg = '' 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 tmpl = process_cmdline(self.taskKwargs['scriptfile']) filefound = False basedir = None sysexecutable = None for i, tmp in enumerate(tmpl): tmp = xbmc.translatePath(tmp) 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 = '/system/bin/sh' elif not sysplat.startswith('win'): sysexecutable = '/bin/bash' else: tmpl[i] = tmp if sysexecutable == '/system/bin/sh': tmpl.insert(0, 'sh') elif sysexecutable == '/bin/bash': tmpl.insert(0, 'bash') cwd = os.getcwd() args = tmpl + self.runtimeargs if needs_shell: args = ' '.join(args) err = False msg += '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 = str(stdoutdata).strip() if stdoutdata != '': msg += _('Process returned data: [%s]\n') % stdoutdata else: msg += _('Process returned no data\n') else: msg += _('Process returned no data\n') if stderrdata is not None: stderrdata = str(stderrdata).strip() if stderrdata != '': msg += _('Process returned error: %s') % stdoutdata except ValueError, e: err = True msg = str(e)
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)