Example #1
0
 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)
Example #2
0
 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)
Example #3
0
 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)
Example #4
0
 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'
Example #5
0
 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()
Example #6
0
 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'
Example #7
0
 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()
Example #8
0
 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()
Example #9
0
 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()
Example #10
0
 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'
Example #11
0
 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'
Example #12
0
 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'
Example #13
0
 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'
Example #14
0
 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'
Example #15
0
 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'
Example #16
0
 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()
Example #17
0
 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()
Example #18
0
 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)
Example #19
0
 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)
Example #20
0
 def test_set(self):
     context = Context('', Mock(), Mock())
     Context.set(context)
     self.assertEqual(Context._current.inst, context)
Example #21
0
 def test_current(self):
     context = Context('', Mock(), Mock())
     Context.set(context)
     self.assertEqual(context, Context.current())