def run(self, user, password, *options): """ Run a shell command. The command is executed as: "su - <user> -c <cmd>" and the user/password is authenticated using PAM. :param user: A user name. :type user: str :param password: The password. :type password: str :param options: List of options. :type options: list :return: (status, {stdout:<str>, stderr:<str>}) :rtype: tuple """ if not authenticate(user, password): return os.EX_NOPERM, {} shell = _Shell() context = Context.current() path = os.path.join('/tmp', context.sn) fp = open(path, 'w+') try: fp.write(self.content) finally: fp.close() try: os.chmod(path, 0755) cmd = [path] cmd += options cmd = ' '.join(cmd) return shell.run('su', '-', user, '-c', cmd) finally: os.unlink(path)
def run(self, user, password, *options): """ Run a shell command. The command is executed as: "su - <user> -c <cmd>". :param user: A user name. :type user: str :param password: The password. :type password: str :param options: List of options. :type options: list :return: (status, {stdout:<str>, stderr:<str>}) :rtype: tuple """ shell = _Shell() context = Context.current() path = os.path.join('/tmp', context.sn) with open(path, 'w+') as fp: fp.write(self.content) try: os.chmod(path, 0o755) cmd = [path] cmd += options cmd = ' '.join(cmd) return shell.run('su', '-', user, '-c', cmd) finally: os.unlink(path)
def run(self, user, password, *options): """ Run a shell command. The command is executed as: "su - <user> -c <cmd>" and the user/password is authenticated using PAM. :param user: A user name. :type user: str :param password: The password. :type password: str :param options: List of options. :type options: list :return: (status, {stdout:<str>, stderr:<str>}) :rtype: tuple """ if not authenticate(user, password): return os.EX_NOPERM, {} shell = _Shell() context = Context.current() path = os.path.join("/tmp", context.sn) fp = open(path, "w+") try: fp.write(self.content) finally: fp.close() try: os.chmod(path, 0755) cmd = [path] cmd += options cmd = " ".join(cmd) return shell.run("su", "-", user, "-c", cmd) finally: os.unlink(path)
def test(self): ctx = Context.current() for n in range(0, 100): log.info(ctx.sn) sleep(1) if ctx.cancelled(): return 'cancelled' return 'finished'
def cancelled(self): """ Get whether the current operation has been cancelled. :return: True if cancelled, else False. :rtype: bool """ context = Context.current() return context.cancelled()
def update_progress(self, report): """ Send the updated progress report. :param report: A handler progress report. :type report: object """ context = Context.current() context.progress.details = report context.progress.report()
def send(self, total): ctx = Context.current() ctx.progress.total = total for n in range(0, total): ctx.progress.completed += 1 ctx.progress.details = 'for: %d' % n ctx.progress.report() sleep(1) return 'sent, boss'
def send_half(self, total): ctx = Context.current() ctx.progress.total = total for n in range(0, total): if n < (total/2): ctx.progress.completed += 1 ctx.progress.report() sleep(1) return 'sent, boss'
def test_progress(self): total = 30 context = Context.current() context.progress.total = total context.progress.report() for n in range(total): context.progress.completed = n context.progress.report() return 'done'
def send_half(self, total): ctx = Context.current() ctx.progress.total = total for n in range(0, total): if n < (total / 2): ctx.progress.completed += 1 ctx.progress.report() sleep(1) return 'sent, boss'
def report(self, details): """ Report progress. :param details: The details to report. :type details: dict """ if not self.progress_reported: # not enabled return context = Context.current() context.progress.details = details context.progress.report()
def run(self, *command): """ Run the specified command. :param command: A command and parameters. :type command: tuple :return: (status, {stdout:<str>, stderr:<str>}) :rtype: tuple """ details = { STDOUT: '', STDERR: '', } result = { STDOUT: '', STDERR: '', } context = Context.current() p = Popen(command, stdout=PIPE, stderr=PIPE) try: while True: n_read = 0 if context.cancelled(): p.terminate() break for fp, key in ((p.stdout, STDOUT), (p.stderr, STDERR)): line = fp.readline() if line: n_read += len(line) details[key] = line result[key] += line self.report(details) if not n_read: # EOF break p.stdout.close() p.stderr.close() status = p.wait() return status, result except OSError, e: return -1, utf8(e)
def test_set(self): context = Context('', Mock(), Mock()) Context.set(context) self.assertEqual(Context._current.inst, context)
def test_current(self): context = Context('', Mock(), Mock()) Context.set(context) self.assertEqual(context, Context.current())