Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id D0203200BB1 for ; Wed, 19 Oct 2016 17:46:19 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id CEE87160AFB; Wed, 19 Oct 2016 15:46:19 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id ED546160AFD for ; Wed, 19 Oct 2016 17:46:18 +0200 (CEST) Received: (qmail 20743 invoked by uid 500); 19 Oct 2016 15:46:18 -0000 Mailing-List: contact dev-help@ariatosca.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ariatosca.incubator.apache.org Delivered-To: mailing list dev@ariatosca.incubator.apache.org Received: (qmail 20701 invoked by uid 99); 19 Oct 2016 15:46:18 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 19 Oct 2016 15:46:18 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id B0788C2737 for ; Wed, 19 Oct 2016 15:46:17 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -6.219 X-Spam-Level: X-Spam-Status: No, score=-6.219 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id E3uUajPtR_Ss for ; Wed, 19 Oct 2016 15:46:15 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 51ED460CEA for ; Wed, 19 Oct 2016 15:46:15 +0000 (UTC) Received: (qmail 20529 invoked by uid 99); 19 Oct 2016 15:46:14 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 19 Oct 2016 15:46:14 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 42557E6998; Wed, 19 Oct 2016 15:46:14 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dankilman@apache.org To: dev@ariatosca.incubator.apache.org Date: Wed, 19 Oct 2016 15:46:18 -0000 Message-Id: <93d2171c72d34f65b912f28ee9c4122d@git.apache.org> In-Reply-To: <6fd5dc8143f44dc5abf3eddfb9abefba@git.apache.org> References: <6fd5dc8143f44dc5abf3eddfb9abefba@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [5/6] incubator-ariatosca git commit: fix process archived-at: Wed, 19 Oct 2016 15:46:20 -0000 fix process Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/a465e6f1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/a465e6f1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/a465e6f1 Branch: refs/heads/pylint-tools Commit: a465e6f1654ba42b144b94a681321c103f7d3e60 Parents: 4c566f7 Author: Dan Kilman Authored: Wed Oct 19 16:37:32 2016 +0300 Committer: Dan Kilman Committed: Wed Oct 19 16:53:08 2016 +0300 ---------------------------------------------------------------------- aria/tools/process.py | 59 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/a465e6f1/aria/tools/process.py ---------------------------------------------------------------------- diff --git a/aria/tools/process.py b/aria/tools/process.py index 7df0026..5a3d8a0 100644 --- a/aria/tools/process.py +++ b/aria/tools/process.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +Subprocess utility methods +""" + import os import subprocess from signal import SIGKILL @@ -23,6 +27,10 @@ from aria.exceptions import ExecutorException, ProcessException class Process(LoggerMixin): + """ + Subprocess wrapper + """ + def __init__( self, args, @@ -32,12 +40,7 @@ class Process(LoggerMixin): env=None, **kwargs): """ - Process class - subprocess wrapper - :param args: - :param stdout: - :param stderr: - :param cwd: - :param env: + Subprocess wrapper """ super(Process, self).__init__(**kwargs) self.args = args @@ -48,7 +51,7 @@ class Process(LoggerMixin): self._stderr = stderr def __repr__(self): - return '{cls.__name__}(args=self.args, cwd=self.cwd)'.format( + return '{cls.__name__}(args={self.args}, cwd={self.cwd})'.format( cls=self.__class__, self=self) def __getattr__(self, item): @@ -56,25 +59,40 @@ class Process(LoggerMixin): @property def name(self): + """ + The process name + """ return self.args[0] @property def pid(self): + """ + The process pid (if running) + """ if self.is_running(): return self.process.pid @property def stdout(self): - assert self.process, 'Need to run before calling thie method' + """ + The process stdout + """ + assert self.process, 'Need to run before calling this method' return self.process.stdout @property def stderr(self): - assert self.process, 'Need to run before calling thie method' + """ + The process stderr + """ + assert self.process, 'Need to run before calling this method' return self.process.stderr @property def return_code(self): + """ + The process return code. Will wait for process to end if it hasn't already + """ if self.process is None: return None if self.is_running(): @@ -85,6 +103,10 @@ class Process(LoggerMixin): return self.process.returncode def terminate(self): + """ + Terminates the process by sending a SIGTERM to it. If the process did not stop after that, + sends a SIGKILL with 1 second interval for a maximum of 10 times. + """ if self.process is not None and self.process.poll() is None: self.logger.debug('terminating process {0:d} ({1})'.format(self.process.pid, self.name)) self.process.terminate() @@ -96,16 +118,22 @@ class Process(LoggerMixin): sleep(1) kill_attempts += 1 - def killpg(self): + def kill(self): + """ + Kill the process by sending a SIGKILL to it + """ if self.is_running(): os.killpg(os.getpgid(self.pid), SIGKILL) def is_running(self): + """ + Returns ``True`` if the process is currently running + """ return self.process.poll() is None if self.process else False def wait(self): """ - Block till child process finishes + Block until process finishes """ assert self.process, 'Need to run before calling thie method' self.process.wait() @@ -113,8 +141,6 @@ class Process(LoggerMixin): def run(self, nice=None, universal_newlines=True): """ Run the child process. This call does not block. - :param int nice: nice on the child process run - :param bool universal_newlines: """ self.logger.debug('Running child process: {0}'.format(' '.join(self.args))) self.process = subprocess.Popen( @@ -128,6 +154,9 @@ class Process(LoggerMixin): universal_newlines=universal_newlines) def run_in_shell(self, nice=None, universal_newlines=True): + """ + Run the child process in a shell. This call does not block. + """ command = ' '.join(self.args) self.logger.debug('Running child process in shell: {0}'.format(command)) self.process = subprocess.Popen( @@ -142,6 +171,10 @@ class Process(LoggerMixin): universal_newlines=universal_newlines) def raise_failure(self): + """ + Raise a ProcessException if the process terminated with a non zero return code. Will wait + for the process to finish if it hasn't already + """ if self.is_running(): self.wait() if self.return_code == 0: