From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 09AB9138D10 for ; Fri, 10 Jul 2015 02:02:06 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 90C37E0886; Fri, 10 Jul 2015 02:02:03 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 70D84E08D0 for ; Fri, 10 Jul 2015 02:02:02 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 3420A340939 for ; Fri, 10 Jul 2015 02:02:01 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id B3320729 for ; Fri, 10 Jul 2015 02:01:59 +0000 (UTC) From: "Anthony G. Basile" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Anthony G. Basile" Message-ID: <1436493877.5b7fbe02062796313ad094892f44aac73372f9f6.blueness@gentoo> Subject: [gentoo-commits] proj/grss:master commit in: grs/ X-VCS-Repository: proj/grss X-VCS-Files: grs/Execute.py X-VCS-Directories: grs/ X-VCS-Committer: blueness X-VCS-Committer-Name: Anthony G. Basile X-VCS-Revision: 5b7fbe02062796313ad094892f44aac73372f9f6 X-VCS-Branch: master Date: Fri, 10 Jul 2015 02:01:59 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 4a5a9850-c242-46e7-88a3-cd56562b476e X-Archives-Hash: 965694ec80931948eccae6037f4e3c9c commit: 5b7fbe02062796313ad094892f44aac73372f9f6 Author: Anthony G. Basile gentoo org> AuthorDate: Fri Jul 10 02:04:37 2015 +0000 Commit: Anthony G. Basile gentoo org> CommitDate: Fri Jul 10 02:04:37 2015 +0000 URL: https://gitweb.gentoo.org/proj/grss.git/commit/?id=5b7fbe02 grs/Execute.py: document and add logging to stderr. grs/Execute.py | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/grs/Execute.py b/grs/Execute.py index 15fe562..f740924 100644 --- a/grs/Execute.py +++ b/grs/Execute.py @@ -4,16 +4,24 @@ import os import signal import shlex import subprocess +import sys from grs.Constants import CONST class Execute(): - """ doc here - more doc - """ + """ Execute a shell command """ def __init__(self, cmd, timeout = 1, extra_env = {}, failok = False, logfile = CONST.LOGFILE): - """ doc here - more doc + """ Execute a shell command. + + cmd - Simple string of the command to be execute as a + fork()-ed child. + timeout - The time in seconds to wait() on the child before + sending a SIGTERM. timeout = None means wait indefinitely. + extra_env - Dictionary of extra environment variables for the fork()-ed + child. Note that the child inherits all the env variables + of the grandparent shell in which grsrun/grsup was spawned. + logfile - A file to log output to. If logfile = None, then we log + to sys.stdout. """ def signalexit(): pid = os.getpid() @@ -25,11 +33,15 @@ class Execute(): except ProcessLookupError: pass - f = open(logfile, 'a') args = shlex.split(cmd) extra_env = dict(os.environ, **extra_env) - proc = subprocess.Popen(args, stdout=f, stderr=f, env=extra_env) + if logfile: + f = open(logfile, 'a') + proc = subprocess.Popen(args, stdout=f, stderr=f, env=extra_env) + else: + f = sys.stderr + proc = subprocess.Popen(args, env=extra_env) try: proc.wait(timeout) @@ -38,13 +50,19 @@ class Execute(): proc.kill() timed_out = True - rc = proc.returncode - if rc != 0: - f.write('EXIT CODE: %d\n' % rc) - if not failok: - signalexit() + if not timed_out: + # rc = None if we had a timeout + rc = proc.returncode + if rc: + f.write('EXIT CODE: %d\n' % rc) + if not failok: + signalexit() if timed_out: - f.write('TIMEOUT ERROR: %s\n' % cmd) + f.write('TIMEOUT ERROR: %s\n' % cmd) + if not failok: + signalexit() - f.close() + # Only close a logfile, don't close sys.stderr! + if logfile: + f.close()